Skip to content

Commit

Permalink
Implement PyMySQL#372: Support session state tracking for GTIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
beamerblvd committed Jul 27, 2019
1 parent 23addef commit fed53c9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ build/
dist/
MySQLdb/release.py
.coverage
.idea
51 changes: 51 additions & 0 deletions MySQLdb/_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,49 @@ _mysql_ConnectionObject_read_query_result(
Py_RETURN_NONE;
}

#if MYSQL_VERSION_ID >= 50707
static char _mysql_ConnectionObject_get_session_track_gtids__doc__[] =
"If the `session_track_gtids` system variable (global or session) is \n\
set to something other than 'OFF' and the client flag `SESSION_TRACK`` \n\
is enabled, you can call this method to retrieve all GTIDs created by \n\
the session. Returns a list of unicode strings.\n\
";

static PyObject *
_mysql_ConnectionObject_get_session_track_gtids(
_mysql_ConnectionObject *self,
PyObject *noargs)
{
int r;
const char *data;
size_t length;

Py_BEGIN_ALLOW_THREADS
r = mysql_session_track_get_first(&(self->connection), SESSION_TRACK_GTIDS, &data, &length);
Py_END_ALLOW_THREADS

PyObject *gtids = PyList_New(0);

while (r == 0)
{
PyObject *gtid = PyUnicode_DecodeUTF8(data, length, NULL);
if (gtid == NULL)
{
Py_DECREF(gtids);
return NULL;
}

PyList_Append(gtids, gtid);

Py_BEGIN_ALLOW_THREADS
r = mysql_session_track_get_next(&(self->connection), SESSION_TRACK_GTIDS, &data, &length);
Py_END_ALLOW_THREADS
}

return gtids;
}
#endif

static char _mysql_ConnectionObject_select_db__doc__[] =
"Causes the database specified by db to become the default\n\
(current) database on the connection specified by mysql. In subsequent\n\
Expand Down Expand Up @@ -2230,6 +2273,14 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
METH_NOARGS,
_mysql_ConnectionObject_read_query_result__doc__,
},
#if MYSQL_VERSION_ID >= 50707
{
"get_session_track_gtids",
(PyCFunction)_mysql_ConnectionObject_get_session_track_gtids,
METH_NOARGS,
_mysql_ConnectionObject_get_session_track_gtids__doc__,
},
#endif
{
"select_db",
(PyCFunction)_mysql_ConnectionObject_select_db,
Expand Down
5 changes: 2 additions & 3 deletions MySQLdb/constants/CLIENT.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
INTERACTIVE = 1024
SSL = 2048
IGNORE_SIGPIPE = 4096
TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35
TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35
RESERVED = 16384
SECURE_CONNECTION = 32768
MULTI_STATEMENTS = 65536
MULTI_RESULTS = 131072


SESSION_TRACK = 8388608 # 1 << 23

0 comments on commit fed53c9

Please sign in to comment.