pg_conn 0.22.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/TODO +2 -1
  3. data/lib/pg_conn/version.rb +1 -1
  4. data/lib/pg_conn.rb +37 -15
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d8571dfbe07ffc9e846741807d87d45373553d987e353693aa4599010804b3e
4
- data.tar.gz: 4d0bd8faced962ac9c0837b3a98f1a6f0ee4fbc0c1c1aa4a12c448472e10376a
3
+ metadata.gz: d7de75668042e3abdc5b983e0b9a3f6c909ff69aeed2f22c632d1c693f3baac0
4
+ data.tar.gz: 90cecbdd79e6478d7f6bd1bc3ae042917bc2683771c89de61aab6c05fa2e5279
5
5
  SHA512:
6
- metadata.gz: 3b3ff2ae7e1fa63f697616e16eff4065e5118ef5fe9cb446837cc30b028df5a385b56848e23546c7718f5f704e622cd35175acbd56aacd6fd56774dec9c34627
7
- data.tar.gz: 4c9428ef6abc6162c068f154575eb1d8c7b63c1aa072af4350c7bacb2c6ea63e0ffea80a470f9ca8b72c635e83ceda7345b54bc6a24d5adc450bf0863fb0df1b
6
+ metadata.gz: 76b775138260668bf75e5bc31e220561f665dffc901349a9ec875e7ec594dee2750e0d979bde80e9045e38e44a3894fe55c2be37faefbe7f35173812cea2a22a
7
+ data.tar.gz: f0e2e9169cd734814cce4e8b73f7ec017cf3fd9a2622290c4acbf06620ddb3b943957ab5dab527ee0e29a9d71ad4efb511f4f219543ca86301d9fd5c4f5838ad
data/TODO CHANGED
@@ -1,5 +1,6 @@
1
1
  TODO
2
- o "use 'drop ... cascade' everywhere
2
+ o Use :elem_type everywhere
3
+ o Use 'drop ... cascade' everywhere
3
4
  o db.context(schema: app_portal, transaction: true) { ... }
4
5
  db.context(set: app_portal, transaction: true) { ... }
5
6
  db.context(add: app_portal, transaction: true) { ... }
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.22.0"
2
+ VERSION = "0.23.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -239,12 +239,16 @@ module PgConn
239
239
  end
240
240
  end
241
241
 
242
- # Quote value as an identifier. Value should be a non-nil string or a symbol
242
+ # Quote argument as an identifier. The argument should be a non-nil string
243
+ # or a symbol
243
244
  def quote_identifier(s)
244
245
  s = s.to_s if s.is_a?(Symbol)
245
246
  @pg_connection.escape_identifier(s)
246
247
  end
247
248
 
249
+ # Quote identifiers and concatenate them using ',' as separator
250
+ def quote_identifiers(idents) = idents.map { |ident| quote_identifier(ident) }.join(", ")
251
+
248
252
  # Quote the value as a string. Emit 'null' if value is nil
249
253
  #
250
254
  # The value can be of any type but is converted to a string using #to_s
@@ -255,7 +259,13 @@ module PgConn
255
259
  # Note that a tuple value (an array) must be quoted using #quote_tuple
256
260
  # because #quote_value would quote the tuple as an array instead of a list
257
261
  # of values
258
- def quote_value(value)
262
+ #
263
+ # 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
265
+ # type when the argument is an empty array. The element types shoud be in
266
+ # the same order as the array arguments. Nested arrays is not supported
267
+ #
268
+ def quote_value(value, elem_type: nil)
259
269
  case value
260
270
  when String; @pg_connection.escape_literal(value)
261
271
  when Integer, Float; value.to_s
@@ -263,23 +273,34 @@ module PgConn
263
273
  when nil; 'null'
264
274
  when Date, DateTime; "'#{value}'"
265
275
  when Time; "'#{value.strftime("%FT%T%:z")}'"
266
- when Array; "array[#{value.map { |elem| quote_value(elem) }.join(', ')}]"
276
+ when Array
277
+ if value.empty?
278
+ elem_type or raise Error, "Empty array without elem_type"
279
+ "array[]::#{elem_type}[]"
280
+ else
281
+ "array[#{value.map { |elem| quote_value(elem) }.join(', ')}]"
282
+ end
267
283
  else
268
284
  @pg_connection.escape_literal(value.to_s)
269
285
  end
270
286
  end
271
287
 
272
- # Quote an array of values as a tuple. Just an alias for #quote_values
273
- def quote_tuple(tuple) = quote_values(tuple)
274
-
275
- # Quote identifiers and concatenate them using ',' as separator
276
- def quote_identifiers(idents) = idents.map { |ident| quote_identifier(ident) }.join(", ")
277
-
278
288
  # Quote values and concatenate them using ',' as separator
279
- def quote_values(values) = values.map { |value| quote_value(value) }.join(", ")
289
+ def quote_values(values, elem_type: nil)
290
+ elem_types = Array(elem_type)
291
+ values.map { |value|
292
+ elem_type = value.is_a?(Array) ? elem_types&.shift : nil
293
+ quote_value(value, elem_type: elem_type)
294
+ }.join(", ")
295
+ end
296
+
297
+ # Quote an array of values as a tuple. Just an alias for #quote_values
298
+ def quote_tuple(tuple, elem_type: nil) = quote_values(tuple, elem_type: elem_type)
280
299
 
281
300
  # Quote an array of tuples
282
- def quote_tuples(tuples) = tuples.map { |tuple| "(#{quote_values(tuple)})" }.join(", ")
301
+ def quote_tuples(tuples, elem_type: nil)
302
+ tuples.map { |tuple| "(#{quote_values(tuple, elem_type: elem_type)})" }.join(", ")
303
+ end
283
304
 
284
305
  # :call-seq:
285
306
  # exist?(query)
@@ -564,10 +585,11 @@ module PgConn
564
585
  # values if the result contained only one column (like #value or #values),
565
586
  # a tuple if the record has multiple columns (like #tuple), and an array of
566
587
  # of tuples if the result contained more than one record with multiple
567
- # columns (like #tuples)
588
+ # columns (like #tuples). If the :proc option is true the "function" is
589
+ # assumed to be a procedure
568
590
  #
569
- def call(name, *args, proc: false) # :proc may interfere with hashes
570
- args_seq = quote_values(args)
591
+ def call(name, *args, elem_type: nil, proc: false) # :proc may interfere with hashes
592
+ args_seq = quote_values(args, elem_type: elem_type)
571
593
  if proc
572
594
  pg_exec "call #{name}(#{args_seq})"
573
595
  return nil
@@ -589,7 +611,7 @@ module PgConn
589
611
  end
590
612
  end
591
613
 
592
- # :call-seq*
614
+ # :call-seq:
593
615
  # insert(table, record|records)
594
616
  # insert(table, fields, record|records|tuples)
595
617
  # insert(schema, table, record|records)
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.22.0
4
+ version: 0.23.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-20 00:00:00.000000000 Z
11
+ date: 2024-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg