提交 7816b985 编写于 作者: A Aaron Patterson

Merge pull request #10619 from alno/top_level_array_in_postgres_json

Support array as root element in Postgresql JSON columns
......@@ -60,7 +60,7 @@ def string_to_hstore(string)
end
def json_to_string(object)
if Hash === object
if Hash === object || Array === object
ActiveSupport::JSON.encode(object)
else
object
......
......@@ -30,6 +30,7 @@ def quote(value, column = nil) #:nodoc:
when Array
case sql_type
when 'point' then super(PostgreSQLColumn.point_to_string(value))
when 'json' then super(PostgreSQLColumn.json_to_string(value))
else
if column.array
"'#{PostgreSQLColumn.array_to_string(value, column, self).gsub(/'/, "''")}'"
......@@ -98,6 +99,7 @@ def type_cast(value, column, array_member = false)
when Array
case column.sql_type
when 'point' then PostgreSQLColumn.point_to_string(value)
when 'json' then PostgreSQLColumn.json_to_string(value)
else
return super(value, column) unless column.array
PostgreSQLColumn.array_to_string(value, column, self)
......
......@@ -83,4 +83,18 @@ def test_null_json
x = JsonDataType.first
assert_equal(nil, x.payload)
end
def test_select_array_json_value
@connection.execute %q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|
x = JsonDataType.first
assert_equal(['v0', {'k1' => 'v1'}], x.payload)
end
def test_rewrite_array_json_value
@connection.execute %q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|
x = JsonDataType.first
x.payload = ['v1', {'k2' => 'v2'}, 'v3']
assert x.save!
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册