Skip to content

Commit

Permalink
Add recursive import to sqlite3 adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed May 31, 2016
1 parent bcf8eba commit 2aa5f31
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/activerecord-import/adapters/sqlite3_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def supports_import?(current_version = sqlite_version)
# elements that are in position >= 1 will be appended to the final SQL.
def insert_many(sql, values, *args) # :nodoc:
number_of_inserts = 0
ids = []

base_sql, post_sql = if sql.is_a?( String )
[sql, '']
elsif sql.is_a?( Array )
Expand All @@ -31,13 +33,19 @@ def insert_many(sql, values, *args) # :nodoc:
value_sets.each do |value_set|
number_of_inserts += 1
sql2insert = base_sql + value_set.join( ',' ) + post_sql
insert( sql2insert, *args )
first_insert_id = insert( sql2insert, *args )
last_insert_id = first_insert_id + value_set.size - 1
ids.concat (first_insert_id..last_insert_id).to_a
end

[number_of_inserts, []]
[number_of_inserts, ids]
end

def next_value_for_sequence(sequence_name)
%{nextval('#{sequence_name}')}
end

def support_setting_primary_key_of_imported_objects?
true
end
end
2 changes: 1 addition & 1 deletion lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def import_without_validations_or_callbacks( column_names, array_of_attributes,
end
else
values_sql.each do |values|
connection.execute(insert_sql + values)
ids << connection.insert(insert_sql + values)
number_inserted += 1
end
end
Expand Down

0 comments on commit 2aa5f31

Please sign in to comment.