From 76e37513e22f4d9a01c7f15eee36fe44388e6670 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 6 Nov 2022 11:19:33 -0500 Subject: Refs #33374 -- Adjusted full match condition handling. Adjusting WhereNode.as_sql() to raise an exception when encoutering a full match just like with empty matches ensures that all case are explicitly handled. --- django/db/models/sql/where.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'django/db/models/sql/where.py') diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 63fdf58d9d..1928ba91b8 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -4,7 +4,7 @@ Code to manage the creation and SQL rendering of 'where' constraints. import operator from functools import reduce -from django.core.exceptions import EmptyResultSet +from django.core.exceptions import EmptyResultSet, FullResultSet from django.db.models.expressions import Case, When from django.db.models.lookups import Exact from django.utils import tree @@ -145,6 +145,8 @@ class WhereNode(tree.Node): sql, params = compiler.compile(child) except EmptyResultSet: empty_needed -= 1 + except FullResultSet: + full_needed -= 1 else: if sql: result.append(sql) @@ -158,24 +160,25 @@ class WhereNode(tree.Node): # counts. if empty_needed == 0: if self.negated: - return "", [] + raise FullResultSet else: raise EmptyResultSet if full_needed == 0: if self.negated: raise EmptyResultSet else: - return "", [] + raise FullResultSet 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 - elif len(result) > 1 or self.resolved: - sql_string = "(%s)" % sql_string + if not sql_string: + raise FullResultSet + 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 + elif len(result) > 1 or self.resolved: + sql_string = "(%s)" % sql_string return sql_string, result_params def get_group_by_cols(self): -- cgit v1.3