| 1 |
""" |
| 2 |
Useful auxiliary data structures for query construction. Not useful outside |
| 3 |
the SQL domain. |
| 4 |
""" |
| 5 |
|
| 6 |
import warnings |
| 7 |
|
| 8 |
from django.core.exceptions import FullResultSet |
| 9 |
from django.db.models.sql.constants import INNER, LOUTER |
| 10 |
from django.utils.deprecation import RemovedInDjango60Warning |
| 11 |
|
| 12 |
|
| 13 |
class MultiJoin(Exception): |
| 14 |
""" |
| 15 |
Used by join construction code to indicate the point at which a |
| 16 |
multi-valued join was attempted (if the caller wants to treat that |
| 17 |
exceptionally). |
| 18 |
""" |
| 19 |
|
| 20 |
def __init__(self, names_pos, path_with_names): |
| 21 |
self.level = names_pos |
| 22 |
# The path travelled, this includes the path to the multijoin. |
| 23 |
self.names_with_path = path_with_names |
| 24 |
|
| 25 |
|
| 26 |
class Empty: |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
class Join: |
| 31 |
""" |
| 32 |
Used by sql.Query and sql.SQLCompiler to generate JOIN clauses into the |
| 33 |
FROM entry. For example, the SQL generated could be |
| 34 |
LEFT OUTER JOIN "sometable" T1 |
| 35 |
ON ("othertable"."sometable_id" = "sometable"."id") |
| 36 |
|
| 37 |
This class is primarily used in Query.alias_map. All entries in alias_map |
| 38 |
must be Join compatible by providing the following attributes and methods: |
| 39 |
- table_name (string) |
| 40 |
- table_alias (possible alias for the table, can be None) |
| 41 |
- join_type (can be None for those entries that aren't joined from |
| 42 |
anything) |
| 43 |
- parent_alias (which table is this join's parent, can be None similarly |
| 44 |
to join_type) |
| 45 |
- as_sql() |
| 46 |
- relabeled_clone() |
| 47 |
""" |
| 48 |
|
| 49 |
def __init__( |
| 50 |
self, |
| 51 |
table_name, |
| 52 |
parent_alias, |
| 53 |
table_alias, |
| 54 |
join_type, |
| 55 |
join_field, |
| 56 |
nullable, |
| 57 |
filtered_relation=None, |
| 58 |
): |
| 59 |
# Join table |
| 60 |
self.table_name = table_name |
| 61 |
self.parent_alias = parent_alias |
| 62 |
# Note: table_alias is not necessarily known at instantiation time. |
| 63 |
self.table_alias = table_alias |
| 64 |
# LOUTER or INNER |
| 65 |
self.join_type = join_type |
| 66 |
# A list of 2-tuples to use in the ON clause of the JOIN. |
| 67 |
# Each 2-tuple will create one join condition in the ON clause. |
| 68 |
if hasattr(join_field, "get_joining_fields"): |
| 69 |
self.join_fields = join_field.get_joining_fields() |
| 70 |
self.join_cols = tuple( |
| 71 |
(lhs_field.column, rhs_field.column) |
| 72 |
for lhs_field, rhs_field in self.join_fields |
| 73 |
) |
| 74 |
else: |
| 75 |
warnings.warn( |
| 76 |
"The usage of get_joining_columns() in Join is deprecated. Implement " |
| 77 |
"get_joining_fields() instead.", |
| 78 |
RemovedInDjango60Warning, |
| 79 |
) |
| 80 |
self.join_fields = None |
| 81 |
self.join_cols = join_field.get_joining_columns() |
| 82 |
# Along which field (or ForeignObjectRel in the reverse join case) |
| 83 |
self.join_field = join_field |
| 84 |
# Is this join nullabled? |
| 85 |
self.nullable = nullable |
| 86 |
self.filtered_relation = filtered_relation |
| 87 |
|
| 88 |
def as_sql(self, compiler, connection): |
| 89 |
""" |
| 90 |
Generate the full |
| 91 |
LEFT OUTER JOIN sometable ON sometable.somecol = othertable.othercol, params |
| 92 |
clause for this join. |
| 93 |
""" |
| 94 |
join_conditions = [] |
| 95 |
params = [] |
| 96 |
qn = compiler.quote_name_unless_alias |
| 97 |
qn2 = connection.ops.quote_name |
| 98 |
# Add a join condition for each pair of joining columns. |
| 99 |
# RemovedInDjango60Warning: when the depraction ends, replace with: |
| 100 |
# for lhs, rhs in self.join_field: |
| 101 |
join_fields = self.join_fields or self.join_cols |
| 102 |
for lhs, rhs in join_fields: |
| 103 |
if isinstance(lhs, str): |
| 104 |
# RemovedInDjango60Warning: when the depraction ends, remove |
| 105 |
# the branch for strings. |
| 106 |
lhs_full_name = "%s.%s" % (qn(self.parent_alias), qn2(lhs)) |
| 107 |
rhs_full_name = "%s.%s" % (qn(self.table_alias), qn2(rhs)) |
| 108 |
else: |
| 109 |
lhs, rhs = connection.ops.prepare_join_on_clause( |
| 110 |
self.parent_alias, lhs, self.table_alias, rhs |
| 111 |
) |
| 112 |
lhs_sql, lhs_params = compiler.compile(lhs) |
| 113 |
lhs_full_name = lhs_sql % lhs_params |
| 114 |
rhs_sql, rhs_params = compiler.compile(rhs) |
| 115 |
rhs_full_name = rhs_sql % rhs_params |
| 116 |
join_conditions.append(f"{lhs_full_name} = {rhs_full_name}") |
| 117 |
|
| 118 |
# Add a single condition inside parentheses for whatever |
| 119 |
# get_extra_restriction() returns. |
| 120 |
extra_cond = self.join_field.get_extra_restriction( |
| 121 |
self.table_alias, self.parent_alias |
| 122 |
) |
| 123 |
if extra_cond: |
| 124 |
extra_sql, extra_params = compiler.compile(extra_cond) |
| 125 |
join_conditions.append("(%s)" % extra_sql) |
| 126 |
params.extend(extra_params) |
| 127 |
if self.filtered_relation: |
| 128 |
try: |
| 129 |
extra_sql, extra_params = compiler.compile(self.filtered_relation) |
| 130 |
except FullResultSet: |
| 131 |
pass |
| 132 |
else: |
| 133 |
join_conditions.append("(%s)" % extra_sql) |
| 134 |
params.extend(extra_params) |
| 135 |
if not join_conditions: |
| 136 |
# This might be a rel on the other end of an actual declared field. |
| 137 |
declared_field = getattr(self.join_field, "field", self.join_field) |
| 138 |
raise ValueError( |
| 139 |
"Join generated an empty ON clause. %s did not yield either " |
| 140 |
"joining columns or extra restrictions." % declared_field.__class__ |
| 141 |
) |
| 142 |
on_clause_sql = " AND ".join(join_conditions) |
| 143 |
alias_str = ( |
| 144 |
"" if self.table_alias == self.table_name else (" %s" % self.table_alias) |
| 145 |
) |
| 146 |
sql = "%s %s%s ON (%s)" % ( |
| 147 |
self.join_type, |
| 148 |
qn(self.table_name), |
| 149 |
alias_str, |
| 150 |
on_clause_sql, |
| 151 |
) |
| 152 |
return sql, params |
| 153 |
|
| 154 |
def relabeled_clone(self, change_map): |
| 155 |
new_parent_alias = change_map.get(self.parent_alias, self.parent_alias) |
| 156 |
new_table_alias = change_map.get(self.table_alias, self.table_alias) |
| 157 |
if self.filtered_relation is not None: |
| 158 |
filtered_relation = self.filtered_relation.relabeled_clone(change_map) |
| 159 |
else: |
| 160 |
filtered_relation = None |
| 161 |
return self.__class__( |
| 162 |
self.table_name, |
| 163 |
new_parent_alias, |
| 164 |
new_table_alias, |
| 165 |
self.join_type, |
| 166 |
self.join_field, |
| 167 |
self.nullable, |
| 168 |
filtered_relation=filtered_relation, |
| 169 |
) |
| 170 |
|
| 171 |
@property |
| 172 |
def identity(self): |
| 173 |
return ( |
| 174 |
self.__class__, |
| 175 |
self.table_name, |
| 176 |
self.parent_alias, |
| 177 |
self.join_field, |
| 178 |
self.filtered_relation, |
| 179 |
) |
| 180 |
|
| 181 |
def __eq__(self, other): |
| 182 |
if not isinstance(other, Join): |
| 183 |
return NotImplemented |
| 184 |
return self.identity == other.identity |
| 185 |
|
| 186 |
def __hash__(self): |
| 187 |
return hash(self.identity) |
| 188 |
|
| 189 |
def demote(self): |
| 190 |
new = self.relabeled_clone({}) |
| 191 |
new.join_type = INNER |
| 192 |
return new |
| 193 |
|
| 194 |
def promote(self): |
| 195 |
new = self.relabeled_clone({}) |
| 196 |
new.join_type = LOUTER |
| 197 |
return new |
| 198 |
|
| 199 |
|
| 200 |
class BaseTable: |
| 201 |
""" |
| 202 |
The BaseTable class is used for base table references in FROM clause. For |
| 203 |
example, the SQL "foo" in |
| 204 |
SELECT * FROM "foo" WHERE somecond |
| 205 |
could be generated by this class. |
| 206 |
""" |
| 207 |
|
| 208 |
join_type = None |
| 209 |
parent_alias = None |
| 210 |
filtered_relation = None |
| 211 |
|
| 212 |
def __init__(self, table_name, alias): |
| 213 |
self.table_name = table_name |
| 214 |
self.table_alias = alias |
| 215 |
|
| 216 |
def as_sql(self, compiler, connection): |
| 217 |
alias_str = ( |
| 218 |
"" if self.table_alias == self.table_name else (" %s" % self.table_alias) |
| 219 |
) |
| 220 |
base_sql = compiler.quote_name_unless_alias(self.table_name) |
| 221 |
return base_sql + alias_str, [] |
| 222 |
|
| 223 |
def relabeled_clone(self, change_map): |
| 224 |
return self.__class__( |
| 225 |
self.table_name, change_map.get(self.table_alias, self.table_alias) |
| 226 |
) |
| 227 |
|
| 228 |
@property |
| 229 |
def identity(self): |
| 230 |
return self.__class__, self.table_name, self.table_alias |
| 231 |
|
| 232 |
def __eq__(self, other): |
| 233 |
if not isinstance(other, BaseTable): |
| 234 |
return NotImplemented |
| 235 |
return self.identity == other.identity |
| 236 |
|
| 237 |
def __hash__(self): |
| 238 |
return hash(self.identity) |