summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-29 11:56:12 +0200
committerGitHub <noreply@github.com>2019-08-29 11:56:12 +0200
commitd275fd04f3fbb1a6cfa2273feb6cb414ce143b8c (patch)
tree2ca3eeabc4ed9662120e902b4a4e5171cb559e5f
parent4137fc2efce2dde48340728b8006fc6d66b9e3a5 (diff)
Refs #25367 -- Simplified OrderBy and Lookup by using Case() instead of RawSQL() on Oracle.
Follow up to efa1908f662c19038a944129c81462485c4a9fe8.
-rw-r--r--django/db/models/expressions.py10
-rw-r--r--django/db/models/lookups.py12
2 files changed, 10 insertions, 12 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
index 9429adf81c..2b59dd301a 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -1163,11 +1163,11 @@ class OrderBy(BaseExpression):
# a CASE WHEN.
if isinstance(self.expression, Exists):
copy = self.copy()
- # XXX: Use Case(When(self.lhs)) once support for boolean
- # expressions is added to When.
- exists_sql, params = compiler.compile(self.expression)
- case_sql = 'CASE WHEN %s THEN 1 ELSE 0 END' % exists_sql
- copy.expression = RawSQL(case_sql, params)
+ copy.expression = Case(
+ When(self.expression, then=True),
+ default=False,
+ output_field=fields.BooleanField(),
+ )
return copy.as_sql(compiler, connection)
return self.as_sql(compiler, connection)
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index f76c1e391b..105dc93251 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -3,8 +3,10 @@ import math
from copy import copy
from django.core.exceptions import EmptyResultSet
-from django.db.models.expressions import Exists, Func, RawSQL, Value
-from django.db.models.fields import DateTimeField, Field, IntegerField
+from django.db.models.expressions import Case, Exists, Func, Value, When
+from django.db.models.fields import (
+ BooleanField, DateTimeField, Field, IntegerField,
+)
from django.db.models.query_utils import RegisterLookupMixin
from django.utils.datastructures import OrderedSet
from django.utils.functional import cached_property
@@ -119,11 +121,7 @@ class Lookup:
exprs = []
for expr in (self.lhs, self.rhs):
if isinstance(expr, Exists):
- # XXX: Use Case(When(self.lhs)) once support for boolean
- # expressions is added to When.
- sql, params = compiler.compile(expr)
- sql = 'CASE WHEN %s THEN 1 ELSE 0 END' % sql
- expr = RawSQL(sql, params)
+ expr = Case(When(expr, then=True), default=False, output_field=BooleanField())
wrapped = True
exprs.append(expr)
lookup = type(self)(*exprs) if wrapped else self