pg_conn 0.25.0 → 0.26.1

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 +5 -0
  3. data/lib/pg_conn/version.rb +1 -1
  4. data/lib/pg_conn.rb +70 -11
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb9303d38e0364b374379d8d0549d461decf393d1a20c5a97da42b1c0b0d7e17
4
- data.tar.gz: 53512feaaff24b3b6e8b7366a0c860dac0d38f8880698570cfa16cffbb7a46b2
3
+ metadata.gz: 6dc75936f3c9bd5db772a2f8a80aa8e8bb25cbd1ae2181492766a7ed6ab9104a
4
+ data.tar.gz: 8ac094777a20c074f4a018b4bb0ea53efff219eb2401e18bf9c5887721fbb1fe
5
5
  SHA512:
6
- metadata.gz: b9efb0689c559425bee8bebb5d984c72222e8bd2377803cc8c1415d5b253ada2f0913dc70b17ef12c0f9677b26e1db60accb5bdb66de41220f7001a2628c013c
7
- data.tar.gz: fcd4f4e04157e2cdf10226f1a4545ab8fd66ce960cf14c732e0eceda9450e3c94b211277da303e8338aff7fbfbb3eb09bcd6b5946e979aa383f5f3b803ac86d7
6
+ metadata.gz: 8651be5de05b08d991cc257dab9cc060c4dfe22f8c22b2489f7e9ecc3654d51c55ad8494287b1cb829dc92a50cf60eb777b8814bcc23b348143caf9339729a8c
7
+ data.tar.gz: 8c11d1e8cb3ceb859438e9ccb98fe38de9ed9126c449c58972cef97d9fb3108c87ea2a18899aea9d500eaa249625d0a1a783176e3fd4e8bc7ebe035cec31f67b
data/TODO CHANGED
@@ -1,4 +1,9 @@
1
1
  TODO
2
+ o Add a <fetch>! method
3
+ value? 0 or 1
4
+ value 1
5
+ values 0 or n
6
+ values! 1 or more
2
7
 
3
8
  o Instrumentation of connection object
4
9
 
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.25.0"
2
+ VERSION = "0.26.1"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -269,8 +269,8 @@ module PgConn
269
269
  #
270
270
  # The :elem_type option can be a postgres type name (String or Symbol) or
271
271
  # an array of type names. It is used as the required explicit element
272
- # type when the argument is an empty array. The element types shoud be in
273
- # the same order as the array arguments. Nested arrays is not supported
272
+ # type when the argument is an empty array. It is not needed if the array
273
+ # is guaranteed to be non-empty. Nested arrays are not supported
274
274
  #
275
275
  def quote_value(value, elem_type: nil)
276
276
  case value
@@ -294,19 +294,42 @@ module PgConn
294
294
 
295
295
  # Quote values and concatenate them using ',' as separator
296
296
  def quote_values(values, elem_type: nil)
297
- elem_types = Array(elem_type)
298
- values.map { |value|
297
+ values.map { |value| quote_value(value, elem_type: elem_type) }.join(", ")
298
+ end
299
+
300
+ # Quote an array of values as a tuple. The element types should be in the
301
+ # same order as the array arguments. #quote_tuples is same as #quote_values
302
+ # except the values may have different types (this makes no difference
303
+ # except in the case when the tuple may contain empty array(s))
304
+ def quote_tuple(tuple, elem_types: nil)
305
+ elem_types = Array(elem_types)
306
+ tuple.map { |value|
299
307
  elem_type = value.is_a?(Array) ? elem_types&.shift : nil
300
308
  quote_value(value, elem_type: elem_type)
301
309
  }.join(", ")
302
310
  end
303
311
 
304
- # Quote an array of values as a tuple. Just an alias for #quote_values
305
- def quote_tuple(tuple, elem_type: nil) = quote_values(tuple, elem_type: elem_type)
306
-
307
312
  # Quote an array of tuples
308
- def quote_tuples(tuples, elem_type: nil)
309
- tuples.map { |tuple| "(#{quote_values(tuple, elem_type: elem_type)})" }.join(", ")
313
+ def quote_tuples(tuples, elem_types: nil)
314
+ tuples.map { |tuple| "(#{quote_tuple(tuple, elem_types: elem_types)})" }.join(", ")
315
+ end
316
+
317
+ # Quote a record and cast it into the given type, the type can also be a
318
+ # table or view. 'data' is an array, hash, or struct representation of the
319
+ # record
320
+ #
321
+ # Note that the fields are retrived from the database so this method is not
322
+ # as fast as the other quote-methods. It is however very convenient when
323
+ # you're testing and need a composite type because record-quoting can
324
+ # easily become unwieldly
325
+ def quote_record(data, schema_name = nil, type, elem_types: nil)
326
+ quote_record_impl(data, schema_name, type, elem_types: elem_types, array: false)
327
+ end
328
+
329
+ # Quote an array of records. The type is the record type, not the type of
330
+ # the enclosing array
331
+ def quote_records(data, schema_name = nil, type, elem_types: nil)
332
+ quote_record_impl(data, schema_name, type, elem_types: elem_types, array: true)
310
333
  end
311
334
 
312
335
  # :call-seq:
@@ -651,7 +674,7 @@ module PgConn
651
674
 
652
675
  # Find method and normalize data
653
676
  if data.is_a?(Array) # Array of tuples
654
- method = :values
677
+ method = :values # The pg_conn method when multiple records are inserted
655
678
  if data.empty?
656
679
  return []
657
680
  elsif data.first.is_a?(Array) # Tuple (array) element. Requires the 'fields' argument
@@ -664,7 +687,7 @@ module PgConn
664
687
  raise ArgumentError
665
688
  end
666
689
  elsif data.is_a?(Hash)
667
- method = :value
690
+ method = :value # The pg_conn method when only one record is inserted
668
691
  fields ||= data.keys
669
692
  tuples = [fields.map { |field| data[field] }]
670
693
  else
@@ -937,6 +960,42 @@ module PgConn
937
960
  end
938
961
  end
939
962
 
963
+ # Common implementation for #quote_record and #quote_records that avoids
964
+ # query the database multiple times while not duplication the code. the
965
+ # :array flag controls the mode
966
+ def quote_record_impl(datas, schema_name = nil, type, elem_types: nil, array: nil)
967
+ pg_type = [schema_name, type].compact.join('.')
968
+ fields = self.values(%(
969
+ select attname
970
+ from pg_attribute
971
+ where
972
+ attrelid = '#{pg_type}'::regclass
973
+ and attnum > 0
974
+ and not attisdropped
975
+ order by attnum
976
+ )).map(&:to_sym)
977
+
978
+ datas = [datas] if !array
979
+
980
+ literals = datas.map { |data|
981
+ values =
982
+ case data
983
+ when Hash; fields.map { |f| data[f] }
984
+ when OpenStruct; fields.map { |f| data.send(f) }
985
+ when Array; data
986
+ else
987
+ raise Error, "Illegal value #{data.inspect}"
988
+ end
989
+ "(#{quote_tuple(values, elem_types: elem_types)})::#{pg_type}"
990
+ }
991
+
992
+ if array
993
+ "array[#{literals.join(', ')}]::#{pg_type}[]"
994
+ else
995
+ literals.first
996
+ end
997
+ end
998
+
940
999
  # :call-seq:
941
1000
  # pg_exec(string)
942
1001
  # pg_exec(array)
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.25.0
4
+ version: 0.26.1
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-14 00:00:00.000000000 Z
11
+ date: 2024-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg