New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
return the inserted ids for postgres bulk insert #178
Changes from all commits
ee7411a
79245a4
07be534
5cd0cfc
c10107f
d254aaf
dce50a6
3507037
23bfe75
6110ef7
3e580bb
a3d3848
2d3593d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,3 @@ | |
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter | ||
include ActiveRecord::Import::PostgreSQLAdapter | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ def insert_many( sql, values, *args ) # :nodoc: | |
sql2insert = base_sql + values.join( ',' ) + post_sql | ||
insert( sql2insert, *args ) | ||
|
||
number_of_inserts | ||
[number_of_inserts,[]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking of making this return an OpenStruct or some basic object in case we decide to ever include more information. That will allow us to do that without breaking the interface again. I obviously didn't think of that happening originally. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, ignore the above comment here. I'm more interested in the public interface that users consume, less so about internal library interfaces. Although I wouldn't be opposed if they all allowed for easier extension or addition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another benefit is that of discovery: With module: > ActiveRecord::Base.connection.method(:post_sql_statements)
=> #<Method: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter(ActiveRecord::Import::PostgreSQLAdapter)#post_sql_statements> With alias: > ActiveRecord::Base.connection.method(:post_sql_statements)
#<Method: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#post_sql_statements> |
||
end | ||
|
||
def pre_sql_statements(options) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,34 @@ | ||
module ActiveRecord::Import::PostgreSQLAdapter | ||
include ActiveRecord::Import::ImportSupport | ||
|
||
def insert_many( sql, values, *args ) # :nodoc: | ||
number_of_inserts = 1 | ||
|
||
base_sql,post_sql = if sql.is_a?( String ) | ||
[ sql, '' ] | ||
elsif sql.is_a?( Array ) | ||
[ sql.shift, sql.join( ' ' ) ] | ||
end | ||
|
||
sql2insert = base_sql + values.join( ',' ) + post_sql | ||
ids = select_values( sql2insert, *args ) | ||
|
||
[number_of_inserts,ids] | ||
end | ||
|
||
def next_value_for_sequence(sequence_name) | ||
%{nextval('#{sequence_name}')} | ||
end | ||
|
||
def post_sql_statements( table_name, options ) # :nodoc: | ||
unless options[:primary_key].blank? | ||
super(table_name, options) << (" RETURNING #{options[:primary_key]}") | ||
else | ||
super(table_name, options) | ||
end | ||
end | ||
|
||
def support_setting_primary_key_of_imported_objects? | ||
true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
class Book < ActiveRecord::Base | ||
belongs_to :topic | ||
belongs_to :topic, :inverse_of=>:books | ||
has_many :chapters, :autosave => true, :inverse_of => :book | ||
has_many :end_notes, :autosave => true, :inverse_of => :book | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Chapter < ActiveRecord::Base | ||
belongs_to :book, :inverse_of=>:chapters | ||
validates :title, :presence => true | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class EndNote < ActiveRecord::Base | ||
belongs_to :book, :inverse_of=>:end_notes | ||
validates :note, :presence => true | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../test_helper') | ||
require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples') | ||
|
||
should_support_postgresql_import_functionality | ||
should_support_postgresql_import_functionality |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating this. I've created a local copy of the branch and have formatted and massaged this a little bit, but it's mostly intact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey thanks - I noticed you have stuff over in the wiki section, but it is important that folks realize why you bothered to write this in the first place.
This is important stuff. I think it should really make its way into the rails core ....