| 1 |
import itertools |
| 2 |
import math |
| 3 |
import warnings |
| 4 |
|
| 5 |
from django.core.exceptions import EmptyResultSet, FullResultSet |
| 6 |
from django.db.backends.base.operations import BaseDatabaseOperations |
| 7 |
from django.db.models.expressions import ( |
| 8 |
Case, |
| 9 |
ColPairs, |
| 10 |
Expression, |
| 11 |
ExpressionList, |
| 12 |
Func, |
| 13 |
Value, |
| 14 |
When, |
| 15 |
) |
| 16 |
from django.db.models.fields import ( |
| 17 |
BooleanField, |
| 18 |
CharField, |
| 19 |
DateTimeField, |
| 20 |
Field, |
| 21 |
IntegerField, |
| 22 |
UUIDField, |
| 23 |
) |
| 24 |
from django.db.models.query_utils import RegisterLookupMixin |
| 25 |
from django.utils.datastructures import OrderedSet |
| 26 |
from django.utils.deprecation import RemovedInDjango60Warning |
| 27 |
from django.utils.functional import cached_property |
| 28 |
from django.utils.hashable import make_hashable |
| 29 |
|
| 30 |
|
| 31 |
class Lookup(Expression): |
| 32 |
lookup_name = None |
| 33 |
prepare_rhs = True |
| 34 |
can_use_none_as_rhs = False |
| 35 |
|
| 36 |
def __init__(self, lhs, rhs): |
| 37 |
self.lhs, self.rhs = lhs, rhs |
| 38 |
self.rhs = self.get_prep_lookup() |
| 39 |
self.lhs = self.get_prep_lhs() |
| 40 |
if hasattr(self.lhs, "get_bilateral_transforms"): |
| 41 |
bilateral_transforms = self.lhs.get_bilateral_transforms() |
| 42 |
else: |
| 43 |
bilateral_transforms = [] |
| 44 |
if bilateral_transforms: |
| 45 |
# Warn the user as soon as possible if they are trying to apply |
| 46 |
# a bilateral transformation on a nested QuerySet: that won't work. |
| 47 |
from django.db.models.sql.query import Query # avoid circular import |
| 48 |
|
| 49 |
if isinstance(rhs, Query): |
| 50 |
raise NotImplementedError( |
| 51 |
"Bilateral transformations on nested querysets are not implemented." |
| 52 |
) |
| 53 |
self.bilateral_transforms = bilateral_transforms |
| 54 |
|
| 55 |
def apply_bilateral_transforms(self, value): |
| 56 |
for transform in self.bilateral_transforms: |
| 57 |
value = transform(value) |
| 58 |
return value |
| 59 |
|
| 60 |
def __repr__(self): |
| 61 |
return f"{self.__class__.__name__}({self.lhs!r}, {self.rhs!r})" |
| 62 |
|
| 63 |
def batch_process_rhs(self, compiler, connection, rhs=None): |
| 64 |
if rhs is None: |
| 65 |
rhs = self.rhs |
| 66 |
if self.bilateral_transforms: |
| 67 |
sqls, sqls_params = [], [] |
| 68 |
for p in rhs: |
| 69 |
value = Value(p, output_field=self.lhs.output_field) |
| 70 |
value = self.apply_bilateral_transforms(value) |
| 71 |
value = value.resolve_expression(compiler.query) |
| 72 |
sql, sql_params = compiler.compile(value) |
| 73 |
sqls.append(sql) |
| 74 |
sqls_params.extend(sql_params) |
| 75 |
else: |
| 76 |
_, params = self.get_db_prep_lookup(rhs, connection) |
| 77 |
sqls, sqls_params = ["%s"] * len(params), params |
| 78 |
return sqls, sqls_params |
| 79 |
|
| 80 |
def get_source_expressions(self): |
| 81 |
if self.rhs_is_direct_value(): |
| 82 |
return [self.lhs] |
| 83 |
return [self.lhs, self.rhs] |
| 84 |
|
| 85 |
def set_source_expressions(self, new_exprs): |
| 86 |
if len(new_exprs) == 1: |
| 87 |
self.lhs = new_exprs[0] |
| 88 |
else: |
| 89 |
self.lhs, self.rhs = new_exprs |
| 90 |
|
| 91 |
def get_prep_lookup(self): |
| 92 |
if not self.prepare_rhs or hasattr(self.rhs, "resolve_expression"): |
| 93 |
return self.rhs |
| 94 |
if hasattr(self.lhs, "output_field"): |
| 95 |
if hasattr(self.lhs.output_field, "get_prep_value"): |
| 96 |
return self.lhs.output_field.get_prep_value(self.rhs) |
| 97 |
elif self.rhs_is_direct_value(): |
| 98 |
return Value(self.rhs) |
| 99 |
return self.rhs |
| 100 |
|
| 101 |
def get_prep_lhs(self): |
| 102 |
if hasattr(self.lhs, "resolve_expression"): |
| 103 |
return self.lhs |
| 104 |
return Value(self.lhs) |
| 105 |
|
| 106 |
def get_db_prep_lookup(self, value, connection): |
| 107 |
return ("%s", [value]) |
| 108 |
|
| 109 |
def process_lhs(self, compiler, connection, lhs=None): |
| 110 |
lhs = lhs or self.lhs |
| 111 |
if hasattr(lhs, "resolve_expression"): |
| 112 |
lhs = lhs.resolve_expression(compiler.query) |
| 113 |
sql, params = compiler.compile(lhs) |
| 114 |
if isinstance(lhs, Lookup): |
| 115 |
# Wrapped in parentheses to respect operator precedence. |
| 116 |
sql = f"({sql})" |
| 117 |
return sql, params |
| 118 |
|
| 119 |
def process_rhs(self, compiler, connection): |
| 120 |
value = self.rhs |
| 121 |
if self.bilateral_transforms: |
| 122 |
if self.rhs_is_direct_value(): |
| 123 |
# Do not call get_db_prep_lookup here as the value will be |
| 124 |
# transformed before being used for lookup |
| 125 |
value = Value(value, output_field=self.lhs.output_field) |
| 126 |
value = self.apply_bilateral_transforms(value) |
| 127 |
value = value.resolve_expression(compiler.query) |
| 128 |
if hasattr(value, "as_sql"): |
| 129 |
sql, params = compiler.compile(value) |
| 130 |
if isinstance(value, ColPairs): |
| 131 |
raise ValueError( |
| 132 |
"CompositePrimaryKey cannot be used as a lookup value." |
| 133 |
) |
| 134 |
# Ensure expression is wrapped in parentheses to respect operator |
| 135 |
# precedence but avoid double wrapping as it can be misinterpreted |
| 136 |
# on some backends (e.g. subqueries on SQLite). |
| 137 |
if not isinstance(value, Value) and sql and sql[0] != "(": |
| 138 |
sql = "(%s)" % sql |
| 139 |
return sql, params |
| 140 |
else: |
| 141 |
return self.get_db_prep_lookup(value, connection) |
| 142 |
|
| 143 |
def rhs_is_direct_value(self): |
| 144 |
return not hasattr(self.rhs, "as_sql") |
| 145 |
|
| 146 |
def get_group_by_cols(self): |
| 147 |
cols = [] |
| 148 |
for source in self.get_source_expressions(): |
| 149 |
cols.extend(source.get_group_by_cols()) |
| 150 |
return cols |
| 151 |
|
| 152 |
def as_oracle(self, compiler, connection): |
| 153 |
# Oracle doesn't allow EXISTS() and filters to be compared to another |
| 154 |
# expression unless they're wrapped in a CASE WHEN. |
| 155 |
wrapped = False |
| 156 |
exprs = [] |
| 157 |
for expr in (self.lhs, self.rhs): |
| 158 |
if connection.ops.conditional_expression_supported_in_where_clause(expr): |
| 159 |
expr = Case(When(expr, then=True), default=False) |
| 160 |
wrapped = True |
| 161 |
exprs.append(expr) |
| 162 |
lookup = type(self)(*exprs) if wrapped else self |
| 163 |
return lookup.as_sql(compiler, connection) |
| 164 |
|
| 165 |
@cached_property |
| 166 |
def output_field(self): |
| 167 |
return BooleanField() |
| 168 |
|
| 169 |
@property |
| 170 |
def identity(self): |
| 171 |
return self.__class__, self.lhs, self.rhs |
| 172 |
|
| 173 |
def __eq__(self, other): |
| 174 |
if not isinstance(other, Lookup): |
| 175 |
return NotImplemented |
| 176 |
return self.identity == other.identity |
| 177 |
|
| 178 |
def __hash__(self): |
| 179 |
return hash(make_hashable(self.identity)) |
| 180 |
|
| 181 |
def resolve_expression( |
| 182 |
self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False |
| 183 |
): |
| 184 |
c = self.copy() |
| 185 |
c.is_summary = summarize |
| 186 |
c.lhs = self.lhs.resolve_expression( |
| 187 |
query, allow_joins, reuse, summarize, for_save |
| 188 |
) |
| 189 |
if hasattr(self.rhs, "resolve_expression"): |
| 190 |
c.rhs = self.rhs.resolve_expression( |
| 191 |
query, allow_joins, reuse, summarize, for_save |
| 192 |
) |
| 193 |
return c |
| 194 |
|
| 195 |
def select_format(self, compiler, sql, params): |
| 196 |
# Wrap filters with a CASE WHEN expression if a database backend |
| 197 |
# (e.g. Oracle) doesn't support boolean expression in SELECT or GROUP |
| 198 |
# BY list. |
| 199 |
if not compiler.connection.features.supports_boolean_expr_in_select_clause: |
| 200 |
sql = f"CASE WHEN {sql} THEN 1 ELSE 0 END" |
| 201 |
return sql, params |
| 202 |
|
| 203 |
@cached_property |
| 204 |
def allowed_default(self): |
| 205 |
return self.lhs.allowed_default and self.rhs.allowed_default |
| 206 |
|
| 207 |
|
| 208 |
class Transform(RegisterLookupMixin, Func): |
| 209 |
""" |
| 210 |
RegisterLookupMixin() is first so that get_lookup() and get_transform() |
| 211 |
first examine self and then check output_field. |
| 212 |
""" |
| 213 |
|
| 214 |
bilateral = False |
| 215 |
arity = 1 |
| 216 |
|
| 217 |
@property |
| 218 |
def lhs(self): |
| 219 |
return self.get_source_expressions()[0] |
| 220 |
|
| 221 |
def get_bilateral_transforms(self): |
| 222 |
if hasattr(self.lhs, "get_bilateral_transforms"): |
| 223 |
bilateral_transforms = self.lhs.get_bilateral_transforms() |
| 224 |
else: |
| 225 |
bilateral_transforms = [] |
| 226 |
if self.bilateral: |
| 227 |
bilateral_transforms.append(self.__class__) |
| 228 |
return bilateral_transforms |
| 229 |
|
| 230 |
|
| 231 |
class BuiltinLookup(Lookup): |
| 232 |
def process_lhs(self, compiler, connection, lhs=None): |
| 233 |
lhs_sql, params = super().process_lhs(compiler, connection, lhs) |
| 234 |
field_internal_type = self.lhs.output_field.get_internal_type() |
| 235 |
if ( |
| 236 |
hasattr(connection.ops.__class__, "field_cast_sql") |
| 237 |
and connection.ops.__class__.field_cast_sql |
| 238 |
is not BaseDatabaseOperations.field_cast_sql |
| 239 |
): |
| 240 |
warnings.warn( |
| 241 |
( |
| 242 |
"The usage of DatabaseOperations.field_cast_sql() is deprecated. " |
| 243 |
"Implement DatabaseOperations.lookup_cast() instead." |
| 244 |
), |
| 245 |
RemovedInDjango60Warning, |
| 246 |
) |
| 247 |
db_type = self.lhs.output_field.db_type(connection=connection) |
| 248 |
lhs_sql = ( |
| 249 |
connection.ops.field_cast_sql(db_type, field_internal_type) % lhs_sql |
| 250 |
) |
| 251 |
lhs_sql = ( |
| 252 |
connection.ops.lookup_cast(self.lookup_name, field_internal_type) % lhs_sql |
| 253 |
) |
| 254 |
return lhs_sql, list(params) |
| 255 |
|
| 256 |
def as_sql(self, compiler, connection): |
| 257 |
lhs_sql, params = self.process_lhs(compiler, connection) |
| 258 |
rhs_sql, rhs_params = self.process_rhs(compiler, connection) |
| 259 |
params.extend(rhs_params) |
| 260 |
rhs_sql = self.get_rhs_op(connection, rhs_sql) |
| 261 |
return "%s %s" % (lhs_sql, rhs_sql), params |
| 262 |
|
| 263 |
def get_rhs_op(self, connection, rhs): |
| 264 |
return connection.operators[self.lookup_name] % rhs |
| 265 |
|
| 266 |
|
| 267 |
class FieldGetDbPrepValueMixin: |
| 268 |
""" |
| 269 |
Some lookups require Field.get_db_prep_value() to be called on their |
| 270 |
inputs. |
| 271 |
""" |
| 272 |
|
| 273 |
get_db_prep_lookup_value_is_iterable = False |
| 274 |
|
| 275 |
def get_db_prep_lookup(self, value, connection): |
| 276 |
# For relational fields, use the 'target_field' attribute of the |
| 277 |
# output_field. |
| 278 |
field = getattr(self.lhs.output_field, "target_field", None) |
| 279 |
get_db_prep_value = ( |
| 280 |
getattr(field, "get_db_prep_value", None) |
| 281 |
or self.lhs.output_field.get_db_prep_value |
| 282 |
) |
| 283 |
if not self.get_db_prep_lookup_value_is_iterable: |
| 284 |
value = [value] |
| 285 |
return ( |
| 286 |
"%s", |
| 287 |
[ |
| 288 |
( |
| 289 |
v |
| 290 |
if hasattr(v, "as_sql") |
| 291 |
else get_db_prep_value(v, connection, prepared=True) |
| 292 |
) |
| 293 |
for v in value |
| 294 |
], |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
class FieldGetDbPrepValueIterableMixin(FieldGetDbPrepValueMixin): |
| 299 |
""" |
| 300 |
Some lookups require Field.get_db_prep_value() to be called on each value |
| 301 |
in an iterable. |
| 302 |
""" |
| 303 |
|
| 304 |
get_db_prep_lookup_value_is_iterable = True |
| 305 |
|
| 306 |
def get_prep_lookup(self): |
| 307 |
if hasattr(self.rhs, "resolve_expression"): |
| 308 |
return self.rhs |
| 309 |
contains_expr = False |
| 310 |
prepared_values = [] |
| 311 |
for rhs_value in self.rhs: |
| 312 |
if hasattr(rhs_value, "resolve_expression"): |
| 313 |
# An expression will be handled by the database but can coexist |
| 314 |
# alongside real values. |
| 315 |
contains_expr = True |
| 316 |
elif ( |
| 317 |
self.prepare_rhs |
| 318 |
and hasattr(self.lhs, "output_field") |
| 319 |
and hasattr(self.lhs.output_field, "get_prep_value") |
| 320 |
): |
| 321 |
rhs_value = self.lhs.output_field.get_prep_value(rhs_value) |
| 322 |
prepared_values.append(rhs_value) |
| 323 |
if contains_expr: |
| 324 |
return ExpressionList( |
| 325 |
*[ |
| 326 |
# Expression defaults `str` to field references while |
| 327 |
# lookups default them to literal values. |
| 328 |
( |
| 329 |
Value(prep_value, self.lhs.output_field) |
| 330 |
if isinstance(prep_value, str) |
| 331 |
else prep_value |
| 332 |
) |
| 333 |
for prep_value in prepared_values |
| 334 |
] |
| 335 |
) |
| 336 |
return prepared_values |
| 337 |
|
| 338 |
def process_rhs(self, compiler, connection): |
| 339 |
if self.rhs_is_direct_value(): |
| 340 |
# rhs should be an iterable of values. Use batch_process_rhs() |
| 341 |
# to prepare/transform those values. |
| 342 |
return self.batch_process_rhs(compiler, connection) |
| 343 |
elif isinstance(self.rhs, ExpressionList): |
| 344 |
# rhs contains at least one expression. Unwrap them and delegate |
| 345 |
# to batch_process_rhs() to prepare/transform those values. |
| 346 |
copy = self.copy() |
| 347 |
copy.rhs = self.rhs.get_source_expressions() |
| 348 |
return copy.process_rhs(compiler, connection) |
| 349 |
else: |
| 350 |
return super().process_rhs(compiler, connection) |
| 351 |
|
| 352 |
def resolve_expression_parameter(self, compiler, connection, sql, param): |
| 353 |
params = [param] |
| 354 |
if hasattr(param, "resolve_expression"): |
| 355 |
param = param.resolve_expression(compiler.query) |
| 356 |
if hasattr(param, "as_sql"): |
| 357 |
sql, params = compiler.compile(param) |
| 358 |
return sql, params |
| 359 |
|
| 360 |
def batch_process_rhs(self, compiler, connection, rhs=None): |
| 361 |
pre_processed = super().batch_process_rhs(compiler, connection, rhs) |
| 362 |
# The params list may contain expressions which compile to a |
| 363 |
# sql/param pair. Zip them to get sql and param pairs that refer to the |
| 364 |
# same argument and attempt to replace them with the result of |
| 365 |
# compiling the param step. |
| 366 |
sql, params = zip( |
| 367 |
*( |
| 368 |
self.resolve_expression_parameter(compiler, connection, sql, param) |
| 369 |
for sql, param in zip(*pre_processed) |
| 370 |
) |
| 371 |
) |
| 372 |
params = itertools.chain.from_iterable(params) |
| 373 |
return sql, tuple(params) |
| 374 |
|
| 375 |
|
| 376 |
class PostgresOperatorLookup(Lookup): |
| 377 |
"""Lookup defined by operators on PostgreSQL.""" |
| 378 |
|
| 379 |
postgres_operator = None |
| 380 |
|
| 381 |
def as_postgresql(self, compiler, connection): |
| 382 |
lhs, lhs_params = self.process_lhs(compiler, connection) |
| 383 |
rhs, rhs_params = self.process_rhs(compiler, connection) |
| 384 |
params = tuple(lhs_params) + tuple(rhs_params) |
| 385 |
return "%s %s %s" % (lhs, self.postgres_operator, rhs), params |
| 386 |
|
| 387 |
|
| 388 |
@Field.register_lookup |
| 389 |
class Exact(FieldGetDbPrepValueMixin, BuiltinLookup): |
| 390 |
lookup_name = "exact" |
| 391 |
|
| 392 |
def get_prep_lookup(self): |
| 393 |
from django.db.models.sql.query import Query # avoid circular import |
| 394 |
|
| 395 |
if isinstance(query := self.rhs, Query): |
| 396 |
if not query.has_limit_one(): |
| 397 |
raise ValueError( |
| 398 |
"The QuerySet value for an exact lookup must be limited to " |
| 399 |
"one result using slicing." |
| 400 |
) |
| 401 |
lhs_len = len(self.lhs) if isinstance(self.lhs, (ColPairs, tuple)) else 1 |
| 402 |
if (rhs_len := query._subquery_fields_len) != lhs_len: |
| 403 |
raise ValueError( |
| 404 |
f"The QuerySet value for the exact lookup must have {lhs_len} " |
| 405 |
f"selected fields (received {rhs_len})" |
| 406 |
) |
| 407 |
if not query.has_select_fields: |
| 408 |
query.clear_select_clause() |
| 409 |
query.add_fields(["pk"]) |
| 410 |
return super().get_prep_lookup() |
| 411 |
|
| 412 |
def as_sql(self, compiler, connection): |
| 413 |
# Avoid comparison against direct rhs if lhs is a boolean value. That |
| 414 |
# turns "boolfield__exact=True" into "WHERE boolean_field" instead of |
| 415 |
# "WHERE boolean_field = True" when allowed. |
| 416 |
if ( |
| 417 |
isinstance(self.rhs, bool) |
| 418 |
and getattr(self.lhs, "conditional", False) |
| 419 |
and connection.ops.conditional_expression_supported_in_where_clause( |
| 420 |
self.lhs |
| 421 |
) |
| 422 |
): |
| 423 |
lhs_sql, params = self.process_lhs(compiler, connection) |
| 424 |
template = "%s" if self.rhs else "NOT %s" |
| 425 |
return template % lhs_sql, params |
| 426 |
return super().as_sql(compiler, connection) |
| 427 |
|
| 428 |
|
| 429 |
@Field.register_lookup |
| 430 |
class IExact(BuiltinLookup): |
| 431 |
lookup_name = "iexact" |
| 432 |
prepare_rhs = False |
| 433 |
|
| 434 |
def process_rhs(self, qn, connection): |
| 435 |
rhs, params = super().process_rhs(qn, connection) |
| 436 |
if params: |
| 437 |
params[0] = connection.ops.prep_for_iexact_query(params[0]) |
| 438 |
return rhs, params |
| 439 |
|
| 440 |
|
| 441 |
@Field.register_lookup |
| 442 |
class GreaterThan(FieldGetDbPrepValueMixin, BuiltinLookup): |
| 443 |
lookup_name = "gt" |
| 444 |
|
| 445 |
|
| 446 |
@Field.register_lookup |
| 447 |
class GreaterThanOrEqual(FieldGetDbPrepValueMixin, BuiltinLookup): |
| 448 |
lookup_name = "gte" |
| 449 |
|
| 450 |
|
| 451 |
@Field.register_lookup |
| 452 |
class LessThan(FieldGetDbPrepValueMixin, BuiltinLookup): |
| 453 |
lookup_name = "lt" |
| 454 |
|
| 455 |
|
| 456 |
@Field.register_lookup |
| 457 |
class LessThanOrEqual(FieldGetDbPrepValueMixin, BuiltinLookup): |
| 458 |
lookup_name = "lte" |
| 459 |
|
| 460 |
|
| 461 |
class IntegerFieldOverflow: |
| 462 |
underflow_exception = EmptyResultSet |
| 463 |
overflow_exception = EmptyResultSet |
| 464 |
|
| 465 |
def process_rhs(self, compiler, connection): |
| 466 |
rhs = self.rhs |
| 467 |
if isinstance(rhs, int): |
| 468 |
field_internal_type = self.lhs.output_field.get_internal_type() |
| 469 |
min_value, max_value = connection.ops.integer_field_range( |
| 470 |
field_internal_type |
| 471 |
) |
| 472 |
if min_value is not None and rhs < min_value: |
| 473 |
raise self.underflow_exception |
| 474 |
if max_value is not None and rhs > max_value: |
| 475 |
raise self.overflow_exception |
| 476 |
return super().process_rhs(compiler, connection) |
| 477 |
|
| 478 |
|
| 479 |
class IntegerFieldFloatRounding: |
| 480 |
""" |
| 481 |
Allow floats to work as query values for IntegerField. Without this, the |
| 482 |
decimal portion of the float would always be discarded. |
| 483 |
""" |
| 484 |
|
| 485 |
def get_prep_lookup(self): |
| 486 |
if isinstance(self.rhs, float): |
| 487 |
self.rhs = math.ceil(self.rhs) |
| 488 |
return super().get_prep_lookup() |
| 489 |
|
| 490 |
|
| 491 |
@IntegerField.register_lookup |
| 492 |
class IntegerFieldExact(IntegerFieldOverflow, Exact): |
| 493 |
pass |
| 494 |
|
| 495 |
|
| 496 |
@IntegerField.register_lookup |
| 497 |
class IntegerGreaterThan(IntegerFieldOverflow, GreaterThan): |
| 498 |
underflow_exception = FullResultSet |
| 499 |
|
| 500 |
|
| 501 |
@IntegerField.register_lookup |
| 502 |
class IntegerGreaterThanOrEqual( |
| 503 |
IntegerFieldOverflow, IntegerFieldFloatRounding, GreaterThanOrEqual |
| 504 |
): |
| 505 |
underflow_exception = FullResultSet |
| 506 |
|
| 507 |
|
| 508 |
@IntegerField.register_lookup |
| 509 |
class IntegerLessThan(IntegerFieldOverflow, IntegerFieldFloatRounding, LessThan): |
| 510 |
overflow_exception = FullResultSet |
| 511 |
|
| 512 |
|
| 513 |
@IntegerField.register_lookup |
| 514 |
class IntegerLessThanOrEqual(IntegerFieldOverflow, LessThanOrEqual): |
| 515 |
overflow_exception = FullResultSet |
| 516 |
|
| 517 |
|
| 518 |
@Field.register_lookup |
| 519 |
class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup): |
| 520 |
lookup_name = "in" |
| 521 |
|
| 522 |
def get_prep_lookup(self): |
| 523 |
from django.db.models.sql.query import Query # avoid circular import |
| 524 |
|
| 525 |
if isinstance(self.rhs, Query): |
| 526 |
lhs_len = len(self.lhs) if isinstance(self.lhs, (ColPairs, tuple)) else 1 |
| 527 |
if (rhs_len := self.rhs._subquery_fields_len) != lhs_len: |
| 528 |
raise ValueError( |
| 529 |
f"The QuerySet value for the 'in' lookup must have {lhs_len} " |
| 530 |
f"selected fields (received {rhs_len})" |
| 531 |
) |
| 532 |
self.rhs.clear_ordering(clear_default=True) |
| 533 |
if not self.rhs.has_select_fields: |
| 534 |
self.rhs.clear_select_clause() |
| 535 |
self.rhs.add_fields(["pk"]) |
| 536 |
return super().get_prep_lookup() |
| 537 |
|
| 538 |
def process_rhs(self, compiler, connection): |
| 539 |
db_rhs = getattr(self.rhs, "_db", None) |
| 540 |
if db_rhs is not None and db_rhs != connection.alias: |
| 541 |
raise ValueError( |
| 542 |
"Subqueries aren't allowed across different databases. Force " |
| 543 |
"the inner query to be evaluated using `list(inner_query)`." |
| 544 |
) |
| 545 |
|
| 546 |
if self.rhs_is_direct_value(): |
| 547 |
# Remove None from the list as NULL is never equal to anything. |
| 548 |
try: |
| 549 |
rhs = OrderedSet(self.rhs) |
| 550 |
rhs.discard(None) |
| 551 |
except TypeError: # Unhashable items in self.rhs |
| 552 |
rhs = [r for r in self.rhs if r is not None] |
| 553 |
|
| 554 |
if not rhs: |
| 555 |
raise EmptyResultSet |
| 556 |
|
| 557 |
# rhs should be an iterable; use batch_process_rhs() to |
| 558 |
# prepare/transform those values. |
| 559 |
sqls, sqls_params = self.batch_process_rhs(compiler, connection, rhs) |
| 560 |
placeholder = "(" + ", ".join(sqls) + ")" |
| 561 |
return (placeholder, sqls_params) |
| 562 |
return super().process_rhs(compiler, connection) |
| 563 |
|
| 564 |
def get_rhs_op(self, connection, rhs): |
| 565 |
return "IN %s" % rhs |
| 566 |
|
| 567 |
def as_sql(self, compiler, connection): |
| 568 |
max_in_list_size = connection.ops.max_in_list_size() |
| 569 |
if ( |
| 570 |
self.rhs_is_direct_value() |
| 571 |
and max_in_list_size |
| 572 |
and len(self.rhs) > max_in_list_size |
| 573 |
): |
| 574 |
return self.split_parameter_list_as_sql(compiler, connection) |
| 575 |
return super().as_sql(compiler, connection) |
| 576 |
|
| 577 |
def split_parameter_list_as_sql(self, compiler, connection): |
| 578 |
# This is a special case for databases which limit the number of |
| 579 |
# elements which can appear in an 'IN' clause. |
| 580 |
max_in_list_size = connection.ops.max_in_list_size() |
| 581 |
lhs, lhs_params = self.process_lhs(compiler, connection) |
| 582 |
rhs, rhs_params = self.batch_process_rhs(compiler, connection) |
| 583 |
in_clause_elements = ["("] |
| 584 |
params = [] |
| 585 |
for offset in range(0, len(rhs_params), max_in_list_size): |
| 586 |
if offset > 0: |
| 587 |
in_clause_elements.append(" OR ") |
| 588 |
in_clause_elements.append("%s IN (" % lhs) |
| 589 |
params.extend(lhs_params) |
| 590 |
sqls = rhs[offset : offset + max_in_list_size] |
| 591 |
sqls_params = rhs_params[offset : offset + max_in_list_size] |
| 592 |
param_group = ", ".join(sqls) |
| 593 |
in_clause_elements.append(param_group) |
| 594 |
in_clause_elements.append(")") |
| 595 |
params.extend(sqls_params) |
| 596 |
in_clause_elements.append(")") |
| 597 |
return "".join(in_clause_elements), params |
| 598 |
|
| 599 |
|
| 600 |
class PatternLookup(BuiltinLookup): |
| 601 |
param_pattern = "%%%s%%" |
| 602 |
prepare_rhs = False |
| 603 |
|
| 604 |
def get_rhs_op(self, connection, rhs): |
| 605 |
# Assume we are in startswith. We need to produce SQL like: |
| 606 |
# col LIKE %s, ['thevalue%'] |
| 607 |
# For python values we can (and should) do that directly in Python, |
| 608 |
# but if the value is for example reference to other column, then |
| 609 |
# we need to add the % pattern match to the lookup by something like |
| 610 |
# col LIKE othercol || '%%' |
| 611 |
# So, for Python values we don't need any special pattern, but for |
| 612 |
# SQL reference values or SQL transformations we need the correct |
| 613 |
# pattern added. |
| 614 |
if hasattr(self.rhs, "as_sql") or self.bilateral_transforms: |
| 615 |
pattern = connection.pattern_ops[self.lookup_name].format( |
| 616 |
connection.pattern_esc |
| 617 |
) |
| 618 |
return pattern.format(rhs) |
| 619 |
else: |
| 620 |
return super().get_rhs_op(connection, rhs) |
| 621 |
|
| 622 |
def process_rhs(self, qn, connection): |
| 623 |
rhs, params = super().process_rhs(qn, connection) |
| 624 |
if self.rhs_is_direct_value() and params and not self.bilateral_transforms: |
| 625 |
params[0] = self.param_pattern % connection.ops.prep_for_like_query( |
| 626 |
params[0] |
| 627 |
) |
| 628 |
return rhs, params |
| 629 |
|
| 630 |
|
| 631 |
@Field.register_lookup |
| 632 |
class Contains(PatternLookup): |
| 633 |
lookup_name = "contains" |
| 634 |
|
| 635 |
|
| 636 |
@Field.register_lookup |
| 637 |
class IContains(Contains): |
| 638 |
lookup_name = "icontains" |
| 639 |
|
| 640 |
|
| 641 |
@Field.register_lookup |
| 642 |
class StartsWith(PatternLookup): |
| 643 |
lookup_name = "startswith" |
| 644 |
param_pattern = "%s%%" |
| 645 |
|
| 646 |
|
| 647 |
@Field.register_lookup |
| 648 |
class IStartsWith(StartsWith): |
| 649 |
lookup_name = "istartswith" |
| 650 |
|
| 651 |
|
| 652 |
@Field.register_lookup |
| 653 |
class EndsWith(PatternLookup): |
| 654 |
lookup_name = "endswith" |
| 655 |
param_pattern = "%%%s" |
| 656 |
|
| 657 |
|
| 658 |
@Field.register_lookup |
| 659 |
class IEndsWith(EndsWith): |
| 660 |
lookup_name = "iendswith" |
| 661 |
|
| 662 |
|
| 663 |
@Field.register_lookup |
| 664 |
class Range(FieldGetDbPrepValueIterableMixin, BuiltinLookup): |
| 665 |
lookup_name = "range" |
| 666 |
|
| 667 |
def get_rhs_op(self, connection, rhs): |
| 668 |
return "BETWEEN %s AND %s" % (rhs[0], rhs[1]) |
| 669 |
|
| 670 |
|
| 671 |
@Field.register_lookup |
| 672 |
class IsNull(BuiltinLookup): |
| 673 |
lookup_name = "isnull" |
| 674 |
prepare_rhs = False |
| 675 |
|
| 676 |
def as_sql(self, compiler, connection): |
| 677 |
if not isinstance(self.rhs, bool): |
| 678 |
raise ValueError( |
| 679 |
"The QuerySet value for an isnull lookup must be True or False." |
| 680 |
) |
| 681 |
if isinstance(self.lhs, Value): |
| 682 |
if self.lhs.value is None or ( |
| 683 |
self.lhs.value == "" |
| 684 |
and connection.features.interprets_empty_strings_as_nulls |
| 685 |
): |
| 686 |
result_exception = FullResultSet if self.rhs else EmptyResultSet |
| 687 |
else: |
| 688 |
result_exception = EmptyResultSet if self.rhs else FullResultSet |
| 689 |
raise result_exception |
| 690 |
sql, params = self.process_lhs(compiler, connection) |
| 691 |
if self.rhs: |
| 692 |
return "%s IS NULL" % sql, params |
| 693 |
else: |
| 694 |
return "%s IS NOT NULL" % sql, params |
| 695 |
|
| 696 |
|
| 697 |
@Field.register_lookup |
| 698 |
class Regex(BuiltinLookup): |
| 699 |
lookup_name = "regex" |
| 700 |
prepare_rhs = False |
| 701 |
|
| 702 |
def as_sql(self, compiler, connection): |
| 703 |
if self.lookup_name in connection.operators: |
| 704 |
return super().as_sql(compiler, connection) |
| 705 |
else: |
| 706 |
lhs, lhs_params = self.process_lhs(compiler, connection) |
| 707 |
rhs, rhs_params = self.process_rhs(compiler, connection) |
| 708 |
sql_template = connection.ops.regex_lookup(self.lookup_name) |
| 709 |
return sql_template % (lhs, rhs), lhs_params + rhs_params |
| 710 |
|
| 711 |
|
| 712 |
@Field.register_lookup |
| 713 |
class IRegex(Regex): |
| 714 |
lookup_name = "iregex" |
| 715 |
|
| 716 |
|
| 717 |
class YearLookup(Lookup): |
| 718 |
def year_lookup_bounds(self, connection, year): |
| 719 |
from django.db.models.functions import ExtractIsoYear |
| 720 |
|
| 721 |
iso_year = isinstance(self.lhs, ExtractIsoYear) |
| 722 |
output_field = self.lhs.lhs.output_field |
| 723 |
if isinstance(output_field, DateTimeField): |
| 724 |
bounds = connection.ops.year_lookup_bounds_for_datetime_field( |
| 725 |
year, |
| 726 |
iso_year=iso_year, |
| 727 |
) |
| 728 |
else: |
| 729 |
bounds = connection.ops.year_lookup_bounds_for_date_field( |
| 730 |
year, |
| 731 |
iso_year=iso_year, |
| 732 |
) |
| 733 |
return bounds |
| 734 |
|
| 735 |
def as_sql(self, compiler, connection): |
| 736 |
# Avoid the extract operation if the rhs is a direct value to allow |
| 737 |
# indexes to be used. |
| 738 |
if self.rhs_is_direct_value(): |
| 739 |
# Skip the extract part by directly using the originating field, |
| 740 |
# that is self.lhs.lhs. |
| 741 |
lhs_sql, params = self.process_lhs(compiler, connection, self.lhs.lhs) |
| 742 |
rhs_sql, _ = self.process_rhs(compiler, connection) |
| 743 |
rhs_sql = self.get_direct_rhs_sql(connection, rhs_sql) |
| 744 |
start, finish = self.year_lookup_bounds(connection, self.rhs) |
| 745 |
params.extend(self.get_bound_params(start, finish)) |
| 746 |
return "%s %s" % (lhs_sql, rhs_sql), params |
| 747 |
return super().as_sql(compiler, connection) |
| 748 |
|
| 749 |
def get_direct_rhs_sql(self, connection, rhs): |
| 750 |
return connection.operators[self.lookup_name] % rhs |
| 751 |
|
| 752 |
def get_bound_params(self, start, finish): |
| 753 |
raise NotImplementedError( |
| 754 |
"subclasses of YearLookup must provide a get_bound_params() method" |
| 755 |
) |
| 756 |
|
| 757 |
|
| 758 |
class YearExact(YearLookup, Exact): |
| 759 |
def get_direct_rhs_sql(self, connection, rhs): |
| 760 |
return "BETWEEN %s AND %s" |
| 761 |
|
| 762 |
def get_bound_params(self, start, finish): |
| 763 |
return (start, finish) |
| 764 |
|
| 765 |
|
| 766 |
class YearGt(YearLookup, GreaterThan): |
| 767 |
def get_bound_params(self, start, finish): |
| 768 |
return (finish,) |
| 769 |
|
| 770 |
|
| 771 |
class YearGte(YearLookup, GreaterThanOrEqual): |
| 772 |
def get_bound_params(self, start, finish): |
| 773 |
return (start,) |
| 774 |
|
| 775 |
|
| 776 |
class YearLt(YearLookup, LessThan): |
| 777 |
def get_bound_params(self, start, finish): |
| 778 |
return (start,) |
| 779 |
|
| 780 |
|
| 781 |
class YearLte(YearLookup, LessThanOrEqual): |
| 782 |
def get_bound_params(self, start, finish): |
| 783 |
return (finish,) |
| 784 |
|
| 785 |
|
| 786 |
class UUIDTextMixin: |
| 787 |
""" |
| 788 |
Strip hyphens from a value when filtering a UUIDField on backends without |
| 789 |
a native datatype for UUID. |
| 790 |
""" |
| 791 |
|
| 792 |
def process_rhs(self, qn, connection): |
| 793 |
if not connection.features.has_native_uuid_field: |
| 794 |
from django.db.models.functions import Replace |
| 795 |
|
| 796 |
if self.rhs_is_direct_value(): |
| 797 |
self.rhs = Value(self.rhs) |
| 798 |
self.rhs = Replace( |
| 799 |
self.rhs, Value("-"), Value(""), output_field=CharField() |
| 800 |
) |
| 801 |
rhs, params = super().process_rhs(qn, connection) |
| 802 |
return rhs, params |
| 803 |
|
| 804 |
|
| 805 |
@UUIDField.register_lookup |
| 806 |
class UUIDIExact(UUIDTextMixin, IExact): |
| 807 |
pass |
| 808 |
|
| 809 |
|
| 810 |
@UUIDField.register_lookup |
| 811 |
class UUIDContains(UUIDTextMixin, Contains): |
| 812 |
pass |
| 813 |
|
| 814 |
|
| 815 |
@UUIDField.register_lookup |
| 816 |
class UUIDIContains(UUIDTextMixin, IContains): |
| 817 |
pass |
| 818 |
|
| 819 |
|
| 820 |
@UUIDField.register_lookup |
| 821 |
class UUIDStartsWith(UUIDTextMixin, StartsWith): |
| 822 |
pass |
| 823 |
|
| 824 |
|
| 825 |
@UUIDField.register_lookup |
| 826 |
class UUIDIStartsWith(UUIDTextMixin, IStartsWith): |
| 827 |
pass |
| 828 |
|
| 829 |
|
| 830 |
@UUIDField.register_lookup |
| 831 |
class UUIDEndsWith(UUIDTextMixin, EndsWith): |
| 832 |
pass |
| 833 |
|
| 834 |
|
| 835 |
@UUIDField.register_lookup |
| 836 |
class UUIDIEndsWith(UUIDTextMixin, IEndsWith): |
| 837 |
pass |