pg_conn 0.35.1 → 0.36.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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pg_conn/version.rb +1 -1
  3. data/lib/pg_conn.rb +33 -35
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c834402fcec956ffcb288fa201d7a6bade6b435002ae4758e1d86ea54f5a656
4
- data.tar.gz: 20fafcbbe4c9f93ab426c6012e91f073e5224ab84d23948da894f4a64b5ef7c9
3
+ metadata.gz: 3d6940809a3f62bfad5cd2a94566a76bcd1944ad5b6ded6df2be159a010b2169
4
+ data.tar.gz: 6f213cb288ba5a85205fd5054fb498e6200b0319eee8c2bbc8c60515705410f7
5
5
  SHA512:
6
- metadata.gz: 1da57a0c7f8daffdaac879899d3bd7d9723a6512e038afda6996329717d9b0246f7b646161ee1f0a82e00d828a12b9328f212ef98b47132914e4c66dc9f1e888
7
- data.tar.gz: 4fd13dd8b4130ebb417379dd3ec62a3476f7d721a7617d14ae2f5b88a0c1f13812dbd440931bc14c02c3db702f9ff181a8b4ab3116a08527293ae6b632c9cd0b
6
+ metadata.gz: a90dab6c75eb2cfd7a99b39676a4aa0dc167462e37e9229d96ed30517b4ff0c691f6b4389ceb6d26c3f2562d7ce4e7dd2be17d054f1e61369989fb9f8c3db8e1
7
+ data.tar.gz: f91893227ef9697eb236051728d509727b2d20b5d9efd7c6f848cf2e887b1abc0e5c8a48da291914372049ab25aee6580b9b74fd422d269c3e7813928032dd23
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.35.1"
2
+ VERSION = "0.36.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -40,13 +40,13 @@ module PgConn
40
40
  #
41
41
  def self.quote_identifier(s)
42
42
  s = s.to_s if s.is_a?(Symbol)
43
- escape_identifier(s).gsub(/\./, '"."').sub(/"\*"/, "*")
43
+ Literal.new escape_identifier(s).gsub(/\./, '"."').sub(/"\*"/, "*")
44
44
  end
45
45
 
46
46
  # Quote identifiers and concatenate them using ',' as separator
47
- def self.quote_identifiers(idents) = idents.map { |ident| quote_identifier(ident) }.join(", ")
47
+ def self.quote_identifiers(idents) = Literal.new idents.map { |ident| quote_identifier(ident) }.join(", ")
48
48
 
49
- # Quote the value as a string. Emit 'null' if value is nil
49
+ # Quote the value as a string. Returns a Literal object
50
50
  #
51
51
  # The value can be of any type but is converted to a string using #to_s
52
52
  # before quoting. This works by default for the regular types Integer,
@@ -55,7 +55,7 @@ module PgConn
55
55
  #
56
56
  # Hashes are quoted as a literal JSON expression converted into the given
57
57
  # :json_type. If :json_type is nil, it is the application's responsibility to
58
- # cast them to either 'json' or 'jsonb'
58
+ # cast the value to either 'json' or 'jsonb'
59
59
  #
60
60
  # Note that a tuple value (an array) must be quoted using #quote_tuple
61
61
  # because #quote_value would quote the tuple as an array value instead of a
@@ -67,30 +67,31 @@ module PgConn
67
67
  # is guaranteed to be non-empty. Nested arrays are not supported
68
68
  #
69
69
  def self.quote_value(value, elem_type: nil, json_type: nil)
70
- case value
71
- when Literal; value
72
- when String; escape_literal(value)
73
- when Integer, Float; value.to_s
74
- when true, false; value.to_s
75
- when nil; 'null'
76
- when Date, DateTime; "'#{value}'"
77
- when Time; "'#{value.strftime("%FT%T%:z")}'"
78
- when Array
79
- if value.empty?
80
- elem_type or raise Error, "Empty array without elem_type"
81
- "array[]::#{elem_type}[]"
70
+ Literal.new \
71
+ case value
72
+ when Literal; value
73
+ when String; escape_literal(value)
74
+ when Integer, Float; value.to_s
75
+ when true, false; value.to_s
76
+ when nil; 'null'
77
+ when Date, DateTime; "'#{value}'"
78
+ when Time; "'#{value.strftime("%FT%T%:z")}'"
79
+ when Array
80
+ if value.empty?
81
+ elem_type or raise Error, "Empty array without elem_type"
82
+ "array[]::#{elem_type}[]"
83
+ else
84
+ "array[#{value.map { |elem| quote_value(elem) }.join(', ')}]"
85
+ end
86
+ when Hash; ["'#{value.to_json}'", json_type].compact.join('::')
82
87
  else
