7
7
from django .core .exceptions import EmptyResultSet
8
8
from django .db .models .sql .compiler import (
9
9
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 (
11
16
SQLInsertCompiler as BaseSQLInsertCompiler ,
17
+ )
18
+ from django .db .models .sql .compiler import (
12
19
SQLUpdateCompiler as BaseSQLUpdateCompiler ,
13
20
)
14
21
from django .db .utils import DatabaseError
@@ -24,50 +31,68 @@ def get_combinator_sql(self, combinator, all):
24
31
features = self .connection .features
25
32
compilers = [
26
33
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 ()
28
36
]
29
37
if not features .supports_slicing_ordering_in_compound :
30
38
for query , compiler in zip (self .query .combined_queries , compilers ):
31
39
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
+ )
33
44
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
+ )
35
49
parts = ()
36
50
for compiler in compilers :
37
51
try :
38
52
# If the columns list is limited, then all combined queries
39
53
# must have the same columns list. Set the selects defined on
40
54
# 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
+ )
47
66
part_sql , part_args = compiler .as_sql ()
48
67
if compiler .query .combinator :
49
68
# Wrap in a subquery if wrapping in parentheses isn't
50
69
# supported.
51
70
if not features .supports_parentheses_in_compound :
52
- part_sql = ' SELECT * FROM ({})' .format (part_sql )
71
+ part_sql = " SELECT * FROM ({})" .format (part_sql )
53
72
# Add parentheses when combining with compound query if not
54
73
# already added for all compound queries.
55
74
elif not features .supports_slicing_ordering_in_compound :
56
- part_sql = ' ({})' .format (part_sql )
75
+ part_sql = " ({})" .format (part_sql )
57
76
parts += ((part_sql , part_args ),)
58
77
except EmptyResultSet :
59
78
# Omit the empty queryset with UNION and with DIFFERENCE if the
60
79
# first queryset is nonempty.
61
- if combinator == 'union' or (combinator == 'difference' and parts ):
80
+ if combinator == "union" or (
81
+ combinator == "difference" and parts
82
+ ):
62
83
continue
63
84
raise
64
85
if not parts :
65
86
raise EmptyResultSet
66
87
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 )]
71
96
params = []
72
97
for part in args_parts :
73
98
params .extend (part )
0 commit comments