diff options
| author | Anssi Kääriäinen <anssi@holvi.com> | 2017-06-28 06:23:37 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-06-29 11:09:11 -0400 |
| commit | f7f5edd50d03e8482f8a6da5fb5202b895d68cd6 (patch) | |
| tree | 263f174fad202fce3d38e7a5c8b4e69b8a6e6310 /django/db/models/sql/query.py | |
| parent | 550cb3a365dee4edfdd1563224d5304de2a57fda (diff) | |
Removed obsolete Query.tables attribute.
Obsolete since Query.alias_map became an OrderedDict (refs #26522).
Diffstat (limited to 'django/db/models/sql/query.py')
| -rw-r--r-- | django/db/models/sql/query.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index d3ab487ba7..b4a87938f7 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -30,6 +30,7 @@ from django.db.models.sql.where import ( AND, OR, ExtraWhere, NothingNode, WhereNode, ) from django.utils.encoding import force_text +from django.utils.functional import cached_property from django.utils.tree import Node __all__ = ['Query', 'RawQuery'] @@ -144,7 +145,6 @@ class Query: # clause to contain other than default fields (values(), subqueries...) # Note that annotations go to annotations dictionary. self.select = () - self.tables = () # Aliases in the order they are created. self.where = where() self.where_class = where # The group_by attribute can have one of the following forms: @@ -217,6 +217,10 @@ class Query: def has_select_fields(self): return bool(self.select or self.annotation_select_mask or self.extra_select_mask) + @cached_property + def base_table(self): + return list(self.alias_map)[0] if self.alias_map else None + def __str__(self): """ Return the query as a string of SQL with the parameter values @@ -274,7 +278,6 @@ class Query: obj.default_ordering = self.default_ordering obj.standard_ordering = self.standard_ordering obj.select = self.select - obj.tables = self.tables obj.where = self.where.clone() obj.where_class = self.where_class obj.group_by = self.group_by @@ -431,7 +434,7 @@ class Query: inner_query.group_by = (self.model._meta.pk.get_col(inner_query.get_initial_alias()),) inner_query.default_cols = False - relabels = {t: 'subquery' for t in inner_query.tables} + relabels = {t: 'subquery' for t in inner_query.alias_map} relabels[None] = 'subquery' # Remove any aggregates marked for reduction from the subquery # and move them to the outer AggregateQuery. @@ -539,7 +542,7 @@ class Query: # Note that we will be creating duplicate joins for non-m2m joins in # the AND case. The results will be correct but this creates too many # joins. This is something that could be fixed later on. - reuse = set() if conjunction else set(self.tables) + reuse = set() if conjunction else set(self.alias_map) # Base table must be present in the query - this is the same # table on both sides. self.get_initial_alias() @@ -549,7 +552,8 @@ class Query: rhs_votes = set() # Now, add the joins from rhs query into the new query (skipping base # table). - for alias in rhs.tables[1:]: + rhs_tables = list(rhs.alias_map)[1:] + for alias in rhs_tables: join = rhs.alias_map[alias] # If the left side of the join was already relabeled, use the # updated alias. @@ -714,7 +718,6 @@ class Query: alias = table_name self.table_map[alias] = [alias] self.alias_refcount[alias] = 1 - self.tables += (alias,) return alias, True def ref_alias(self, alias): @@ -864,12 +867,9 @@ class Query: self.subq_aliases = self.subq_aliases.union([self.alias_prefix]) outer_query.subq_aliases = outer_query.subq_aliases.union(self.subq_aliases) change_map = OrderedDict() - tables = list(self.tables) - for pos, alias in enumerate(tables): + for pos, alias in enumerate(self.alias_map): new_alias = '%s%d' % (self.alias_prefix, pos) change_map[alias] = new_alias - tables[pos] = new_alias - self.tables = tuple(tables) self.change_aliases(change_map) def get_initial_alias(self): @@ -877,8 +877,8 @@ class Query: Return the first alias for this query, after increasing its reference count. """ - if self.tables: - alias = self.tables[0] + if self.alias_map: + alias = self.base_table self.ref_alias(alias) else: alias = self.join(BaseTable(self.get_meta().db_table, None)) @@ -1910,7 +1910,10 @@ class Query: # Trim and operate only on tables that were generated for # the lookup part of the query. That is, avoid trimming # joins generated for F() expressions. - lookup_tables = [t for t in self.tables if t in self._lookup_joins or t == self.tables[0]] + lookup_tables = [ + t for t in self.alias_map + if t in self._lookup_joins or t == self.base_table + ] for trimmed_paths, path in enumerate(all_paths): if path.m2m: break @@ -1950,7 +1953,7 @@ class Query: select_alias = lookup_tables[trimmed_paths] # The found starting point is likely a Join instead of a BaseTable reference. # But the first entry in the query's FROM clause must not be a JOIN. - for table in self.tables: + for table in self.alias_map: if self.alias_refcount[table] > 0: self.alias_map[table] = BaseTable(self.alias_map[table].table_name, table) break |
