Skip to content

Commit

Permalink
Always quote hstore keys and values
Browse files Browse the repository at this point in the history
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
rf- committed Apr 25, 2012
1 parent 0cc32c5 commit 0c46dbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion activerecord/test/cases/adapters/postgresql/hstore_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

require "cases/helper"
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
Expand Down Expand Up @@ -134,13 +136,19 @@ def test_arrow
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
Expand Down

0 comments on commit 0c46dbb

Please sign in to comment.