pg_conn 0.23.0 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/TODO +18 -6
  3. data/lib/pg_conn/version.rb +1 -1
  4. data/lib/pg_conn.rb +35 -7
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7de75668042e3abdc5b983e0b9a3f6c909ff69aeed2f22c632d1c693f3baac0
4
- data.tar.gz: 90cecbdd79e6478d7f6bd1bc3ae042917bc2683771c89de61aab6c05fa2e5279
3
+ metadata.gz: eb9303d38e0364b374379d8d0549d461decf393d1a20c5a97da42b1c0b0d7e17
4
+ data.tar.gz: 53512feaaff24b3b6e8b7366a0c860dac0d38f8880698570cfa16cffbb7a46b2
5
5
  SHA512:
6
- metadata.gz: 76b775138260668bf75e5bc31e220561f665dffc901349a9ec875e7ec594dee2750e0d979bde80e9045e38e44a3894fe55c2be37faefbe7f35173812cea2a22a
7
- data.tar.gz: f0e2e9169cd734814cce4e8b73f7ec017cf3fd9a2622290c4acbf06620ddb3b943957ab5dab527ee0e29a9d71ad4efb511f4f219543ca86301d9fd5c4f5838ad
6
+ metadata.gz: b9efb0689c559425bee8bebb5d984c72222e8bd2377803cc8c1415d5b253ada2f0913dc70b17ef12c0f9677b26e1db60accb5bdb66de41220f7001a2628c013c
7
+ data.tar.gz: fcd4f4e04157e2cdf10226f1a4545ab8fd66ce960cf14c732e0eceda9450e3c94b211277da303e8338aff7fbfbb3eb09bcd6b5946e979aa383f5f3b803ac86d7
data/TODO CHANGED
@@ -1,4 +1,20 @@
1
1
  TODO
2
+
3
+ o Instrumentation of connection object
4
+
5
+ # Augment conn object exec method to emit duration of the query
6
+ conn.define_singleton_method(:original_exec, conn.method(:exec))
7
+ conn.define_singleton_method(:exec) do |*args|
8
+ t0 = Time.now
9
+ r = original_exec(*args)
10
+ t1 = Time.now
11
+ @duration = ((t1 - t0) * 1000).round()
12
+ r
13
+ end
14
+ conn.define_singleton_method(:duration) do
15
+ @duration
16
+ end
17
+
2
18
  o Use :elem_type everywhere
3
19
  o Use 'drop ... cascade' everywhere
4
20
  o db.context(schema: app_portal, transaction: true) { ... }
@@ -19,7 +35,6 @@ TODO
19
35
 
20
36
  server.call :sp_nic_update_comtext, str_id, str_comtext, site.current_user.id
21
37
 
22
-
23
38
  o Have a 'with' method that combines multiple brachet-methods:
24
39
 
25
40
  conn.with(schema: public, transation: true) { ... }
@@ -34,9 +49,8 @@ TODO
34
49
  struct # exists
35
50
  structs
36
51
 
37
-
38
52
  o Make rdbms, role, schema, and session methods part of a PgModel file that
39
- monkey patch PgConn objects to include a #model method that returns the
53
+ monkey patch PgConn objects to include a #model method that returns the
40
54
  top-level rdbms object
41
55
 
42
56
  o #group method
@@ -67,8 +81,6 @@ TODO
67
81
  end
68
82
  end
69
83
 
70
-
71
-
72
84
  o Use enumerators
73
85
  o Use SQL parameters. Optionally with a different syntax:
74
86
  $var - Value that gets single-quoted
@@ -93,7 +105,7 @@ TODO
93
105
  # grant(role, to_role)
94
106
  # grant(privilege, subject, to_role)
95
107
  def grant(role, to_role)
96
- #
108
+ #
97
109
  conn.exec "grant #{role} to #{to_role}"
98
110
  end
99
111
  o Allow a :type argument to all query methods that can be used to specify the
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.23.0"
2
+ VERSION = "0.25.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -220,6 +220,13 @@ module PgConn
220
220
  @savepoints = nil # Stack of savepoint names. Nil if no transaction in progress
221
221
  end
222
222
 
223
+ # Reset connection but keep noise level (TODO: How about the other
224
+ # per-session settings in #initialize? Are they cleared by #reset too?)
225
+ def reset
226
+ @pg_connection.reset
227
+ @pg_connection.exec "set client_min_messages to warning;" # Silence warnings
228
+ end
229
+
223
230
  # Close the database connection. TODO: Rename 'close'
224
231
  def terminate()
225
232
  @pg_connection.close if @pg_connection && !@pg_connection.finished?
@@ -261,7 +268,7 @@ module PgConn
261
268
  # of values
262
269
  #
263
270
  # The :elem_type option can be a postgres type name (String or Symbol) or
264
- # an array of type names. They are used as the required explicit element
271
+ # an array of type names. It is used as the required explicit element
265
272
  # type when the argument is an empty array. The element types shoud be in
266
273
  # the same order as the array arguments. Nested arrays is not supported
267
274
  #
@@ -579,14 +586,16 @@ module PgConn
579
586
  h
580
587
  end
581
588
 
582
- # Return the value of calling the given function (which can be a String or
583
- # a Symbol and can contain the schema of the function). It dynamically
589
+ # Return the value of calling the given postgres function. It dynamically
584
590
  # detects the structure of the result and return a value or an array of
585
591
  # values if the result contained only one column (like #value or #values),
586
592
  # a tuple if the record has multiple columns (like #tuple), and an array of
587
593
  # of tuples if the result contained more than one record with multiple
588
- # columns (like #tuples). If the :proc option is true the "function" is
589
- # assumed to be a procedure
594
+ # columns (like #tuples).
595
+ #
596
+ # The name argument can be a String or a Symbol that may contain the schema
597
+ # of the function. If the :proc option is true the "function" is assumed
598
+ # to be a procedure
590
599
  #
591
600
  def call(name, *args, elem_type: nil, proc: false) # :proc may interfere with hashes
592
601
  args_seq = quote_values(args, elem_type: elem_type)
@@ -621,7 +630,10 @@ module PgConn
621
630
  #
622
631
  # There is no variant that takes a single tuple because it would then be
623
632
  # impossible to have array or hash field values
624
- 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
+
625
637
  # Add schema (=nil) if absent
626
638
  args.unshift nil if args.size == 2 || (args.size == 3 && args[1].is_a?(Array))
627
639
 
@@ -656,17 +668,33 @@ module PgConn
656
668
  fields ||= data.keys
657
669
  tuples = [fields.map { |field| data[field] }]
658
670
  else
659
- raise ArgumentError
671
+ raise ArgumentError, "Illegal argument '#{data.inspect}'"
660
672
  end
661
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
+
662
684
  # Execute SQL statement using either :value or :values depending on data arity
663
685
  self.send method, %(
664
686
  insert into #{table} (#{quote_identifiers(fields)})
665
687
  values #{quote_tuples(tuples)}
688
+ #{upsert_sql}
666
689
  returning id
667
690
  )
668
691
  end
669
692
 
693
+ # Use upsert. Currently on 'on conflict do nothing' is supported
694
+ def upsert(*args)
695
+ insert(*args, upsert: true)
696
+ end
697
+
670
698
  # Update record(s)
671
699
  def update(schema = nil, table, expr, hash)
672
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.23.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-08-24 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