summaryrefslogtreecommitdiff
path: root/django/db/backends/base
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/backends/base')
-rw-r--r--django/db/backends/base/features.py3
-rw-r--r--django/db/backends/base/operations.py25
2 files changed, 24 insertions, 4 deletions
diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py
index 6770c177c1..c9e78b5746 100644
--- a/django/db/backends/base/features.py
+++ b/django/db/backends/base/features.py
@@ -78,6 +78,9 @@ class BaseDatabaseFeatures:
# Does the backend ignore unnecessary ORDER BY clauses in subqueries?
ignores_unnecessary_order_by_in_subqueries = True
+ # Is there a true datatype for boolean?
+ has_native_boolean_field = False
+
# Is there a true datatype for uuid?
has_native_uuid_field = False
diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py
index 29ef7d93d1..db45d6922e 100644
--- a/django/db/backends/base/operations.py
+++ b/django/db/backends/base/operations.py
@@ -8,8 +8,10 @@ import sqlparse
from django.conf import settings
from django.db import NotSupportedError, models, transaction
-from django.db.models.expressions import Col
+from django.db.models import Exists, ExpressionWrapper, Lookup
+from django.db.models.expressions import Col, RawSQL
from django.db.models.fields.composite import CompositePrimaryKey
+from django.db.models.sql.where import WhereNode
from django.utils import timezone
from django.utils.duration import duration_microseconds
from django.utils.encoding import force_str
@@ -716,10 +718,25 @@ class BaseDatabaseOperations:
def conditional_expression_supported_in_where_clause(self, expression):
"""
- Return True, if the conditional expression is supported in the WHERE
- clause.
+ Return True, if the conditional expression is directly supported in the
+ WHERE clause.
"""
- return True
+ # If the backend supports native boolean field it can accept any
+ # direct conditional expression usage.
+ if self.connection.features.has_native_boolean_field:
+ return True
+ # Most backends support direct EXISTS and lookups usage.
+ if isinstance(expression, (Exists, Lookup, WhereNode)):
+ return True
+ # Nested expression wrappers should be unwrapped.
+ if isinstance(expression, ExpressionWrapper) and expression.conditional:
+ return self.conditional_expression_supported_in_where_clause(
+ expression.expression
+ )
+ # Trust that direct usage of RawSQL can be used by itself.
+ if isinstance(expression, RawSQL) and expression.conditional:
+ return True
+ return False
def combine_expression(self, connector, sub_expressions):
"""