multi_insert 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/multi_insert/hooks/active_record.rb +2 -2
- data/lib/multi_insert/query.rb +2 -2
- data/lib/multi_insert/query_builder.rb +10 -4
- data/lib/multi_insert/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: c75ea9caa10a1bc32e4351f25612fc7008916d29
|
4
|
+
data.tar.gz: 84f9e785d7dace05d0cc89f5104eadba95815153
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9379b6694ca7040cf7b8df04d2f182c228d5589edccd15774c8e907ac45fef607cd042ff6e184a2ec6e425c82356ea4e4a8c6ff2a7c7475ed6ec1cd7c98f5215
|
7
|
+
data.tar.gz: 9bb296fee356a6134ee71c2d0502654685e9f07c423f4426a8509c58265d42b6d0fc2f01096b4605ba15b230734b61e8d0833756abe224fb1a448cb7a5d7ed2a
|
@@ -2,7 +2,7 @@ require 'active_record'
|
|
2
2
|
require 'multi_insert/query'
|
3
3
|
|
4
4
|
class ActiveRecord::Base
|
5
|
-
def self.multi_insert(columns, values)
|
6
|
-
::MultiInsert::Query.new(table_name, columns, values)
|
5
|
+
def self.multi_insert(columns, values, opts = {})
|
6
|
+
::MultiInsert::Query.new(table_name, columns, values, opts)
|
7
7
|
end
|
8
8
|
end
|
data/lib/multi_insert/query.rb
CHANGED
@@ -3,9 +3,9 @@ require 'multi_insert/query_builder'
|
|
3
3
|
|
4
4
|
module MultiInsert
|
5
5
|
class Query
|
6
|
-
def initialize(table, columns, values)
|
6
|
+
def initialize(table, columns, values, opts = {})
|
7
7
|
@table = table.to_sym
|
8
|
-
@sql_insert = ::MultiInsert::QueryBuilder.insert(table, columns, values)
|
8
|
+
@sql_insert = ::MultiInsert::QueryBuilder.insert(table, columns, values, opts = {})
|
9
9
|
end
|
10
10
|
|
11
11
|
def returning(columns)
|
@@ -2,19 +2,25 @@ require 'active_record'
|
|
2
2
|
|
3
3
|
module MultiInsert
|
4
4
|
module QueryBuilder
|
5
|
-
def self.insert(table, columns, values)
|
5
|
+
def self.insert(table, columns, values, opts = {})
|
6
6
|
ar = ActiveRecord::Base.connection
|
7
7
|
|
8
|
-
|
8
|
+
options = {time: true}
|
9
|
+
options.merge!(opts)
|
10
|
+
|
11
|
+
now = Time.now.to_s(:db) if options[:time]
|
9
12
|
table = ar.quote_table_name(table.to_s)
|
10
13
|
|
11
14
|
# Format columns
|
12
|
-
columns = columns + [:created_at, :updated_at]
|
15
|
+
columns = columns + [:created_at, :updated_at] if options[:time]
|
13
16
|
columns = columns.map!{|c| ar.quote_column_name(c.to_s)}
|
14
17
|
columns = join_params(columns)
|
15
18
|
|
16
19
|
# Format values
|
17
|
-
|
20
|
+
if options[:time]
|
21
|
+
values = values.map{|v| v + [now, now]}
|
22
|
+
end
|
23
|
+
values = values.map{|v| join_params(v.map{|vv| ar.quote(vv.to_s)})}.join(',')
|
18
24
|
|
19
25
|
"INSERT INTO #{table} #{columns} VALUES #{values}"
|
20
26
|
end
|
data/lib/multi_insert/version.rb
CHANGED