diff --git a/bulk_data_methods.gemspec b/bulk_data_methods.gemspec index a3004a9..d86de5d 100644 --- a/bulk_data_methods.gemspec +++ b/bulk_data_methods.gemspec @@ -15,8 +15,8 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.homepage = 'http://github.com/fiksu/bulk_data_methods' s.add_dependency "pg" - s.add_dependency "activerecord", '>= 3.0', '< 5.0' - s.add_development_dependency "rails", '>= 3.0', '< 5.0' + s.add_dependency "activerecord", '>= 3.0'#, '< 5.0' + s.add_development_dependency "rails", '>= 3.0' s.add_development_dependency "rspec-rails" s.add_development_dependency "activeresource", '>= 3.0.0' end diff --git a/lib/bulk_data_methods/mixin.rb b/lib/bulk_data_methods/mixin.rb index eb813d6..4ee83a9 100644 --- a/lib/bulk_data_methods/mixin.rb +++ b/lib/bulk_data_methods/mixin.rb @@ -1,4 +1,3 @@ - module BulkDataMethods # exception thrown when row data structures are inconsistent between rows in single call to {#create_many} or {#update_many} @@ -105,7 +104,7 @@ def create_many(rows, options = {}) end end column_values = column_names.map do |column_name| - quote_value(row[column_name], columns_hash[column_name.to_s]) + connection.quote(row[column_name], columns_hash[column_name.to_s]) end.join(',') "(#{column_values})" end.each_slice(options[:slice_size]) do |insert_slice| @@ -236,9 +235,17 @@ def update_many(rows, options = {}) column_name = column_name.to_s columns_hash_value = columns_hash[column_name] if i == 0 - "#{quote_value(column_value, columns_hash_value)}::#{columns_hash_value.sql_type} as #{column_name}" + if columns_hash_value.sql_type=="jsonb" && !column_value.nil? && !column_value!='NULL' + "'#{connection.quote(column_value)}'::#{columns_hash_value.sql_type} as #{column_name}" + else + "#{connection.quote(column_value)}::#{columns_hash_value.sql_type} as #{column_name}" + end else - quote_value(column_value, columns_hash_value) + if columns_hash_value.sql_type=="jsonb" && !column_value.nil? && !column_value!='NULL' + "'#{connection.quote(column_value)}'" + else + connection.quote(column_value) + end end end.join(',') end @@ -257,7 +264,8 @@ def update_many(rows, options = {}) #{eval(returning_clause)} SQL sql = sql_update_string -#puts "SQL> #{sql}" +#binding.pry +#puts "SQL> #{sql}" returning += find_by_sql(sql_update_string) end end diff --git a/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql/quoting.rb b/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql/quoting.rb new file mode 100644 index 0000000..86132d3 --- /dev/null +++ b/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql/quoting.rb @@ -0,0 +1,21 @@ +# module ActiveRecord +# module ConnectionAdapters +# module PostgreSQL +# module Quoting +# +# private +# # ****** BEGIN PATCH ****** +# # when column of postgresql is an array, call super and fail. for example with [2] or ["2"]. +# # add condition for array elswhere call super +# # @param [Value] +# def _quote(value) +# case value +# when Array then "'{#{value.join(',')}}'" +# else +# super +# end +# end +# end +# end +# end +# end \ No newline at end of file diff --git a/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql_adapter.rb b/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql_adapter.rb index 4864cc0..e2aa765 100644 --- a/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql_adapter.rb +++ b/lib/bulk_data_methods/patches/active_record/connection_adapters/postgresql_adapter.rb @@ -1,11 +1,28 @@ require 'active_record' require 'active_record/connection_adapters/abstract_adapter' require 'active_record/connection_adapters/postgresql_adapter' +require 'active_record/connection_adapters/postgresql/quoting' module ActiveRecord module ConnectionAdapters - + module PostgreSQL + module Quoting + + private + # ****** BEGIN PATCH ****** + # when column of postgresql is an array, call super and fail. for example with [2] or ["2"]. + # add condition for array elswhere call super + # @param [Value] + def _quote(value) + case value + when Array then "'{#{value.join(',')}}'" + else + super + end + end + end + end # Patches extending the postgres adapter with new operations for managing # sequences (and sets of sequence values). # diff --git a/lib/bulk_data_methods/version.rb b/lib/bulk_data_methods/version.rb index 21c55d6..e3d88fa 100644 --- a/lib/bulk_data_methods/version.rb +++ b/lib/bulk_data_methods/version.rb @@ -1,3 +1,3 @@ module BulkDataMethods - VERSION = "3.0.1" + VERSION = "3.0.5" end