|
4 | 4 | import json |
5 | 5 |
|
6 | 6 | from django import VERSION |
7 | | -from django.db.models import BooleanField |
8 | | -from django.db.models.functions import Cast |
| 7 | +from django.db import NotSupportedError |
| 8 | +from django.db.models import BooleanField, Value |
| 9 | +from django.db.models.functions import Cast, NthValue |
9 | 10 | from django.db.models.functions.math import ATan2, Log, Ln, Mod, Round |
10 | | -from django.db.models.expressions import Case, Exists, OrderBy, When |
| 11 | +from django.db.models.expressions import Case, Exists, OrderBy, When, Window |
11 | 12 | from django.db.models.lookups import Lookup, In |
12 | 13 | from django.db.models import lookups |
13 | 14 |
|
@@ -51,10 +52,21 @@ def sqlserver_mod(self, compiler, connection): |
51 | 52 | ) |
52 | 53 |
|
53 | 54 |
|
| 55 | +def sqlserver_nth_value(self, compiler, connection, **extra_content): |
| 56 | + raise NotSupportedError('This backend does not support the NthValue function') |
| 57 | + |
| 58 | + |
54 | 59 | def sqlserver_round(self, compiler, connection, **extra_context): |
55 | 60 | return self.as_sql(compiler, connection, template='%(function)s(%(expressions)s, 0)', **extra_context) |
56 | 61 |
|
57 | 62 |
|
| 63 | +def sqlserver_window(self, compiler, connection, template=None): |
| 64 | + # MSSQL window functions require an OVER clause with ORDER BY |
| 65 | + if self.order_by is None: |
| 66 | + self.order_by = Value('SELECT NULL') |
| 67 | + return self.as_sql(compiler, connection, template) |
| 68 | + |
| 69 | + |
58 | 70 | def sqlserver_exists(self, compiler, connection, template=None, **extra_context): |
59 | 71 | # MS SQL doesn't allow EXISTS() in the SELECT list, so wrap it with a |
60 | 72 | # CASE WHEN expression. Change the template since the When expression |
@@ -179,11 +191,14 @@ def json_HasKeyLookup(self, compiler, connection): |
179 | 191 | Ln.as_microsoft = sqlserver_ln |
180 | 192 | Log.as_microsoft = sqlserver_log |
181 | 193 | Mod.as_microsoft = sqlserver_mod |
| 194 | +NthValue.as_microsoft = sqlserver_nth_value |
182 | 195 | Round.as_microsoft = sqlserver_round |
| 196 | +Window.as_microsoft = sqlserver_window |
183 | 197 |
|
184 | 198 | if DJANGO3: |
185 | 199 | Lookup.as_microsoft = sqlserver_lookup |
186 | 200 | else: |
187 | 201 | Exists.as_microsoft = sqlserver_exists |
188 | 202 |
|
189 | 203 | OrderBy.as_microsoft = sqlserver_orderby |
| 204 | + |
0 commit comments