diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/db/models/sql/where.py | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/db/models/sql/where.py')
| -rw-r--r-- | django/db/models/sql/where.py | 57 |
1 files changed, 35 insertions, 22 deletions
diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 50ff13be75..532780fd98 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -7,8 +7,8 @@ from django.utils import tree from django.utils.functional import cached_property # Connection types -AND = 'AND' -OR = 'OR' +AND = "AND" +OR = "OR" class WhereNode(tree.Node): @@ -25,6 +25,7 @@ class WhereNode(tree.Node): relabeled_clone() method or relabel_aliases() and clone() methods and contains_aggregate attribute. """ + default = AND resolved = False conditional = True @@ -40,15 +41,15 @@ class WhereNode(tree.Node): in_negated = negated ^ self.negated # If the effective connector is OR and this node contains an aggregate, # then we need to push the whole branch to HAVING clause. - may_need_split = ( - (in_negated and self.connector == AND) or - (not in_negated and self.connector == OR)) + may_need_split = (in_negated and self.connector == AND) or ( + not in_negated and self.connector == OR + ) if may_need_split and self.contains_aggregate: return None, self where_parts = [] having_parts = [] for c in self.children: - if hasattr(c, 'split_having'): + if hasattr(c, "split_having"): where_part, having_part = c.split_having(in_negated) if where_part is not None: where_parts.append(where_part) @@ -58,8 +59,16 @@ class WhereNode(tree.Node): having_parts.append(c) else: where_parts.append(c) - having_node = self.__class__(having_parts, self.connector, self.negated) if having_parts else None - where_node = self.__class__(where_parts, self.connector, self.negated) if where_parts else None + having_node = ( + self.__class__(having_parts, self.connector, self.negated) + if having_parts + else None + ) + where_node = ( + self.__class__(where_parts, self.connector, self.negated) + if where_parts + else None + ) return where_node, having_node def as_sql(self, compiler, connection): @@ -94,24 +103,24 @@ class WhereNode(tree.Node): # counts. if empty_needed == 0: if self.negated: - return '', [] + return "", [] else: raise EmptyResultSet if full_needed == 0: if self.negated: raise EmptyResultSet else: - return '', [] - conn = ' %s ' % self.connector + return "", [] + conn = " %s " % self.connector sql_string = conn.join(result) if sql_string: if self.negated: # Some backends (Oracle at least) need parentheses # around the inner SQL in the negated case, even if the # inner SQL contains just a single expression. - sql_string = 'NOT (%s)' % sql_string + sql_string = "NOT (%s)" % sql_string elif len(result) > 1 or self.resolved: - sql_string = '(%s)' % sql_string + sql_string = "(%s)" % sql_string return sql_string, result_params def get_group_by_cols(self, alias=None): @@ -133,10 +142,10 @@ class WhereNode(tree.Node): mapping old (current) alias values to the new values. """ for pos, child in enumerate(self.children): - if hasattr(child, 'relabel_aliases'): + if hasattr(child, "relabel_aliases"): # For example another WhereNode child.relabel_aliases(change_map) - elif hasattr(child, 'relabeled_clone'): + elif hasattr(child, "relabeled_clone"): self.children[pos] = child.relabeled_clone(change_map) def clone(self): @@ -146,10 +155,12 @@ class WhereNode(tree.Node): value) tuples, or objects supporting .clone(). """ clone = self.__class__._new_instance( - children=None, connector=self.connector, negated=self.negated, + children=None, + connector=self.connector, + negated=self.negated, ) for child in self.children: - if hasattr(child, 'clone'): + if hasattr(child, "clone"): clone.children.append(child.clone()) else: clone.children.append(child) @@ -185,18 +196,18 @@ class WhereNode(tree.Node): @staticmethod def _resolve_leaf(expr, query, *args, **kwargs): - if hasattr(expr, 'resolve_expression'): + if hasattr(expr, "resolve_expression"): expr = expr.resolve_expression(query, *args, **kwargs) return expr @classmethod def _resolve_node(cls, node, query, *args, **kwargs): - if hasattr(node, 'children'): + if hasattr(node, "children"): for child in node.children: cls._resolve_node(child, query, *args, **kwargs) - if hasattr(node, 'lhs'): + if hasattr(node, "lhs"): node.lhs = cls._resolve_leaf(node.lhs, query, *args, **kwargs) - if hasattr(node, 'rhs'): + if hasattr(node, "rhs"): node.rhs = cls._resolve_leaf(node.rhs, query, *args, **kwargs) def resolve_expression(self, *args, **kwargs): @@ -208,6 +219,7 @@ class WhereNode(tree.Node): @cached_property def output_field(self): from django.db.models import BooleanField + return BooleanField() def select_format(self, compiler, sql, params): @@ -215,7 +227,7 @@ class WhereNode(tree.Node): # (e.g. Oracle) doesn't support boolean expression in SELECT or GROUP # BY list. if not compiler.connection.features.supports_boolean_expr_in_select_clause: - sql = f'CASE WHEN {sql} THEN 1 ELSE 0 END' + sql = f"CASE WHEN {sql} THEN 1 ELSE 0 END" return sql, params def get_db_converters(self, connection): @@ -227,6 +239,7 @@ class WhereNode(tree.Node): class NothingNode: """A node that matches nothing.""" + contains_aggregate = False def as_sql(self, compiler=None, connection=None): |
