if self.conn is None:
raise e.OperationalError("escape_literal failed: no connection provided")
- if self.conn.pgconn_ptr is NULL:
+ if self.conn._pgconn_ptr is NULL:
raise e.OperationalError("the connection is closed")
_buffer_as_string_and_size(data, &ptr, &length)
- out = libpq.PQescapeLiteral(self.conn.pgconn_ptr, ptr, length)
+ out = libpq.PQescapeLiteral(self.conn._pgconn_ptr, ptr, length)
if out is NULL:
raise e.OperationalError(
f"escape_literal failed: {error_message(self.conn)}"
if self.conn is None:
raise e.OperationalError("escape_identifier failed: no connection provided")
- if self.conn.pgconn_ptr is NULL:
+ if self.conn._pgconn_ptr is NULL:
raise e.OperationalError("the connection is closed")
- out = libpq.PQescapeIdentifier(self.conn.pgconn_ptr, ptr, length)
+ out = libpq.PQescapeIdentifier(self.conn._pgconn_ptr, ptr, length)
if out is NULL:
raise e.OperationalError(
f"escape_identifier failed: {error_message(self.conn)}"
PyByteArray_Resize(rv, length * 2 + 1)
if self.conn is not None:
- if self.conn.pgconn_ptr is NULL:
+ if self.conn._pgconn_ptr is NULL:
raise e.OperationalError("the connection is closed")
len_out = libpq.PQescapeStringConn(
- self.conn.pgconn_ptr, PyByteArray_AS_STRING(rv),
+ self.conn._pgconn_ptr, PyByteArray_AS_STRING(rv),
ptr, length, &error
)
if error:
cdef char *ptr
cdef Py_ssize_t length
- if self.conn is not None and self.conn.pgconn_ptr is NULL:
+ if self.conn is not None and self.conn._pgconn_ptr is NULL:
raise e.OperationalError("the connection is closed")
_buffer_as_string_and_size(data, &ptr, &length)
if self.conn is not None:
out = libpq.PQescapeByteaConn(
- self.conn.pgconn_ptr, <unsigned char *>ptr, length, &len_out)
+ self.conn._pgconn_ptr, <unsigned char *>ptr, length, &len_out)
else:
out = libpq.PQescapeBytea(<unsigned char *>ptr, length, &len_out)
# not needed, but let's keep it symmetric with the escaping:
# if a connection is passed in, it must be valid.
if self.conn is not None:
- if self.conn.pgconn_ptr is NULL:
+ if self.conn._pgconn_ptr is NULL:
raise e.OperationalError("the connection is closed")
cdef size_t len_out
@staticmethod
cdef PGconn _from_ptr(libpq.PGconn *ptr):
cdef PGconn rv = PGconn.__new__(PGconn)
- rv.pgconn_ptr = ptr
+ rv._pgconn_ptr = ptr
libpq.PQsetNoticeReceiver(ptr, notice_receiver, <void *>rv)
return rv
def __cinit__(self):
- self.pgconn_ptr = NULL
+ self._pgconn_ptr = NULL
self._procpid = getpid()
def __dealloc__(self):
return _call_int(self, <conn_int_f>libpq.PQconnectPoll)
def finish(self) -> None:
- if self.pgconn_ptr is not NULL:
- libpq.PQfinish(self.pgconn_ptr)
- self.pgconn_ptr = NULL
+ if self._pgconn_ptr is not NULL:
+ libpq.PQfinish(self._pgconn_ptr)
+ self._pgconn_ptr = NULL
@property
def pgconn_ptr(self) -> Optional[int]:
- if self.pgconn_ptr:
- return <long><void *>self.pgconn_ptr
+ if self._pgconn_ptr:
+ return <long><void *>self._pgconn_ptr
else:
return None
@property
def info(self) -> List["ConninfoOption"]:
_ensure_pgconn(self)
- cdef libpq.PQconninfoOption *opts = libpq.PQconninfo(self.pgconn_ptr)
+ cdef libpq.PQconninfoOption *opts = libpq.PQconninfo(self._pgconn_ptr)
if opts is NULL:
raise MemoryError("couldn't allocate connection info")
rv = _options_from_array(opts)
def reset(self) -> None:
_ensure_pgconn(self)
- libpq.PQreset(self.pgconn_ptr)
+ libpq.PQreset(self._pgconn_ptr)
def reset_start(self) -> None:
- if not libpq.PQresetStart(self.pgconn_ptr):
+ if not libpq.PQresetStart(self._pgconn_ptr):
raise e.OperationalError("couldn't reset connection")
def reset_poll(self) -> int:
from psycopg3.pq import _pq_ctypes
_ensure_pgconn(self)
- ctypes_ptr = ctypes.cast(
- <long><void *>self.pgconn_ptr, _pq_ctypes.PGconn_ptr)
- return _pq_ctypes.PQhostaddr(ctypes_ptr)
+ return _pq_ctypes.PQhostaddr(
+ ctypes.cast(self.pgconn_ptr, _pq_ctypes.PGconn_ptr))
@property
def port(self) -> bytes:
@property
def status(self) -> int:
- return libpq.PQstatus(self.pgconn_ptr)
+ return libpq.PQstatus(self._pgconn_ptr)
@property
def transaction_status(self) -> int:
- return libpq.PQtransactionStatus(self.pgconn_ptr)
+ return libpq.PQtransactionStatus(self._pgconn_ptr)
def parameter_status(self, const char *name) -> Optional[bytes]:
_ensure_pgconn(self)
- cdef const char *rv = libpq.PQparameterStatus(self.pgconn_ptr, name)
+ cdef const char *rv = libpq.PQparameterStatus(self._pgconn_ptr, name)
if rv is not NULL:
return rv
else:
@property
def error_message(self) -> bytes:
- return libpq.PQerrorMessage(self.pgconn_ptr)
+ return libpq.PQerrorMessage(self._pgconn_ptr)
@property
def protocol_version(self) -> int:
@property
def needs_password(self) -> bool:
- return bool(libpq.PQconnectionNeedsPassword(self.pgconn_ptr))
+ return bool(libpq.PQconnectionNeedsPassword(self._pgconn_ptr))
@property
def used_password(self) -> bool:
- return bool(libpq.PQconnectionUsedPassword(self.pgconn_ptr))
+ return bool(libpq.PQconnectionUsedPassword(self._pgconn_ptr))
@property
def ssl_in_use(self) -> bool:
_ensure_pgconn(self)
cdef libpq.PGresult *pgresult
with nogil:
- pgresult = libpq.PQexec(self.pgconn_ptr, command)
+ pgresult = libpq.PQexec(self._pgconn_ptr, command)
if pgresult is NULL:
raise MemoryError("couldn't allocate PGresult")
_ensure_pgconn(self)
cdef int rv
with nogil:
- rv = libpq.PQsendQuery(self.pgconn_ptr, command)
+ rv = libpq.PQsendQuery(self._pgconn_ptr, command)
if not rv:
raise e.OperationalError(f"sending query failed: {error_message(self)}")
cdef libpq.PGresult *pgresult
with nogil:
pgresult = libpq.PQexecParams(
- self.pgconn_ptr, command, cnparams, ctypes,
+ self._pgconn_ptr, command, cnparams, ctypes,
<const char *const *>cvalues, clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if pgresult is NULL:
cdef int rv
with nogil:
rv = libpq.PQsendQueryParams(
- self.pgconn_ptr, command, cnparams, ctypes,
+ self._pgconn_ptr, command, cnparams, ctypes,
<const char *const *>cvalues, clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if not rv:
cdef int rv
with nogil:
rv = libpq.PQsendPrepare(
- self.pgconn_ptr, name, command, nparams, atypes
+ self._pgconn_ptr, name, command, nparams, atypes
)
PyMem_Free(atypes)
if not rv:
cdef int rv
with nogil:
rv = libpq.PQsendQueryPrepared(
- self.pgconn_ptr, name, cnparams, <const char *const *>cvalues,
+ self._pgconn_ptr, name, cnparams, <const char *const *>cvalues,
clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if not rv:
cdef libpq.PGresult *rv
with nogil:
rv = libpq.PQprepare(
- self.pgconn_ptr, name, command, nparams, atypes)
+ self._pgconn_ptr, name, command, nparams, atypes)
PyMem_Free(atypes)
if rv is NULL:
raise MemoryError("couldn't allocate PGresult")
cdef libpq.PGresult *rv
with nogil:
rv = libpq.PQexecPrepared(
- self.pgconn_ptr, name, cnparams,
+ self._pgconn_ptr, name, cnparams,
<const char *const *>cvalues,
clengths, cformats, result_format)
def describe_prepared(self, const char *name) -> PGresult:
_ensure_pgconn(self)
- cdef libpq.PGresult *rv = libpq.PQdescribePrepared(self.pgconn_ptr, name)
+ cdef libpq.PGresult *rv = libpq.PQdescribePrepared(self._pgconn_ptr, name)
if rv is NULL:
raise MemoryError("couldn't allocate PGresult")
return PGresult._from_ptr(rv)
def send_describe_prepared(self, const char *name) -> None:
_ensure_pgconn(self)
- cdef int rv = libpq.PQsendDescribePrepared(self.pgconn_ptr, name)
+ cdef int rv = libpq.PQsendDescribePrepared(self._pgconn_ptr, name)
if not rv:
raise e.OperationalError(
f"sending describe prepared failed: {error_message(self)}"
def describe_portal(self, const char *name) -> PGresult:
_ensure_pgconn(self)
- cdef libpq.PGresult *rv = libpq.PQdescribePortal(self.pgconn_ptr, name)
+ cdef libpq.PGresult *rv = libpq.PQdescribePortal(self._pgconn_ptr, name)
if rv is NULL:
raise MemoryError("couldn't allocate PGresult")
return PGresult._from_ptr(rv)
def send_describe_portal(self, const char *name) -> None:
_ensure_pgconn(self)
- cdef int rv = libpq.PQsendDescribePortal(self.pgconn_ptr, name)
+ cdef int rv = libpq.PQsendDescribePortal(self._pgconn_ptr, name)
if not rv:
raise e.OperationalError(
f"sending describe prepared failed: {error_message(self)}"
)
def get_result(self) -> Optional["PGresult"]:
- cdef libpq.PGresult *pgresult = libpq.PQgetResult(self.pgconn_ptr)
+ cdef libpq.PGresult *pgresult = libpq.PQgetResult(self._pgconn_ptr)
if pgresult is NULL:
return None
return PGresult._from_ptr(pgresult)
def consume_input(self) -> None:
- if 1 != libpq.PQconsumeInput(self.pgconn_ptr):
+ if 1 != libpq.PQconsumeInput(self._pgconn_ptr):
raise e.OperationalError(f"consuming input failed: {error_message(self)}")
def is_busy(self) -> int:
cdef int rv
with nogil:
- rv = libpq.PQisBusy(self.pgconn_ptr)
+ rv = libpq.PQisBusy(self._pgconn_ptr)
return rv
@property
def nonblocking(self) -> int:
- return libpq.PQisnonblocking(self.pgconn_ptr)
+ return libpq.PQisnonblocking(self._pgconn_ptr)
@nonblocking.setter
def nonblocking(self, int arg) -> None:
- if 0 > libpq.PQsetnonblocking(self.pgconn_ptr, arg):
+ if 0 > libpq.PQsetnonblocking(self._pgconn_ptr, arg):
raise e.OperationalError(f"setting nonblocking failed: {error_message(self)}")
def flush(self) -> int:
- if self.pgconn_ptr == NULL:
+ if self._pgconn_ptr == NULL:
raise e.OperationalError(f"flushing failed: the connection is closed")
- cdef int rv = libpq.PQflush(self.pgconn_ptr)
+ cdef int rv = libpq.PQflush(self._pgconn_ptr)
if rv < 0:
raise e.OperationalError(f"flushing failed: {error_message(self)}")
return rv
def set_single_row_mode(self) -> None:
- if not libpq.PQsetSingleRowMode(self.pgconn_ptr):
+ if not libpq.PQsetSingleRowMode(self._pgconn_ptr):
raise e.OperationalError("setting single row mode failed")
def get_cancel(self) -> PGcancel:
- cdef libpq.PGcancel *ptr = libpq.PQgetCancel(self.pgconn_ptr)
+ cdef libpq.PGcancel *ptr = libpq.PQgetCancel(self._pgconn_ptr)
if not ptr:
raise e.OperationalError("couldn't create cancel object")
return PGcancel._from_ptr(ptr)
cpdef object notifies(self):
cdef libpq.PGnotify *ptr
with nogil:
- ptr = libpq.PQnotifies(self.pgconn_ptr)
+ ptr = libpq.PQnotifies(self._pgconn_ptr)
if ptr:
ret = PGnotify(ptr.relname, ptr.be_pid, ptr.extra)
libpq.PQfreemem(ptr)
cdef Py_ssize_t length
_buffer_as_string_and_size(buffer, &cbuffer, &length)
- rv = libpq.PQputCopyData(self.pgconn_ptr, cbuffer, length)
+ rv = libpq.PQputCopyData(self._pgconn_ptr, cbuffer, length)
if rv < 0:
raise e.OperationalError(f"sending copy data failed: {error_message(self)}")
return rv
cdef const char *cerr = NULL
if error is not None:
cerr = PyBytes_AsString(error)
- rv = libpq.PQputCopyEnd(self.pgconn_ptr, cerr)
+ rv = libpq.PQputCopyEnd(self._pgconn_ptr, cerr)
if rv < 0:
raise e.OperationalError(f"sending copy end failed: {error_message(self)}")
return rv
def get_copy_data(self, int async_) -> Tuple[int, memoryview]:
cdef char *buffer_ptr = NULL
cdef int nbytes
- nbytes = libpq.PQgetCopyData(self.pgconn_ptr, &buffer_ptr, async_)
+ nbytes = libpq.PQgetCopyData(self._pgconn_ptr, &buffer_ptr, async_)
if nbytes == -2:
raise e.OperationalError(f"receiving copy data failed: {error_message(self)}")
if buffer_ptr is not NULL:
def make_empty_result(self, int exec_status) -> PGresult:
cdef libpq.PGresult *rv = libpq.PQmakeEmptyPGresult(
- self.pgconn_ptr, <libpq.ExecStatusType>exec_status)
+ self._pgconn_ptr, <libpq.ExecStatusType>exec_status)
if not rv:
raise MemoryError("couldn't allocate empty PGresult")
return PGresult._from_ptr(rv)
cdef int _ensure_pgconn(PGconn pgconn) except 0:
- if pgconn.pgconn_ptr is not NULL:
+ if pgconn._pgconn_ptr is not NULL:
return 1
raise e.OperationalError("the connection is closed")
"""
if not _ensure_pgconn(pgconn):
return NULL
- cdef char *rv = func(pgconn.pgconn_ptr)
+ cdef char *rv = func(pgconn._pgconn_ptr)
assert rv is not NULL
return rv
"""
if not _ensure_pgconn(pgconn):
return -2
- return func(pgconn.pgconn_ptr)
+ return func(pgconn._pgconn_ptr)
cdef void notice_receiver(void *arg, const libpq.PGresult *res_ptr) with gil: