提交 89653eda 编写于 作者: R Ryuta Kamizono

`sql_for_insert` returns values for passing to `exec_insert`

上级 b2874aec
......@@ -106,7 +106,7 @@ def exec_update(sql, name, binds)
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 @@ def exec_update(sql, name, binds)
# 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 @@ def join_to_update(update, select, key) # :nodoc:
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 @@ def select_prepared(sql, name = nil, binds = [])
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)
......
......@@ -72,16 +72,6 @@ def select_rows(sql, name = nil, binds = [])
end
end
# 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 = []) # :nodoc:
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
super
end
# The internal PostgreSQL identifier of the money data type.
MONEY_COLUMN_TYPE_OID = 790 #:nodoc:
# The internal PostgreSQL identifier of the BYTEA data type.
......@@ -162,12 +152,18 @@ def exec_delete(sql, name = 'SQL', binds = [])
end
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) # :nodoc:
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?
sql = "#{sql} RETURNING #{quote_column_name(pk)}"
end
[sql, binds]
super
end
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
......
......@@ -139,8 +139,8 @@ def test_exec_insert_default_values_quoted_schema_with_returning_disabled_and_no
def test_sql_for_insert_with_returning_disabled
connection = connection_without_insert_returning
result = connection.sql_for_insert('sql', nil, nil, nil, 'binds')
assert_equal ['sql', 'binds'], result
sql, binds = connection.sql_for_insert('sql', nil, nil, nil, 'binds')
assert_equal ['sql', 'binds'], [sql, binds]
end
def test_serial_sequence
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册