4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb', line 4
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
sql, binds = to_sql_and_binds(arel, binds)
value = exec_insert(sql, name, binds, pk, sequence_name)
return id_value if id_value
if pk.is_a?(Array) && value.respond_to?(:empty?) && !value.empty?
pk.map {|key| value.first[key]}
elsif pk.is_a?(Array)
result = Array.new(pk.size)
auto_key = pk.find do |key|
attribute = arel.ast.relation[key]
column = column_for_attribute(attribute)
if column.respond_to?(:auto_increment?)
column.auto_increment?
end
end
if auto_key
result[pk.index(auto_key)] = last_inserted_id(value)
end
result
else
last_inserted_id(value)
end
end
|