Skip to content

Commit 4b39e93

Browse files
authored
Use mysql_real_escape_string_quote() if exists (#109)
2 parents 19ad211 + a83baad commit 4b39e93

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

_mysql.c

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ static PyObject *_mysql_server_init(
251251
&cmd_args, &groups))
252252
return NULL;
253253

254-
#if MYSQL_VERSION_ID >= 40000
255254
if (cmd_args) {
256255
if (!PySequence_Check(cmd_args)) {
257256
PyErr_SetString(PyExc_TypeError,
@@ -318,7 +317,6 @@ static PyObject *_mysql_server_init(
318317
_mysql_Exception(NULL);
319318
goto finish;
320319
}
321-
#endif
322320
ret = Py_None;
323321
Py_INCREF(Py_None);
324322
_mysql_server_init_done = 1;
@@ -336,9 +334,7 @@ static PyObject *_mysql_server_end(
336334
PyObject *self,
337335
PyObject *args) {
338336
if (_mysql_server_init_done) {
339-
#if MYSQL_VERSION_ID >= 40000
340337
mysql_server_end();
341-
#endif
342338
_mysql_server_init_done = 0;
343339
Py_INCREF(Py_None);
344340
return Py_None;
@@ -1063,18 +1059,19 @@ _mysql_escape_string(
10631059
str = PyBytes_FromStringAndSize((char *) NULL, size*2+1);
10641060
if (!str) return PyErr_NoMemory();
10651061
out = PyBytes_AS_STRING(str);
1066-
#if MYSQL_VERSION_ID < 32321
1067-
len = mysql_escape_string(out, in, size);
1068-
#else
10691062
check_server_init(NULL);
10701063

10711064
if (self && PyModule_Check((PyObject*)self))
10721065
self = NULL;
1073-
if (self && self->open)
1066+
if (self && self->open) {
1067+
#if MYSQL_VERSION_ID >= 50706
1068+
len = mysql_real_escape_string_quote(&(self->connection), out, in, size, '\'');
1069+
#else
10741070
len = mysql_real_escape_string(&(self->connection), out, in, size);
1075-
else
1076-
len = mysql_escape_string(out, in, size);
10771071
#endif
1072+
} else {
1073+
len = mysql_escape_string(out, in, size);
1074+
}
10781075
if (_PyBytes_Resize(&str, len) < 0) return NULL;
10791076
return (str);
10801077
}
@@ -1123,15 +1120,16 @@ _mysql_string_literal(
11231120
return PyErr_NoMemory();
11241121
}
11251122
out = PyBytes_AS_STRING(str);
1126-
#if MYSQL_VERSION_ID < 32321
1127-
len = mysql_escape_string(out+1, in, size);
1128-
#else
11291123
check_server_init(NULL);
1130-
if (self && self->open)
1124+
if (self && self->open) {
1125+
#if MYSQL_VERSION_ID >= 50706
1126+
len = mysql_real_escape_string_quote(&(self->connection), out+1, in, size, '\'');
1127+
#else
11311128
len = mysql_real_escape_string(&(self->connection), out+1, in, size);
1132-
else
1133-
len = mysql_escape_string(out+1, in, size);
11341129
#endif
1130+
} else {
1131+
len = mysql_escape_string(out+1, in, size);
1132+
}
11351133
*out = *(out+len+1) = '\'';
11361134
if (_PyBytes_Resize(&str, len+2) < 0) return NULL;
11371135
Py_DECREF(s);
@@ -1593,8 +1591,6 @@ _mysql_ResultObject_fetch_row(
15931591
return NULL;
15941592
}
15951593

1596-
#if MYSQL_VERSION_ID >= 32303
1597-
15981594
static char _mysql_ConnectionObject_change_user__doc__[] =
15991595
"Changes the user and causes the database specified by db to\n\
16001596
become the default (current) database on the connection\n\
@@ -1633,7 +1629,6 @@ _mysql_ConnectionObject_change_user(
16331629
Py_INCREF(Py_None);
16341630
return Py_None;
16351631
}
1636-
#endif
16371632

16381633
static char _mysql_ConnectionObject_character_set_name__doc__[] =
16391634
"Returns the default character set for the current connection.\n\
@@ -1651,7 +1646,6 @@ _mysql_ConnectionObject_character_set_name(
16511646
return PyString_FromString(s);
16521647
}
16531648

1654-
#if MYSQL_VERSION_ID >= 50007
16551649
static char _mysql_ConnectionObject_set_character_set__doc__[] =
16561650
"Sets the default character set for the current connection.\n\
16571651
Non-standard.\n\
@@ -1673,7 +1667,6 @@ _mysql_ConnectionObject_set_character_set(
16731667
Py_INCREF(Py_None);
16741668
return Py_None;
16751669
}
1676-
#endif
16771670

16781671
#if MYSQL_VERSION_ID >= 50010
16791672
static char _mysql_ConnectionObject_get_character_set_info__doc__[] =
@@ -2039,11 +2032,7 @@ _mysql_ConnectionObject_shutdown(
20392032
int r;
20402033
check_connection(self);
20412034
Py_BEGIN_ALLOW_THREADS
2042-
r = mysql_shutdown(&(self->connection)
2043-
#if MYSQL_VERSION_ID >= 40103
2044-
, SHUTDOWN_DEFAULT
2045-
#endif
2046-
);
2035+
r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT);
20472036
Py_END_ALLOW_THREADS
20482037
if (r) return _mysql_Exception(self);
20492038
Py_INCREF(Py_None);
@@ -2333,14 +2322,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
23332322
METH_NOARGS,
23342323
_mysql_ConnectionObject_character_set_name__doc__
23352324
},
2336-
#if MYSQL_VERSION_ID >= 50007
23372325
{
23382326
"set_character_set",
23392327
(PyCFunction)_mysql_ConnectionObject_set_character_set,
23402328
METH_VARARGS,
23412329
_mysql_ConnectionObject_set_character_set__doc__
23422330
},
2343-
#endif
23442331
#if MYSQL_VERSION_ID >= 50010
23452332
{
23462333
"get_character_set_info",

0 commit comments

Comments
 (0)