Skip to content

Commit

Permalink
CONPY-184
Browse files Browse the repository at this point in the history
Display status of connection, cursor and pool class in
string representation.

If an object (cursor, class, connection pool) was closed, the
string representation (tp_repr) now shows the status of the object.

Example:

>>> import mariadb
>>> connection=mariadb.connect()
>>> connection
<mariadb.connection connected to 'localhost' at 0x7f94d77c3b40>
>>> connection.close()
>>> connection
<mariadb.connection (closed) at 0x7f94d77c3b40>
  • Loading branch information
9EOR9 committed Dec 13, 2021
1 parent 5831a69 commit 263f428
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
9 changes: 5 additions & 4 deletions include/mariadb_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ typedef struct {
enum enum_tpc_state tpc_state;
char xid[150]; /* large enough, to hold 2 * MAX_TPC_XID size + integer value */
PyObject *dsn; /* always null */
const char *host;
/* const char *tls_cipher;
const char *tls_version;
const char *host;
const char *unix_socket;
int port;
const char *charset;
Expand All @@ -195,6 +195,7 @@ typedef struct {
PyThreadState *thread_state;
unsigned long thread_id;
char *server_info;
uint8_t closed;
} MrdbConnection;

typedef struct {
Expand Down Expand Up @@ -265,7 +266,7 @@ typedef struct {
uint8_t is_prepared;
char is_buffered;
uint8_t fetched;
uint8_t is_closed;
uint8_t closed;
uint8_t reprepare;
PyThreadState *thread_state;
enum enum_paramstyle paramstyle;
Expand Down Expand Up @@ -434,9 +435,9 @@ MrdbParser_parse(MrdbParser *p, uint8_t is_batch, char *errmsg, size_t errmsg_le
}

#define MARIADB_CHECK_STMT(cursor)\
if (!cursor->connection->mysql || cursor->is_closed)\
if (!cursor->connection->mysql || cursor->closed)\
{\
(cursor)->is_closed= 1;\
(cursor)->closed= 1;\
mariadb_throw_exception(cursor->stmt, Mariadb_ProgrammingError, 1,\
"Invalid cursor or not connected");\
}
Expand Down
11 changes: 9 additions & 2 deletions mariadb/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
POOL_IDLE_TIMEOUT = 1800

class ConnectionPool(object):
r"""
"""
Class defining a pool of database connections
MariaDB Connector/Python supports simple connection pooling.
Expand All @@ -39,7 +39,7 @@ class ConnectionPool(object):
* pool_name (str) -- Name of connection pool
* pool_size (int)=5 -- Size of pool. If not specified default value of 5 will be used. Maximum allowed number is 64.
* pool_size (int)=5 -- Size of pool. If not specified default value of 5 will be used. Maximum allowed number is 64.
* pool_reset_connection (bool)=True -- Will reset the connection before returning it to the pool. Default value is True.
"""
Expand All @@ -63,6 +63,7 @@ def __init__(self, *args, **kwargs):
self._pool_args = {}
self._conn_args = {}
self._lock_pool = _thread.RLock()
self.__closed= 0

key_words= ["pool_name", "pool_size", "pool_reset_connection"]

Expand Down Expand Up @@ -115,6 +116,12 @@ def __init__(self, *args, **kwargs):
# store connection pool in _CONNECTION_POOLS
mariadb._CONNECTION_POOLS[self._pool_args["name"]]= self

def __repr__(self):
if (self.__closed):
return "<mariadb.connectionPool.ConnectionPool object (closed) at %s>" % (hex(id(self)),)
else:
return "<mariadb.connectionPool.ConnectionPool object (name=%s) at %s>" % (self.pool_name, hex(id(self)))

def add_connection(self, connection= None):
"""
Adds a connection object to the connection pool.
Expand Down
1 change: 1 addition & 0 deletions mariadb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, *args, **kwargs):
self.__last_used = 0
self.tpc_state= TPC_STATE.NONE
self._xid= None
self.__closed= None

autocommit= kwargs.pop("autocommit", False)
self._converter= kwargs.pop("converter", None)
Expand Down
22 changes: 21 additions & 1 deletion mariadb/mariadb_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ PyMemberDef MrdbConnection_Members[] =
offsetof(MrdbConnection, dsn),
READONLY,
"Data source name (dsn)"},
{"_closed",
T_BOOL,
offsetof(MrdbConnection, closed),
READONLY,
"Indicates if connection was closed"},
{NULL} /* always last */
};

