| 1 |
from django.db.models.sql.constants import INNER |
| 2 |
|
| 3 |
from ._quoting import quote_name |
| 4 |
|
| 5 |
|
| 6 |
class QJoin: |
| 7 |
"""Join clause with join condition from Q object clause |
| 8 |
|
| 9 |
:param parent_alias: Alias of parent table. |
| 10 |
:param table_name: Name of joined table. |
| 11 |
:param table_alias: Alias of joined table. |
| 12 |
:param on_clause: Query `where_class` instance represenging the ON clause. |
| 13 |
:param join_type: Join type (INNER or LOUTER). |
| 14 |
""" |
| 15 |
|
| 16 |
filtered_relation = None |
| 17 |
|
| 18 |
def __init__(self, parent_alias, table_name, table_alias, |
| 19 |
on_clause, join_type=INNER, nullable=None): |
| 20 |
self.parent_alias = parent_alias |
| 21 |
self.table_name = table_name |
| 22 |
self.table_alias = table_alias |
| 23 |
self.on_clause = on_clause |
| 24 |
self.join_type = join_type # LOUTER or INNER |
| 25 |
self.nullable = join_type != INNER if nullable is None else nullable |
| 26 |
|
| 27 |
@property |
| 28 |
def identity(self): |
| 29 |
return ( |
| 30 |
self.__class__, |
| 31 |
self.table_name, |
| 32 |
self.parent_alias, |
| 33 |
self.join_type, |
| 34 |
self.on_clause, |
| 35 |
) |
| 36 |
|
| 37 |
def __hash__(self): |
| 38 |
return hash(self.identity) |
| 39 |
|
| 40 |
def __eq__(self, other): |
| 41 |
if not isinstance(other, QJoin): |
| 42 |
return NotImplemented |
| 43 |
return self.identity == other.identity |
| 44 |
|
| 45 |
def equals(self, other): |
| 46 |
return self.identity == other.identity |
| 47 |
|
| 48 |
def as_sql(self, compiler, connection): |
| 49 |
"""Generate join clause SQL""" |
| 50 |
on_clause_sql, params = self.on_clause.as_sql(compiler, connection) |
| 51 |
qn = quote_name(compiler) |
| 52 |
if self.table_alias == self.table_name: |
| 53 |
alias = '' |
| 54 |
else: |
| 55 |
# Quote the alias like the column references pointing at it, |
| 56 |
# otherwise the database case-folds one but not the other. |
| 57 |
alias = ' %s' % qn(self.table_alias) |
| 58 |
sql = '%s %s%s ON %s' % ( |
| 59 |
self.join_type, |
| 60 |
qn(self.table_name), |
| 61 |
alias, |
| 62 |
on_clause_sql |
| 63 |
) |
| 64 |
return sql, params |
| 65 |
|
| 66 |
def relabeled_clone(self, change_map): |
| 67 |
return self.__class__( |
| 68 |
parent_alias=change_map.get(self.parent_alias, self.parent_alias), |
| 69 |
table_name=self.table_name, |
| 70 |
table_alias=change_map.get(self.table_alias, self.table_alias), |
| 71 |
on_clause=self.on_clause.relabeled_clone(change_map), |
| 72 |
join_type=self.join_type, |
| 73 |
nullable=self.nullable, |
| 74 |
) |
| 75 |
|
| 76 |
class join_field: |
| 77 |
# `Join.join_field` is used internally by `Join` as well as in |
| 78 |
# `QuerySet.resolve_expression()`: |
| 79 |
# |
| 80 |
# isinstance(table, Join) |
| 81 |
# and table.join_field.related_model._meta.db_table != alias |
| 82 |
# |
| 83 |
# Currently that does not apply here since `QJoin` is not an |
| 84 |
# instance of `Join`, although maybe it should? Maybe this |
| 85 |
# should have `related_model._meta.db_table` return |
| 86 |
# `<QJoin>.table_name` or `<QJoin>.table_alias`? |
| 87 |
# |
| 88 |
# `PathInfo.join_field` is another similarly named attribute in |
| 89 |
# Django that has a much more complicated interface, but luckily |
| 90 |
# seems unrelated to `Join.join_field`. |
| 91 |
|
| 92 |
class related_model: |
| 93 |
class _meta: |
| 94 |
# for QuerySet.set_group_by(allow_aliases=True) |
| 95 |
local_concrete_fields = () |