Skip to content

Commit

Permalink
connection class:
Browse files Browse the repository at this point in the history
moved kill method to python class
  • Loading branch information
9EOR9 committed Jul 22, 2021
1 parent 5870f5d commit ea97f66
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 90 deletions.
1 change: 1 addition & 0 deletions include/mariadb_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ typedef struct {
PyThreadState *thread_state;
PyObject *converter;
unsigned long server_version;
unsigned long thread_id;
char *server_info;
} MrdbConnection;

Expand Down
7 changes: 7 additions & 0 deletions mariadb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ def rollback(self):
self._execute_command("ROLLBACK")
self._read_response()

def kill(self, id):
if not isinstance(id, int):
raise mariadb.ProgrammingError("id must be of type int.")
stmt= "KILL %s" % id
self._execute_command(stmt)
self._read_response()

def get_server_version(self):
return self.server_version_info

Expand Down
103 changes: 13 additions & 90 deletions mariadb/mariadb_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,12 @@ const char *mariadb_default_collation= "utf8mb4_general_ci";
void
MrdbConnection_dealloc(MrdbConnection *self);

static PyObject
*MrdbConnection_cursor(MrdbConnection *self, PyObject *args, PyObject *kwargs);

static PyObject *
MrdbConnection_exception(PyObject *self, void *closure);

static PyObject *
MrdbConnection_query(MrdbConnection *self, PyObject *args);

#define GETTER_EXCEPTION(name, exception, doc)\
{ name,MrdbConnection_exception, NULL, doc, &exception }

static PyObject *
MrdbConnection_getid(MrdbConnection *self, void *closure);

static PyObject *
MrdbConnection_getuser(MrdbConnection *self, void *closure);

