Renamed 'clause' variable to 'lookup_type' in django.db.models.query.lookup_inner

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3322 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-07-11 03:21:59 +00:00
parent 79b7fc17a1
commit c262e19670
1 changed files with 29 additions and 38 deletions

View File

@ -85,8 +85,8 @@ class QuerySet(object):
self._where = [] # List of extra WHERE clauses to use. self._where = [] # List of extra WHERE clauses to use.
self._params = [] # List of params to use for extra WHERE clauses. self._params = [] # List of params to use for extra WHERE clauses.
self._tables = [] # List of extra tables to use. self._tables = [] # List of extra tables to use.
self._offset = None # OFFSET clause self._offset = None # OFFSET clause.
self._limit = None # LIMIT clause self._limit = None # LIMIT clause.
self._result_cache = None self._result_cache = None
######################## ########################
@ -710,28 +710,28 @@ def parse_lookup(kwarg_items, opts):
if value is not None: if value is not None:
path = kwarg.split(LOOKUP_SEPARATOR) path = kwarg.split(LOOKUP_SEPARATOR)
# Extract the last elements of the kwarg. # Extract the last elements of the kwarg.
# The very-last is the clause (equals, like, etc). # The very-last is the lookup_type (equals, like, etc).
# The second-last is the table column on which the clause is # The second-last is the table column on which the lookup_type is
# to be performed. # to be performed.
# The exceptions to this are: # The exceptions to this are:
# 1) "pk", which is an implicit id__exact; # 1) "pk", which is an implicit id__exact;
# if we find "pk", make the clause "exact', and insert # if we find "pk", make the lookup_type "exact', and insert
# a dummy name of None, which we will replace when # a dummy name of None, which we will replace when
# we know which table column to grab as the primary key. # we know which table column to grab as the primary key.
# 2) If there is only one part, or the last part is not a query # 2) If there is only one part, or the last part is not a query
# term, assume that the query is an __exact # term, assume that the query is an __exact
clause = path.pop() lookup_type = path.pop()
if clause == 'pk': if lookup_type == 'pk':
clause = 'exact' lookup_type = 'exact'
path.append(None) path.append(None)
elif len(path) == 0 or clause not in QUERY_TERMS: elif len(path) == 0 or lookup_type not in QUERY_TERMS:
path.append(clause) path.append(lookup_type)
clause = 'exact' lookup_type = 'exact'
if len(path) < 1: if len(path) < 1:
raise TypeError, "Cannot parse keyword query %r" % kwarg raise TypeError, "Cannot parse keyword query %r" % kwarg
joins2, where2, params2 = lookup_inner(path, clause, value, opts, opts.db_table, None) joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None)
joins.update(joins2) joins.update(joins2)
where.extend(where2) where.extend(where2)
params.extend(params2) params.extend(params2)
@ -754,7 +754,7 @@ def find_field(name, field_list, related_query):
return None return None
return matches[0] return matches[0]
def lookup_inner(path, clause, value, opts, table, column): def lookup_inner(path, lookup_type, value, opts, table, column):
joins, where, params = SortedDict(), [], [] joins, where, params = SortedDict(), [], []
current_opts = opts current_opts = opts
current_table = table current_table = table
@ -835,47 +835,41 @@ def lookup_inner(path, clause, value, opts, table, column):
else: # No match found. else: # No match found.
raise TypeError, "Cannot resolve keyword '%s' into field" % name raise TypeError, "Cannot resolve keyword '%s' into field" % name
# Check to see if an intermediate join is required between current_table # Check whether an intermediate join is required between current_table
# and new_table. # and new_table.
if intermediate_table: if intermediate_table:
joins[backend.quote_name(current_table)] = ( joins[backend.quote_name(current_table)] = (
backend.quote_name(intermediate_table), backend.quote_name(intermediate_table), "LEFT OUTER JOIN",
"LEFT OUTER JOIN",
"%s.%s = %s.%s" % \ "%s.%s = %s.%s" % \
(backend.quote_name(table), (backend.quote_name(table), backend.quote_name(current_opts.pk.column),
backend.quote_name(current_opts.pk.column), backend.quote_name(current_table), backend.quote_name(intermediate_column))
backend.quote_name(current_table),
backend.quote_name(intermediate_column))
) )
if path: if path:
# There are elements left in the path. More joins are required. # There are elements left in the path. More joins are required.
if len(path) == 1 and path[0] in (new_opts.pk.name, None) \ if len(path) == 1 and path[0] in (new_opts.pk.name, None) \
and clause in ('exact', 'isnull') and not join_required: and lookup_type in ('exact', 'isnull') and not join_required:
# If the next and final name query is for a primary key, # If the next and final name query is for a primary key,
# and the search is for isnull/exact, then the current # and the search is for isnull/exact, then the current
# (for N-1) or intermediate (for N-N) table can be used # (for N-1) or intermediate (for N-N) table can be used
# for the search - no need to join an extra table just # for the search. No need to join an extra table just
# to check the primary key. # to check the primary key.
new_table = current_table new_table = current_table
else: else:
# There are 1 or more name queries pending, and we have ruled out # There are 1 or more name queries pending, and we have ruled out
# any shortcuts; therefore, a join is required. # any shortcuts; therefore, a join is required.
joins[backend.quote_name(new_table)] = ( joins[backend.quote_name(new_table)] = (
backend.quote_name(new_opts.db_table), backend.quote_name(new_opts.db_table), "INNER JOIN",
"INNER JOIN",
"%s.%s = %s.%s" % "%s.%s = %s.%s" %
(backend.quote_name(current_table), (backend.quote_name(current_table), backend.quote_name(join_column),
backend.quote_name(join_column), backend.quote_name(new_table), backend.quote_name(new_column))
backend.quote_name(new_table),
backend.quote_name(new_column))
) )
# If we have made the join, we don't need to tell subsequent # If we have made the join, we don't need to tell subsequent
# recursive calls about the column name we joined on. # recursive calls about the column name we joined on.
join_column = None join_column = None
# There are name queries remaining. Recurse deeper. # There are name queries remaining. Recurse deeper.
joins2, where2, params2 = lookup_inner(path, clause, value, new_opts, new_table, join_column) joins2, where2, params2 = lookup_inner(path, lookup_type, value, new_opts, new_table, join_column)
joins.update(joins2) joins.update(joins2)
where.extend(where2) where.extend(where2)
@ -891,13 +885,10 @@ def lookup_inner(path, clause, value, opts, table, column):
# Join is required; query operates on joined table. # Join is required; query operates on joined table.
column = new_opts.pk.name column = new_opts.pk.name
joins[backend.quote_name(new_table)] = ( joins[backend.quote_name(new_table)] = (
backend.quote_name(new_opts.db_table), backend.quote_name(new_opts.db_table), "INNER JOIN",
"INNER JOIN",
"%s.%s = %s.%s" % "%s.%s = %s.%s" %
(backend.quote_name(current_table), (backend.quote_name(current_table), backend.quote_name(join_column),
backend.quote_name(join_column), backend.quote_name(new_table), backend.quote_name(new_column))
backend.quote_name(new_table),
backend.quote_name(new_column))
) )
current_table = new_table current_table = new_table
else: else:
@ -909,7 +900,7 @@ def lookup_inner(path, clause, value, opts, table, column):
# Last query term is a related object from an N-N relation. # Last query term is a related object from an N-N relation.
# Join from intermediate table is sufficient. # Join from intermediate table is sufficient.
column = join_column column = join_column
elif name == current_opts.pk.name and clause in ('exact', 'isnull') and current_column: elif name == current_opts.pk.name and lookup_type in ('exact', 'isnull') and current_column:
# Last query term is for a primary key. If previous iterations # Last query term is for a primary key. If previous iterations
# introduced a current/intermediate table that can be used to # introduced a current/intermediate table that can be used to
# optimize the query, then use that table and column name. # optimize the query, then use that table and column name.
@ -918,8 +909,8 @@ def lookup_inner(path, clause, value, opts, table, column):
# Last query term was a normal field. # Last query term was a normal field.
column = field.column column = field.column
where.append(get_where_clause(clause, current_table + '.', column, value)) where.append(get_where_clause(lookup_type, current_table + '.', column, value))
params.extend(field.get_db_prep_lookup(clause, value)) params.extend(field.get_db_prep_lookup(lookup_type, value))
return joins, where, params return joins, where, params