77from django .core .exceptions import EmptyResultSet
88from django .db .models .sql .compiler import (
99 SQLAggregateCompiler as BaseSQLAggregateCompiler ,
10+ )
11+ from django .db .models .sql .compiler import (
1012 SQLCompiler as BaseSQLCompiler ,
13+ )
14+ from django .db .models .sql .compiler import (
1115 SQLDeleteCompiler as BaseSQLDeleteCompiler ,
16+ )
17+ from django .db .models .sql .compiler import (
1218 SQLInsertCompiler as BaseSQLInsertCompiler ,
19+ )
20+ from django .db .models .sql .compiler import (
1321 SQLUpdateCompiler as BaseSQLUpdateCompiler ,
1422)
1523from django .db .utils import DatabaseError
24+
1625from django_spanner import USING_DJANGO_3
1726
1827
@@ -49,29 +58,23 @@ def get_combinator_sql(self, combinator, all):
4958 if not query .is_empty ()
5059 ]
5160 if not features .supports_slicing_ordering_in_compound :
52- for query , compiler in zip (
53- self .query .combined_queries , compilers
54- ):
61+ for query , compiler in zip (self .query .combined_queries , compilers ):
5562 if query .low_mark or query .high_mark :
5663 raise DatabaseError (
5764 "LIMIT/OFFSET not allowed in subqueries of compound "
5865 "statements."
5966 )
6067 if compiler .get_order_by ():
6168 raise DatabaseError (
62- "ORDER BY not allowed in subqueries of compound "
63- "statements."
69+ "ORDER BY not allowed in subqueries of compound statements."
6470 )
6571 parts = ()
6672 for compiler in compilers :
6773 try :
6874 # If the columns list is limited, then all combined queries
6975 # must have the same columns list. Set the selects defined on
7076 # the query on all combined queries, if not already set.
71- if (
72- not compiler .query .values_select
73- and self .query .values_select
74- ):
77+ if not compiler .query .values_select and self .query .values_select :
7578 compiler .query .set_values (
7679 (
7780 * self .query .extra_select ,
@@ -87,17 +90,13 @@ def get_combinator_sql(self, combinator, all):
8790 part_sql = "SELECT * FROM ({})" .format (part_sql )
8891 # Add parentheses when combining with compound query if not
8992 # already added for all compound queries.
90- elif (
91- not features .supports_slicing_ordering_in_compound
92- ):
93+ elif not features .supports_slicing_ordering_in_compound :
9394 part_sql = "({})" .format (part_sql )
9495 parts += ((part_sql , part_args ),)
9596 except EmptyResultSet :
9697 # Omit the empty queryset with UNION and with DIFFERENCE if the
9798 # first queryset is nonempty.
98- if combinator == "union" or (
99- combinator == "difference" and parts
100- ):
99+ if combinator == "union" or (combinator == "difference" and parts ):
101100 continue
102101 raise
103102 if not parts :
@@ -106,11 +105,7 @@ def get_combinator_sql(self, combinator, all):
106105 # This is the only line that is changed from the Django core
107106 # implementation of this method
108107 combinator_sql += " ALL" if all else " DISTINCT"
109- braces = (
110- "({})"
111- if features .supports_slicing_ordering_in_compound
112- else "{}"
113- )
108+ braces = "({})" if features .supports_slicing_ordering_in_compound else "{}"
114109 sql_parts , args_parts = zip (
115110 * ((braces .format (sql ), args ) for sql , args in parts )
116111 )
@@ -126,9 +121,7 @@ def get_combinator_sql(self, combinator, all):
126121 else :
127122 features = self .connection .features
128123 compilers = [
129- query .get_compiler (
130- self .using , self .connection , self .elide_empty
131- )
124+ query .get_compiler (self .using , self .connection , self .elide_empty )
132125 for query in self .query .combined_queries
133126 ]
134127 if not features .supports_slicing_ordering_in_compound :
@@ -154,10 +147,7 @@ def get_combinator_sql(self, combinator, all):
154147 # If the columns list is limited, then all combined queries
155148 # must have the same columns list. Set the selects defined on
156149 # the query on all combined queries, if not already set.
157- if (
158- not compiler .query .values_select
159- and self .query .values_select
160- ):
150+ if not compiler .query .values_select and self .query .values_select :
161151 compiler .query = compiler .query .clone ()
162152 compiler .query .set_values (
163153 (
@@ -166,9 +156,7 @@ def get_combinator_sql(self, combinator, all):
166156 * self .query .annotation_select ,
167157 )
168158 )
169- part_sql , part_args = compiler .as_sql (
170- with_col_aliases = True
171- )
159+ part_sql , part_args = compiler .as_sql (with_col_aliases = True )
172160 if compiler .query .combinator :
173161 # Wrap in a subquery if wrapping in parentheses isn't
174162 # supported.
@@ -190,9 +178,7 @@ def get_combinator_sql(self, combinator, all):
190178 except EmptyResultSet :
191179 # Omit the empty queryset with UNION and with DIFFERENCE if the
192180 # first queryset is nonempty.
193- if combinator == "union" or (
194- combinator == "difference" and parts
195- ):
181+ if combinator == "union" or (combinator == "difference" and parts ):
196182 continue
197183 raise
198184 if not parts :
0 commit comments