pg_conn 0.42.1 → 0.44.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 +52 -36
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1885ef3f2d7a35d098e8bf78136148634c7b2d828b33477235a3cd1e38765df6
4
- data.tar.gz: 853369281c40c6c6fc60a80785f6166d27fcc500cf7f1e216e348adfed778c30
3
+ metadata.gz: 51ccb11b750c683d57a02f274b4479366eced6d46c8908fed1e47aca302ff367
4
+ data.tar.gz: ad97bab6ef9fb0e7f821088a58772252ba72c4c9b695703b6ccd3a32fb1f5f80
5
5
  SHA512:
6
- metadata.gz: b839c16a46f84ae89f8bb941d08972f24edd3f3bf777d17197ad1cdaf5bc8e5ab368135e8222dac202a3b3d670c04c027e7a9afff00a0b851af8d899f93baef9
7
- data.tar.gz: a9171073a19b5ed326eaf58a4d4fbcd4b61fad8bc1154e0c000e99b61c94b068ba8f70c14e773bd9eb455d0d7dd111a2bac02c4646c07b1bd01843d134338cb4
6
+ metadata.gz: 629d0d6afb690bc1ef2f3490d395f81cf5177f86803f5e6049621fec73bcef07ed4b4c644cbf8e834059c7219076f2c62b1ad3b36e7fa1db344a415c9073213c
7
+ data.tar.gz: 50cbacb421a2e4f4332c53737d0b9e8cf88e2b6555fe3c814d728d957f5d1477065f4a5089ff7c11ca77c33634aabf8440bd6aebccee8b92e7c509b7a18ba3ef
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.42.1"
2
+ VERSION = "0.44.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -113,6 +113,10 @@ module PgConn
113
113
  Literal.new tuples.map { |tuple| "(#{quote_tuple(tuple, **opts)})" }.join(", ")
114
114
  end
115
115
 
116
+ # Quote array elements as a comma separated list of values and enclosed en
117
+ # parenthesis. Eg. [1, 2] => "(1, 2)"
118
+ def self.quote_list(array, **opts) = quote_tuples([array], **opts)
119
+
116
120
  # Used to mark strings as literals that should not be quoted. This is the
117
121
  # case for row and record values
118
122
  class Literal < String; end
@@ -161,6 +165,31 @@ module PgConn
161
165
  # #exec or #transaction block. The timestamp includes the current time zone
162
166
  attr_reader :timestamptz
163
167
 
168
+ # Write a Sql statement to the logger if defined. Return the given sql
169
+ def log(sql)
170
+ case @logger
171
+ when nil; # do nothing
172
+ when IO, StringIO; @logger.puts sql
173
+ when Proc; @logger.call sql
174
+ else
175
+ raise ArgumentError
176
+ end
177
+ sql # for convenience in #pg_exec
178
+ end
179
+
180
+ # Return true if logging
181
+ def log?() = @logger != false
182
+
183
+ # Control logging of SQL commands. It can be assigned true, false, nil, an
184
+ # unary Proc object, or a IO or StringIO object. True causes the message to
185
+ # be printed to standard error, false and nil ignores it
186
+ def logger=(logger) @logger = (logger == true ? $stdout : logger || nil) end
187
+
188
+ # Return current logger or nil if not logging. Note that the logger object
189
+ # is equal to $stdout if the logger is set to true and nil if it is set to
190
+ # false
191
+ def logger() = @logger
192
+
164
193
  # Controls error messages. It can be assigned true, false, nil, or a Proc
165
194
  # object that recieves the message. True causes the message to be printed
166
195
  # to standard error, false ignores it, and nil resets the state to the
@@ -209,31 +238,7 @@ module PgConn
209
238
  def debug?() !debug.nil? end
210
239
  def debug=(value) set_option(:debug, value) end
211
240
 
212
- # Log SQL or return current logger if sql is nil
213
- def log(sql = nil)
214
- case sql and dst = @options[:log]
215
- when false; # do nothing
216
- when true; $stderr.puts sql
217
- when Proc; dst.call sql
218
- when IO; dst.puts sql
219
- when StringIO; dst.puts sql
220
- when nil; return @options[:log]
221
- else
222
- raise "Oops"
223
- end
224
- sql # for convenience in #pg_exec
225
- end
226
-
227
- # Return true if logging is enabled
228
- def log?() @options[:log] != false end
229
-
230
- # Control logging of SQL commands. It can be assigned true, false, nil, an
231
- # unary Proc object, or a IO object. True causes the message to be printed
232
- # to standard error, false ignores it, and nil resets the state to the
233
- # default given when the connection was initialized. Default false
234
- def log=(value) set_option(:log, value) end
235
-
236
- DEFAULT_OPTIONS = { silent: false, warning: true, notice: false, info: false, debug: false, log: false }
241
+ DEFAULT_OPTIONS = { silent: false, warning: true, notice: false, info: false, debug: false }
237
242
 
