From 7f209b45bc0eab6a898f3098accbbb63d0df5242 Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Fri, 26 Jul 2019 21:50:33 -0500 Subject: [PATCH] Implement #372: Support session state tracking for GTIDs --- .gitignore | 1 + MySQLdb/_mysql.c | 51 +++++++++++++++++++++++++++++++++++++ MySQLdb/constants/CLIENT.py | 5 ++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 42bbfb5d..a5797f8e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ build/ dist/ MySQLdb/release.py .coverage +.idea diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 45df37c3..93a3fc89 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1802,6 +1802,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\ @@ -2222,6 +2265,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, diff --git a/MySQLdb/constants/CLIENT.py b/MySQLdb/constants/CLIENT.py index 6559917b..a2841e58 100644 --- a/MySQLdb/constants/CLIENT.py +++ b/MySQLdb/constants/CLIENT.py @@ -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