Skip to content

Commit dbb4345

Browse files
committed
BUG#34228442: Fix NO_BACKSLASH_ESCAPES SQL mode support in c-ext
In MySQL 5.7.6, the C API function mysql_real_escape_string_quote(), has been implemented as a replacement for mysql_real_escape_string() because the latter function can fail to properly encode characters when the NO_BACKSLASH_ESCAPES SQL mode is enabled. This patch fixes the NO_BACKSLASH_ESCAPES SQL mode when using the c-ext by using mysql_real_escape_string_quote() function for servers >= 5.7.6. Change-Id: Ief95be49f33b80d7e9d27fafa4b727e33c2e4045
1 parent ba32254 commit dbb4345

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ v8.0.30
1717
- WL#15035: Enforce PEP 7 and PEP 8 coding style
1818
- WL#14822: Refactor the authentication plugin mechanism
1919
- WL#14815: Support OpenSSL 3.0
20+
- BUG#34228442: Fix NO_BACKSLASH_ESCAPES SQL mode support in c-ext
2021
- BUG#34127959: Add isolation level support in Django backend
2122
- BUG#33923516: Allow tuple of dictionaries as "failover" argument
2223
- BUG#28821983: Fix rounding errors for decimal values

src/mysql_capi.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,13 @@ MySQL_escape_string(MySQL *self, PyObject *value)
14551455
to = PyBytes_FromStringAndSize(NULL, from_size * 2 + 1);
14561456
to_str = PyBytes_AsString(to);
14571457

1458+
#if MYSQL_VERSION_ID >= 50706
1459+
escaped_size = (Py_ssize_t)mysql_real_escape_string_quote(&self->session, to_str, from_str,
1460+
(unsigned long)from_size, '\'');
1461+
#else
14581462
escaped_size = (Py_ssize_t)mysql_real_escape_string(&self->session, to_str, from_str,
14591463
(unsigned long)from_size);
1464+
#endif
14601465

14611466
_PyBytes_Resize(&to, escaped_size);
14621467
Py_XDECREF(from);

tests/test_bugs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6915,3 +6915,28 @@ def test_decimal_update(self):
69156915
self.assertEqual(res, Decimal("100000000000.00000203"))
69166916

69176917
cur.execute(f"DROP TABLE IF EXISTS {table}")
6918+
6919+
6920+
class BugOra34228442(tests.MySQLConnectorTests):
6921+
"""BUG#34228442: Fix NO_BACKSLASH_ESCAPES SQL mode support in c-ext."""
6922+
6923+
@foreach_cnx()
6924+
def test_no_backslash_escapes(self):
6925+
table = "BugOra34228442"
6926+
self.cnx.sql_mode = [constants.SQLMode.NO_BACKSLASH_ESCAPES]
6927+
with self.cnx.cursor() as cur:
6928+
cur.execute(f"DROP TABLE IF EXISTS {table}")
6929+
cur.execute(
6930+
f"""
6931+
CREATE TABLE {table} (
6932+
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
6933+
`text` VARCHAR(255)
6934+
)
6935+
"""
6936+
)
6937+
cur.execute(f"INSERT INTO {table} (`text`) VALUES ('test')")
6938+
cur.execute(f"SELECT text FROM {table} WHERE text = %s", ["test"])
6939+
res = cur.fetchall()
6940+
self.assertEqual(res[0][0], "test")
6941+
self.assertEqual(self.cnx.sql_mode, "NO_BACKSLASH_ESCAPES")
6942+
cur.execute(f"DROP TABLE IF EXISTS {table}")

0 commit comments

Comments
 (0)