Skip to content

Commit d275fd0

Browse files
authored
Refs #25367 -- Simplified OrderBy and Lookup by using Case() instead of RawSQL() on Oracle.
Follow up to efa1908.
1 parent 4137fc2 commit d275fd0

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

django/db/models/expressions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,11 +1163,11 @@ def as_oracle(self, compiler, connection):
11631163
# a CASE WHEN.
11641164
if isinstance(self.expression, Exists):
11651165
copy = self.copy()
1166-
# XXX: Use Case(When(self.lhs)) once support for boolean
1167-
# expressions is added to When.
1168-
exists_sql, params = compiler.compile(self.expression)
1169-
case_sql = 'CASE WHEN %s THEN 1 ELSE 0 END' % exists_sql
1170-
copy.expression = RawSQL(case_sql, params)
1166+
copy.expression = Case(
1167+
When(self.expression, then=True),
1168+
default=False,
1169+
output_field=fields.BooleanField(),
1170+
)
11711171
return copy.as_sql(compiler, connection)
11721172
return self.as_sql(compiler, connection)
11731173

django/db/models/lookups.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from copy import copy
44

55
from django.core.exceptions import EmptyResultSet
6-
from django.db.models.expressions import Exists, Func, RawSQL, Value
7-
from django.db.models.fields import DateTimeField, Field, IntegerField
6+
from django.db.models.expressions import Case, Exists, Func, Value, When
7+
from django.db.models.fields import (
8+
BooleanField, DateTimeField, Field, IntegerField,
9+
)
810
from django.db.models.query_utils import RegisterLookupMixin
911
from django.utils.datastructures import OrderedSet
1012
from django.utils.functional import cached_property
@@ -119,11 +121,7 @@ def as_oracle(self, compiler, connection):
119121
exprs = []
120122
for expr in (self.lhs, self.rhs):
121123
if isinstance(expr, Exists):
122-
# XXX: Use Case(When(self.lhs)) once support for boolean
123-
# expressions is added to When.
124-
sql, params = compiler.compile(expr)
125-
sql = 'CASE WHEN %s THEN 1 ELSE 0 END' % sql
126-
expr = RawSQL(sql, params)
124+
expr = Case(When(expr, then=True), default=False, output_field=BooleanField())
127125
wrapped = True
128126
exprs.append(expr)
129127
lookup = type(self)(*exprs) if wrapped else self

0 commit comments

Comments
 (0)