From e66896226a41eb9e56b08d7adf29b44c0cd87dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20C=2E=20Leit=C3=A3o?= Date: Fri, 13 Jun 2014 10:12:31 +0200 Subject: [PATCH 1/3] Fixed #22826 -- Improved internal usage of Query.setup_joins. --- django/db/models/sql/expressions.py | 6 +++--- django/db/models/sql/query.py | 9 ++++----- django/db/models/sql/subqueries.py | 5 ++--- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/django/db/models/sql/expressions.py b/django/db/models/sql/expressions.py index 70646cfc61..e15cc2642c 100644 --- a/django/db/models/sql/expressions.py +++ b/django/db/models/sql/expressions.py @@ -57,9 +57,9 @@ class SQLEvaluator(object): self.cols.append((node, query.aggregate_select[node.name])) else: try: - field, sources, opts, join_list, path = query.setup_joins( - field_list, query.get_meta(), - query.get_initial_alias(), self.reuse) + _, sources, _, join_list, path = query.setup_joins( + field_list, query.get_meta(), query.get_initial_alias(), + can_reuse=self.reuse) self._used_joins = join_list targets, _, join_list = query.trim_joins(sources, join_list, path) if self.reuse is not None: diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 6421f38796..292322b9cb 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1008,7 +1008,7 @@ class Query(object): # Join promotion note - we must not remove any rows here, so use # outer join if there isn't any existing join. - field, sources, opts, join_list, path = self.setup_joins( + _, sources, opts, join_list, path = self.setup_joins( field_list, opts, self.get_initial_alias()) # Process the join chain to see if it can be trimmed @@ -1158,7 +1158,7 @@ class Query(object): try: field, sources, opts, join_list, path = self.setup_joins( - parts, opts, alias, can_reuse, allow_many) + parts, opts, alias, can_reuse=can_reuse, allow_many=allow_many) # split_exclude() needs to know which joins were generated for the # lookup parts self._lookup_joins = join_list @@ -1605,9 +1605,8 @@ class Query(object): for name in field_names: # Join promotion note - we must not remove any rows here, so # if there is no existing joins, use outer join. - field, targets, u2, joins, path = self.setup_joins( - name.split(LOOKUP_SEP), opts, alias, can_reuse=None, - allow_many=allow_m2m) + _, targets, _, joins, path = self.setup_joins( + name.split(LOOKUP_SEP), opts, alias, allow_many=allow_m2m) targets, final_alias, joins = self.trim_joins(targets, joins, path) for target in targets: self.select.append(SelectInfo((final_alias, target.column), target)) diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index cfda1f552c..5becdacd76 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -215,7 +215,7 @@ class DateQuery(Query): Converts the query into an extraction query. """ try: - result = self.setup_joins( + field, _, _, joins, _ = self.setup_joins( field_name.split(LOOKUP_SEP), self.get_meta(), self.get_initial_alias(), @@ -224,9 +224,8 @@ class DateQuery(Query): raise FieldDoesNotExist("%s has no field named '%s'" % ( self.get_meta().object_name, field_name )) - field = result[0] self._check_field(field) # overridden in DateTimeQuery - alias = result[3][-1] + alias = joins[-1] select = self._get_select((alias, field.column), lookup_type) self.clear_select_clause() self.select = [SelectInfo(select, None)] From 0b980ef85779835d5600564f1327c478712a4882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20C=2E=20Leit=C3=A3o?= Date: Sat, 14 Jun 2014 12:48:47 +0200 Subject: [PATCH 2/3] Removed unnecessary attribute assigment to SQLCompiler. --- django/db/models/sql/compiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 6792824ecd..fbb0152874 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -96,7 +96,7 @@ class SQLCompiler(object): # However we do not want to get rid of stuff done in pre_sql_setup(), # as the pre_sql_setup will modify query state in a way that forbids # another run of it. - self.refcounts_before = self.query.alias_refcount.copy() + refcounts_before = self.query.alias_refcount.copy() out_cols, s_params = self.get_columns(with_col_aliases) ordering, o_params, ordering_group_by = self.get_ordering() @@ -169,7 +169,7 @@ class SQLCompiler(object): result.append(self.connection.ops.for_update_sql(nowait=nowait)) # Finally do cleanup - get rid of the joins we created above. - self.query.reset_refcounts(self.refcounts_before) + self.query.reset_refcounts(refcounts_before) return ' '.join(result), tuple(params) From d2cbcbcc76a0718d45ca2305c8a55b86eb4c58c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20C=2E=20Leit=C3=A3o?= Date: Sat, 14 Jun 2014 12:50:18 +0200 Subject: [PATCH 3/3] Improved code style of Query.table_alias() usage. --- django/db/models/sql/compiler.py | 2 +- django/db/models/sql/query.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index fbb0152874..59b31dcb79 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -546,7 +546,7 @@ class SQLCompiler(object): result.append('%s%s%s' % (connector, qn(name), alias_str)) first = False for t in self.query.extra_tables: - alias, unused = self.query.table_alias(t) + alias, _ = self.query.table_alias(t) # Only add the alias if it's not already present (the table_alias() # calls increments the refcount, so an alias refcount of one means # this is the only reference. diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 292322b9cb..ff86e8b45b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -664,16 +664,16 @@ class Query(object): If 'create' is true, a new alias is always created. Otherwise, the most recently created alias for the table (if one exists) is reused. """ - current = self.table_map.get(table_name) - if not create and current: - alias = current[0] + alias_list = self.table_map.get(table_name) + if not create and alias_list: + alias = alias_list[0] self.alias_refcount[alias] += 1 return alias, False # Create a new alias for this table. - if current: + if alias_list: alias = '%s%d' % (self.alias_prefix, len(self.alias_map) + 1) - current.append(alias) + alias_list.append(alias) else: # The first occurrence of a table uses the table name directly. alias = table_name @@ -900,7 +900,7 @@ class Query(object): return alias # No reuse is possible, so we need a new alias. - alias, _ = self.table_alias(table, True) + alias, _ = self.table_alias(table, create=True) if not lhs: # Not all tables need to be joined to anything. No join type # means the later columns are ignored.