Skip to content

Commit 7f209b4

Browse files
committed
Implement PyMySQL#372: Support session state tracking for GTIDs
1 parent c3dcb16 commit 7f209b4

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ build/
1212
dist/
1313
MySQLdb/release.py
1414
.coverage
15+
.idea

MySQLdb/_mysql.c

+51
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,49 @@ _mysql_ConnectionObject_read_query_result(
18021802
Py_RETURN_NONE;
18031803
}
18041804

1805+
#if MYSQL_VERSION_ID >= 50707
1806+
static char _mysql_ConnectionObject_get_session_track_gtids__doc__[] =
1807+
"If the `session_track_gtids` system variable (global or session) is \n\
1808+
set to something other than 'OFF' and the client flag `SESSION_TRACK`` \n\
1809+
is enabled, you can call this method to retrieve all GTIDs created by \n\
1810+
the session. Returns a list of unicode strings.\n\
1811+
";
1812+
1813+
static PyObject *
1814+
_mysql_ConnectionObject_get_session_track_gtids(
1815+
_mysql_ConnectionObject *self,
1816+
PyObject *noargs)
1817+
{
1818+
int r;
1819+
const char *data;
1820+
size_t length;
1821+
1822+
Py_BEGIN_ALLOW_THREADS
1823+
r = mysql_session_track_get_first(&(self->connection), SESSION_TRACK_GTIDS, &data, &length);
1824+
Py_END_ALLOW_THREADS
1825+
1826+
PyObject *gtids = PyList_New(0);
1827+
1828+
while (r == 0)
1829+
{
1830+
PyObject *gtid = PyUnicode_DecodeUTF8(data, length, NULL);
1831+
if (gtid == NULL)
1832+
{
1833+
Py_DECREF(gtids);
1834+
return NULL;
1835+
}
1836+
1837+
PyList_Append(gtids, gtid);
1838+
1839+
Py_BEGIN_ALLOW_THREADS
1840+
r = mysql_session_track_get_next(&(self->connection), SESSION_TRACK_GTIDS, &data, &length);
1841+
Py_END_ALLOW_THREADS
1842+
}
1843+
1844+
return gtids;
1845+
}
1846+
#endif
1847+
18051848
static char _mysql_ConnectionObject_select_db__doc__[] =
18061849
"Causes the database specified by db to become the default\n\
18071850
(current) database on the connection specified by mysql. In subsequent\n\
@@ -2222,6 +2265,14 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
22222265
METH_NOARGS,
22232266
_mysql_ConnectionObject_read_query_result__doc__,
22242267
},
2268+
#if MYSQL_VERSION_ID >= 50707
2269+
{
2270+
"get_session_track_gtids",
2271+
(PyCFunction)_mysql_ConnectionObject_get_session_track_gtids,
2272+
METH_NOARGS,
2273+
_mysql_ConnectionObject_get_session_track_gtids__doc__,
2274+
},
2275+
#endif
22252276
{
22262277
"select_db",
22272278
(PyCFunction)_mysql_ConnectionObject_select_db,

MySQLdb/constants/CLIENT.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
INTERACTIVE = 1024
2121
SSL = 2048
2222
IGNORE_SIGPIPE = 4096
23-
TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35
23+
TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35
2424
RESERVED = 16384
2525
SECURE_CONNECTION = 32768
2626
MULTI_STATEMENTS = 65536
2727
MULTI_RESULTS = 131072
28-
29-
28+
SESSION_TRACK = 8388608 # 1 << 23

0 commit comments

Comments
 (0)