Skip to content

Commit fed53c9

Browse files
committed
Implement PyMySQL#372: Support session state tracking for GTIDs
1 parent 23addef commit fed53c9

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
@@ -1810,6 +1810,49 @@ _mysql_ConnectionObject_read_query_result(
18101810
Py_RETURN_NONE;
18111811
}
18121812

1813+
#if MYSQL_VERSION_ID >= 50707
1814+
static char _mysql_ConnectionObject_get_session_track_gtids__doc__[] =
1815+
"If the `session_track_gtids` system variable (global or session) is \n\
1816+
set to something other than 'OFF' and the client flag `SESSION_TRACK`` \n\
1817+
is enabled, you can call this method to retrieve all GTIDs created by \n\
1818+
the session. Returns a list of unicode strings.\n\
1819+
";
1820+
1821+
static PyObject *
1822+
_mysql_ConnectionObject_get_session_track_gtids(
1823+
_mysql_ConnectionObject *self,
1824+
PyObject *noargs)
1825+
{
1826+
int r;
1827+
const char *data;
1828+
size_t length;
1829+
1830+
Py_BEGIN_ALLOW_THREADS
1831+
r = mysql_session_track_get_first(&(self->connection), SESSION_TRACK_GTIDS, &data, &length);
1832+
Py_END_ALLOW_THREADS
1833+
1834+
PyObject *gtids = PyList_New(0);
1835+
1836+
while (r == 0)
1837+
{
1838+
PyObject *gtid = PyUnicode_DecodeUTF8(data, length, NULL);
1839+
if (gtid == NULL)
1840+
{
1841+
Py_DECREF(gtids);
1842+
return NULL;
1843+
}
1844+
1845+
PyList_Append(gtids, gtid);
1846+
1847+
Py_BEGIN_ALLOW_THREADS
1848+
r = mysql_session_track_get_next(&(self->connection), SESSION_TRACK_GTIDS, &data, &length);
1849+
Py_END_ALLOW_THREADS
1850+
}
1851+
1852+
return gtids;
1853+
}
1854+
#endif
1855+
18131856
static char _mysql_ConnectionObject_select_db__doc__[] =
18141857
"Causes the database specified by db to become the default\n\
18151858
(current) database on the connection specified by mysql. In subsequent\n\
@@ -2230,6 +2273,14 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
22302273
METH_NOARGS,
22312274
_mysql_ConnectionObject_read_query_result__doc__,
22322275
},
2276+
#if MYSQL_VERSION_ID >= 50707
2277+
{
2278+
"get_session_track_gtids",
2279+
(PyCFunction)_mysql_ConnectionObject_get_session_track_gtids,
2280+
METH_NOARGS,
2281+
_mysql_ConnectionObject_get_session_track_gtids__doc__,
2282+
},
2283+
#endif
22332284
{
22342285
"select_db",
22352286
(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)