]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added PGconn.transaction_status
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 14 Mar 2020 11:25:08 +0000 (00:25 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 14 Mar 2020 11:25:08 +0000 (00:25 +1300)
psycopg3/_pq_ctypes.py
psycopg3/pq.py
psycopg3/pq_ctypes.py
psycopg3/pq_enums.py
tests/test_pq.py

index 9d624a9ad1ec3b84b5ca1519f640bbe44dcc34e5..aeb7747951216bdedde922d425638ca7725aadbc 100644 (file)
@@ -133,6 +133,10 @@ PQstatus = pq.PQstatus
 PQstatus.argtypes = [PGconn_ptr]
 PQstatus.restype = c_int
 
+PQtransactionStatus = pq.PQtransactionStatus
+PQtransactionStatus.argtypes = [PGconn_ptr]
+PQtransactionStatus.restype = c_int
+
 PQerrorMessage = pq.PQerrorMessage
 PQerrorMessage.argtypes = [PGconn_ptr]
 PQerrorMessage.restype = c_char_p
index 35b4dd390442f5898696555f046bbcaaef685b17..460da89f630ea75c5e0c73cafbe3bc039fd60122 100644 (file)
@@ -9,7 +9,12 @@ implementation-dependant but all the implementations share the same interface.
 
 # Copyright (C) 2020 The Psycopg Team
 
-from .pq_enums import ConnStatus, PostgresPollingStatus, PGPing
+from .pq_enums import (
+    ConnStatus,
+    PostgresPollingStatus,
+    TransactionStatus,
+    PGPing,
+)
 
 from . import pq_ctypes as pq_module
 
@@ -19,6 +24,7 @@ PQerror = pq_module.PQerror
 __all__ = (
     "ConnStatus",
     "PostgresPollingStatus",
+    "TransactionStatus",
     "PGPing",
     "PGconn",
     "PQerror",
index 87bb1e87e1d1570df610165da321999b671c8edd..2675b519671fd2b52cde132613a8fdf2b95c41e3 100644 (file)
@@ -11,7 +11,12 @@ implementation.
 from collections import namedtuple
 from ctypes import c_char_p, pointer
 
-from .pq_enums import ConnStatus, PostgresPollingStatus, PGPing
+from .pq_enums import (
+    ConnStatus,
+    PostgresPollingStatus,
+    TransactionStatus,
+    PGPing,
+)
 from . import _pq_ctypes as impl
 
 
@@ -26,9 +31,7 @@ class PGconn:
         self.pgconn_ptr = pgconn_ptr
 
     def __del__(self):
-        self.pgconn_ptr, p = None, self.pgconn_ptr
-        if p is not None:
-            impl.PQfinish(p)
+        self.finish()
 
     @classmethod
     def connect(cls, conninfo):
@@ -54,6 +57,11 @@ class PGconn:
         rv = impl.PQconnectPoll(self.pgconn_ptr)
         return PostgresPollingStatus(rv)
 
+    def finish(self):
+        self.pgconn_ptr, p = None, self.pgconn_ptr
+        if p is not None:
+            impl.PQfinish(p)
+
     @classmethod
     def get_defaults(cls):
         opts = impl.PQconndefaults()
@@ -155,6 +163,11 @@ class PGconn:
         rv = impl.PQstatus(self.pgconn_ptr)
         return ConnStatus(rv)
 
+    @property
+    def transaction_status(self):
+        rv = impl.PQtransactionStatus(self.pgconn_ptr)
+        return TransactionStatus(rv)
+
     @property
     def error_message(self):
         return self._decode(impl.PQerrorMessage(self.pgconn_ptr))
index 83edca782b2071e36fb23ff0daf5a674cb6730cd..5c095834617d6e4c14ad27cb87d5e21ddea77621 100644 (file)
@@ -32,6 +32,14 @@ class PostgresPollingStatus(IntEnum):
     PGRES_POLLING_ACTIVE = auto()
 
 
+class TransactionStatus(IntEnum):
+    PQTRANS_IDLE = 0
+    PQTRANS_ACTIVE = auto()
+    PQTRANS_INTRANS = auto()
+    PQTRANS_INERROR = auto()
+    PQTRANS_UNKNOWN = auto()
+
+
 class PGPing(IntEnum):
     PQPING_OK = 0
     PQPING_REJECT = auto()
index e98efdc51d60fdd6f0a7edb922143c9d594b53e0..1773569908985ef4a67f5ccee01e6f8c4a356b58 100644 (file)
@@ -174,3 +174,10 @@ def test_hostaddr(pgconn):
 def test_tty(pgconn):
     tty = [o.val for o in pgconn.info if o.keyword == "tty"][0]
     assert pgconn.tty == tty
+
+
+def test_transaction_status(pq, pgconn):
+    assert pgconn.transaction_status == pq.TransactionStatus.PQTRANS_IDLE
+    # TODO: test other states
+    pgconn.finish()
+    assert pgconn.transaction_status == pq.TransactionStatus.PQTRANS_UNKNOWN