Skip to content

Commit 531dca6

Browse files
authored
Simplify params for SQL params (#1174)
Per https://docs.djangoproject.com/en/6.0/releases/6.0/\#custom-orm-expressions-should-return-params-as-a-tuple, ensure we always use a tuple for params, and the simpler unpacking syntax for combining them.
1 parent 7862ae2 commit 531dca6

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

src/django_mysql/models/expressions.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def as_sql(
7272
field, field_params = compiler.compile(self.lhs)
7373
value, value_params = compiler.compile(self.rhs)
7474

75-
sql = self.sql_expression % (field, value)
76-
params = tuple(value_params) + tuple(field_params)
77-
78-
return sql, params
75+
return (
76+
self.sql_expression % (field, value),
77+
(*value_params, *field_params),
78+
)
7979

8080

8181
class AppendLeftListF(TwoSidedExpression):
@@ -148,8 +148,7 @@ def as_sql(
148148
) -> tuple[str, tuple[Any, ...]]:
149149
field, field_params = compiler.compile(self.lhs)
150150

151-
sql = self.sql_expression % (field)
152-
return sql, tuple(field_params)
151+
return (self.sql_expression % (field), field_params)
153152

154153

155154
class PopLeftListF(BaseExpression):
@@ -180,8 +179,7 @@ def as_sql(
180179
) -> tuple[str, tuple[Any, ...]]:
181180
field, field_params = compiler.compile(self.lhs)
182181

183-
sql = self.sql_expression % (field)
184-
return sql, tuple(field_params)
182+
return (self.sql_expression % (field), field_params)
185183

186184

187185
class SetF:
@@ -227,10 +225,10 @@ def as_sql(
227225
field, field_params = compiler.compile(self.lhs)
228226
value, value_params = compiler.compile(self.rhs)
229227

230-
sql = self.sql_expression % (value, field)
231-
params = tuple(value_params) + tuple(field_params)
232-
233-
return sql, params
228+
return (
229+
self.sql_expression % (value, field),
230+
(*value_params, *field_params),
231+
)
234232

235233

236234
class RemoveSetF(TwoSidedExpression):
@@ -280,7 +278,7 @@ def as_sql(
280278
field, field_params = compiler.compile(self.lhs)
281279
value, value_params = compiler.compile(self.rhs)
282280

283-
sql = self.sql_expression % (value, field)
284-
params = tuple(value_params) + tuple(field_params)
285-
286-
return sql, params
281+
return (
282+
self.sql_expression % (value, field),
283+
(*value_params, *field_params),
284+
)

src/django_mysql/models/fields/dynamic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def as_sql(
342342
lhs, params = compiler.compile(self.lhs)
343343
return (
344344
f"COLUMN_GET({lhs}, %s AS {self.data_type})",
345-
tuple(params) + (self.key_name,),
345+
(*params, self.key_name),
346346
)
347347

348348

src/django_mysql/models/fields/lists.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from collections.abc import Callable, Iterable
3+
from collections.abc import Callable
44
from typing import Any, cast
55

66
from django.core import checks
@@ -216,12 +216,14 @@ def __init__(self, index: int, *args: Any, **kwargs: Any) -> None:
216216

217217
def as_sql(
218218
self, qn: Callable[[str], str], connection: BaseDatabaseWrapper
219-
) -> tuple[str, Iterable[Any]]:
219+
) -> tuple[str, tuple[Any, ...]]:
220220
lhs, lhs_params = self.process_lhs(qn, connection)
221221
rhs, rhs_params = self.process_rhs(qn, connection)
222-
params = tuple(lhs_params) + tuple(rhs_params)
223222
# Put rhs on the left since that's the order FIND_IN_SET uses
224-
return f"(FIND_IN_SET({rhs}, {lhs}) = {self.index})", params
223+
return (
224+
f"(FIND_IN_SET({rhs}, {lhs}) = {self.index})",
225+
(*lhs_params, *rhs_params),
226+
)
225227

226228

227229
class IndexLookupFactory:

src/django_mysql/models/lookups.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from collections.abc import Callable, Iterable
3+
from collections.abc import Callable
44
from typing import Any
55

66
from django.db.backends.base.base import BaseDatabaseWrapper
@@ -23,11 +23,13 @@ def as_sql(
2323
self,
2424
qn: Callable[[str], str],
2525
connection: BaseDatabaseWrapper,
26-
) -> tuple[str, Iterable[Any]]:
26+
) -> tuple[str, tuple[Any, ...]]:
2727
lhs, lhs_params = self.process_lhs(qn, connection)
2828
rhs, rhs_params = self.process_rhs(qn, connection)
29-
params = tuple(lhs_params) + tuple(rhs_params)
30-
return f"{lhs} SOUNDS LIKE {rhs}", params
29+
return (
30+
f"{lhs} SOUNDS LIKE {rhs}",
31+
(*lhs_params, *rhs_params),
32+
)
3133

3234

3335
class Soundex(Transform):
@@ -36,7 +38,7 @@ class Soundex(Transform):
3638

3739
def as_sql(
3840
self, compiler: SQLCompiler, connection: BaseDatabaseWrapper
39-
) -> tuple[str, Iterable[Any]]:
41+
) -> tuple[str, tuple[Any, ...]]:
4042
lhs, params = compiler.compile(self.lhs)
4143
return f"SOUNDEX({lhs})", params
4244

@@ -62,12 +64,14 @@ def get_prep_lookup(self) -> Any:
6264

6365
def as_sql(
6466
self, qn: Callable[[str], str], connection: BaseDatabaseWrapper
65-
) -> tuple[str, Iterable[Any]]:
67+
) -> tuple[str, tuple[Any, ...]]:
6668
lhs, lhs_params = self.process_lhs(qn, connection)
6769
rhs, rhs_params = self.process_rhs(qn, connection)
6870
# Put rhs (and params) on the left since that's the order FIND_IN_SET uses
69-
params = tuple(rhs_params) + tuple(lhs_params)
70-
return f"FIND_IN_SET({rhs}, {lhs})", params
71+
return (
72+
f"FIND_IN_SET({rhs}, {lhs})",
73+
(*rhs_params, *lhs_params),
74+
)
7175

7276

7377
class SetIContains(SetContains):
@@ -82,8 +86,10 @@ class DynColHasKey(Lookup):
8286

8387
def as_sql(
8488
self, qn: Callable[[str], str], connection: BaseDatabaseWrapper
85-
) -> tuple[str, Iterable[Any]]:
89+
) -> tuple[str, tuple[Any, ...]]:
8690
lhs, lhs_params = self.process_lhs(qn, connection)
8791
rhs, rhs_params = self.process_rhs(qn, connection)
88-
params = tuple(lhs_params) + tuple(rhs_params)
89-
return f"COLUMN_EXISTS({lhs}, {rhs})", params
92+
return (
93+
f"COLUMN_EXISTS({lhs}, {rhs})",
94+
(*lhs_params, *rhs_params),
95+
)

src/django_mysql/models/transforms.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from collections.abc import Iterable
43
from typing import Any
54

65
from django.db.backends.base.base import BaseDatabaseWrapper
@@ -25,6 +24,6 @@ class SetLength(Transform):
2524

2625
def as_sql(
2726
self, compiler: SQLCompiler, connection: BaseDatabaseWrapper
28-
) -> tuple[str, Iterable[Any]]:
27+
) -> tuple[str, tuple[Any, ...]]:
2928
lhs, params = compiler.compile(self.lhs)
3029
return self.expr % (lhs, lhs, lhs), params

tests/testapp/test_dynamicfield.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class DumbTransform(Transform):
111111

112112
def as_sql(self, compiler, connection):
113113
lhs, params = compiler.compile(self.lhs)
114-
return "%s", ["dumb"]
114+
return "%s", ("dumb",)
115115

116116

117117
DynamicField.register_lookup(DumbTransform)

0 commit comments

Comments
 (0)