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
# 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
__all__ = (
"ConnStatus",
"PostgresPollingStatus",
+ "TransactionStatus",
"PGPing",
"PGconn",
"PQerror",
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
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):
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()
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))
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()
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