pg_conn 0.14.0 → 0.15.1

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 +40 -28
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d29017a92f97bb843ada8a10d00ab0a130e0e572e9775f21ddacb9f6d9ca4e66
4
- data.tar.gz: e56a31694ec01c3ef15ae35b1f5a28304dad179a918c69a687e08fb267f1b30e
3
+ metadata.gz: 5ebb0df0000ecb59d313c906b608bdd267aaef84edcb4be633650601c453768b
4
+ data.tar.gz: 96d5e1db6d3644fef73dbb69f50acd7deec6dc79404641615d034586d1fddcf0
5
5
  SHA512:
6
- metadata.gz: a920f75a6d118bdc44e5442adbe38e6ecbc6872ac010f3c29390a25db4e95605541d4bc1dd67a0f7ac1332611119d983211aaff1acda885aca07ed1a464705a7
7
- data.tar.gz: 4339f922abbdeb6db692a8906a5c5fea4aa21a645be69df90a39473cc2453c9f79c684b8b2e69996a343cc368dcaeaecc960154dbcc78003ec230bd3605363ad
6
+ metadata.gz: 1cba9680627ce4ba4c5b8aa3df5505ab407e6096c26560799680a340640cef3b6a107319c58c1230ee130f1811d569367f8819d81f1be957069703b03d0bbb8b
7
+ data.tar.gz: 4c578bd594f740f9b07d288a1ae37de9d54cee7009da5f4d493a58ed324821cd4258ebce916ddfd68527e773c4396cd866da8054d4558d31c95962c6fd9ce9d1
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.14.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,22 +700,26 @@ 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)
704
- transaction? or raise Error, "No transaction in progress"
705
- if savepoint = @savepoints.pop
706
- if !commit
707
- pg_exec("rollback to savepoint #{savepoint}")
708
- pg_exec("release savepoint #{savepoint}")
708
+ def pop_transaction(commit: true, fail: true, exception: true)
709
+ if transaction?
710
+ if savepoint = @savepoints.pop
711
+ if !commit
712
+ pg_exec("rollback to savepoint #{savepoint}")
713
+ pg_exec("release savepoint #{savepoint}")
714
+ else
715
+ pg_exec("release savepoint #{savepoint}")
716
+ end
709
717
  else
710
- pg_exec("release savepoint #{savepoint}")
718
+ @savepoints = nil
719
+ pg_exec(commit ? "commit" : "rollback")
711
720
  end
712
721
  else
713
- @savepoints = nil
714
- pg_exec(commit ? "commit" : "rollback")
722
+ fail and raise Error, "No transaction in progress"
715
723
  end
716
724
  end
717
725
 
@@ -725,8 +733,10 @@ module PgConn
725
733
  begin
726
734
  pg_exec("rollback")
727
735
  rescue PG::Error
736
+ ;
728
737
  end
729
738
  @savepoints = nil
739
+ true
730
740
  end
731
741
 
732
742
  # Start a transaction. If called with a block, the block is executed within
@@ -745,13 +755,14 @@ module PgConn
745
755
  push_transaction
746
756
  result = yield
747
757
  rescue PgConn::Rollback
748
- pop_transaction(commit: false)
758
+ pop_transaction(commit: false, fail: false)
749
759
  return nil
750
760
  rescue PG::Error
761
+ cancel_transaction
751
762
  @savepoints = nil
752
763
  raise
753
764
  end
754
- pop_transaction(commit: commit)
765
+ pop_transaction(commit: commit, fail: false)
755
766
  result
756
767
  else
757
768
  push_transaction
@@ -809,7 +820,6 @@ module PgConn
809
820
  # TODO: Fix silent by not handling exceptions
810
821
  def pg_exec(arg, silent: false)
811
822
  if @pg_connection
812
- @error = @err = nil
813
823
  begin
814
824
  last_stmt = nil # To make the current SQL statement visible to the rescue clause. FIXME Not used?
815
825
  if arg.is_a?(String)
@@ -823,9 +833,11 @@ module PgConn
823
833
  last_stmt = stmts.last
824
834
  @pg_connection.exec(stmts.join(";\n"))
825
835
  end
826
-
827
836
  rescue PG::Error => ex
828
- @error = ex
837
+ if @error.nil?
838
+ @error = ex
839
+ @err = nil
840
+ end
829
841
  if !silent # FIXME Why do we handle this?
830
842
  $stderr.puts arg
831
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.14.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