Refs #25367 -- Simplified OrderBy and Lookup by using Case() instead of RawSQL() on Oracle.

Follow up to efa1908f66.
This commit is contained in:
Mariusz Felisiak 2019-08-29 11:56:12 +02:00 committed by GitHub
parent 4137fc2efc
commit d275fd04f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 12 deletions

View File

@ -1163,11 +1163,11 @@ class OrderBy(BaseExpression):
# a CASE WHEN. # a CASE WHEN.
if isinstance(self.expression, Exists): if isinstance(self.expression, Exists):
copy = self.copy() copy = self.copy()
# XXX: Use Case(When(self.lhs)) once support for boolean copy.expression = Case(
# expressions is added to When. When(self.expression, then=True),
exists_sql, params = compiler.compile(self.expression) default=False,
case_sql = 'CASE WHEN %s THEN 1 ELSE 0 END' % exists_sql output_field=fields.BooleanField(),
copy.expression = RawSQL(case_sql, params) )
return copy.as_sql(compiler, connection) return copy.as_sql(compiler, connection)
return self.as_sql(compiler, connection) return self.as_sql(compiler, connection)

View File

@ -3,8 +3,10 @@ import math
from copy import copy from copy import copy
from django.core.exceptions import EmptyResultSet from django.core.exceptions import EmptyResultSet
from django.db.models.expressions import Exists, Func, RawSQL, Value from django.db.models.expressions import Case, Exists, Func, Value, When
from django.db.models.fields import DateTimeField, Field, IntegerField from django.db.models.fields import (
BooleanField, DateTimeField, Field, IntegerField,
)
from django.db.models.query_utils import RegisterLookupMixin from django.db.models.query_utils import RegisterLookupMixin
from django.utils.datastructures import OrderedSet from django.utils.datastructures import OrderedSet
from django.utils.functional import cached_property from django.utils.functional import cached_property
@ -119,11 +121,7 @@ class Lookup:
exprs = [] exprs = []
for expr in (self.lhs, self.rhs): for expr in (self.lhs, self.rhs):
if isinstance(expr, Exists): if isinstance(expr, Exists):
# XXX: Use Case(When(self.lhs)) once support for boolean expr = Case(When(expr, then=True), default=False, output_field=BooleanField())
# expressions is added to When.
sql, params = compiler.compile(expr)
sql = 'CASE WHEN %s THEN 1 ELSE 0 END' % sql
expr = RawSQL(sql, params)
wrapped = True wrapped = True
exprs.append(expr) exprs.append(expr)
lookup = type(self)(*exprs) if wrapped else self lookup = type(self)(*exprs) if wrapped else self