@@ -25,13 +25,14 @@ USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
2525OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2626PERFORMANCE OF THIS SOFTWARE.
2727*/
28-
28+ #include <stdbool.h>
2929#include "mysql.h"
3030#include "mysqld_error.h"
3131
3232#if MYSQL_VERSION_ID >= 80000
3333// https://github.com/mysql/mysql-server/commit/eb821c023cedc029ca0b06456dfae365106bee84
34- #define my_bool _Bool
34+ // my_bool was typedef of char before MySQL 8.0.0.
35+ #define my_bool bool
3536#endif
3637
3738#if ((MYSQL_VERSION_ID >= 50555 && MYSQL_VERSION_ID <= 50599 ) || \
@@ -71,7 +72,8 @@ static PyObject *_mysql_NotSupportedError;
7172typedef struct {
7273 PyObject_HEAD
7374 MYSQL connection ;
74- int open ;
75+ bool open ;
76+ bool reconnect ;
7577 PyObject * converter ;
7678} _mysql_ConnectionObject ;
7779
@@ -443,7 +445,8 @@ _mysql_ConnectionObject_Initialize(
443445 * auth_plugin = NULL ;
444446
445447 self -> converter = NULL ;
446- self -> open = 0 ;
448+ self -> open = false;
449+ self -> reconnect = false;
447450
448451 if (!PyArg_ParseTupleAndKeywords (args , kwargs ,
449452 "|ssssisOiiisssiOsiiiss:connect" ,
@@ -487,7 +490,7 @@ _mysql_ConnectionObject_Initialize(
487490 PyErr_SetNone (PyExc_MemoryError );
488491 return -1 ;
489492 }
490- self -> open = 1 ;
493+ self -> open = true ;
491494
492495 if (connect_timeout ) {
493496 unsigned int timeout = connect_timeout ;
@@ -686,7 +689,7 @@ _mysql_ConnectionObject_close(
686689 Py_BEGIN_ALLOW_THREADS
687690 mysql_close (& (self -> connection ));
688691 Py_END_ALLOW_THREADS
689- self -> open = 0 ;
692+ self -> open = false ;
690693 _mysql_ConnectionObject_clear (self );
691694 Py_RETURN_NONE ;
692695}
@@ -1852,18 +1855,18 @@ _mysql_ResultObject_num_rows(
18521855}
18531856
18541857static char _mysql_ConnectionObject_ping__doc__ [] =
1855- "Checks whether or not the connection to the server is\n\
1856- working. If it has gone down, an automatic reconnection is\n\
1857- attempted.\n\
1858+ "Checks whether or not the connection to the server is working.\n\
18581859\n\
18591860This function can be used by clients that remain idle for a\n\
18601861long while, to check whether or not the server has closed the\n\
1861- connection and reconnect if necessary .\n\
1862+ connection.\n\
18621863\n\
18631864New in 1.2.2: Accepts an optional reconnect parameter. If True,\n\
18641865then the client will attempt reconnection. Note that this setting\n\
18651866is persistent. By default, this is on in MySQL<5.0.3, and off\n\
18661867thereafter.\n\
1868+ MySQL 8.0.33 deprecated the MYSQL_OPT_RECONNECT option so reconnect\n\
1869+ parameter is also deprecated in mysqlclient 2.2.1.\n\
18671870\n\
18681871Non-standard. You should assume that ping() performs an\n\
18691872implicit rollback; use only when starting a new transaction.\n\
@@ -1875,17 +1878,24 @@ _mysql_ConnectionObject_ping(
18751878 _mysql_ConnectionObject * self ,
18761879 PyObject * args )
18771880{
1878- int r , reconnect = -1 ;
1879- if (!PyArg_ParseTuple (args , "|I " , & reconnect )) return NULL ;
1881+ int reconnect = 0 ;
1882+ if (!PyArg_ParseTuple (args , "|p " , & reconnect )) return NULL ;
18801883 check_connection (self );
1881- if (reconnect != -1 ) {
1884+ if (reconnect != (self -> reconnect == true)) {
1885+ // libmysqlclient show warning to stderr when MYSQL_OPT_RECONNECT is used.
1886+ // so we avoid using it as possible for now.
1887+ // TODO: Warn when reconnect is true.
1888+ // MySQL 8.0.33 show warning to stderr already.
1889+ // We will emit Pytohn warning in future.
18821890 my_bool recon = (my_bool )reconnect ;
18831891 mysql_options (& self -> connection , MYSQL_OPT_RECONNECT , & recon );
1892+ self -> reconnect = (bool )reconnect ;
18841893 }
1894+ int r ;
18851895 Py_BEGIN_ALLOW_THREADS
18861896 r = mysql_ping (& (self -> connection ));
18871897 Py_END_ALLOW_THREADS
1888- if (r ) return _mysql_Exception (self );
1898+ if (r ) return _mysql_Exception (self );
18891899 Py_RETURN_NONE ;
18901900}
18911901
@@ -2165,7 +2175,7 @@ _mysql_ConnectionObject_dealloc(
21652175 PyObject_GC_UnTrack (self );
21662176 if (self -> open ) {
21672177 mysql_close (& (self -> connection ));
2168- self -> open = 0 ;
2178+ self -> open = false ;
21692179 }
21702180 Py_CLEAR (self -> converter );
21712181 MyFree (self );
0 commit comments