1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #22945 from kamipo/refactor_connection_insert_sql

Refactor `connection.insert_sql`
This commit is contained in:
Rafael França 2016-01-06 18:44:23 -02:00
commit 6a42f0093e
4 changed files with 10 additions and 35 deletions

View file

@ -115,9 +115,7 @@ module ActiveRecord
# If the next id was calculated in advance (as in Oracle), it should be # If the next id was calculated in advance (as in Oracle), it should be
# passed in as +id_value+. # passed in as +id_value+.
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
sql, binds = sql_for_insert(to_sql(arel, binds), pk, id_value, sequence_name, binds) insert_sql(to_sql(arel, binds), name, pk, id_value, sequence_name, binds)
value = exec_insert(sql, name, binds, pk, sequence_name)
id_value || last_inserted_id(value)
end end
alias create insert alias create insert
@ -352,6 +350,13 @@ module ActiveRecord
end end
alias join_to_delete join_to_update 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 protected
# Returns a subquery for the given key using the join information. # Returns a subquery for the given key using the join information.
@ -370,12 +375,6 @@ module ActiveRecord
exec_query(sql, name, binds, prepare: true) exec_query(sql, name, binds, prepare: true)
end end
# Returns the last auto-generated ID from the affected table.
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
execute(sql, name)
id_value
end
# Executes the update statement and returns the number of rows affected. # Executes the update statement and returns the number of rows affected.
def update_sql(sql, name = nil) def update_sql(sql, name = nil)
execute(sql, name) execute(sql, name)

View file

@ -136,11 +136,6 @@ module ActiveRecord
alias exec_without_stmt exec_query alias exec_without_stmt exec_query
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
super
id_value || @connection.last_id
end
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil) def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
execute to_sql(sql, binds), name execute to_sql(sql, binds), name
end end

View file

@ -73,21 +73,13 @@ module ActiveRecord
end end
# Executes an INSERT query and returns the new record's ID # 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) def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) # :nodoc:
unless pk unless pk
# Extract the table from the insert sql. Yuck. # Extract the table from the insert sql. Yuck.
table_ref = extract_table_ref_from_insert_sql(sql) table_ref = extract_table_ref_from_insert_sql(sql)
pk = primary_key(table_ref) if table_ref pk = primary_key(table_ref) if table_ref
end end
if pk && use_insert_returning?
select_value("#{sql} RETURNING #{quote_column_name(pk)}")
elsif pk
super super
last_insert_id_value(sequence_name || default_sequence_name(table_ref, pk))
else
super
end
end end
# The internal PostgreSQL identifier of the money data type. # The internal PostgreSQL identifier of the money data type.
@ -171,12 +163,6 @@ module ActiveRecord
alias :exec_update :exec_delete alias :exec_update :exec_delete
def sql_for_insert(sql, pk, id_value, sequence_name, binds) def sql_for_insert(sql, pk, id_value, sequence_name, binds)
unless pk
# Extract the table from the insert sql. Yuck.
table_ref = extract_table_ref_from_insert_sql(sql)
pk = primary_key(table_ref) if table_ref
end
if pk && use_insert_returning? if pk && use_insert_returning?
sql = "#{sql} RETURNING #{quote_column_name(pk)}" sql = "#{sql} RETURNING #{quote_column_name(pk)}"
end end

View file

@ -290,11 +290,6 @@ module ActiveRecord
super sql, name super sql, name
end end
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
super
id_value || @connection.last_insert_row_id
end
def select_rows(sql, name = nil, binds = []) def select_rows(sql, name = nil, binds = [])
exec_query(sql, name, binds).rows exec_query(sql, name, binds).rows
end end