diff options
author | Ryan Fitzgerald <rwfitzge@gmail.com> | 2012-04-24 20:43:10 -0700 |
---|---|---|
committer | Ryan Fitzgerald <rwfitzge@gmail.com> | 2012-04-24 20:46:51 -0700 |
commit | 0c46dbbc4557acb1e0af1c8ce697305398070922 (patch) | |
tree | 2c705020e4c200c198fe7566c03c2be1206bdce7 | |
parent | 0cc32c5fd7f875de61262b430bca23825691899b (diff) | |
download | rails-0c46dbbc4557acb1e0af1c8ce697305398070922.tar.gz rails-0c46dbbc4557acb1e0af1c8ce697305398070922.tar.bz2 rails-0c46dbbc4557acb1e0af1c8ce697305398070922.zip |
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.
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/hstore_test.rb | 10 |
2 files changed, 10 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 10a178e369..8248eb9d0b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -88,9 +88,8 @@ module ActiveRecord 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: diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 1644a58d92..05d38ec1f1 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/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 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase assert_cycle('a=>b' => 'bar', '1"foo' => '2') end + def test_quoting_special_characters + assert_cycle('ca' => 'cà', '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 |