14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/activerecord/insert_many.rb', line 14
def insert_many(fixtures, table_name=self.table_name)
return if fixtures.empty?
columns = schema_cache.columns_hash(table_name)
sample = fixtures.first
key_list = sample.map { |name, value| quote_column_name(name) }
value_lists = fixtures.map do |fixture|
binds = fixture.map do |name, value|
name = name.to_s
if column = columns[name]
type = lookup_cast_type_from_column(column)
Relation::QueryAttribute.new(name, value, type)
else
raise Fixture::FixtureError, %(table "#{table_name}" has no column named #{name.inspect}.)
end
end
prepare_binds_for_database(binds).map do |value|
begin
quote(value)
rescue TypeError
quote(YAML.dump(value))
end
end
end
primary_key_column = schema_cache.primary_keys(table_name)
returning = supports_returning? && primary_key_column.present? ? " RETURNING #{primary_key_column}" : ""
execute "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES #{value_lists.map { |value| "(#{value.join(', ')})" }.join(",")}#{returning}", "Fixture Insert"
end
|