lookups.py 27 KB

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