_fields_ = []
+class PGresult_struct(Structure):
+ _fields_ = []
+
+
class PQconninfoOption_struct(Structure):
_fields_ = [
("keyword", c_char_p),
PGconn_ptr = POINTER(PGconn_struct)
+PGresult_ptr = POINTER(PGresult_struct)
PQconninfoOption_ptr = POINTER(PQconninfoOption_struct)
PQsslInUse.argtypes = [PGconn_ptr]
PQsslInUse.restype = c_int
+# TODO: PQsslAttribute, PQsslAttributeNames, PQsslStruct, PQgetssl
+
+
+# 33.3. Command Execution Functions
+
+PQexec = pq.PQexec
+PQexec.argtypes = [PGconn_ptr, c_char_p]
+PQexec.restype = PGresult_ptr
+
+PQresultStatus = pq.PQresultStatus
+PQresultStatus.argtypes = [PGresult_ptr]
+PQresultStatus.restype = c_int
+
+PQclear = pq.PQclear
+PQclear.argtypes = [PGresult_ptr]
+PQclear.restype = None
+
# 33.11. Miscellaneous Functions
from .pq_enums import (
ConnStatus,
PollingStatus,
+ ExecStatus,
TransactionStatus,
Ping,
)
"ConnStatus",
"PollingStatus",
"TransactionStatus",
+ "ExecStatus",
"Ping",
"PGconn",
"Conninfo",
from .pq_enums import (
ConnStatus,
PollingStatus,
+ ExecStatus,
TransactionStatus,
Ping,
)
def ssl_in_use(self):
return bool(impl.PQsslInUse(self.pgconn_ptr))
+ def exec_(self, command):
+ rv = impl.PQexec(self.pgconn_ptr, self._encode(command))
+ if rv is None:
+ raise MemoryError("couldn't allocate PGresult")
+ return PGresult(rv)
+
def _encode(self, s):
if isinstance(s, bytes):
return s
return b.decode("utf8", "replace")
+class PGresult:
+ __slots__ = ("pgresult_ptr",)
+
+ def __init__(self, pgresult_ptr):
+ self.pgresult_ptr = pgresult_ptr
+
+ def __del__(self):
+ self.clear()
+
+ def clear(self):
+ self.pgresult_ptr, p = None, self.pgresult_ptr
+ if p is not None:
+ impl.PQclear(p)
+
+ @property
+ def status(self):
+ rv = impl.PQresultStatus(self.pgresult_ptr)
+ return ExecStatus(rv)
+
+
ConninfoOption = namedtuple(
"ConninfoOption", "keyword envvar compiled val label dispatcher dispsize"
)
PGRES_POLLING_ACTIVE = auto()
+class ExecStatus(IntEnum):
+ PGRES_EMPTY_QUERY = 0
+ PGRES_COMMAND_OK = auto()
+ PGRES_TUPLES_OK = auto()
+ PGRES_COPY_OUT = auto()
+ PGRES_COPY_IN = auto()
+ PGRES_BAD_RESPONSE = auto()
+ PGRES_NONFATAL_ERROR = auto()
+ PGRES_FATAL_ERROR = auto()
+ PGRES_COPY_BOTH = auto()
+ PGRES_SINGLE_TUPLE = auto()
+
+
class TransactionStatus(IntEnum):
PQTRANS_IDLE = 0
PQTRANS_ACTIVE = auto()
--- /dev/null
+#!/usr/bin/env python3
+
+
+def test_exec_empty(pq, pgconn):
+ res = pgconn.exec_(b"")
+ assert res.status == pq.ExecStatus.PGRES_EMPTY_QUERY
+
+
+def test_exec_command(pq, pgconn):
+ res = pgconn.exec_("set timezone to utc")
+ assert res.status == pq.ExecStatus.PGRES_COMMAND_OK