Refactor remaining supports_import? split to share code in values_sql_for_column_names_and_attributes. This means both with properly support sequences.
Rather than pass a ton of variables (sequence_name, columns, column_names, &c.), move the method out of the connection. Also, use Column#primary to check primary key status.
This commit is contained in:
parent
5f4d9b3290
commit
3756e4f2b4
|
@ -259,25 +259,16 @@ class ActiveRecord::Base
|
||||||
array_of_attributes.first.each_with_index { |arr,i| columns << columns_hash[ column_names[i].to_s ] }
|
array_of_attributes.first.each_with_index { |arr,i| columns << columns_hash[ column_names[i].to_s ] }
|
||||||
|
|
||||||
columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})"
|
columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})"
|
||||||
|
values_sql = values_sql_for_attributes(array_of_attributes)
|
||||||
if not supports_import?
|
if not supports_import?
|
||||||
insert_statements, values = [], []
|
|
||||||
number_inserted = 0
|
number_inserted = 0
|
||||||
array_of_attributes.each do |arr|
|
values_sql.each do |values|
|
||||||
my_values = arr.each_with_index.map do |val,j|
|
connection.execute("INSERT INTO #{quoted_table_name} #{columns_sql} VALUES#{values}")
|
||||||
if !sequence_name.blank? && column_names[j] == primary_key && val.nil?
|
|
||||||
connection.next_value_for_sequence(sequence_name)
|
|
||||||
else
|
|
||||||
connection.quote( val, columns[j] )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
insert_statements << "INSERT INTO #{quoted_table_name} #{columns_sql} VALUES(" + my_values.join( ',' ) + ")"
|
|
||||||
connection.execute( insert_statements.last )
|
|
||||||
number_inserted += 1
|
number_inserted += 1
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
# generate the sql
|
# generate the sql
|
||||||
insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} #{columns_sql} VALUES "
|
insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} #{columns_sql} VALUES "
|
||||||
values_sql = connection.values_sql_for_column_names_and_attributes( columns, array_of_attributes )
|
|
||||||
post_sql_statements = connection.post_sql_statements( quoted_table_name, options )
|
post_sql_statements = connection.post_sql_statements( quoted_table_name, options )
|
||||||
|
|
||||||
# perform the inserts
|
# perform the inserts
|
||||||
|
@ -290,6 +281,21 @@ class ActiveRecord::Base
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# Returns SQL the VALUES for an INSERT statement given the passed in +columns+
|
||||||
|
# and +array_of_attributes+.
|
||||||
|
def values_sql_for_attributes(array_of_attributes ) # :nodoc:
|
||||||
|
array_of_attributes.map do |arr|
|
||||||
|
my_values = arr.each_with_index.map do |val,j|
|
||||||
|
if !sequence_name.blank? && columns[j].primary && val.nil?
|
||||||
|
connection.next_value_for_sequence(sequence_name)
|
||||||
|
else
|
||||||
|
connection.quote(val, columns[j])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
"(#{my_values.join(',')})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def add_special_rails_stamps( column_names, array_of_attributes, options )
|
def add_special_rails_stamps( column_names, array_of_attributes, options )
|
||||||
AREXT_RAILS_COLUMNS[:create].each_pair do |key, blk|
|
AREXT_RAILS_COLUMNS[:create].each_pair do |key, blk|
|
||||||
if self.column_names.include?(key)
|
if self.column_names.include?(key)
|
||||||
|
|
Loading…
Reference in a new issue