diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-01-16 03:42:55 -0200 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-01-16 03:42:55 -0200 |
commit | efe346f23a5c7489e32125529995640715ff8975 (patch) | |
tree | 314cbc683a61495c0ac76f7ee04f2226dd372d0c /activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb | |
parent | d3e2f1069857d278d162af2d3de1d9a3c3197f4b (diff) | |
parent | 89653edaf6845d50cc5ac6bc19815e581454ce77 (diff) | |
download | rails-efe346f23a5c7489e32125529995640715ff8975.tar.gz rails-efe346f23a5c7489e32125529995640715ff8975.tar.bz2 rails-efe346f23a5c7489e32125529995640715ff8975.zip |
Merge pull request #23067 from kamipo/sql_for_insert_returns_values_for_passing_to_exec_insert
`sql_for_insert` returns values for passing to `exec_insert`
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index c4f525336b..7a2a1a0e33 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -106,7 +106,7 @@ module ActiveRecord exec_query(sql, name, binds) end - # Returns the last auto-generated ID from the affected table. + # Executes an INSERT query and returns the new record's ID # # +id_value+ will be returned unless the value is nil, in # which case the database will attempt to calculate the last inserted @@ -115,9 +115,12 @@ module ActiveRecord # If the next id was calculated in advance (as in Oracle), it should be # passed in as +id_value+. def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) - insert_sql(to_sql(arel, binds), name, pk, id_value, sequence_name, binds) + sql, binds, pk, sequence_name = sql_for_insert(to_sql(arel, binds), pk, id_value, sequence_name, binds) + value = exec_insert(sql, name, binds, pk, sequence_name) + id_value || last_inserted_id(value) end alias create insert + alias insert_sql insert # Executes the update statement and returns the number of rows affected. def update(arel, name = nil, binds = []) @@ -353,13 +356,6 @@ module ActiveRecord end alias join_to_delete join_to_update - # Executes an INSERT query and returns the new record's ID - def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) - sql, binds = sql_for_insert(sql, pk, id_value, sequence_name, binds) - value = exec_insert(sql, name, binds, pk, sequence_name) - id_value || last_inserted_id(value) - end - protected # Returns a subquery for the given key using the join information. @@ -379,7 +375,7 @@ module ActiveRecord end def sql_for_insert(sql, pk, id_value, sequence_name, binds) - [sql, binds] + [sql, binds, pk, sequence_name] end def last_inserted_id(result) |