pg_conn 0.40.0 → 0.41.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 +43 -11
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f946804765203fcdb2c40e704831e488e2a47c1975ce371217faa528c533141c
4
- data.tar.gz: fa08dce1b7e10162aaa5e2cd20e0ada630f95a794462528a73969c6bd5ad484c
3
+ metadata.gz: 3fb72128df77cad4c2a3e5992f2d72a727af4b57fc898b8c2f7102ebd8df7ebd
4
+ data.tar.gz: ced62377173f5c33d428bec72c321a934947b5f4a24cac8467ad0c0955131a32
5
5
  SHA512:
6
- metadata.gz: 41262613001be5709f861d125af4287a82968ce60878dca3c885a34033d0ef78786b76a774738123fbd97ffdaaad5821cf480899cc56772f3691757c831de8a7
7
- data.tar.gz: de131f33563c6ec6e52c03d4537dd3ab730506534e84a0aae1aefaefc763a05fd45376713a4ee2d91177967c2def949c566bf137376f6c61496f0e7f691ad97f
6
+ metadata.gz: 0becdbeef4a4feb64c550e03d167143f59dda2a6eb986119eababab944cb22fefd36b13fd6fe2a4ea2866c8297bf2000f8683a3a86a47753a3cd462f214e007f
7
+ data.tar.gz: 829f2be06bfe9a278544b5c909841c418e3b248e8287dad640f43c28039f2dfc3ad028052f435c20248187774ddbbcbdd1d6df412b6b1f52fd481d5b5bfa2ce8
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.40.0"
2
+ VERSION = "0.41.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -209,7 +209,31 @@ module PgConn
209
209
  def debug?() !debug.nil? end
210
210
  def debug=(value) set_option(:debug, value) end
211
211
 
212
- DEFAULT_OPTIONS = { silent: false, warning: true, notice: false, info: false, debug: false }
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 }
213
237
 
214
238
  # TODO: Move error message handling into the same framework as notice and
215
239
  # warning but we have a name collision just below that would need to be
@@ -912,22 +936,25 @@ module PgConn
912
936
  # very useful in RSpec tests
913
937
  #
914
938
  # Local options are :search_path that runs the block with the given
915
- # schemas, :username that runs the block as the given user, and :commit
916
- # that runs the block in a transaction if true or false; true commits the
917
- # transaction and false rolls it back. It is not run in a transaction if
918
- # :commit is nil
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
919
943
  #
920
944
  def with(**options, &block)
921
945
  search_path = options.delete(:search_path)
922
946
  username = options.delete(:username)
947
+ logging = options.delete(:log) || false
923
948
  commit = options.delete(:commit)
924
949
 
925
950
  saved_options = @options.dup
926
951
  saved_search_path = self.search_path if search_path
952
+ saved_logger = self.log if logging
927
953
 
928
954
  begin
929
955
  set_options(options)
930
956
  self.search_path = search_path if search_path
957
+ self.log = logging
931
958
 
932
959
  inner = lambda {
933
960
  if !commit.nil?
@@ -945,6 +972,7 @@ module PgConn
945
972
  inner.call
946
973
  end
947
974
  ensure
975
+ self.log = saved_logger if logging
948
976
  self.search_path = saved_search_path if search_path
949
977
  set_options(saved_options)
950
978
  end
@@ -1320,12 +1348,16 @@ module PgConn
1320
1348
  # Assign default
1321
1349
  value = @default_options[option] if value.nil?
1322
1350
 
1351
+ # Set new value. Can be true, false, or a Proc object. The :log option
1352
+ # also allows an IO og StringIO object
1353
+ @options[option] = value
1354
+
1355
+ # Exit early if log
1356
+ return if option == :log
1357
+
1323
1358
  # Find current message level
1324
1359
  old_level = DEFAULT_PRODUCER.keys.find { |level| @producers[level] } || :error
1325
1360
 
1326
- # Set new value. Can be true, false, or a Proc object
1327
- @options[option] = value
1328
-
1329
1361
  # Set producer
1330
1362
  case option
1331
1363
  when :silent
@@ -1404,17 +1436,17 @@ module PgConn
1404
1436
  saved_silent = self.silent
1405
1437
  self.silent = silent
1406
1438
 
1407
- last_stmt = nil # To make the current SQL statement visible to the rescue clause. FIXME Not used?
1439
+ last_stmt = nil # To make the last (failed) SQL statement visible to the rescue clause. FIXME Not used?
1408
1440
  if arg.is_a?(String)
1409
1441
  return nil if arg == ""
1410
1442
  last_stmt = arg
1411
- @pg_connection.exec(last_stmt)
1443
+ @pg_connection.exec(log arg)
1412
1444
  else
1413
1445
  stmts = arg.flatten.compact
1414
1446
  return nil if stmts.empty?
1415
1447
  # stmts.unshift("set on_error_exit stop")
1416
1448
  last_stmt = stmts.last
1417
- @pg_connection.exec(stmts.join(";\n"))
1449
+ @pg_connection.exec(log stmts.join(";\n"))
1418
1450
  end
1419
1451
  rescue PG::Error => ex
1420
1452
  if @error.nil?
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.40.0
4
+ version: 0.41.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen