Skip to content

Commit cffa5ab

Browse files
committed
BUG#37820231: Text based django ORM filters doesn't work with Connector/Python
This patch fixes the issue where text based django ORM filters are not working properly when Connector/Python is getting used as the connector backend by updating the underlying deprecated MySQL commands being used to execute case-sensitive search queries before returning the result queryset. Change-Id: Ida85baa28664aae62a2c216f04c8efb20373b862
1 parent f72a132 commit cffa5ab

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Full release notes:
1111
v9.4.0
1212
======
1313

14+
- BUG#37820231: Text based django ORM filters doesn't work with Connector/Python
1415
- BUG#37806057: Rename extra option (when installing wheel package) to install webauthn functionality dependencies
1516
- BUG#36452514: Missing version info resources
1617
- BUG#34950958: MySQL Python Connector doesn't work with ssh in the same process

mysql-connector-python/lib/mysql/connector/django/base.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,14 @@ class DatabaseWrapper(BaseDatabaseWrapper): # pylint: disable=abstract-method
278278
operators = {
279279
"exact": "= %s",
280280
"iexact": "LIKE %s",
281-
"contains": "LIKE BINARY %s",
281+
"contains": "LIKE CAST(%s AS BINARY)",
282282
"icontains": "LIKE %s",
283-
"regex": "REGEXP BINARY %s",
284-
"iregex": "REGEXP %s",
285283
"gt": "> %s",
286284
"gte": ">= %s",
287285
"lt": "< %s",
288286
"lte": "<= %s",
289-
"startswith": "LIKE BINARY %s",
290-
"endswith": "LIKE BINARY %s",
287+
"startswith": "LIKE CAST(%s AS BINARY)",
288+
"endswith": "LIKE CAST(%s AS BINARY)",
291289
"istartswith": "LIKE %s",
292290
"iendswith": "LIKE %s",
293291
}
@@ -302,11 +300,11 @@ class DatabaseWrapper(BaseDatabaseWrapper): # pylint: disable=abstract-method
302300
# the LIKE operator.
303301
pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\\', '\\\\'), '%%', '\%%'), '_', '\_')"
304302
pattern_ops = {
305-
"contains": "LIKE BINARY CONCAT('%%', {}, '%%')",
303+
"contains": "LIKE CAST(CONCAT('%%', {}, '%%') AS BINARY)",
306304
"icontains": "LIKE CONCAT('%%', {}, '%%')",
307-
"startswith": "LIKE BINARY CONCAT({}, '%%')",
305+
"startswith": "LIKE CAST(CONCAT({}, '%%') AS BINARY)",
308306
"istartswith": "LIKE CONCAT({}, '%%')",
309-
"endswith": "LIKE BINARY CONCAT('%%', {})",
307+
"endswith": "LIKE CAST(CONCAT('%%', {}) AS BINARY)",
310308
"iendswith": "LIKE CONCAT('%%', {})",
311309
}
312310

@@ -355,9 +353,6 @@ def __getattr__(self, attr: str) -> bool:
355353

356354
def get_connection_params(self) -> Dict[str, Any]:
357355
kwargs = {
358-
"charset": "utf8",
359-
"use_unicode": True,
360-
"buffered": False,
361356
"consume_results": True,
362357
}
363358

@@ -582,13 +577,17 @@ def mysql_server_data(self) -> Dict[str, Any]:
582577
@cached_property
583578
def mysql_server_info(self) -> Any:
584579
"""Return MySQL version."""
580+
if self.connection:
581+
return self.connection.server_info
585582
with self.temporary_connection() as cursor:
586583
cursor.execute("SELECT VERSION()")
587584
return cursor.fetchone()[0]
588585

589586
@cached_property
590587
def mysql_version(self) -> Tuple[int, ...]:
591588
"""Return MySQL version."""
589+
if self.connection:
590+
return self.connection.server_version
592591
config = self.get_connection_params()
593592
with mysql.connector.connect(**config) as conn:
594593
server_version: Tuple[int, ...] = conn.server_version
@@ -597,6 +596,8 @@ def mysql_version(self) -> Tuple[int, ...]:
597596
@cached_property
598597
def sql_mode(self) -> Set[str]:
599598
"""Return SQL mode."""
599+
if self.connection:
600+
return set(self.connection.sql_mode.split(","))
600601
with self.cursor() as cursor:
601602
cursor.execute("SELECT @@sql_mode")
602603
sql_mode = cursor.fetchone()

0 commit comments

Comments
 (0)