From: Daniele Varrazzo Date: Sat, 14 Mar 2020 11:25:08 +0000 (+1300) Subject: Added PGconn.transaction_status X-Git-Tag: 3.0.dev0~724 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=24eef11b96eccfe2b3cf7faf013cc7a79ae3199c;p=thirdparty%2Fpsycopg.git Added PGconn.transaction_status --- diff --git a/psycopg3/_pq_ctypes.py b/psycopg3/_pq_ctypes.py index 9d624a9ad..aeb774795 100644 --- a/psycopg3/_pq_ctypes.py +++ b/psycopg3/_pq_ctypes.py @@ -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 diff --git a/psycopg3/pq.py b/psycopg3/pq.py index 35b4dd390..460da89f6 100644 --- a/psycopg3/pq.py +++ b/psycopg3/pq.py @@ -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", diff --git a/psycopg3/pq_ctypes.py b/psycopg3/pq_ctypes.py index 87bb1e87e..2675b5196 100644 --- a/psycopg3/pq_ctypes.py +++ b/psycopg3/pq_ctypes.py @@ -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)) diff --git a/psycopg3/pq_enums.py b/psycopg3/pq_enums.py index 83edca782..5c0958346 100644 --- a/psycopg3/pq_enums.py +++ b/psycopg3/pq_enums.py @@ -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() diff --git a/tests/test_pq.py b/tests/test_pq.py index e98efdc51..177356990 100644 --- a/tests/test_pq.py +++ b/tests/test_pq.py @@ -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