83
- "array[#{value.map { |elem| quote_value(elem) }.join(', ')}]"
88
+ escape_literal(value.to_s)
84
89
  end
85
- when Hash; ["'#{value.to_json}'", json_type].compact.join('::')
86
- else
87
- escape_literal(value.to_s)
88
- end
89
90
  end
90
91
 
91
92
  # Quote values and concatenate them using ',' as separator
92
93
  def self.quote_values(values, **opts)
93
- values.map { |value| quote_value(value, **opts) }.join(", ")
94
+ Literal.new values.map { |value| quote_value(value, **opts) }.join(", ")
94
95
  end
95
96
 
96
97
  # Quote an array of values as a tuple. The element types should be in the
@@ -101,7 +102,7 @@ module PgConn
101
102
  # Note that it is :elem_types (plural) and not :elem_type
102
103
  def self.quote_tuple(tuple, elem_types: nil, **opts)
103
104
  elem_types = Array(elem_types)
104
- tuple.map { |value|
105
+ Literal.new tuple.map { |value|
105
106
  elem_type = value.is_a?(Array) ? elem_types&.shift : nil
106
107
  quote_value(value, **opts, elem_type: elem_type)
107
108
  }.join(", ")
@@ -109,7 +110,7 @@ module PgConn
109
110
 
110
111
  # Quote an array of tuples
111
112
  def self.quote_tuples(tuples, **opts)
112
- tuples.map { |tuple| "(#{quote_tuple(tuple, **opts)})" }.join(", ")
113
+ Literal.new tuples.map { |tuple| "(#{quote_tuple(tuple, **opts)})" }.join(", ")
113
114
  end
114
115
 
115
116
  # Used to mark strings as literals that should not be quoted. This is the
@@ -429,8 +430,7 @@ module PgConn
429
430
  end
430
431
  end
431
432
 
432
- # Mark argument as already being quoted. TODO: Make this the default in all
433
- # quote methods
433
+ # Mark string argument as already being quoted
434
434
  def literal(arg) Literal.new(arg) end
435
435
 
436
436
  # Connection member method variations of the PgConn quote class methods
@@ -1155,17 +1155,16 @@ module PgConn
1155
1155
  end
1156
1156
 
1157
1157
  # Common implementation for #quote_record and #quote_records that avoids
1158
- # querying the database multiple times or duplication the code. The :array
1159
- # flag is true when called via #quote_records
1158
+ # querying the database multiple times or duplication the code
1160
1159
  #
1161
1160
  # @data can be a Hash, Array, or OpenStruct. Hash keys must be symbols or
1162
1161
  # strings, they belong to the same namespace so :k and "k" refer to the
1163
- # same value
1162
+ # same value. The :array flag is true when called via #quote_records
1164
1163
  #
1165
1164
  # Note that #quote_record_impl queries the database for information about
1166
1165
  # the type. TODO Cache this information?
1167
- def quote_record_impl(
1168
- datas, schema_name = nil, type, elem_types: nil, array: nil, **opts)
1166
+ #
1167
+ def quote_record_impl(datas, schema_name = nil, type, elem_types: nil, array: nil, **opts)
1169
1168
  datas = [datas] if !array
1170
1169
 
1171
1170
  pg_type = [schema_name, type].compact.join('.')
@@ -1192,10 +1191,9 @@ module PgConn
1192
1191
  }
1193
1192
 
1194
1193
  if array
1195
- # papg_type(table, where_clause, fields...)
1196
- "array[#{literals.join(', ')}]::#{pg_type}[]"
1194
+ Literal.new "array[#{literals.join(', ')}]::#{pg_type}[]"
1197
1195
  else
1198
- literals.first
1196
+ Literal.new literals.first
1199
1197
  end
1200
1198
  end
1201
1199
 
@@ -1235,7 +1233,7 @@ module PgConn
1235
1233
  end
1236
1234
 
1237
1235
  STDOUT_PRODUCER = lambda { |msg| $stdout.puts msg }
1238
- STDERR_PRODUCER = lambda { |msg| $stderr.puts msg }
1236
+ STDERR_PRODUCER = lambda { |msg| $stderr.puts msg; $stderr.flush }
1239
1237
  ERROR_PRODUCER = lambda { |msg, stmt| $stderr.puts stmt, nil, msg; $stderr.flush }
1240
1238
 
1241
1239
  # Map from message level to default producer. Note that we rely on the key
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.35.1
4
+ version: 0.36.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: 2025-03-18 00:00:00.000000000 Z
11
+ date: 2025-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg