pg_conn 0.24.0 → 0.25.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
  SHA256:
3
- metadata.gz: 45c913cadea7c545637f0291133fd43f727beef5efe726377c406e720c105d7d
4
- data.tar.gz: 25ed1737f4bba574d02c7d1e1d823957de723f86e2eeca94f9ea8ef6160c191e
3
+ metadata.gz: eb9303d38e0364b374379d8d0549d461decf393d1a20c5a97da42b1c0b0d7e17
4
+ data.tar.gz: 53512feaaff24b3b6e8b7366a0c860dac0d38f8880698570cfa16cffbb7a46b2
5
5
  SHA512:
6
- metadata.gz: 696866896b4bea3ebec5313398f580581627535ccf7fca01f94f6f7e9a6198ceff7173b81d21d3725d4d7f0ac7118428a0eab2a098d3f360913926c46d460eaf
7
- data.tar.gz: de5d2541d1384bfa7721f7ec3274f1a232b97d9bc08791f6f5a3c5783b19d5a8d65ac25d7d37cea0e47e27fc4391dad4eb0f15f96584875e0ed526d98bb1a520
6
+ metadata.gz: b9efb0689c559425bee8bebb5d984c72222e8bd2377803cc8c1415d5b253ada2f0913dc70b17ef12c0f9677b26e1db60accb5bdb66de41220f7001a2628c013c
7
+ data.tar.gz: fcd4f4e04157e2cdf10226f1a4545ab8fd66ce960cf14c732e0eceda9450e3c94b211277da303e8338aff7fbfbb3eb09bcd6b5946e979aa383f5f3b803ac86d7
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.24.0"
2
+ VERSION = "0.25.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -630,7 +630,10 @@ module PgConn
630
630
  #
631
631
  # There is no variant that takes a single tuple because it would then be
632
632
  # impossible to have array or hash field values
633
- def insert(*args)
633
+ def insert(*args, upsert: nil, **opts)
634
+ # Add options to args except the special :upsert option
635
+ args << opts if !opts.empty?
636
+
634
637
  # Add schema (=nil) if absent
635
638
  args.unshift nil if args.size == 2 || (args.size == 3 && args[1].is_a?(Array))
636
639
 
@@ -665,17 +668,33 @@ module PgConn
665
668
  fields ||= data.keys
666
669
  tuples = [fields.map { |field| data[field] }]
667
670
  else
668
- raise ArgumentError
671
+ raise ArgumentError, "Illegal argument '#{data.inspect}'"
669
672
  end
670
673
 
674
+ # On-conflict clause
675
+ upsert_sql =
676
+ case upsert
677
+ when true; "on conflict do nothing"
678
+ when String; "on conlict #{upsert}"
679
+ when false, nil; ""
680
+ else
681
+ raise ArgumentError, "Illegal value for :upsert option: #{upsert.inspect}"
682
+ end
683
+
671
684
  # Execute SQL statement using either :value or :values depending on data arity
672
685
  self.send method, %(
673
686
  insert into #{table} (#{quote_identifiers(fields)})
674
687
  values #{quote_tuples(tuples)}
688
+ #{upsert_sql}
675
689
  returning id
676
690
  )
677
691
  end
678
692
 
693
+ # Use upsert. Currently on 'on conflict do nothing' is supported
694
+ def upsert(*args)
695
+ insert(*args, upsert: true)
696
+ end
697
+
679
698
  # Update record(s)
680
699
  def update(schema = nil, table, expr, hash)
681
700
  table = [schema, table].compact.join(".")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.0
4
+ version: 0.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-07 00:00:00.000000000 Z
11
+ date: 2024-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg