pg_conn 0.42.1 → 0.43.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 +36 -33
  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: b9b707fbab740595f05ebe65cbd36f20e996875f568b1c26ad37b60cfb42cb65
4
+ data.tar.gz: e5984072e4e09eb9f7ac76915f00d29d08e1766e1ad8edef9b90d56fd61a7313
5
5
  SHA512:
6
- metadata.gz: b839c16a46f84ae89f8bb941d08972f24edd3f3bf777d17197ad1cdaf5bc8e5ab368135e8222dac202a3b3d670c04c027e7a9afff00a0b851af8d899f93baef9
7
- data.tar.gz: a9171073a19b5ed326eaf58a4d4fbcd4b61fad8bc1154e0c000e99b61c94b068ba8f70c14e773bd9eb455d0d7dd111a2bac02c4646c07b1bd01843d134338cb4
6
+ metadata.gz: ed0c6002be578ca08f8a002b438c34eada70090bf362ea5fb9d642559101fc53827b42b2cf962e60803c02c011df5b6eef92bf5de5045faa18b47affc9a7a71c
7
+ data.tar.gz: d75913f57aac31fcb60c91f06958a746b20cd4559bd0feda169f12c48c173aa721423bacdfb883ed5612106f4e1320add1913d39e72d943abde483adb25a30df
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.42.1"
2
+ VERSION = "0.43.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -161,6 +161,31 @@ module PgConn
161
161
  # #exec or #transaction block. The timestamp includes the current time zone
162
162
  attr_reader :timestamptz
163
163
 
164
+ # Write a Sql statement to the logger if defined. Return the given sql
165
+ def log(sql)
166
+ case @logger
167
+ when nil; # do nothing
168
+ when IO, StringIO; @logger.puts sql
169
+ when Proc; @logger.call sql
170
+ else
171
+ raise ArgumentError
172
+ end
173
+ sql # for convenience in #pg_exec
174
+ end
175
+
176
+ # Return true if logging
177
+ def log?() = @logger != false
178
+
179
+ # Control logging of SQL commands. It can be assigned true, false, nil, an
180
+ # unary Proc object, or a IO or StringIO object. True causes the message to
181
+ # be printed to standard error, false and nil ignores it
182
+ def logger=(logger) @logger = (logger == true ? $stdout : logger || nil) end
183
+
184
+ # Return current logger or nil if not logging. Note that the logger object
185
+ # is equal to $stdout if the logger is set to true and nil if it is set to
186
+ # false
187
+ def logger() = @logger
188
+
164
189
  # Controls error messages. It can be assigned true, false, nil, or a Proc
165
190
  # object that recieves the message. True causes the message to be printed
166
191
  # to standard error, false ignores it, and nil resets the state to the
@@ -209,31 +234,7 @@ module PgConn
209
234
  def debug?() !debug.nil? end
210
235
  def debug=(value) set_option(:debug, value) end
211
236
 
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 }
237
+ DEFAULT_OPTIONS = { silent: false, warning: true, notice: false, info: false, debug: false }
237
238
 
238
239
  # TODO: Move error message handling into the same framework as notice and
239
240
  # warning but we have a name collision just below that would need to be
@@ -425,6 +426,7 @@ module PgConn
425
426
  @rdbms = RdbmsMethods.new(self)
426
427
  @session = SessionMethods.new(self)
427
428
  @savepoints = nil # Stack of savepoint names. Nil if no transaction in progress
429
+ @log = nil
428
430
  end
429
431
 
430
432
  # Reset connection but keep noise level (TODO: How about the other
@@ -936,25 +938,26 @@ module PgConn
936
938
  # very useful in RSpec tests
937
939
  #
938
940
  # 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
941
+ # schemas, :username that runs the block as the given user, :log that
942
+ # controls logging (see #logger=), :commit that runs the block in a
943
+ # transaction if true or false; true commits the transaction and false
944
+ # rolls it back (very rarely useful). It is not run in a transaction if
945
+ # :commit is nil
943
946
  #
944
947
  def with(**options, &block)
945
948
  search_path = options.delete(:search_path)
946
949
  username = options.delete(:username)
947
- logging = options.delete(:log) || false
950
+ log = options.delete(:log)
948
951
  commit = options.delete(:commit)
949
952
 
950
953
  saved_options = @options.dup
951
954
  saved_search_path = self.search_path if search_path
952
- saved_logger = self.log if logging
955
+ saved_logger = self.logger if log
953
956
 
954
957
  begin
955
958
  set_options(options)
956
959
  self.search_path = search_path if search_path
957
- self.log = logging
960
+ self.logger = log if !log.nil?
958
961
 
959
962
  inner = lambda {
960
963
  if !commit.nil?
@@ -972,7 +975,7 @@ module PgConn
972
975
  inner.call
973
976
  end
974
977
  ensure
975
- self.log = saved_logger if logging
978
+ self.logger = saved_logger if log
976
979
  self.search_path = saved_search_path if search_path
977
980
  set_options(saved_options)
978
981
  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.43.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen