pg_conn 0.15.0 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
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 +29 -20
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca254f7c17749713f46f07448221683e5975c570407aff61ee440f82a37466ca
4
- data.tar.gz: 86486e52477bbf36009d69f9322061077b5860582e4fc7ced67e2d88bad5ee0b
3
+ metadata.gz: 5ebb0df0000ecb59d313c906b608bdd267aaef84edcb4be633650601c453768b
4
+ data.tar.gz: 96d5e1db6d3644fef73dbb69f50acd7deec6dc79404641615d034586d1fddcf0
5
5
  SHA512:
6
- metadata.gz: b0981561372e9039eda5c8eb85948c63559e1875f4764dfaa3b32d52fa501b62522b2acde9a58036aa6978bb9b454cb66bec788e9d141dbc50b3ce5e14ee9941
7
- data.tar.gz: 70a3927ceec9d61962002e301c817479e043878ec122ed1d265c9ed9372fb0abc744eaaf266e983b6352179f42e983752c030a12e878699c24b37fde8c6c663e
6
+ metadata.gz: 1cba9680627ce4ba4c5b8aa3df5505ab407e6096c26560799680a340640cef3b6a107319c58c1230ee130f1811d569367f8819d81f1be957069703b03d0bbb8b
7
+ data.tar.gz: 4c578bd594f740f9b07d288a1ae37de9d54cee7009da5f4d493a58ed324821cd4258ebce916ddfd68527e773c4396cd866da8054d4558d31c95962c6fd9ce9d1
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.15.0"
2
+ VERSION = "0.15.1"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -67,7 +67,10 @@ module PgConn
67
67
  # #exec or #transaction block
68
68
  attr_reader :timestamp
69
69
 
70
- # PG::Error object if the last statement failed; otherwise nil
70
+ # PG::Error object of the first failed statement in the transaction;
71
+ # otherwise nil. It is cleared at the beginning of a transaction so be sure
72
+ # to save it before you run any cleanup code that may initiate new
73
+ # transactions
71
74
  attr_reader :error
72
75
 
73
76
  # Tuple of error message, lineno, and charno of the error object where each
@@ -607,24 +610,17 @@ module PgConn
607
610
  #
608
611
  # TODO: Make sure the transaction stack is emptied on postgres errors
609
612
  def exec(sql, commit: true, fail: true, silent: false)
610
- begin
611
- transaction(commit: commit) { execute(sql, fail: fail, silent: silent) }
612
- rescue PG::Error
613
- raise if fail
614
- cancel_transaction
615
- return nil
616
- end
613
+ transaction(commit: commit) { execute(sql, fail: fail, silent: silent) }
617
614
  end
618
615
 
619
616
  # Like #exec but returns true/false depending on if the command succeeded.
620
- # There is not corresponding #exeucte? method because any failure rolls
617
+ # There is not a corresponding #execute? method because any failure rolls
621
618
  # back the whole transaction stack. TODO: Check which exceptions that
622
619
  # should be captured
623
620
  def exec?(sql, commit: true, silent: true)
624
621
  begin
625
622
  exec(sql, commit: commit, fail: true, silent: silent)
626
623
  rescue PG::Error
627
- cancel_transaction
628
624
  return false
629
625
  end
630
626
  return true
@@ -643,6 +639,7 @@ module PgConn
643
639
  begin
644
640
  pg_exec(sql, silent: silent)&.cmd_tuples
645
641
  rescue PG::Error
642
+ cancel_transaction
646
643
  raise if fail
647
644
  return nil
648
645
  end
@@ -672,7 +669,7 @@ module PgConn
672
669
 
673
670
  def commit()
674
671
  if transaction?
675
- pop_transaction
672
+ pop_transaction(fail: false)
676
673
  else
677
674
  pg_exec("commit")
678
675
  end
@@ -680,11 +677,18 @@ module PgConn
680
677
 
681
678
  def rollback() raise Rollback end
682
679
 
683
- # True if a transaction is in progress. Note that this requires all
684
- # transactions to be started using PgConn's transaction methods;
685
- # transactions started using raw SQL are not registered
680
+ # True if a transaction is in progress
681
+ #
682
+ # Note that this requires all transactions to be started using PgConn's
683
+ # transaction methods; transactions started using raw SQL are not
684
+ # registered
686
685
  def transaction?() !@savepoints.nil? end
687
686
 
687
+ # True if a database transaction is in progress
688
+ def database_transaction?
689
+ pg_exec("select transaction_timestamp() != statement_timestamp()", fail: false)
690
+ end
691
+
688
692
  # Returns number of transaction or savepoint levels
689
693
  def transactions() @savepoints ? 1 + @savepoints.size : 0 end
690
694
 
@@ -696,11 +700,12 @@ module PgConn
696
700
  else
697
701
  @savepoints = []
698
702
  pg_exec("begin")
703
+ @error = @err = nil
699
704
  @timestamp = pg_exec("select current_timestamp").values[0][0] if @pg_connection
700
705
  end
701
706
  end
702
707
 
703
- def pop_transaction(commit: true, fail: true)
708
+ def pop_transaction(commit: true, fail: true, exception: true)
704
709
  if transaction?
705
710
  if savepoint = @savepoints.pop
706
711
  if !commit
@@ -728,8 +733,10 @@ module PgConn
728
733
  begin
729
734
  pg_exec("rollback")
730
735
  rescue PG::Error
736
+ ;
731
737
  end
732
738
  @savepoints = nil
739
+ true
733
740
  end
734
741
 
735
742
  # Start a transaction. If called with a block, the block is executed within
@@ -748,13 +755,14 @@ module PgConn
748
755
  push_transaction
749
756
  result = yield
750
757
  rescue PgConn::Rollback
751
- pop_transaction(commit: false)
758
+ pop_transaction(commit: false, fail: false)
752
759
  return nil
753
760
  rescue PG::Error
761
+ cancel_transaction
754
762
  @savepoints = nil
755
763
  raise
756
764
  end
757
- pop_transaction(commit: commit)
765
+ pop_transaction(commit: commit, fail: false)
758
766
  result
759
767
  else
760
768
  push_transaction
@@ -812,7 +820,6 @@ module PgConn
812
820
  # TODO: Fix silent by not handling exceptions
813
821
  def pg_exec(arg, silent: false)
814
822
  if @pg_connection
815
- @error = @err = nil
816
823
  begin
817
824
  last_stmt = nil # To make the current SQL statement visible to the rescue clause. FIXME Not used?
818
825
  if arg.is_a?(String)
@@ -826,9 +833,11 @@ module PgConn
826
833
  last_stmt = stmts.last
827
834
  @pg_connection.exec(stmts.join(";\n"))
828
835
  end
829
-
830
836
  rescue PG::Error => ex
831
- @error = ex
837
+ if @error.nil?
838
+ @error = ex
839
+ @err = nil
840
+ end
832
841
  if !silent # FIXME Why do we handle this?
833
842
  $stderr.puts arg
834
843
  $stderr.puts
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.15.0
4
+ version: 0.15.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-04-11 00:00:00.000000000 Z
11
+ date: 2024-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg