Skip to content

Commit cd5b70b

Browse files
committed
BUG#22906307: MySQLConverter.escape() does not work for dates
The MySQLConverter.escape() raises an error when the value types are datetime objects. With this patch, MySQLConverter.escape() will not try to escape values that are not bytes or str types. Change-Id: Iba28a74ab81a5289c329cee285c57d35e571bcb5
1 parent 4851913 commit cd5b70b

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ v8.0.32
3030
- BUG#24364556: Improve warning behavior
3131
- BUG#23342572: Allow dictionaries as parameters in prepared statements
3232
- BUG#23339387: Add MySQLCursorPreparedDict option
33+
- BUG#22906307: MySQLConverter.escape() does not work for dates
3334

3435
v8.0.31
3536
=======

lib/mysql/connector/conversion.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from .custom_types import HexLiteral
4242
from .types import (
4343
DescriptionType,
44-
EscapeSupportedTypes,
4544
RowType,
4645
StrOrBytes,
4746
ToMysqlInputTypes,
@@ -170,27 +169,23 @@ def __init__(
170169
] = {}
171170

172171
@staticmethod
173-
def escape(value: EscapeSupportedTypes) -> EscapeSupportedTypes:
172+
def escape(value: Any) -> Any:
174173
"""
175174
Escapes special characters as they are expected to by when MySQL
176175
receives them.
177176
As found in MySQL source mysys/charset.c
178177
179178
Returns the value if not a string, or the escaped string.
180179
"""
181-
if value is None:
182-
return value
183-
if isinstance(value, NUMERIC_TYPES):
184-
return value
185180
if isinstance(value, (bytes, bytearray)):
186181
value = value.replace(b"\\", b"\\\\")
187182
value = value.replace(b"\n", b"\\n")
188183
value = value.replace(b"\r", b"\\r")
189184
value = value.replace(b"\047", b"\134\047") # single quotes
190185
value = value.replace(b"\042", b"\134\042") # double quotes
191186
value = value.replace(b"\032", b"\134\032") # for Win32
192-
else:
193-
value = value.replace("\\", "\\\\") # type: ignore[union-attr]
187+
elif isinstance(value, str) and not isinstance(value, HexLiteral):
188+
value = value.replace("\\", "\\\\")
194189
value = value.replace("\n", "\\n")
195190
value = value.replace("\r", "\\r")
196191
value = value.replace("\047", "\134\047") # single quotes

lib/mysql/connector/django/schema.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,13 @@
3535
DatabaseSchemaEditor as MySQLDatabaseSchemaEditor,
3636
)
3737

38-
from mysql.connector.types import EscapeSupportedTypes
39-
4038

4139
class DatabaseSchemaEditor(MySQLDatabaseSchemaEditor):
4240
"""This class is responsible for emitting schema-changing statements to the
4341
databases.
4442
"""
4543

46-
def quote_value(
47-
self,
48-
value: EscapeSupportedTypes,
49-
) -> EscapeSupportedTypes:
44+
def quote_value(self, value: Any) -> Any:
5045
"""Quote value."""
5146
self.connection.ensure_connection()
5247
if isinstance(value, str):

lib/mysql/connector/types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
]
5656
]
5757
ToMysqlOutputTypes = Optional[Union[int, float, bytes, "HexLiteral"]]
58-
EscapeSupportedTypes = Optional[Union[int, float, Decimal, "HexLiteral", StrOrBytes]]
5958

6059

6160
""" Network """

tests/test_conversion.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ def test_escape(self):
189189
int(1281288), # should stay the same
190190
float(3.14), # should stay the same
191191
Decimal("3.14"), # should stay a Decimal
192+
datetime.date(2022, 11, 7),
193+
datetime.timedelta(hours=35, minutes=34, seconds=10),
194+
datetime.datetime(2022, 11, 4, 9, 11, 0),
195+
time.strptime("30 Nov 2022", "%d %b %Y"),
192196
r"back\slash",
193197
"newline\n",
194198
"return\r",
@@ -202,6 +206,10 @@ def test_escape(self):
202206
1281288,
203207
float(3.14),
204208
Decimal("3.14"),
209+
datetime.date(2022, 11, 7),
210+
datetime.timedelta(hours=35, minutes=34, seconds=10),
211+
datetime.datetime(2022, 11, 4, 9, 11, 0),
212+
time.strptime("30 Nov 2022", "%d %b %Y"),
205213
"back\\\\slash",
206214
"newline\\n",
207215
"return\\r",

0 commit comments

Comments
 (0)