Skip to content

Commit 4e14e58

Browse files
committed
BUG#35912790: Binary strings are converted when using prepared statements
When using a prepared statement cursor in the pure Python implementation, binary strings are wrongly converted to regular strings. This patch fixes this issue by checking the binary flag of the field. Change-Id: I59e89036890ac2ae614b339eaa435cc089bb423d
1 parent 17b5c9c commit 4e14e58

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ v8.3.0
1515
- WL#15983: Stop using mysql_ssl_set api
1616
- WL#15982: Remove use of mysql_shutdown
1717
- WL#15942: Improve type hints and standardize byte type handling
18+
- BUG#35912790: Binary strings are converted when using prepared statements
1819

1920
v8.2.0
2021
======

lib/mysql/connector/protocol.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,9 @@ def _parse_binary_values(
798798
elif field[1] == FieldType.TIME:
799799
(packet, value) = self._parse_binary_time(packet)
800800
values.append(value)
801+
elif field[7] == FieldFlag.BINARY or field[8] == 63: # "binary" charset
802+
(packet, value) = utils.read_lc_string(packet)
803+
values.append(value)
801804
else:
802805
(packet, value) = utils.read_lc_string(packet)
803806
try:

tests/cext/test_cext_cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ def test_execute(self):
10161016
self.assertEqual(len(rows), 1)
10171017
self.assertEqual(rows[0][1:], self.exp)
10181018

1019+
# Test binary string
1020+
self.cur.execute("SELECT BINARY 'ham'")
1021+
exp = [(bytearray(b"ham"),)]
1022+
self.assertEqual(exp, self.cur.fetchall())
1023+
10191024
def test_executemany(self):
10201025
data = [self.data[:], self.data[:]]
10211026
self.cur.executemany(self.insert_stmt.format(self.tbl), data)

tests/test_cursor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,11 @@ def test_execute(self):
13581358
stmt = "SELECT SQRT(POW(%(value1)s, 2) + POW(%(unknown)s, 2)) AS hypotenuse"
13591359
self.assertRaises(errors.ProgrammingError, cur.execute, stmt, data)
13601360

1361+
# Test binary string
1362+
cur.execute("SELECT BINARY 'ham'")
1363+
exp = [(bytearray(b"ham"),)]
1364+
self.assertEqual(exp, cur.fetchall())
1365+
13611366
def test_executemany(self):
13621367
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
13631368

tests/test_protocol.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,15 @@ def test__parse_binary_values(self):
465465
#
466466

467467
fields = [
468-
("aStr", 253, None, None, None, None, 0, 1),
469-
("aFloat", 246, None, None, None, None, 0, 129),
470-
("aDouble", 246, None, None, None, None, 0, 129),
471-
("aDate", 10, None, None, None, None, 1, 128),
472-
("aDateTime", 12, None, None, None, None, 1, 128),
473-
("aTime", 11, None, None, None, None, 1, 128),
474-
("aNull", 6, None, None, None, None, 1, 128),
475-
("aBlob", 252, None, None, None, None, 1, 144),
476-
("aVarBinary", 253, None, None, None, None, 1, 128),
468+
("aStr", 253, None, None, None, None, 0, 1, 45),
469+
("aFloat", 246, None, None, None, None, 0, 129, 63),
470+
("aDouble", 246, None, None, None, None, 0, 129, 63),
471+
("aDate", 10, None, None, None, None, 1, 128, 63),
472+
("aDateTime", 12, None, None, None, None, 1, 128, 63),
473+
("aTime", 11, None, None, None, None, 1, 128, 63),
474+
("aNull", 6, None, None, None, None, 1, 128, 63),
475+
("aBlob", 252, None, None, None, None, 1, 144, 63),
476+
("aVarBinary", 253, None, None, None, None, 1, 128, 63),
477477
]
478478

479479
packet = bytearray(

0 commit comments

Comments
 (0)