multi_insert 0.1.7 → 0.2.0
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/query.rb +28 -3
- data/lib/multi_insert/query_builder.rb +35 -0
- 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: 832a9abb0d1f4a46f8f0a751ec1dce7a93cc01de
|
4
|
+
data.tar.gz: 415a740b0be619998f708ae58fd8144155327be9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dbe2dd054728c86813136dbc22ce6484c6cdd3c508fea3dcc6bde96ed3fa73a197bfcbccdd0dc5442ab3cb9971e138bb2c0d3a481a498c80d2c0c0b5f0a37aa
|
7
|
+
data.tar.gz: 8a6474bcd0c7099ab5737b3a38736167e1eea0e0516914d3ed4a88b2e2553bcf7b994c289fa6cfd77891b2313291daf0805d85ef27e7143f7748190f8f54b277
|
data/lib/multi_insert/query.rb
CHANGED
@@ -3,8 +3,26 @@ require 'multi_insert/query_builder'
|
|
3
3
|
|
4
4
|
module MultiInsert
|
5
5
|
class Query
|
6
|
+
class OnConflict
|
7
|
+
def initialize(query, column)
|
8
|
+
@query = query
|
9
|
+
@column = column.to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def do_nothing
|
13
|
+
@query.on_conflict_sql(::MultiInsert::QueryBuilder.on_conflict_do_nothing(@column))
|
14
|
+
end
|
15
|
+
|
16
|
+
def do_update(values)
|
17
|
+
@query.on_conflict_sql(::MultiInsert::QueryBuilder.on_conflict_do_update(@column, values, @query.opts))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :opts
|
22
|
+
|
6
23
|
def initialize(table, columns, values, opts = {})
|
7
24
|
@table = table.to_sym
|
25
|
+
@opts = opts
|
8
26
|
@sql_insert = ::MultiInsert::QueryBuilder.insert(table, columns, values, opts)
|
9
27
|
end
|
10
28
|
|
@@ -20,10 +38,17 @@ module MultiInsert
|
|
20
38
|
self
|
21
39
|
end
|
22
40
|
|
41
|
+
def on_conflict(column)
|
42
|
+
::MultiInsert::Query::OnConflict.new(self, column)
|
43
|
+
end
|
44
|
+
|
45
|
+
def on_conflict_sql(sql)
|
46
|
+
@sql_on_conflict = sql
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
23
50
|
def to_sql
|
24
|
-
|
25
|
-
sql = "#{sql} #{@sql_returning}" unless @sql_returning.nil?
|
26
|
-
sql
|
51
|
+
[@sql_insert, @sql_on_conflict, @sql_returning].reject(&:nil?).join(' ')
|
27
52
|
end
|
28
53
|
|
29
54
|
def to_s
|
@@ -33,6 +33,41 @@ module MultiInsert
|
|
33
33
|
"RETURNING #{columns}"
|
34
34
|
end
|
35
35
|
|
36
|
+
def self.on_conflict(column)
|
37
|
+
column = ActiveRecord::Base.connection.quote_column_name(column.to_s)
|
38
|
+
"ON CONFLICT (#{column})"
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.on_conflict_do_nothing(column)
|
42
|
+
"#{on_conflict(column)} DO NOTHING"
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.on_conflict_do_update(column, values, opts = {})
|
46
|
+
opts = INSERT_DEFAULTS.merge(opts)
|
47
|
+
if values.is_a?(Symbol) || values.is_a?(String)
|
48
|
+
tmp = {}
|
49
|
+
tmp[values] = :excluded
|
50
|
+
elsif values.is_a?(Array)
|
51
|
+
values = values.product([:excluded]).to_h
|
52
|
+
end
|
53
|
+
if opts[:time]
|
54
|
+
now = Time.now.to_s(:db)
|
55
|
+
values[:updated_at] = now
|
56
|
+
end
|
57
|
+
values.map! do |key, value|
|
58
|
+
v = nil
|
59
|
+
key = ActiveRecord::Base.connection.quote_column_name(key)
|
60
|
+
if value == :excluded
|
61
|
+
v = "excluded.#{key}"
|
62
|
+
else
|
63
|
+
v = ActiveRecord::Base.connection.quote(value)
|
64
|
+
end
|
65
|
+
"#{key} = #{v}"
|
66
|
+
end
|
67
|
+
values = values.join(', ')
|
68
|
+
"#{on_conflict(column)} DO UPDATE SET #{values}"
|
69
|
+
end
|
70
|
+
|
36
71
|
def self.join_params(params)
|
37
72
|
"(" + params.join(',') + ")"
|
38
73
|
end
|
data/lib/multi_insert/version.rb
CHANGED