From 73bba4c1e1f7fa23aa1a126971338d94ae42398f Mon Sep 17 00:00:00 2001 From: Tadas Tamosauskas Date: Sat, 28 Sep 2013 16:10:59 +0100 Subject: Serialize postgres' hstore, json and array types correctly in AR update methods. Fixes #12261. Closes #12395. Conflicts: activerecord/CHANGELOG.md activerecord/test/cases/adapters/postgresql/array_test.rb activerecord/test/cases/adapters/postgresql/json_test.rb --- activerecord/lib/active_record/sanitization.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/sanitization.rb') diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index cab8fd745a..bc78c3858a 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -101,7 +101,7 @@ module ActiveRecord # # => "status = NULL , group_id = 1" def sanitize_sql_hash_for_assignment(attrs, table) attrs.map do |attr, value| - "#{connection.quote_table_name_for_assignment(table, attr)} = #{quote_bound_value(value)}" + "#{connection.quote_table_name_for_assignment(table, attr)} = #{quote_bound_value(value, connection, columns_hash[attr.to_s])}" end.join(', ') end @@ -152,15 +152,16 @@ module ActiveRecord end end - def quote_bound_value(value, c = connection) #:nodoc: - if value.respond_to?(:map) && !value.acts_like?(:string) + def quote_bound_value(value, c = connection, column = nil) #:nodoc: + db_native_type = column && (column.respond_to?(:array) && column.array || [:json, :hstore].include?(column.type) ) + if value.respond_to?(:map) && !value.acts_like?(:string) && !db_native_type if value.respond_to?(:empty?) && value.empty? c.quote(nil) else value.map { |v| c.quote(v) }.join(',') end else - c.quote(value) + c.quote(value, column) end end -- cgit v1.2.3 From 9e1740af9c1a00980b30c66aba3c223c33e646e4 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 20 Dec 2013 08:45:54 -0200 Subject: Tidy up fix for PG extensions quoting Always pass in the column for quote_bound_value and quote using it in case it exists there. --- activerecord/lib/active_record/sanitization.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/sanitization.rb') diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index bc78c3858a..dacaec26b7 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -100,8 +100,9 @@ module ActiveRecord # { status: nil, group_id: 1 } # # => "status = NULL , group_id = 1" def sanitize_sql_hash_for_assignment(attrs, table) + c = connection attrs.map do |attr, value| - "#{connection.quote_table_name_for_assignment(table, attr)} = #{quote_bound_value(value, connection, columns_hash[attr.to_s])}" + "#{c.quote_table_name_for_assignment(table, attr)} = #{quote_bound_value(value, c, columns_hash[attr.to_s])}" end.join(', ') end @@ -153,15 +154,16 @@ module ActiveRecord end def quote_bound_value(value, c = connection, column = nil) #:nodoc: - db_native_type = column && (column.respond_to?(:array) && column.array || [:json, :hstore].include?(column.type) ) - if value.respond_to?(:map) && !value.acts_like?(:string) && !db_native_type + if column + c.quote(value, column) + elsif value.respond_to?(:map) && !value.acts_like?(:string) if value.respond_to?(:empty?) && value.empty? c.quote(nil) else value.map { |v| c.quote(v) }.join(',') end else - c.quote(value, column) + c.quote(value) end end -- cgit v1.2.3