Expand Down Expand Up @@ -97,8 +88,6 @@ MrdbConnection_readresponse(MrdbConnection *self);
static PyGetSetDef
MrdbConnection_sets[]=
{
{"connection_id", (getter)MrdbConnection_getid, NULL,
connection_connection_id__doc__, NULL},
{"database", (getter)MrdbConnection_getdb, (setter)MrdbConnection_setdb,
connection_database__doc__, NULL},
{"auto_reconnect", (getter)MrdbConnection_getreconnect,
Expand All @@ -125,19 +114,13 @@ MrdbConnection_sets[]=
static PyMethodDef
MrdbConnection_Methods[] =
{
{"_query", (PyCFunction)MrdbConnection_query,
METH_VARARGS,
NULL},
/* PEP-249 methods */
{"close", (PyCFunction)MrdbConnection_close,
METH_NOARGS,
connection_close__doc__},
{"connect", (PyCFunction)MrdbConnection_connect,
METH_VARARGS | METH_KEYWORDS,
connection_connect__doc__},
{"cursor", (PyCFunction)MrdbConnection_cursor,
METH_VARARGS | METH_KEYWORDS,
connection_cursor__doc__},
/*TPC methods */
{"tpc_recover",
(PyCFunction)MrdbConnection_tpc_recover,
Expand All @@ -154,11 +137,6 @@ MrdbConnection_Methods[] =
METH_VARARGS,
connection_change_user__doc__
},
{ "kill",
(PyCFunction)MrdbConnection_kill,
METH_VARARGS,
connection_kill__doc__
},
{ "reconnect",
(PyCFunction)MrdbConnection_reconnect,
METH_NOARGS,
Expand Down Expand Up @@ -199,6 +177,11 @@ PyMemberDef MrdbConnection_Members[] =
offsetof(MrdbConnection, converter),
READONLY,
"Conversion dictionary"},
{"connection_id",
T_ULONG,
offsetof(MrdbConnection, thread_id),
READONLY,
connection_connection_id__doc__},
{"collation",
T_STRING,
offsetof(MrdbConnection, collation),
Expand Down Expand Up @@ -246,7 +229,7 @@ PyMemberDef MrdbConnection_Members[] =
"TLS protocol version used by connection"},
{"client_capabilities",
T_ULONG,
offsetof(MrdbConnection, server_capabilities),
offsetof(MrdbConnection, client_capabilities),
READONLY,
"Client capabilities"},
{"server_capabilities",
Expand Down Expand Up @@ -419,12 +402,15 @@ MrdbConnection_Initialize(MrdbConnection *self,
mysql_real_connect(self->mysql, host, user, password, schema, port,
socket, client_flags);
Py_END_ALLOW_THREADS;

if (mysql_errno(self->mysql))
{
mariadb_throw_exception(self->mysql, NULL, 0, NULL);
goto end;
}

self->thread_id= mysql_thread_id(self->mysql);

/* CONPY-129: server_version_info */
self->server_version= mysql_get_server_version(self->mysql);
self->server_info= mysql_get_server_info(self->mysql);
Expand Down Expand Up @@ -598,19 +584,6 @@ PyObject *MrdbConnection_close(MrdbConnection *self)
Py_RETURN_NONE;
}

static PyObject *MrdbConnection_cursor(MrdbConnection *self,
PyObject *args,
PyObject *kwargs)
{
PyObject *cursor= NULL;
PyObject *conn = NULL;

conn= Py_BuildValue("(O)", self);
cursor= PyObject_Call((PyObject *)&MrdbCursor_Type, conn, kwargs);
Py_DECREF(conn);
return cursor;
}

static PyObject *
MrdbConnection_exception(PyObject *self, void *closure)
{
Expand Down Expand Up @@ -742,6 +715,9 @@ PyObject *MrdbConnection_ping(MrdbConnection *self)
return NULL;
}

/* in case a reconnect occured, we need to obtain new thread_id */
self->thread_id= mysql_thread_id(self->mysql);

Py_RETURN_NONE;
}
/* }}} */
Expand Down Expand Up @@ -772,39 +748,6 @@ PyObject *MrdbConnection_change_user(MrdbConnection *self,
}
/* }}} */

/* {{{ MrdbConnection_kill */
PyObject *MrdbConnection_kill(MrdbConnection *self, PyObject *args)
{
int rc;
unsigned long thread_id= 0;

MARIADB_CHECK_CONNECTION(self, NULL);
if (!PyArg_ParseTuple(args, "l", &thread_id))
return NULL;

Py_BEGIN_ALLOW_THREADS;
rc= mysql_kill(self->mysql, thread_id);
Py_END_ALLOW_THREADS;

if (rc)
{
mariadb_throw_exception(self->mysql, Mariadb_DatabaseError, 0, NULL);
return NULL;
}
Py_RETURN_NONE;
}

/* {{{ MrdbConnection_getid */
static PyObject *MrdbConnection_getid(MrdbConnection *self, void *closure)
{
PyObject *p;

MARIADB_CHECK_CONNECTION(self, NULL);
p= PyLong_FromUnsignedLong(mysql_thread_id(self->mysql));
return p;
}
/* }}} */

/* {{{ MrdbConnection_getreconnect */
static PyObject *MrdbConnection_getreconnect(MrdbConnection *self,
void *closure)
Expand Down Expand Up @@ -932,6 +875,7 @@ PyObject *MrdbConnection_reconnect(MrdbConnection *self)
return NULL;
}
/* get capabilities */
self->thread_id= mysql_thread_id(self->mysql);
MrdbConnection_GetCapabilities(self);
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -999,27 +943,6 @@ static PyObject *MrdbConnection_get_server_status(MrdbConnection *self)
return PyLong_FromLong((long)server_status);
}

static PyObject *MrdbConnection_query(MrdbConnection *self, PyObject *args)
{
char *cmd;
int rc;

if (!PyArg_ParseTuple(args, "s", &cmd))
return NULL;


Py_BEGIN_ALLOW_THREADS;
rc= mysql_send_query(self->mysql, cmd, strlen(cmd));
Py_END_ALLOW_THREADS;

if (rc)
{
mariadb_throw_exception(self->mysql, NULL, 0, NULL);
return NULL;
}
Py_RETURN_NONE;
}

static PyObject *MrdbConnection_readresponse(MrdbConnection *self)
{
int rc;
Expand Down

0 comments on commit ea97f66

Please sign in to comment.