238
243
  # TODO: Move error message handling into the same framework as notice and
239
244
  # warning but we have a name collision just below that would need to be
@@ -425,6 +430,7 @@ module PgConn
425
430
  @rdbms = RdbmsMethods.new(self)
426
431
  @session = SessionMethods.new(self)
427
432
  @savepoints = nil # Stack of savepoint names. Nil if no transaction in progress
433
+ @log = nil
428
434
  end
429
435
 
430
436
  # Reset connection but keep noise level (TODO: How about the other
@@ -468,6 +474,9 @@ module PgConn
468
474
  def quote_tuple(tuple, **opts) = PgConn.quote_tuple(tuple, json_type: self.default_json_type, **opts)
469
475
  def quote_tuples(tuples, **opts) = PgConn.quote_tuples(tuples, json_type: self.default_json_type, **opts)
470
476
 
477
+ # Quote an array as a list. Eg. ["1", 2] => ('1', 2)
478
+ def quote_list(values, **opts) = quote_tuples([values], **opts)
479
+
471
480
  # Quote a record and cast it into the given type, the type can also be a
472
481
  # table or view. 'data' is an array, hash, or struct representation of the
473
482
  # record
@@ -825,6 +834,7 @@ module PgConn
825
834
  # :call-seq:
826
835
  # insert(table, record|records)
827
836
  # insert(table, fields, record|records|tuples|values)
837
+ #
828
838
  # insert(schema, table, record|records)
829
839
  # insert(schema, table, fields, record|records|tuples|values)
830
840
  #
@@ -832,6 +842,12 @@ module PgConn
832
842
  #
833
843
  # There is no variant that takes a single tuple because it would then be
834
844
  # impossible to have array or hash field values
845
+ #
846
+ # TODO
847
+ # insert(table, [fields], field-name-map, object|objects)
848
+ # field-name-map:
849
+ # { database-column-name: object-method-name } # calls method on orbejt
850
+ #
835
851
  def insert(*args, upsert: nil, **opts)
836
852
  # Normalize arguments
837
853
 
@@ -932,29 +948,29 @@ module PgConn
932
948
  # Execute block with the given set of global or local options and reset
933
949
  # them afterwards
934
950
  #
935
- # The global options :silent, :notice and :warning are supported, they're
936
- # very useful in RSpec tests
951
+ # Global options are :silent, :notice and :warning, they're very useful in
952
+ # RSpec tests
937
953
  #
938
954
  # Local options are :search_path that runs the block with the given
939
- # schemas, :username that runs the block as the given user, :log that controls logging,
940
- # SQL, :commit that runs the block in a transaction if true or false; true
941
- # commits the transaction and false rolls it back. It is not run in a
942
- # transaction if :commit is nil
943
- #
955
+ # schemas, :username that runs the block as the given user, :commit that
956
+ # runs the block in a transaction if true or false; true commits the
957
+ # transaction and false rolls it back (very rarely useful). It is not run
958
+ # in a transaction if :commit is nil. :log controls logging like
959
+ # #logger= but nil (the default) is a nop
944
960
  def with(**options, &block)
945
961
  search_path = options.delete(:search_path)
946
962
  username = options.delete(:username)
947
- logging = options.delete(:log) || false
963
+ log = options.delete(:log)
948
964
  commit = options.delete(:commit)
949
965
 
950
966
  saved_options = @options.dup
951
967
  saved_search_path = self.search_path if search_path
952
- saved_logger = self.log if logging
968
+ saved_logger = self.logger if log
953
969
 
954
970
  begin
955
971
  set_options(options)
956
972
  self.search_path = search_path if search_path
957
- self.log = logging
973
+ self.logger = log if !log.nil?
958
974
 
959
975
  inner = lambda {
960
976
  if !commit.nil?
@@ -972,7 +988,7 @@ module PgConn
972
988
  inner.call
973
989
  end
974
990
  ensure
975
- self.log = saved_logger if logging
991
+ self.logger = saved_logger if log
976
992
  self.search_path = saved_search_path if search_path
977
993
  set_options(saved_options)
978
994
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.42.1
4
+ version: 0.44.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen