44import json
55
66from django import VERSION
7- from django . core import validators
7+
88from django .db import NotSupportedError , connections , transaction
9- from django .db .models import BooleanField , CheckConstraint , Value
10- from django .db .models .expressions import Case , Exists , Expression , OrderBy , When , Window
11- from django .db .models .fields import BinaryField , Field
9+ from django .db .models import BooleanField , Value
1210from django .db .models .functions import Cast , NthValue
13- from django .db .models .functions .math import ATan2 , Ln , Log , Mod , Round
14- from django .db .models .lookups import In , Lookup
15- from django .db .models .query import QuerySet
11+ from django .db .models .functions .math import ATan2 , Log , Ln , Mod , Round
12+ from django .db .models .expressions import Case , Exists , OrderBy , When , Window , Expression
13+ from django .db .models .lookups import Lookup , In
14+ from django .db .models import lookups , CheckConstraint
15+ from django .db .models .fields import BinaryField , Field
1616from django .db .models .sql .query import Query
17+ from django .db .models .query import QuerySet
18+ from django .core import validators
1719
1820if VERSION >= (3 , 1 ):
1921 from django .db .models .fields .json import (
@@ -65,11 +67,9 @@ def sqlserver_nth_value(self, compiler, connection, **extra_content):
6567def sqlserver_round (self , compiler , connection , ** extra_context ):
6668 return self .as_sql (compiler , connection , template = '%(function)s(%(expressions)s, 0)' , ** extra_context )
6769
68-
6970def sqlserver_random (self , compiler , connection , ** extra_context ):
7071 return self .as_sql (compiler , connection , function = 'RAND' , ** extra_context )
7172
72-
7373def sqlserver_window (self , compiler , connection , template = None ):
7474 # MSSQL window functions require an OVER clause with ORDER BY
7575 if self .order_by is None :
@@ -125,13 +125,6 @@ def sqlserver_orderby(self, compiler, connection):
125125
126126
127127def split_parameter_list_as_sql (self , compiler , connection ):
128- if connection .vendor == 'microsoft' :
129- return mssql_split_parameter_list_as_sql (self , compiler , connection )
130- else :
131- return in_split_parameter_list_as_sql (self , compiler , connection )
132-
133-
134- def mssql_split_parameter_list_as_sql (self , compiler , connection ):
135128 # Insert In clause parameters 1000 at a time into a temp table.
136129 lhs , _ = self .process_lhs (compiler , connection )
137130 _ , rhs_params = self .batch_process_rhs (compiler , connection )
@@ -150,29 +143,26 @@ def mssql_split_parameter_list_as_sql(self, compiler, connection):
150143
151144 return in_clause , ()
152145
153-
154146def unquote_json_rhs (rhs_params ):
155147 for value in rhs_params :
156148 value = json .loads (value )
157149 if not isinstance (value , (list , dict )):
158150 rhs_params = [param .replace ('"' , '' ) for param in rhs_params ]
159151 return rhs_params
160152
161-
162153def json_KeyTransformExact_process_rhs (self , compiler , connection ):
163- rhs , rhs_params = key_transform_exact_process_rhs (self , compiler , connection )
164- if connection .vendor == 'microsoft' :
165- rhs_params = unquote_json_rhs (rhs_params )
166- return rhs , rhs_params
154+ if isinstance (self .rhs , KeyTransform ):
155+ return super (lookups .Exact , self ).process_rhs (compiler , connection )
156+ rhs , rhs_params = super (KeyTransformExact , self ).process_rhs (compiler , connection )
167157
158+ return rhs , unquote_json_rhs (rhs_params )
168159
169160def json_KeyTransformIn (self , compiler , connection ):
170161 lhs , _ = super (KeyTransformIn , self ).process_lhs (compiler , connection )
171162 rhs , rhs_params = super (KeyTransformIn , self ).process_rhs (compiler , connection )
172163
173164 return (lhs + ' IN ' + rhs , unquote_json_rhs (rhs_params ))
174165
175-
176166def json_HasKeyLookup (self , compiler , connection ):
177167 # Process JSON path from the left-hand side.
178168 if isinstance (self .lhs , KeyTransform ):
@@ -203,7 +193,6 @@ def json_HasKeyLookup(self, compiler, connection):
203193
204194 return sql % tuple (rhs_params ), []
205195
206-
207196def BinaryField_init (self , * args , ** kwargs ):
208197 # Add max_length option for BinaryField, default to max
209198 kwargs .setdefault ('editable' , False )
@@ -213,7 +202,6 @@ def BinaryField_init(self, *args, **kwargs):
213202 else :
214203 self .max_length = 'max'
215204
216-
217205def _get_check_sql (self , model , schema_editor ):
218206 if VERSION >= (3 , 1 ):
219207 query = Query (model = model , alias_cols = False )
@@ -222,16 +210,13 @@ def _get_check_sql(self, model, schema_editor):
222210 where = query .build_where (self .check )
223211 compiler = query .get_compiler (connection = schema_editor .connection )
224212 sql , params = where .as_sql (compiler , schema_editor .connection )
225- if schema_editor .connection .vendor == 'microsoft' :
226- try :
227- for p in params :
228- str (p ).encode ('ascii' )
229- except UnicodeEncodeError :
230- sql = sql .replace ('%s' , 'N%s' )
213+ try :
214+ for p in params : str (p ).encode ('ascii' )
215+ except UnicodeEncodeError :
216+ sql = sql .replace ('%s' , 'N%s' )
231217
232218 return sql % tuple (schema_editor .quote_value (p ) for p in params )
233219
234-
235220def bulk_update_with_default (self , objs , fields , batch_size = None , default = 0 ):
236221 """
237222 Update the given fields in each of the given objects in the database.
@@ -270,10 +255,10 @@ def bulk_update_with_default(self, objs, fields, batch_size=None, default=0):
270255 attr = getattr (obj , field .attname )
271256 if not isinstance (attr , Expression ):
272257 if attr is None :
273- value_none_counter += 1
258+ value_none_counter += 1
274259 attr = Value (attr , output_field = field )
275260 when_statements .append (When (pk = obj .pk , then = attr ))
276- if connections [ self . db ]. vendor == 'microsoft' and value_none_counter == len (when_statements ):
261+ if ( value_none_counter == len (when_statements ) ):
277262 case_statement = Case (* when_statements , output_field = field , default = Value (default ))
278263 else :
279264 case_statement = Case (* when_statements , output_field = field )
@@ -287,15 +272,10 @@ def bulk_update_with_default(self, objs, fields, batch_size=None, default=0):
287272 rows_updated += self .filter (pk__in = pks ).update (** update_kwargs )
288273 return rows_updated
289274
290-
291275ATan2 .as_microsoft = sqlserver_atan2
292- # Need copy of old In.split_parameter_list_as_sql for other backends to call
293- in_split_parameter_list_as_sql = In .split_parameter_list_as_sql
294276In .split_parameter_list_as_sql = split_parameter_list_as_sql
295277if VERSION >= (3 , 1 ):
296278 KeyTransformIn .as_microsoft = json_KeyTransformIn
297- # Need copy of old KeyTransformExact.process_rhs to call later
298- key_transform_exact_process_rhs = KeyTransformExact .process_rhs
299279 KeyTransformExact .process_rhs = json_KeyTransformExact_process_rhs
300280 HasKeyLookup .as_microsoft = json_HasKeyLookup
301281Ln .as_microsoft = sqlserver_ln
0 commit comments