Skip to content
Browse files

Always quote hstore keys and values

escape_hstore uses quotation marks around keys and values only if it
seems necessary. However, it currently breaks in the presence of some
non-ASCII characters. Instead of trying to guess exactly which
characters are safe, it seems better to always use quotes.
  • Loading branch information...
1 parent 0cc32c5 commit 0c46dbbc4557acb1e0af1c8ce697305398070922 @rf- rf- committed
View
3 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -88,9 +88,8 @@ def string_to_hstore(string)
def escape_hstore(value)
value.nil? ? 'NULL'
- : value =~ /[=\s,>]/ ? '"%s"' % value.gsub(/(["\\])/, '\\\\\1')
: value == "" ? '""'
- : value.to_s.gsub(/(["\\])/, '\\\\\1')
+ : '"%s"' % value.gsub(/(["\\])/, '\\\\\1')
end
end
# :startdoc:
View
10 activerecord/test/cases/adapters/postgresql/hstore_test.rb
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
require "cases/helper"
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
@@ -134,13 +136,19 @@ def test_arrow
assert_cycle('a=>b' => 'bar', '1"foo' => '2')
end
+ def test_quoting_special_characters
+ assert_cycle('ca' => '', 'ac' => 'àc')
+ end
+
private
def assert_cycle hash
+ # test creation
x = Hstore.create!(:tags => hash)
x.reload
assert_equal(hash, x.tags)
- # make sure updates work
+ # test updating
+ x = Hstore.create!(:tags => {})
x.tags = hash
x.save!
x.reload

0 comments on commit 0c46dbb

Please sign in to comment.
Something went wrong with that request. Please try again.