activerecord-insert_many 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/activerecord/insert_many.rb +25 -22
- data/lib/activerecord/insert_many/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9222fd773f9ee759b910f2edfda04fde4ea3802b
|
4
|
+
data.tar.gz: a317d1422da83df65b18063628ee6905ff005bdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5fb0d324f4cea1332e10bf161773e50f0ec1fc7ae020dba4a7db90834912850c3f6d2f9b7414eda62b39325866380a0aa23749cbb55304f6eb2b341b101e7d1
|
7
|
+
data.tar.gz: 75e48eed96ce5823ee9e2e7dd71c50379d5a9c48bd96be0781752f965f45970290f48bdf82219480517fa46eeaf387d947a5f919fc3cf8d398a99fdfff0f4d24
|
@@ -2,37 +2,40 @@ require "activerecord/insert_many/version"
|
|
2
2
|
require "active_record"
|
3
3
|
require "active_record/connection_adapters/abstract_adapter"
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
5
|
+
module ActiveRecord
|
6
|
+
module InsertMany
|
7
|
+
def insert_many(fixtures)
|
8
|
+
connection.insert_many(fixtures, table_name)
|
9
|
+
end
|
8
10
|
end
|
9
|
-
end
|
10
11
|
|
11
|
-
module InsertManyStatement
|
12
|
-
|
13
|
-
|
12
|
+
module InsertManyStatement
|
13
|
+
def insert_many(fixtures, table_name=self.table_name)
|
14
|
+
return if fixtures.empty?
|
14
15
|
|
15
|
-
|
16
|
+
columns = schema_cache.columns_hash(table_name)
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
sample = fixtures.first
|
19
|
+
key_list = sample.map { |name, value| quote_column_name(name) }
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
value_lists = fixtures.map do |fixture|
|
22
|
+
fixture.map do |name, value|
|
23
|
+
quote(value, columns[name])
|
24
|
+
end
|
23
25
|
end
|
24
|
-
end
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
primary_key_column = schema_cache.primary_keys(table_name)
|
28
|
+
returning = supports_returning? && primary_key_column.present? ? " RETURNING #{primary_key_column}" : ""
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
execute "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES #{value_lists.map { |value| "(#{value.join(', ')})" }.join(",")}#{returning}", "Fixture Insert"
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
def supports_returning?
|
34
|
+
self.class.name == "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
|
35
|
+
end
|
34
36
|
end
|
37
|
+
|
35
38
|
end
|
36
39
|
|
37
|
-
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, InsertManyStatement
|
38
|
-
ActiveRecord::Base.extend InsertMany
|
40
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, ActiveRecord::InsertManyStatement
|
41
|
+
ActiveRecord::Base.extend ActiveRecord::InsertMany
|