import_many, import_*: returns last_inserted_id

This commit is contained in:
Denis Knauf 2013-01-03 17:07:10 +01:00
parent 1c675d7c31
commit 038a29682e
2 changed files with 17 additions and 15 deletions

View file

@ -34,7 +34,7 @@ module ActiveRecord::Import::AbstractAdapter
# elements that are in position >= 1 will be appended to the final SQL. # elements that are in position >= 1 will be appended to the final SQL.
def insert_many( sql, values, *args ) # :nodoc: def insert_many( sql, values, *args ) # :nodoc:
# the number of inserts default # the number of inserts default
number_of_inserts = 0 number_of_inserts, last_inserted_id = 0, nil
base_sql,post_sql = if sql.is_a?( String ) base_sql,post_sql = if sql.is_a?( String )
[ sql, '' ] [ sql, '' ]
@ -59,17 +59,17 @@ module ActiveRecord::Import::AbstractAdapter
if NO_MAX_PACKET == max or total_bytes < max if NO_MAX_PACKET == max or total_bytes < max
number_of_inserts += 1 number_of_inserts += 1
sql2insert = base_sql + values.join( ',' ) + post_sql sql2insert = base_sql + values.join( ',' ) + post_sql
insert( sql2insert, *args ) last_inserted_id = insert( sql2insert, *args )
else else
value_sets = self.class.get_insert_value_sets( values, sql_size, max ) value_sets = self.class.get_insert_value_sets( values, sql_size, max )
value_sets.each do |values| value_sets.each do |values|
number_of_inserts += 1 number_of_inserts += 1
sql2insert = base_sql + values.join( ',' ) + post_sql sql2insert = base_sql + values.join( ',' ) + post_sql
insert( sql2insert, *args ) last_inserted_id = insert( sql2insert, *args )
end end
end end
number_of_inserts [number_of_inserts, last_inserted_id]
end end
def pre_sql_statements(options) def pre_sql_statements(options)

View file

@ -3,7 +3,7 @@ require "ostruct"
module ActiveRecord::Import::ConnectionAdapters ; end module ActiveRecord::Import::ConnectionAdapters ; end
module ActiveRecord::Import #:nodoc: module ActiveRecord::Import #:nodoc:
class Result < Struct.new(:failed_instances, :num_inserts) class Result < Struct.new(:failed_instances, :num_inserts, :last_inserted_id)
end end
module ImportSupport #:nodoc: module ImportSupport #:nodoc:
@ -162,9 +162,10 @@ class ActiveRecord::Base
# BlogPost.import columns, attributes, :on_duplicate_key_update=>{ :title => :title } # BlogPost.import columns, attributes, :on_duplicate_key_update=>{ :title => :title }
# #
# = Returns # = Returns
# This returns an object which responds to +failed_instances+ and +num_inserts+. # This returns an object which responds to +failed_instances+, +num_inserts+, +last_inserted_id+.
# * failed_instances - an array of objects that fails validation and were not committed to the database. An empty array if no validation is performed. # * failed_instances - an array of objects that fails validation and were not committed to the database. An empty array if no validation is performed.
# * num_inserts - the number of insert statements it took to import the data # * num_inserts - the number of insert statements it took to import the data.
# * last_inserted_id - the last inserted id. Should be the id of the latest inserted row.
def import( *args ) def import( *args )
options = { :validate=>true, :timestamps=>true } options = { :validate=>true, :timestamps=>true }
options.merge!( args.pop ) if args.last.is_a? Hash options.merge!( args.pop ) if args.last.is_a? Hash
@ -191,7 +192,7 @@ class ActiveRecord::Base
end end
# supports empty array # supports empty array
elsif args.last.is_a?( Array ) and args.last.empty? elsif args.last.is_a?( Array ) and args.last.empty?
return ActiveRecord::Import::Result.new([], 0) if args.last.empty? return ActiveRecord::Import::Result.new([], 0, nil) if args.last.empty?
# supports 2-element array and array # supports 2-element array and array
elsif args.size == 2 and args.first.is_a?( Array ) and args.last.is_a?( Array ) elsif args.size == 2 and args.first.is_a?( Array ) and args.last.is_a?( Array )
column_names, array_of_attributes = args column_names, array_of_attributes = args
@ -218,8 +219,8 @@ class ActiveRecord::Base
return_obj = if is_validating return_obj = if is_validating
import_with_validations( column_names, array_of_attributes, options ) import_with_validations( column_names, array_of_attributes, options )
else else
num_inserts = import_without_validations_or_callbacks( column_names, array_of_attributes, options ) [num_inserts, last_inserted_id] = import_without_validations_or_callbacks( column_names, array_of_attributes, options )
ActiveRecord::Import::Result.new([], num_inserts) ActiveRecord::Import::Result.new([], num_inserts, last_inserted_id)
end end
if options[:synchronize] if options[:synchronize]
@ -261,12 +262,12 @@ class ActiveRecord::Base
end end
array_of_attributes.compact! array_of_attributes.compact!
num_inserts = if array_of_attributes.empty? || options[:all_or_none] && failed_instances.any? num_inserts, last_inserted_id = if array_of_attributes.empty? || options[:all_or_none] && failed_instances.any?
0 [0, nil]
else else
import_without_validations_or_callbacks( column_names, array_of_attributes, options ) import_without_validations_or_callbacks( column_names, array_of_attributes, options )
end end
ActiveRecord::Import::Result.new(failed_instances, num_inserts) ActiveRecord::Import::Result.new(failed_instances, num_inserts, last_inserted_id)
end end
# Imports the passed in +column_names+ and +array_of_attributes+ # Imports the passed in +column_names+ and +array_of_attributes+
@ -276,6 +277,7 @@ 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={} )
number_inserted, last_inserted_id = 0, nil
scope_columns, scope_values = scope_attributes.to_a.transpose scope_columns, scope_values = scope_attributes.to_a.transpose
unless scope_columns.blank? unless scope_columns.blank?
@ -305,11 +307,11 @@ class ActiveRecord::Base
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
number_inserted = connection.insert_many( [ insert_sql, post_sql_statements ].flatten, number_inserted, last_inserted_id = connection.insert_many( [ insert_sql, post_sql_statements ].flatten,
values_sql, values_sql,
"#{self.class.name} Create Many Without Validations Or Callbacks" ) "#{self.class.name} Create Many Without Validations Or Callbacks" )
end end
number_inserted [number_inserted, last_inserted_id]
end end
private private