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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14fe30a32e5c6287987c5ef9c096ac641711c65a
4
- data.tar.gz: 5c08654357b2f14e7599a9a63f9246d821c31223
3
+ metadata.gz: 832a9abb0d1f4a46f8f0a751ec1dce7a93cc01de
4
+ data.tar.gz: 415a740b0be619998f708ae58fd8144155327be9
5
5
  SHA512:
6
- metadata.gz: f5b0e2453ac9474b8d7f37d69af7911bbe800e3c46f55949ce66e45f465b91bb3228ba1b392be125343b1650accb033d9cec751dc7d29529d3d6190b61e3bc8f
7
- data.tar.gz: 66f3ccccc3d543da757f4f627b1b165dde379f9ccf70c15ebac9ab1ea078cd008e226795da6fdd306e5a5b6f0cd75692c2e0d35dc7fb1f7ba5d196939174e2ff
6
+ metadata.gz: 1dbe2dd054728c86813136dbc22ce6484c6cdd3c508fea3dcc6bde96ed3fa73a197bfcbccdd0dc5442ab3cb9971e138bb2c0d3a481a498c80d2c0c0b5f0a37aa
7
+ data.tar.gz: 8a6474bcd0c7099ab5737b3a38736167e1eea0e0516914d3ed4a88b2e2553bcf7b994c289fa6cfd77891b2313291daf0805d85ef27e7143f7748190f8f54b277
@@ -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
- sql = @sql_insert
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
@@ -1,3 +1,3 @@
1
1
  module MultiInsert
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nax