Expand Down Expand Up @@ -307,6 +312,7 @@ MrdbConnection_Initialize(MrdbConnection *self,
}

self->thread_id= mysql_thread_id(self->mysql);
mariadb_get_infov(self->mysql, MARIADB_CONNECTION_HOST, &self->host);

has_error= 0;
end:
Expand All @@ -332,6 +338,19 @@ static int MrdbConnection_traverse(
return 0;
}

static PyObject *MrdbConnection_repr(MrdbConnection *self)
{
char cobj_repr[384];

if (!self->closed)
snprintf(cobj_repr, 384, "<mariadb.connection connected to '%s' at %p>",
self->host, self);
else
snprintf(cobj_repr, 384, "<mariadb.connection (closed) at %p>",
self);
return PyUnicode_FromString(cobj_repr);
}

PyTypeObject MrdbConnection_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"mariadb.connection",
Expand All @@ -342,7 +361,7 @@ PyTypeObject MrdbConnection_Type = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* PyAsyncMethods* */
0, /* tp_repr */
(reprfunc)MrdbConnection_repr, /* tp_repr */

/* Method suites for standard classes */

Expand Down Expand Up @@ -467,6 +486,7 @@ PyObject *MrdbConnection_close(MrdbConnection *self)
mysql_close(self->mysql);
Py_END_ALLOW_THREADS
self->mysql= NULL;
self->closed= 1;
Py_RETURN_NONE;
}

Expand Down
22 changes: 17 additions & 5 deletions mariadb/mariadb_cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ static int MrdbCursor_traverse(
return 0;
}

static PyObject *MrdbCursor_repr(MrdbCursor *self)
{
char cobj_repr[384];

if (!self->closed)
snprintf(cobj_repr, 384, "<mariadb.cursor at %p>", self);
else
snprintf(cobj_repr, 384, "<mariadb.cursor (closed) at %p>",
self);
return PyUnicode_FromString(cobj_repr);
}

PyTypeObject MrdbCursor_Type =
{
PyVarObject_HEAD_INIT(NULL, 0)
Expand All @@ -329,7 +341,7 @@ PyTypeObject MrdbCursor_Type =
0, /* tp_getattr */
0, /* tp_setattr */
0, /* PyAsyncMethods * */
0, /* tp_repr */
(reprfunc)MrdbCursor_repr, /* tp_repr */

/* Method suites for standard classes */

Expand Down Expand Up @@ -503,7 +515,7 @@ static void ma_set_result_column_value(MrdbCursor *self, PyObject *row, uint32_t
static
void ma_cursor_close(MrdbCursor *self)
{
if (!self->is_closed)
if (!self->closed)
{
MrdbCursor_clear_result(self);
if (!self->parseinfo.is_text && self->stmt)
Expand All @@ -524,11 +536,11 @@ void ma_cursor_close(MrdbCursor *self)
}

MrdbCursor_clearparseinfo(&self->parseinfo);
self->is_closed= 1;
self->closed= 1;
}
}

static
static
PyObject * MrdbCursor_close(MrdbCursor *self)
{
ma_cursor_close(self);
Expand Down Expand Up @@ -949,7 +961,7 @@ MrdbCursor_iternext(PyObject *self)
static PyObject
*MrdbCursor_closed(MrdbCursor *self)
{
if (self->is_closed || self->connection->mysql == NULL)
if (self->closed || self->connection->mysql == NULL)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
Expand Down

0 comments on commit 263f428

Please sign in to comment.