77from django .core .exceptions import EmptyResultSet
88from django .db .models .sql .compiler import (
99 SQLAggregateCompiler as BaseSQLAggregateCompiler ,
10- SQLCompiler as BaseSQLCompiler , SQLDeleteCompiler as BaseSQLDeleteCompiler ,
10+ )
11+ from django .db .models .sql .compiler import SQLCompiler as BaseSQLCompiler
12+ from django .db .models .sql .compiler import (
13+ SQLDeleteCompiler as BaseSQLDeleteCompiler ,
14+ )
15+ from django .db .models .sql .compiler import (
1116 SQLInsertCompiler as BaseSQLInsertCompiler ,
17+ )
18+ from django .db .models .sql .compiler import (
1219 SQLUpdateCompiler as BaseSQLUpdateCompiler ,
1320)
1421from django .db .utils import DatabaseError
@@ -24,50 +31,68 @@ def get_combinator_sql(self, combinator, all):
2431 features = self .connection .features
2532 compilers = [
2633 query .get_compiler (self .using , self .connection )
27- for query in self .query .combined_queries if not query .is_empty ()
34+ for query in self .query .combined_queries
35+ if not query .is_empty ()
2836 ]
2937 if not features .supports_slicing_ordering_in_compound :
3038 for query , compiler in zip (self .query .combined_queries , compilers ):
3139 if query .low_mark or query .high_mark :
32- raise DatabaseError ('LIMIT/OFFSET not allowed in subqueries of compound statements.' )
40+ raise DatabaseError (
41+ "LIMIT/OFFSET not allowed in subqueries of compound "
42+ "statements."
43+ )
3344 if compiler .get_order_by ():
34- raise DatabaseError ('ORDER BY not allowed in subqueries of compound statements.' )
45+ raise DatabaseError (
46+ "ORDER BY not allowed in subqueries of compound "
47+ "statements."
48+ )
3549 parts = ()
3650 for compiler in compilers :
3751 try :
3852 # If the columns list is limited, then all combined queries
3953 # must have the same columns list. Set the selects defined on
4054 # the query on all combined queries, if not already set.
41- if not compiler .query .values_select and self .query .values_select :
42- compiler .query .set_values ((
43- * self .query .extra_select ,
44- * self .query .values_select ,
45- * self .query .annotation_select ,
46- ))
55+ if (
56+ not compiler .query .values_select
57+ and self .query .values_select
58+ ):
59+ compiler .query .set_values (
60+ (
61+ * self .query .extra_select ,
62+ * self .query .values_select ,
63+ * self .query .annotation_select ,
64+ )
65+ )
4766 part_sql , part_args = compiler .as_sql ()
4867 if compiler .query .combinator :
4968 # Wrap in a subquery if wrapping in parentheses isn't
5069 # supported.
5170 if not features .supports_parentheses_in_compound :
52- part_sql = ' SELECT * FROM ({})' .format (part_sql )
71+ part_sql = " SELECT * FROM ({})" .format (part_sql )
5372 # Add parentheses when combining with compound query if not
5473 # already added for all compound queries.
5574 elif not features .supports_slicing_ordering_in_compound :
56- part_sql = ' ({})' .format (part_sql )
75+ part_sql = " ({})" .format (part_sql )
5776 parts += ((part_sql , part_args ),)
5877 except EmptyResultSet :
5978 # Omit the empty queryset with UNION and with DIFFERENCE if the
6079 # first queryset is nonempty.
61- if combinator == 'union' or (combinator == 'difference' and parts ):
80+ if combinator == "union" or (
81+ combinator == "difference" and parts
82+ ):
6283 continue
6384 raise
6485 if not parts :
6586 raise EmptyResultSet
6687 combinator_sql = self .connection .ops .set_operators [combinator ]
67- combinator_sql += ' ALL' if all else ' DISTINCT'
68- braces = '({})' if features .supports_slicing_ordering_in_compound else '{}'
69- sql_parts , args_parts = zip (* ((braces .format (sql ), args ) for sql , args in parts ))
70- result = [' {} ' .format (combinator_sql ).join (sql_parts )]
88+ combinator_sql += " ALL" if all else " DISTINCT"
89+ braces = (
90+ "({})" if features .supports_slicing_ordering_in_compound else "{}"
91+ )
92+ sql_parts , args_parts = zip (
93+ * ((braces .format (sql ), args ) for sql , args in parts )
94+ )
95+ result = [" {} " .format (combinator_sql ).join (sql_parts )]
7196 params = []
7297 for part in args_parts :
7398 params .extend (part )
0 commit comments