Extract columns_sql local more generally in #import_without_validations_or_callbacks. This is DRY-er, more consistent between the supports_import? cases and enables us to inline #quote_column_names

This commit is contained in:
Ben Woosley 2010-11-16 23:22:10 -08:00 committed by Zach Dennis
parent 6b2153a841
commit 5f4d9b3290

View file

@ -255,12 +255,11 @@ class ActiveRecord::Base
# information on +column_names+, +array_of_attributes_ and # information on +column_names+, +array_of_attributes_ and
# +options+. # +options+.
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} ) def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
escaped_column_names = quote_column_names( column_names )
columns = [] columns = []
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(',')})"
if not supports_import? if not supports_import?
columns_sql = "(" + escaped_column_names.join( ',' ) + ")"
insert_statements, values = [], [] insert_statements, values = [], []
number_inserted = 0 number_inserted = 0
array_of_attributes.each do |arr| array_of_attributes.each do |arr|
@ -277,7 +276,7 @@ class ActiveRecord::Base
end end
else else
# generate the sql # generate the sql
insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} (#{escaped_column_names.join(',')}) 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 ) 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 )
@ -288,13 +287,7 @@ class ActiveRecord::Base
end end
number_inserted number_inserted
end end
# Returns an array of quoted column names
def quote_column_names( names )
names.map{ |name| connection.quote_column_name( name ) }
end
private private
def add_special_rails_stamps( column_names, array_of_attributes, options ) def add_special_rails_stamps( column_names, array_of_attributes, options )