Skip to content

Commit 7f0f5fa

Browse files
committed
Merge branch 'OskarPersson-exists' into azure-1.11
2 parents 6aee54b + f4c60e5 commit 7f0f5fa

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ Limitations
234234
The following features are currently not supported:
235235

236236
- Altering a model field from or to AutoField at migration
237-
- `Exists <https://docs.djangoproject.com/en/1.11/ref/models/expressions/#django.db.models.Exists>`__ subqueries
238237

239238
Notice
240239
------

sql_server/pyodbc/compiler.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from itertools import chain
22

33
from django.db.models.aggregates import Avg, Count, StdDev, Variance
4-
from django.db.models.expressions import OrderBy, Ref, Value
4+
from django.db.models.expressions import Exists, OrderBy, Ref, Value
55
from django.db.models.functions import ConcatPair, Greatest, Least, Length, Substr
66
from django.db.models.sql import compiler
77
from django.db.transaction import TransactionManagementError
@@ -39,6 +39,14 @@ def _as_sql_least(self, compiler, connection):
3939
def _as_sql_length(self, compiler, connection):
4040
return self.as_sql(compiler, connection, function='LEN')
4141

42+
def _as_sql_exists(self, compiler, connection, template=None, **extra_context):
43+
# MS SQL doesn't allow EXISTS() in the SELECT list, so wrap it with a
44+
# CASE WHEN expression. Change the template since the When expression
45+
# requires a left hand side (column) to compare against.
46+
sql, params = self.as_sql(compiler, connection, template, **extra_context)
47+
sql = 'CASE WHEN {} THEN 1 ELSE 0 END'.format(sql)
48+
return sql, params
49+
4250
def _as_sql_order_by(self, compiler, connection):
4351
template = None
4452
if self.nulls_last:
@@ -243,6 +251,8 @@ def _as_microsoft(self, node):
243251
as_microsoft = _as_sql_least
244252
elif isinstance(node, Length):
245253
as_microsoft = _as_sql_length
254+
elif isinstance(node, Exists):
255+
as_microsoft = _as_sql_exists
246256
elif isinstance(node, OrderBy):
247257
as_microsoft = _as_sql_order_by
248258
elif isinstance(node, StdDev):

sql_server/pyodbc/operations.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from django.conf import settings
77
from django.db.backends.base.operations import BaseDatabaseOperations
8-
from django.db.models.expressions import Exists
98
from django.db.models.functions import Greatest, Least
109
from django.utils import timezone
1110
from django.utils.encoding import force_text
@@ -83,13 +82,6 @@ def check_expression_support(self, expression):
8382
raise NotImplementedError(
8483
'SQL Server has no support for %s function.' %
8584
f.function)
86-
# SQL Server doesn't allow to use EXISTS in a selection list
87-
unsupported_expressions = (Exists, )
88-
for e in unsupported_expressions:
89-
if isinstance(expression, e):
90-
raise NotImplementedError(
91-
"the backend doesn't support %s expression." %
92-
e.__name__)
9385

9486
def combine_duration_expression(self, connector, sub_expressions):
9587
lhs, rhs = sub_expressions

0 commit comments

Comments
 (0)