app/django/db/models/sql/query.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     9 
     9 
    10 from copy import deepcopy
    10 from copy import deepcopy
    11 
    11 
    12 from django.utils.tree import Node
    12 from django.utils.tree import Node
    13 from django.utils.datastructures import SortedDict
    13 from django.utils.datastructures import SortedDict
    14 from django.dispatch import dispatcher
    14 from django.utils.encoding import force_unicode
    15 from django.db import connection
    15 from django.db import connection
    16 from django.db.models import signals
    16 from django.db.models import signals
       
    17 from django.db.models.fields import FieldDoesNotExist
       
    18 from django.db.models.query_utils import select_related_descend
    17 from django.db.models.sql.where import WhereNode, EverythingNode, AND, OR
    19 from django.db.models.sql.where import WhereNode, EverythingNode, AND, OR
    18 from django.db.models.sql.datastructures import Count
    20 from django.db.models.sql.datastructures import Count
    19 from django.db.models.fields import FieldDoesNotExist
       
    20 from django.core.exceptions import FieldError
    21 from django.core.exceptions import FieldError
    21 from datastructures import EmptyResultSet, Empty, MultiJoin
    22 from datastructures import EmptyResultSet, Empty, MultiJoin
    22 from constants import *
    23 from constants import *
    23 
    24 
    24 try:
    25 try:
    54         self.standard_ordering = True
    55         self.standard_ordering = True
    55         self.ordering_aliases = []
    56         self.ordering_aliases = []
    56         self.start_meta = None
    57         self.start_meta = None
    57         self.select_fields = []
    58         self.select_fields = []
    58         self.related_select_fields = []
    59         self.related_select_fields = []
       
    60         self.dupe_avoidance = {}
       
    61         self.used_aliases = set()
       
    62         self.filter_is_sticky = False
    59 
    63 
    60         # SQL-related attributes
    64         # SQL-related attributes
    61         self.select = []
    65         self.select = []
    62         self.tables = []    # Aliases in the order they are created.
    66         self.tables = []    # Aliases in the order they are created.
    63         self.where = where()
    67         self.where = where()
    74         # recursion. Can be changed by the depth parameter to select_related().
    78         # recursion. Can be changed by the depth parameter to select_related().
    75         self.max_depth = 5
    79         self.max_depth = 5
    76 
    80 
    77         # These are for extensions. The contents are more or less appended
    81         # These are for extensions. The contents are more or less appended
    78         # verbatim to the appropriate clause.
    82         # verbatim to the appropriate clause.
    79         self.extra_select = {}  # Maps col_alias -> col_sql.
    83         self.extra_select = SortedDict()  # Maps col_alias -> (col_sql, params).
    80         self.extra_select_params = ()
       
    81         self.extra_tables = ()
    84         self.extra_tables = ()
    82         self.extra_where = ()
    85         self.extra_where = ()
    83         self.extra_params = ()
    86         self.extra_params = ()
    84         self.extra_order_by = ()
    87         self.extra_order_by = ()
    85 
    88 
   102     def __getstate__(self):
   105     def __getstate__(self):
   103         """
   106         """
   104         Pickling support.
   107         Pickling support.
   105         """
   108         """
   106         obj_dict = self.__dict__.copy()
   109         obj_dict = self.__dict__.copy()
       
   110         obj_dict['related_select_fields'] = []
       
   111         obj_dict['related_select_cols'] = []
   107         del obj_dict['connection']
   112         del obj_dict['connection']
   108         return obj_dict
   113         return obj_dict
   109 
   114 
   110     def __setstate__(self, obj_dict):
   115     def __setstate__(self, obj_dict):
   111         """
   116         """
   162         obj.standard_ordering = self.standard_ordering
   167         obj.standard_ordering = self.standard_ordering
   163         obj.ordering_aliases = []
   168         obj.ordering_aliases = []
   164         obj.start_meta = self.start_meta
   169         obj.start_meta = self.start_meta
   165         obj.select_fields = self.select_fields[:]
   170         obj.select_fields = self.select_fields[:]
   166         obj.related_select_fields = self.related_select_fields[:]
   171         obj.related_select_fields = self.related_select_fields[:]
       
   172         obj.dupe_avoidance = self.dupe_avoidance.copy()
   167         obj.select = self.select[:]
   173         obj.select = self.select[:]
   168         obj.tables = self.tables[:]
   174         obj.tables = self.tables[:]
   169         obj.where = deepcopy(self.where)
   175         obj.where = deepcopy(self.where)
   170         obj.where_class = self.where_class
   176         obj.where_class = self.where_class
   171         obj.group_by = self.group_by[:]
   177         obj.group_by = self.group_by[:]
   175         obj.distinct = self.distinct
   181         obj.distinct = self.distinct
   176         obj.select_related = self.select_related
   182         obj.select_related = self.select_related
   177         obj.related_select_cols = []
   183         obj.related_select_cols = []
   178         obj.max_depth = self.max_depth
   184         obj.max_depth = self.max_depth
   179         obj.extra_select = self.extra_select.copy()
   185         obj.extra_select = self.extra_select.copy()
   180         obj.extra_select_params = self.extra_select_params
       
   181         obj.extra_tables = self.extra_tables
   186         obj.extra_tables = self.extra_tables
   182         obj.extra_where = self.extra_where
   187         obj.extra_where = self.extra_where
   183         obj.extra_params = self.extra_params
   188         obj.extra_params = self.extra_params
   184         obj.extra_order_by = self.extra_order_by
   189         obj.extra_order_by = self.extra_order_by
       
   190         if self.filter_is_sticky and self.used_aliases:
       
   191             obj.used_aliases = self.used_aliases.copy()
       
   192         else:
       
   193             obj.used_aliases = set()
       
   194         obj.filter_is_sticky = False
   185         obj.__dict__.update(kwargs)
   195         obj.__dict__.update(kwargs)
   186         if hasattr(obj, '_setup_query'):
   196         if hasattr(obj, '_setup_query'):
   187             obj._setup_query()
   197             obj._setup_query()
   188         return obj
   198         return obj
   189 
   199 
   190     def results_iter(self):
   200     def results_iter(self):
   191         """
   201         """
   192         Returns an iterator over the results from executing this query.
   202         Returns an iterator over the results from executing this query.
   193         """
   203         """
   194         resolve_columns = hasattr(self, 'resolve_columns')
   204         resolve_columns = hasattr(self, 'resolve_columns')
   195         if resolve_columns:
   205         fields = None
   196             if self.select_fields:
       
   197                 fields = self.select_fields + self.related_select_fields
       
   198             else:
       
   199                 fields = self.model._meta.fields
       
   200         for rows in self.execute_sql(MULTI):
   206         for rows in self.execute_sql(MULTI):
   201             for row in rows:
   207             for row in rows:
   202                 if resolve_columns:
   208                 if resolve_columns:
       
   209                     if fields is None:
       
   210                         # We only set this up here because
       
   211                         # related_select_fields isn't populated until
       
   212                         # execute_sql() has been called.
       
   213                         if self.select_fields:
       
   214                             fields = self.select_fields + self.related_select_fields
       
   215                         else:
       
   216                             fields = self.model._meta.fields
   203                     row = self.resolve_columns(row, fields)
   217                     row = self.resolve_columns(row, fields)
   204                 yield row
   218                 yield row
   205 
   219 
   206     def get_count(self):
   220     def get_count(self):
   207         """
   221         """
   212         obj.clear_ordering(True)
   226         obj.clear_ordering(True)
   213         obj.clear_limits()
   227         obj.clear_limits()
   214         obj.select_related = False
   228         obj.select_related = False
   215         obj.related_select_cols = []
   229         obj.related_select_cols = []
   216         obj.related_select_fields = []
   230         obj.related_select_fields = []
   217         if obj.distinct and len(obj.select) > 1:
   231         if len(obj.select) > 1:
   218             obj = self.clone(CountQuery, _query=obj, where=self.where_class(),
   232             obj = self.clone(CountQuery, _query=obj, where=self.where_class(),
   219                     distinct=False)
   233                     distinct=False)
   220             obj.select = []
   234             obj.select = []
   221             obj.extra_select = {}
   235             obj.extra_select = SortedDict()
   222         obj.add_count_column()
   236         obj.add_count_column()
   223         data = obj.execute_sql(SINGLE)
   237         data = obj.execute_sql(SINGLE)
   224         if not data:
   238         if not data:
   225             return 0
   239             return 0
   226         number = data[0]
   240         number = data[0]
   249         # This must come after 'select' and 'ordering' -- see docstring of
   263         # This must come after 'select' and 'ordering' -- see docstring of
   250         # get_from_clause() for details.
   264         # get_from_clause() for details.
   251         from_, f_params = self.get_from_clause()
   265         from_, f_params = self.get_from_clause()
   252 
   266 
   253         where, w_params = self.where.as_sql(qn=self.quote_name_unless_alias)
   267         where, w_params = self.where.as_sql(qn=self.quote_name_unless_alias)
   254         params = list(self.extra_select_params)
   268         params = []
       
   269         for val in self.extra_select.itervalues():
       
   270             params.extend(val[1])
   255 
   271 
   256         result = ['SELECT']
   272         result = ['SELECT']
   257         if self.distinct:
   273         if self.distinct:
   258             result.append('DISTINCT')
   274             result.append('DISTINCT')
   259         result.append(', '.join(out_cols + self.ordering_aliases))
   275         result.append(', '.join(out_cols + self.ordering_aliases))
   274 
   290 
   275         if self.group_by:
   291         if self.group_by:
   276             grouping = self.get_grouping()
   292             grouping = self.get_grouping()
   277             result.append('GROUP BY %s' % ', '.join(grouping))
   293             result.append('GROUP BY %s' % ', '.join(grouping))
   278 
   294 
       
   295         if self.having:
       
   296             having, h_params = self.get_having()
       
   297             result.append('HAVING %s' % ', '.join(having))
       
   298             params.extend(h_params)
       
   299 
   279         if ordering:
   300         if ordering:
   280             result.append('ORDER BY %s' % ', '.join(ordering))
   301             result.append('ORDER BY %s' % ', '.join(ordering))
   281 
   302 
   282         # FIXME: Pull this out to make life easier for Oracle et al.
       
   283         if with_limits:
   303         if with_limits:
   284             if self.high_mark:
   304             if self.high_mark is not None:
   285                 result.append('LIMIT %d' % (self.high_mark - self.low_mark))
   305                 result.append('LIMIT %d' % (self.high_mark - self.low_mark))
   286             if self.low_mark:
   306             if self.low_mark:
   287                 if not self.high_mark:
   307                 if self.high_mark is None:
   288                     val = self.connection.ops.no_limit_value()
   308                     val = self.connection.ops.no_limit_value()
   289                     if val:
   309                     if val:
   290                         result.append('LIMIT %d' % val)
   310                         result.append('LIMIT %d' % val)
   291                 result.append('OFFSET %d' % self.low_mark)
   311                 result.append('OFFSET %d' % self.low_mark)
   292 
   312 
   360             else:
   380             else:
   361                 item = deepcopy(col)
   381                 item = deepcopy(col)
   362                 item.relabel_aliases(change_map)
   382                 item.relabel_aliases(change_map)
   363                 self.select.append(item)
   383                 self.select.append(item)
   364         self.select_fields = rhs.select_fields[:]
   384         self.select_fields = rhs.select_fields[:]
   365         self.extra_select = rhs.extra_select.copy()
   385 
   366         self.extra_tables = rhs.extra_tables
   386         if connector == OR:
   367         self.extra_where = rhs.extra_where
   387             # It would be nice to be able to handle this, but the queries don't
   368         self.extra_params = rhs.extra_params
   388             # really make sense (or return consistent value sets). Not worth
       
   389             # the extra complexity when you can write a real query instead.
       
   390             if self.extra_select and rhs.extra_select:
       
   391                 raise ValueError("When merging querysets using 'or', you "
       
   392                         "cannot have extra(select=...) on both sides.")
       
   393             if self.extra_where and rhs.extra_where:
       
   394                 raise ValueError("When merging querysets using 'or', you "
       
   395                         "cannot have extra(where=...) on both sides.")
       
   396         self.extra_select.update(rhs.extra_select)
       
   397         self.extra_tables += rhs.extra_tables
       
   398         self.extra_where += rhs.extra_where
       
   399         self.extra_params += rhs.extra_params
   369 
   400 
   370         # Ordering uses the 'rhs' ordering, unless it has none, in which case
   401         # Ordering uses the 'rhs' ordering, unless it has none, in which case
   371         # the current ordering is used.
   402         # the current ordering is used.
   372         self.order_by = rhs.order_by and rhs.order_by[:] or self.order_by
   403         self.order_by = rhs.order_by and rhs.order_by[:] or self.order_by
   373         self.extra_order_by = rhs.extra_order_by or self.extra_order_by
   404         self.extra_order_by = rhs.extra_order_by or self.extra_order_by
   393         (without the table names) are given unique aliases. This is needed in
   424         (without the table names) are given unique aliases. This is needed in
   394         some cases to avoid ambiguitity with nested queries.
   425         some cases to avoid ambiguitity with nested queries.
   395         """
   426         """
   396         qn = self.quote_name_unless_alias
   427         qn = self.quote_name_unless_alias
   397         qn2 = self.connection.ops.quote_name
   428         qn2 = self.connection.ops.quote_name
   398         result = ['(%s) AS %s' % (col, qn2(alias)) for alias, col in self.extra_select.iteritems()]
   429         result = ['(%s) AS %s' % (col[0], qn2(alias)) for alias, col in self.extra_select.iteritems()]
   399         aliases = set(self.extra_select.keys())
   430         aliases = set(self.extra_select.keys())
   400         if with_aliases:
   431         if with_aliases:
   401             col_aliases = aliases.copy()
   432             col_aliases = aliases.copy()
   402         else:
   433         else:
   403             col_aliases = set()
   434             col_aliases = set()
   437                 col_aliases.add(col)
   468                 col_aliases.add(col)
   438 
   469 
   439         self._select_aliases = aliases
   470         self._select_aliases = aliases
   440         return result
   471         return result
   441 
   472 
   442     def get_default_columns(self, with_aliases=False, col_aliases=None):
   473     def get_default_columns(self, with_aliases=False, col_aliases=None,
       
   474             start_alias=None, opts=None, as_pairs=False):
   443         """
   475         """
   444         Computes the default columns for selecting every field in the base
   476         Computes the default columns for selecting every field in the base
   445         model.
   477         model.
   446 
   478 
   447         Returns a list of strings, quoted appropriately for use in SQL
   479         Returns a list of strings, quoted appropriately for use in SQL
   448         directly, as well as a set of aliases used in the select statement.
   480         directly, as well as a set of aliases used in the select statement (if
       
   481         'as_pairs' is True, returns a list of (alias, col_name) pairs instead
       
   482         of strings as the first component and None as the second component).
   449         """
   483         """
   450         result = []
   484         result = []
   451         table_alias = self.tables[0]
   485         if opts is None:
   452         root_pk = self.model._meta.pk.column
   486             opts = self.model._meta
       
   487         if start_alias:
       
   488             table_alias = start_alias
       
   489         else:
       
   490             table_alias = self.tables[0]
       
   491         root_pk = opts.pk.column
   453         seen = {None: table_alias}
   492         seen = {None: table_alias}
   454         qn = self.quote_name_unless_alias
   493         qn = self.quote_name_unless_alias
   455         qn2 = self.connection.ops.quote_name
   494         qn2 = self.connection.ops.quote_name
   456         aliases = set()
   495         aliases = set()
   457         for field, model in self.model._meta.get_fields_with_model():
   496         for field, model in opts.get_fields_with_model():
   458             try:
   497             try:
   459                 alias = seen[model]
   498                 alias = seen[model]
   460             except KeyError:
   499             except KeyError:
   461                 alias = self.join((table_alias, model._meta.db_table,
   500                 alias = self.join((table_alias, model._meta.db_table,
   462                         root_pk, model._meta.pk.column))
   501                         root_pk, model._meta.pk.column))
   463                 seen[model] = alias
   502                 seen[model] = alias
       
   503             if as_pairs:
       
   504                 result.append((alias, field.column))
       
   505                 continue
   464             if with_aliases and field.column in col_aliases:
   506             if with_aliases and field.column in col_aliases:
   465                 c_alias = 'Col%d' % len(col_aliases)
   507                 c_alias = 'Col%d' % len(col_aliases)
   466                 result.append('%s.%s AS %s' % (qn(alias),
   508                 result.append('%s.%s AS %s' % (qn(alias),
   467                     qn2(field.column), c_alias))
   509                     qn2(field.column), c_alias))
   468                 col_aliases.add(c_alias)
   510                 col_aliases.add(c_alias)
   471                 r = '%s.%s' % (qn(alias), qn2(field.column))
   513                 r = '%s.%s' % (qn(alias), qn2(field.column))
   472                 result.append(r)
   514                 result.append(r)
   473                 aliases.add(r)
   515                 aliases.add(r)
   474                 if with_aliases:
   516                 if with_aliases:
   475                     col_aliases.add(field.column)
   517                     col_aliases.add(field.column)
       
   518         if as_pairs:
       
   519             return result, None
   476         return result, aliases
   520         return result, aliases
   477 
   521 
   478     def get_from_clause(self):
   522     def get_from_clause(self):
   479         """
   523         """
   480         Returns a list of strings that are joined together to go after the
   524         Returns a list of strings that are joined together to go after the
   508                 connector = not first and ', ' or ''
   552                 connector = not first and ', ' or ''
   509                 result.append('%s%s%s' % (connector, qn(name), alias_str))
   553                 result.append('%s%s%s' % (connector, qn(name), alias_str))
   510             first = False
   554             first = False
   511         for t in self.extra_tables:
   555         for t in self.extra_tables:
   512             alias, unused = self.table_alias(t)
   556             alias, unused = self.table_alias(t)
   513             if alias not in self.alias_map:
   557             # Only add the alias if it's not already present (the table_alias()
       
   558             # calls increments the refcount, so an alias refcount of one means
       
   559             # this is the only reference.
       
   560             if alias not in self.alias_map or self.alias_refcount[alias] == 1:
   514                 connector = not first and ', ' or ''
   561                 connector = not first and ', ' or ''
   515                 result.append('%s%s' % (connector, qn(alias)))
   562                 result.append('%s%s' % (connector, qn(alias)))
   516                 first = False
   563                 first = False
   517         return result, []
   564         return result, []
   518 
   565 
   529                 result.append(col.as_sql(qn))
   576                 result.append(col.as_sql(qn))
   530             else:
   577             else:
   531                 result.append(str(col))
   578                 result.append(str(col))
   532         return result
   579         return result
   533 
   580 
       
   581     def get_having(self):
       
   582         """
       
   583         Returns a tuple representing the SQL elements in the "having" clause.
       
   584         By default, the elements of self.having have their as_sql() method
       
   585         called or are returned unchanged (if they don't have an as_sql()
       
   586         method).
       
   587         """
       
   588         result = []
       
   589         params = []
       
   590         for elt in self.having:
       
   591             if hasattr(elt, 'as_sql'):
       
   592                 sql, params = elt.as_sql()
       
   593                 result.append(sql)
       
   594                 params.extend(params)
       
   595             else:
       
   596                 result.append(elt)
       
   597         return result, params
       
   598 
   534     def get_ordering(self):
   599     def get_ordering(self):
   535         """
   600         """
   536         Returns list representing the SQL elements in the "order by" clause.
   601         Returns list representing the SQL elements in the "order by" clause.
   537         Also sets the ordering_aliases attribute on this instance to a list of
   602         Also sets the ordering_aliases attribute on this instance to a list of
   538         extra aliases needed in the select.
   603         extra aliases needed in the select.
   541         so this should be run *before* get_from_clause().
   606         so this should be run *before* get_from_clause().
   542         """
   607         """
   543         if self.extra_order_by:
   608         if self.extra_order_by:
   544             ordering = self.extra_order_by
   609             ordering = self.extra_order_by
   545         elif not self.default_ordering:
   610         elif not self.default_ordering:
   546             ordering = []
   611             ordering = self.order_by
   547         else:
   612         else:
   548             ordering = self.order_by or self.model._meta.ordering
   613             ordering = self.order_by or self.model._meta.ordering
   549         qn = self.quote_name_unless_alias
   614         qn = self.quote_name_unless_alias
   550         qn2 = self.connection.ops.quote_name
   615         qn2 = self.connection.ops.quote_name
   551         distinct = self.distinct
   616         distinct = self.distinct
   585                     if distinct and elt not in select_aliases:
   650                     if distinct and elt not in select_aliases:
   586                         ordering_aliases.append(elt)
   651                         ordering_aliases.append(elt)
   587                     result.append('%s %s' % (elt, order))
   652                     result.append('%s %s' % (elt, order))
   588             else:
   653             else:
   589                 col, order = get_order_dir(field, asc)
   654                 col, order = get_order_dir(field, asc)
   590                 elt = qn(col)
   655                 elt = qn2(col)
   591                 if distinct and elt not in select_aliases:
   656                 if distinct and col not in select_aliases:
   592                     ordering_aliases.append(elt)
   657                     ordering_aliases.append(elt)
   593                 result.append('%s %s' % (elt, order))
   658                 result.append('%s %s' % (elt, order))
   594         self.ordering_aliases = ordering_aliases
   659         self.ordering_aliases = ordering_aliases
   595         return result
   660         return result
   596 
   661 
   603         """
   668         """
   604         name, order = get_order_dir(name, default_order)
   669         name, order = get_order_dir(name, default_order)
   605         pieces = name.split(LOOKUP_SEP)
   670         pieces = name.split(LOOKUP_SEP)
   606         if not alias:
   671         if not alias:
   607             alias = self.get_initial_alias()
   672             alias = self.get_initial_alias()
   608         field, target, opts, joins, last = self.setup_joins(pieces, opts,
   673         field, target, opts, joins, last, extra = self.setup_joins(pieces,
   609                 alias, False)
   674                 opts, alias, False)
   610         alias = joins[-1]
   675         alias = joins[-1]
   611         col = target.column
   676         col = target.column
       
   677         if not field.rel:
       
   678             # To avoid inadvertent trimming of a necessary alias, use the
       
   679             # refcount to show that we are referencing a non-relation field on
       
   680             # the model.
       
   681             self.ref_alias(alias)
       
   682 
       
   683         # Must use left outer joins for nullable fields.
       
   684         self.promote_alias_chain(joins)
   612 
   685 
   613         # If we get to this point and the field is a relation to another model,
   686         # If we get to this point and the field is a relation to another model,
   614         # append the default ordering for that model.
   687         # append the default ordering for that model.
   615         if field.rel and len(joins) > 1 and opts.ordering:
   688         if field.rel and len(joins) > 1 and opts.ordering:
   616             # Firstly, avoid infinite loops.
   689             # Firstly, avoid infinite loops.
   629 
   702 
   630         if alias:
   703         if alias:
   631             # We have to do the same "final join" optimisation as in
   704             # We have to do the same "final join" optimisation as in
   632             # add_filter, since the final column might not otherwise be part of
   705             # add_filter, since the final column might not otherwise be part of
   633             # the select set (so we can't order on it).
   706             # the select set (so we can't order on it).
   634             join = self.alias_map[alias]
   707             while 1:
   635             if col == join[RHS_JOIN_COL]:
   708                 join = self.alias_map[alias]
       
   709                 if col != join[RHS_JOIN_COL]:
       
   710                     break
   636                 self.unref_alias(alias)
   711                 self.unref_alias(alias)
   637                 alias = join[LHS_ALIAS]
   712                 alias = join[LHS_ALIAS]
   638                 col = join[LHS_JOIN_COL]
   713                 col = join[LHS_JOIN_COL]
   639         return [(alias, col, order)]
   714         return [(alias, col, order)]
   640 
   715 
   659         else:
   734         else:
   660             # The first occurence of a table uses the table name directly.
   735             # The first occurence of a table uses the table name directly.
   661             alias = table_name
   736             alias = table_name
   662             self.table_map[alias] = [alias]
   737             self.table_map[alias] = [alias]
   663         self.alias_refcount[alias] = 1
   738         self.alias_refcount[alias] = 1
   664         #self.alias_map[alias] = None
       
   665         self.tables.append(alias)
   739         self.tables.append(alias)
   666         return alias, True
   740         return alias, True
   667 
   741 
   668     def ref_alias(self, alias):
   742     def ref_alias(self, alias):
   669         """ Increases the reference count for this alias. """
   743         """ Increases the reference count for this alias. """
   677         """
   751         """
   678         Promotes the join type of an alias to an outer join if it's possible
   752         Promotes the join type of an alias to an outer join if it's possible
   679         for the join to contain NULL values on the left. If 'unconditional' is
   753         for the join to contain NULL values on the left. If 'unconditional' is
   680         False, the join is only promoted if it is nullable, otherwise it is
   754         False, the join is only promoted if it is nullable, otherwise it is
   681         always promoted.
   755         always promoted.
       
   756 
       
   757         Returns True if the join was promoted.
   682         """
   758         """
   683         if ((unconditional or self.alias_map[alias][NULLABLE]) and
   759         if ((unconditional or self.alias_map[alias][NULLABLE]) and
   684                 self.alias_map[alias] != self.LOUTER):
   760                 self.alias_map[alias][JOIN_TYPE] != self.LOUTER):
   685             data = list(self.alias_map[alias])
   761             data = list(self.alias_map[alias])
   686             data[JOIN_TYPE] = self.LOUTER
   762             data[JOIN_TYPE] = self.LOUTER
   687             self.alias_map[alias] = tuple(data)
   763             self.alias_map[alias] = tuple(data)
       
   764             return True
       
   765         return False
       
   766 
       
   767     def promote_alias_chain(self, chain, must_promote=False):
       
   768         """
       
   769         Walks along a chain of aliases, promoting the first nullable join and
       
   770         any joins following that. If 'must_promote' is True, all the aliases in
       
   771         the chain are promoted.
       
   772         """
       
   773         for alias in chain:
       
   774             if self.promote_alias(alias, must_promote):
       
   775                 must_promote = True
       
   776 
       
   777     def promote_unused_aliases(self, initial_refcounts, used_aliases):
       
   778         """
       
   779         Given a "before" copy of the alias_refcounts dictionary (as
       
   780         'initial_refcounts') and a collection of aliases that may have been
       
   781         changed or created, works out which aliases have been created since
       
   782         then and which ones haven't been used and promotes all of those
       
   783         aliases, plus any children of theirs in the alias tree, to outer joins.
       
   784         """
       
   785         # FIXME: There's some (a lot of!) overlap with the similar OR promotion
       
   786         # in add_filter(). It's not quite identical, but is very similar. So
       
   787         # pulling out the common bits is something for later.
       
   788         considered = {}
       
   789         for alias in self.tables:
       
   790             if alias not in used_aliases:
       
   791                 continue
       
   792             if (alias not in initial_refcounts or
       
   793                     self.alias_refcount[alias] == initial_refcounts[alias]):
       
   794                 parent = self.alias_map[alias][LHS_ALIAS]
       
   795                 must_promote = considered.get(parent, False)
       
   796                 promoted = self.promote_alias(alias, must_promote)
       
   797                 considered[alias] = must_promote or promoted
   688 
   798 
   689     def change_aliases(self, change_map):
   799     def change_aliases(self, change_map):
   690         """
   800         """
   691         Changes the aliases in change_map (which maps old-alias -> new-alias),
   801         Changes the aliases in change_map (which maps old-alias -> new-alias),
   692         relabelling any references to them in select columns and the where
   802         relabelling any references to them in select columns and the where
   696 
   806 
   697         # 1. Update references in "select" and "where".
   807         # 1. Update references in "select" and "where".
   698         self.where.relabel_aliases(change_map)
   808         self.where.relabel_aliases(change_map)
   699         for pos, col in enumerate(self.select):
   809         for pos, col in enumerate(self.select):
   700             if isinstance(col, (list, tuple)):
   810             if isinstance(col, (list, tuple)):
       
   811                 old_alias = col[0]
   701                 self.select[pos] = (change_map.get(old_alias, old_alias), col[1])
   812                 self.select[pos] = (change_map.get(old_alias, old_alias), col[1])
   702             else:
   813             else:
   703                 col.relabel_aliases(change_map)
   814                 col.relabel_aliases(change_map)
   704 
   815 
   705         # 2. Rename the alias in the internal table/alias datastructures.
   816         # 2. Rename the alias in the internal table/alias datastructures.
   747         produce a similar result (a new prefix and relabelled aliases).
   858         produce a similar result (a new prefix and relabelled aliases).
   748 
   859 
   749         The 'exceptions' parameter is a container that holds alias names which
   860         The 'exceptions' parameter is a container that holds alias names which
   750         should not be changed.
   861         should not be changed.
   751         """
   862         """
   752         assert ord(self.alias_prefix) < ord('Z')
   863         current = ord(self.alias_prefix)
   753         self.alias_prefix = chr(ord(self.alias_prefix) + 1)
   864         assert current < ord('Z')
       
   865         prefix = chr(current + 1)
       
   866         self.alias_prefix = prefix
   754         change_map = {}
   867         change_map = {}
   755         prefix = self.alias_prefix
       
   756         for pos, alias in enumerate(self.tables):
   868         for pos, alias in enumerate(self.tables):
   757             if alias in exceptions:
   869             if alias in exceptions:
   758                 continue
   870                 continue
   759             new_alias = '%s%d' % (prefix, pos)
   871             new_alias = '%s%d' % (prefix, pos)
   760             change_map[alias] = new_alias
   872             change_map[alias] = new_alias
   817         else:
   929         else:
   818             lhs_table = lhs
   930             lhs_table = lhs
   819 
   931 
   820         if reuse and always_create and table in self.table_map:
   932         if reuse and always_create and table in self.table_map:
   821             # Convert the 'reuse' to case to be "exclude everything but the
   933             # Convert the 'reuse' to case to be "exclude everything but the
   822             # reusable set for this table".
   934             # reusable set, minus exclusions, for this table".
   823             exclusions = set(self.table_map[table]).difference(reuse)
   935             exclusions = set(self.table_map[table]).difference(reuse).union(set(exclusions))
   824             always_create = False
   936             always_create = False
   825         t_ident = (lhs_table, table, lhs_col, col)
   937         t_ident = (lhs_table, table, lhs_col, col)
   826         if not always_create:
   938         if not always_create:
   827             for alias in self.join_map.get(t_ident, ()):
   939             for alias in self.join_map.get(t_ident, ()):
   828                 if alias not in exclusions:
   940                 if alias not in exclusions:
       
   941                     if lhs_table and not self.alias_refcount[self.alias_map[alias][LHS_ALIAS]]:
       
   942                         # The LHS of this join tuple is no longer part of the
       
   943                         # query, so skip this possibility.
       
   944                         continue
       
   945                     if self.alias_map[alias][LHS_ALIAS] != lhs:
       
   946                         continue
   829                     self.ref_alias(alias)
   947                     self.ref_alias(alias)
   830                     if promote:
   948                     if promote:
   831                         self.promote_alias(alias)
   949                         self.promote_alias(alias)
   832                     return alias
   950                     return alias
   833 
   951 
   849             self.join_map[t_ident] = (alias,)
   967             self.join_map[t_ident] = (alias,)
   850         self.rev_join_map[alias] = t_ident
   968         self.rev_join_map[alias] = t_ident
   851         return alias
   969         return alias
   852 
   970 
   853     def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
   971     def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
   854             used=None, requested=None, restricted=None):
   972             used=None, requested=None, restricted=None, nullable=None,
       
   973             dupe_set=None, avoid_set=None):
   855         """
   974         """
   856         Fill in the information needed for a select_related query. The current
   975         Fill in the information needed for a select_related query. The current
   857         depth is measured as the number of connections away from the root model
   976         depth is measured as the number of connections away from the root model
   858         (for example, cur_depth=1 means we are looking at models with direct
   977         (for example, cur_depth=1 means we are looking at models with direct
   859         connections to the root model).
   978         connections to the root model).
   860         """
   979         """
   861         if not restricted and self.max_depth and cur_depth > self.max_depth:
   980         if not restricted and self.max_depth and cur_depth > self.max_depth:
   862             # We've recursed far enough; bail out.
   981             # We've recursed far enough; bail out.
   863             return
   982             return
       
   983 
   864         if not opts:
   984         if not opts:
   865             opts = self.get_meta()
   985             opts = self.get_meta()
   866             root_alias = self.get_initial_alias()
   986             root_alias = self.get_initial_alias()
   867             self.related_select_cols = []
   987             self.related_select_cols = []
   868             self.related_select_fields = []
   988             self.related_select_fields = []
   869         if not used:
   989         if not used:
   870             used = set()
   990             used = set()
       
   991         if dupe_set is None:
       
   992             dupe_set = set()
       
   993         if avoid_set is None:
       
   994             avoid_set = set()
       
   995         orig_dupe_set = dupe_set
   871 
   996 
   872         # Setup for the case when only particular related fields should be
   997         # Setup for the case when only particular related fields should be
   873         # included in the related selection.
   998         # included in the related selection.
   874         if requested is None and restricted is not False:
   999         if requested is None and restricted is not False:
   875             if isinstance(self.select_related, dict):
  1000             if isinstance(self.select_related, dict):
   877                 restricted = True
  1002                 restricted = True
   878             else:
  1003             else:
   879                 restricted = False
  1004                 restricted = False
   880 
  1005 
   881         for f, model in opts.get_fields_with_model():
  1006         for f, model in opts.get_fields_with_model():
   882             if (not f.rel or (restricted and f.name not in requested) or
  1007             if not select_related_descend(f, restricted, requested):
   883                     (not restricted and f.null) or f.rel.parent_link):
       
   884                 continue
  1008                 continue
       
  1009             # The "avoid" set is aliases we want to avoid just for this
       
  1010             # particular branch of the recursion. They aren't permanently
       
  1011             # forbidden from reuse in the related selection tables (which is
       
  1012             # what "used" specifies).
       
  1013             avoid = avoid_set.copy()
       
  1014             dupe_set = orig_dupe_set.copy()
   885             table = f.rel.to._meta.db_table
  1015             table = f.rel.to._meta.db_table
       
  1016             if nullable or f.null:
       
  1017                 promote = True
       
  1018             else:
       
  1019                 promote = False
   886             if model:
  1020             if model:
   887                 int_opts = opts
  1021                 int_opts = opts
   888                 alias = root_alias
  1022                 alias = root_alias
   889                 for int_model in opts.get_base_chain(model):
  1023                 for int_model in opts.get_base_chain(model):
   890                     lhs_col = int_opts.parents[int_model].column
  1024                     lhs_col = int_opts.parents[int_model].column
       
  1025                     dedupe = lhs_col in opts.duplicate_targets
       
  1026                     if dedupe:
       
  1027                         avoid.update(self.dupe_avoidance.get(id(opts), lhs_col),
       
  1028                                 ())
       
  1029                         dupe_set.add((opts, lhs_col))
   891                     int_opts = int_model._meta
  1030                     int_opts = int_model._meta
   892                     alias = self.join((alias, int_opts.db_table, lhs_col,
  1031                     alias = self.join((alias, int_opts.db_table, lhs_col,
   893                             int_opts.pk.column), exclusions=used,
  1032                             int_opts.pk.column), exclusions=used,
   894                             promote=f.null)
  1033                             promote=promote)
       
  1034                     for (dupe_opts, dupe_col) in dupe_set:
       
  1035                         self.update_dupe_avoidance(dupe_opts, dupe_col, alias)
   895             else:
  1036             else:
   896                 alias = root_alias
  1037                 alias = root_alias
       
  1038 
       
  1039             dedupe = f.column in opts.duplicate_targets
       
  1040             if dupe_set or dedupe:
       
  1041                 avoid.update(self.dupe_avoidance.get((id(opts), f.column), ()))
       
  1042                 if dedupe:
       
  1043                     dupe_set.add((opts, f.column))
       
  1044 
   897             alias = self.join((alias, table, f.column,
  1045             alias = self.join((alias, table, f.column,
   898                     f.rel.get_related_field().column), exclusions=used,
  1046                     f.rel.get_related_field().column),
   899                     promote=f.null)
  1047                     exclusions=used.union(avoid), promote=promote)
   900             used.add(alias)
  1048             used.add(alias)
   901             self.related_select_cols.extend([(alias, f2.column)
  1049             self.related_select_cols.extend(self.get_default_columns(
   902                     for f2 in f.rel.to._meta.fields])
  1050                 start_alias=alias, opts=f.rel.to._meta, as_pairs=True)[0])
   903             self.related_select_fields.extend(f.rel.to._meta.fields)
  1051             self.related_select_fields.extend(f.rel.to._meta.fields)
   904             if restricted:
  1052             if restricted:
   905                 next = requested.get(f.name, {})
  1053                 next = requested.get(f.name, {})
   906             else:
  1054             else:
   907                 next = False
  1055                 next = False
       
  1056             if f.null is not None:
       
  1057                 new_nullable = f.null
       
  1058             else:
       
  1059                 new_nullable = None
       
  1060             for dupe_opts, dupe_col in dupe_set:
       
  1061                 self.update_dupe_avoidance(dupe_opts, dupe_col, alias)
   908             self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1,
  1062             self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1,
   909                     used, next, restricted)
  1063                     used, next, restricted, new_nullable, dupe_set, avoid)
   910 
  1064 
   911     def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
  1065     def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
   912             can_reuse=None):
  1066             can_reuse=None, process_extras=True):
   913         """
  1067         """
   914         Add a single filter to the query. The 'filter_expr' is a pair:
  1068         Add a single filter to the query. The 'filter_expr' is a pair:
   915         (filter_string, value). E.g. ('name__contains', 'fred')
  1069         (filter_string, value). E.g. ('name__contains', 'fred')
   916 
  1070 
   917         If 'negate' is True, this is an exclude() filter. It's important to
  1071         If 'negate' is True, this is an exclude() filter. It's important to
   927         If 'can_reuse' is a set, we are processing a component of a
  1081         If 'can_reuse' is a set, we are processing a component of a
   928         multi-component filter (e.g. filter(Q1, Q2)). In this case, 'can_reuse'
  1082         multi-component filter (e.g. filter(Q1, Q2)). In this case, 'can_reuse'
   929         will be a set of table aliases that can be reused in this filter, even
  1083         will be a set of table aliases that can be reused in this filter, even
   930         if we would otherwise force the creation of new aliases for a join
  1084         if we would otherwise force the creation of new aliases for a join
   931         (needed for nested Q-filters). The set is updated by this method.
  1085         (needed for nested Q-filters). The set is updated by this method.
       
  1086 
       
  1087         If 'process_extras' is set, any extra filters returned from the table
       
  1088         joining process will be processed. This parameter is set to False
       
  1089         during the processing of extra filters to avoid infinite recursion.
   932         """
  1090         """
   933         arg, value = filter_expr
  1091         arg, value = filter_expr
   934         parts = arg.split(LOOKUP_SEP)
  1092         parts = arg.split(LOOKUP_SEP)
   935         if not parts:
  1093         if not parts:
   936             raise FieldError("Cannot parse keyword query %r" % arg)
  1094             raise FieldError("Cannot parse keyword query %r" % arg)
   946         if value is None:
  1104         if value is None:
   947             if lookup_type != 'exact':
  1105             if lookup_type != 'exact':
   948                 raise ValueError("Cannot use None as a query value")
  1106                 raise ValueError("Cannot use None as a query value")
   949             lookup_type = 'isnull'
  1107             lookup_type = 'isnull'
   950             value = True
  1108             value = True
       
  1109         elif (value == '' and lookup_type == 'exact' and
       
  1110               connection.features.interprets_empty_strings_as_nulls):
       
  1111             lookup_type = 'isnull'
       
  1112             value = True
   951         elif callable(value):
  1113         elif callable(value):
   952             value = value()
  1114             value = value()
   953 
  1115 
   954         opts = self.get_meta()
  1116         opts = self.get_meta()
   955         alias = self.get_initial_alias()
  1117         alias = self.get_initial_alias()
   956         allow_many = trim or not negate
  1118         allow_many = trim or not negate
   957 
  1119 
   958         try:
  1120         try:
   959             field, target, opts, join_list, last = self.setup_joins(parts, opts,
  1121             field, target, opts, join_list, last, extra_filters = self.setup_joins(
   960                     alias, True, allow_many, can_reuse=can_reuse)
  1122                     parts, opts, alias, True, allow_many, can_reuse=can_reuse,
       
  1123                     negate=negate, process_extras=process_extras)
   961         except MultiJoin, e:
  1124         except MultiJoin, e:
   962             self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]))
  1125             self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
       
  1126                     can_reuse)
   963             return
  1127             return
   964         final = len(join_list)
  1128         final = len(join_list)
   965         penultimate = last.pop()
  1129         penultimate = last.pop()
   966         if penultimate == final:
  1130         if penultimate == final:
   967             penultimate = last.pop()
  1131             penultimate = last.pop()
   975                 self.unref_alias(alias)
  1139                 self.unref_alias(alias)
   976         else:
  1140         else:
   977             col = target.column
  1141             col = target.column
   978         alias = join_list[-1]
  1142         alias = join_list[-1]
   979 
  1143 
   980         if final > 1:
  1144         while final > 1:
   981             # An optimization: if the final join is against the same column as
  1145             # An optimization: if the final join is against the same column as
   982             # we are comparing against, we can go back one step in the join
  1146             # we are comparing against, we can go back one step in the join
   983             # chain and compare against the lhs of the join instead. The result
  1147             # chain and compare against the lhs of the join instead (and then
   984             # (potentially) involves one less table join.
  1148             # repeat the optimization). The result, potentially, involves less
       
  1149             # table joins.
   985             join = self.alias_map[alias]
  1150             join = self.alias_map[alias]
   986             if col == join[RHS_JOIN_COL]:
  1151             if col != join[RHS_JOIN_COL]:
   987                 self.unref_alias(alias)
  1152                 break
   988                 alias = join[LHS_ALIAS]
  1153             self.unref_alias(alias)
   989                 col = join[LHS_JOIN_COL]
  1154             alias = join[LHS_ALIAS]
   990                 join_list = join_list[:-1]
  1155             col = join[LHS_JOIN_COL]
   991                 final -= 1
  1156             join_list = join_list[:-1]
   992                 if final == penultimate:
  1157             final -= 1
   993                     penultimate = last.pop()
  1158             if final == penultimate:
       
  1159                 penultimate = last.pop()
   994 
  1160 
   995         if (lookup_type == 'isnull' and value is True and not negate and
  1161         if (lookup_type == 'isnull' and value is True and not negate and
   996                 final > 1):
  1162                 final > 1):
   997             # If the comparison is against NULL, we need to use a left outer
  1163             # If the comparison is against NULL, we need to use a left outer
   998             # join when connecting to the previous model. We make that
  1164             # join when connecting to the previous model. We make that
  1007             # make the new additions (and any existing ones not used in the new
  1173             # make the new additions (and any existing ones not used in the new
  1008             # join list) an outer join.
  1174             # join list) an outer join.
  1009             join_it = iter(join_list)
  1175             join_it = iter(join_list)
  1010             table_it = iter(self.tables)
  1176             table_it = iter(self.tables)
  1011             join_it.next(), table_it.next()
  1177             join_it.next(), table_it.next()
       
  1178             table_promote = False
       
  1179             join_promote = False
  1012             for join in join_it:
  1180             for join in join_it:
  1013                 table = table_it.next()
  1181                 table = table_it.next()
  1014                 if join == table and self.alias_refcount[join] > 1:
  1182                 if join == table and self.alias_refcount[join] > 1:
  1015                     continue
  1183                     continue
  1016                 self.promote_alias(join)
  1184                 join_promote = self.promote_alias(join)
  1017                 if table != join:
  1185                 if table != join:
  1018                     self.promote_alias(table)
  1186                     table_promote = self.promote_alias(table)
  1019                 break
  1187                 break
  1020             for join in join_it:
  1188             self.promote_alias_chain(join_it, join_promote)
  1021                 self.promote_alias(join)
  1189             self.promote_alias_chain(table_it, table_promote)
  1022             for table in table_it:
       
  1023                 # Some of these will have been promoted from the join_list, but
       
  1024                 # that's harmless.
       
  1025                 self.promote_alias(table)
       
  1026 
  1190 
  1027         self.where.add((alias, col, field, lookup_type, value), connector)
  1191         self.where.add((alias, col, field, lookup_type, value), connector)
       
  1192 
  1028         if negate:
  1193         if negate:
  1029             for alias in join_list:
  1194             self.promote_alias_chain(join_list)
  1030                 self.promote_alias(alias)
  1195             if lookup_type != 'isnull':
  1031             if final > 1 and lookup_type != 'isnull':
  1196                 if final > 1:
  1032                 for alias in join_list:
  1197                     for alias in join_list:
  1033                     if self.alias_map[alias] == self.LOUTER:
  1198                         if self.alias_map[alias][JOIN_TYPE] == self.LOUTER:
  1034                         j_col = self.alias_map[alias][RHS_JOIN_COL]
  1199                             j_col = self.alias_map[alias][RHS_JOIN_COL]
  1035                         entry = Node([(alias, j_col, None, 'isnull', True)])
  1200                             entry = self.where_class()
  1036                         entry.negate()
  1201                             entry.add((alias, j_col, None, 'isnull', True), AND)
  1037                         self.where.add(entry, AND)
  1202                             entry.negate()
  1038                         break
  1203                             self.where.add(entry, AND)
       
  1204                             break
       
  1205                 elif not (lookup_type == 'in' and not value) and field.null:
       
  1206                     # Leaky abstraction artifact: We have to specifically
       
  1207                     # exclude the "foo__in=[]" case from this handling, because
       
  1208                     # it's short-circuited in the Where class.
       
  1209                     entry = self.where_class()
       
  1210                     entry.add((alias, col, None, 'isnull', True), AND)
       
  1211                     entry.negate()
       
  1212                     self.where.add(entry, AND)
       
  1213 
  1039         if can_reuse is not None:
  1214         if can_reuse is not None:
  1040             can_reuse.update(join_list)
  1215             can_reuse.update(join_list)
       
  1216         if process_extras:
       
  1217             for filter in extra_filters:
       
  1218                 self.add_filter(filter, negate=negate, can_reuse=can_reuse,
       
  1219                         process_extras=False)
  1041 
  1220 
  1042     def add_q(self, q_object, used_aliases=None):
  1221     def add_q(self, q_object, used_aliases=None):
  1043         """
  1222         """
  1044         Adds a Q-object to the current filter.
  1223         Adds a Q-object to the current filter.
  1045 
  1224 
  1046         Can also be used to add anything that has an 'add_to_query()' method.
  1225         Can also be used to add anything that has an 'add_to_query()' method.
  1047         """
  1226         """
  1048         if used_aliases is None:
  1227         if used_aliases is None:
  1049             used_aliases = set()
  1228             used_aliases = self.used_aliases
  1050         if hasattr(q_object, 'add_to_query'):
  1229         if hasattr(q_object, 'add_to_query'):
  1051             # Complex custom objects are responsible for adding themselves.
  1230             # Complex custom objects are responsible for adding themselves.
  1052             q_object.add_to_query(self, used_aliases)
  1231             q_object.add_to_query(self, used_aliases)
  1053             return
       
  1054 
       
  1055         if self.where and q_object.connector != AND and len(q_object) > 1:
       
  1056             self.where.start_subtree(AND)
       
  1057             subtree = True
       
  1058         else:
  1232         else:
  1059             subtree = False
  1233             if self.where and q_object.connector != AND and len(q_object) > 1:
  1060         connector = AND
  1234                 self.where.start_subtree(AND)
  1061         for child in q_object.children:
  1235                 subtree = True
  1062             if isinstance(child, Node):
  1236             else:
  1063                 self.where.start_subtree(connector)
  1237                 subtree = False
  1064                 self.add_q(child, used_aliases)
  1238             connector = AND
       
  1239             for child in q_object.children:
       
  1240                 if connector == OR:
       
  1241                     refcounts_before = self.alias_refcount.copy()
       
  1242                 if isinstance(child, Node):
       
  1243                     self.where.start_subtree(connector)
       
  1244                     self.add_q(child, used_aliases)
       
  1245                     self.where.end_subtree()
       
  1246                 else:
       
  1247                     self.add_filter(child, connector, q_object.negated,
       
  1248                             can_reuse=used_aliases)
       
  1249                 if connector == OR:
       
  1250                     # Aliases that were newly added or not used at all need to
       
  1251                     # be promoted to outer joins if they are nullable relations.
       
  1252                     # (they shouldn't turn the whole conditional into the empty
       
  1253                     # set just because they don't match anything).
       
  1254                     self.promote_unused_aliases(refcounts_before, used_aliases)
       
  1255                 connector = q_object.connector
       
  1256             if q_object.negated:
       
  1257                 self.where.negate()
       
  1258             if subtree:
  1065                 self.where.end_subtree()
  1259                 self.where.end_subtree()
  1066             else:
  1260         if self.filter_is_sticky:
  1067                 self.add_filter(child, connector, q_object.negated,
  1261             self.used_aliases = used_aliases
  1068                         can_reuse=used_aliases)
       
  1069             connector = q_object.connector
       
  1070         if q_object.negated:
       
  1071             self.where.negate()
       
  1072         if subtree:
       
  1073             self.where.end_subtree()
       
  1074 
  1262 
  1075     def setup_joins(self, names, opts, alias, dupe_multis, allow_many=True,
  1263     def setup_joins(self, names, opts, alias, dupe_multis, allow_many=True,
  1076             allow_explicit_fk=False, can_reuse=None):
  1264             allow_explicit_fk=False, can_reuse=None, negate=False,
       
  1265             process_extras=True):
  1077         """
  1266         """
  1078         Compute the necessary table joins for the passage through the fields
  1267         Compute the necessary table joins for the passage through the fields
  1079         given in 'names'. 'opts' is the Options class for the current model
  1268         given in 'names'. 'opts' is the Options class for the current model
  1080         (which gives the table we are joining to), 'alias' is the alias for the
  1269         (which gives the table we are joining to), 'alias' is the alias for the
  1081         table we are joining to. If dupe_multis is True, any many-to-many or
  1270         table we are joining to. If dupe_multis is True, any many-to-many or
  1082         many-to-one joins will always create a new alias (necessary for
  1271         many-to-one joins will always create a new alias (necessary for
  1083         disjunctive filters).
  1272         disjunctive filters). If can_reuse is not None, it's a list of aliases
       
  1273         that can be reused in these joins (nothing else can be reused in this
       
  1274         case). Finally, 'negate' is used in the same sense as for add_filter()
       
  1275         -- it indicates an exclude() filter, or something similar. It is only
       
  1276         passed in here so that it can be passed to a field's extra_filter() for
       
  1277         customised behaviour.
  1084 
  1278 
  1085         Returns the final field involved in the join, the target database
  1279         Returns the final field involved in the join, the target database
  1086         column (used for any 'where' constraint), the final 'opts' value and the
  1280         column (used for any 'where' constraint), the final 'opts' value and the
  1087         list of tables joined.
  1281         list of tables joined.
  1088         """
  1282         """
  1089         joins = [alias]
  1283         joins = [alias]
  1090         last = [0]
  1284         last = [0]
       
  1285         dupe_set = set()
       
  1286         exclusions = set()
       
  1287         extra_filters = []
  1091         for pos, name in enumerate(names):
  1288         for pos, name in enumerate(names):
       
  1289             try:
       
  1290                 exclusions.add(int_alias)
       
  1291             except NameError:
       
  1292                 pass
       
  1293             exclusions.add(alias)
  1092             last.append(len(joins))
  1294             last.append(len(joins))
  1093             if name == 'pk':
  1295             if name == 'pk':
  1094                 name = opts.pk.name
  1296                 name = opts.pk.name
  1095 
  1297 
  1096             try:
  1298             try:
  1105                         break
  1307                         break
  1106                 else:
  1308                 else:
  1107                     names = opts.get_all_field_names()
  1309                     names = opts.get_all_field_names()
  1108                     raise FieldError("Cannot resolve keyword %r into field. "
  1310                     raise FieldError("Cannot resolve keyword %r into field. "
  1109                             "Choices are: %s" % (name, ", ".join(names)))
  1311                             "Choices are: %s" % (name, ", ".join(names)))
       
  1312 
  1110             if not allow_many and (m2m or not direct):
  1313             if not allow_many and (m2m or not direct):
  1111                 for alias in joins:
  1314                 for alias in joins:
  1112                     self.unref_alias(alias)
  1315                     self.unref_alias(alias)
  1113                 raise MultiJoin(pos + 1)
  1316                 raise MultiJoin(pos + 1)
  1114             if model:
  1317             if model:
  1115                 # The field lives on a base class of the current model.
  1318                 # The field lives on a base class of the current model.
  1116                 alias_list = []
       
  1117                 for int_model in opts.get_base_chain(model):
  1319                 for int_model in opts.get_base_chain(model):
  1118                     lhs_col = opts.parents[int_model].column
  1320                     lhs_col = opts.parents[int_model].column
       
  1321                     dedupe = lhs_col in opts.duplicate_targets
       
  1322                     if dedupe:
       
  1323                         exclusions.update(self.dupe_avoidance.get(
       
  1324                                 (id(opts), lhs_col), ()))
       
  1325                         dupe_set.add((opts, lhs_col))
  1119                     opts = int_model._meta
  1326                     opts = int_model._meta
  1120                     alias = self.join((alias, opts.db_table, lhs_col,
  1327                     alias = self.join((alias, opts.db_table, lhs_col,
  1121                             opts.pk.column), exclusions=joins)
  1328                             opts.pk.column), exclusions=exclusions)
  1122                     joins.append(alias)
  1329                     joins.append(alias)
       
  1330                     exclusions.add(alias)
       
  1331                     for (dupe_opts, dupe_col) in dupe_set:
       
  1332                         self.update_dupe_avoidance(dupe_opts, dupe_col, alias)
  1123             cached_data = opts._join_cache.get(name)
  1333             cached_data = opts._join_cache.get(name)
  1124             orig_opts = opts
  1334             orig_opts = opts
  1125 
  1335             dupe_col = direct and field.column or field.field.column
       
  1336             dedupe = dupe_col in opts.duplicate_targets
       
  1337             if dupe_set or dedupe:
       
  1338                 if dedupe:
       
  1339                     dupe_set.add((opts, dupe_col))
       
  1340                 exclusions.update(self.dupe_avoidance.get((id(opts), dupe_col),
       
  1341                         ()))
       
  1342 
       
  1343             if process_extras and hasattr(field, 'extra_filters'):
       
  1344                 extra_filters.extend(field.extra_filters(names, pos, negate))
  1126             if direct:
  1345             if direct:
  1127                 if m2m:
  1346                 if m2m:
  1128                     # Many-to-many field defined on the current model.
  1347                     # Many-to-many field defined on the current model.
  1129                     if cached_data:
  1348                     if cached_data:
  1130                         (table1, from_col1, to_col1, table2, from_col2,
  1349                         (table1, from_col1, to_col1, table2, from_col2,
  1141                         orig_opts._join_cache[name] = (table1, from_col1,
  1360                         orig_opts._join_cache[name] = (table1, from_col1,
  1142                                 to_col1, table2, from_col2, to_col2, opts,
  1361                                 to_col1, table2, from_col2, to_col2, opts,
  1143                                 target)
  1362                                 target)
  1144 
  1363 
  1145                     int_alias = self.join((alias, table1, from_col1, to_col1),
  1364                     int_alias = self.join((alias, table1, from_col1, to_col1),
  1146                             dupe_multis, joins, nullable=True, reuse=can_reuse)
  1365                             dupe_multis, exclusions, nullable=True,
  1147                     alias = self.join((int_alias, table2, from_col2, to_col2),
  1366                             reuse=can_reuse)
  1148                             dupe_multis, joins, nullable=True, reuse=can_reuse)
  1367                     if int_alias == table2 and from_col2 == to_col2:
  1149                     joins.extend([int_alias, alias])
  1368                         joins.append(int_alias)
       
  1369                         alias = int_alias
       
  1370                     else:
       
  1371                         alias = self.join(
       
  1372                                 (int_alias, table2, from_col2, to_col2),
       
  1373                                 dupe_multis, exclusions, nullable=True,
       
  1374                                 reuse=can_reuse)
       
  1375                         joins.extend([int_alias, alias])
  1150                 elif field.rel:
  1376                 elif field.rel:
  1151                     # One-to-one or many-to-one field
  1377                     # One-to-one or many-to-one field
  1152                     if cached_data:
  1378                     if cached_data:
  1153                         (table, from_col, to_col, opts, target) = cached_data
  1379                         (table, from_col, to_col, opts, target) = cached_data
  1154                     else:
  1380                     else:
  1159                         to_col = target.column
  1385                         to_col = target.column
  1160                         orig_opts._join_cache[name] = (table, from_col, to_col,
  1386                         orig_opts._join_cache[name] = (table, from_col, to_col,
  1161                                 opts, target)
  1387                                 opts, target)
  1162 
  1388 
  1163                     alias = self.join((alias, table, from_col, to_col),
  1389                     alias = self.join((alias, table, from_col, to_col),
  1164                             exclusions=joins, nullable=field.null)
  1390                             exclusions=exclusions, nullable=field.null)
  1165                     joins.append(alias)
  1391                     joins.append(alias)
  1166                 else:
  1392                 else:
  1167                     # Non-relation fields.
  1393                     # Non-relation fields.
  1168                     target = field
  1394                     target = field
  1169                     break
  1395                     break
  1187                         orig_opts._join_cache[name] = (table1, from_col1,
  1413                         orig_opts._join_cache[name] = (table1, from_col1,
  1188                                 to_col1, table2, from_col2, to_col2, opts,
  1414                                 to_col1, table2, from_col2, to_col2, opts,
  1189                                 target)
  1415                                 target)
  1190 
  1416 
  1191                     int_alias = self.join((alias, table1, from_col1, to_col1),
  1417                     int_alias = self.join((alias, table1, from_col1, to_col1),
  1192                             dupe_multis, joins, nullable=True, reuse=can_reuse)
  1418                             dupe_multis, exclusions, nullable=True,
       
  1419                             reuse=can_reuse)
  1193                     alias = self.join((int_alias, table2, from_col2, to_col2),
  1420                     alias = self.join((int_alias, table2, from_col2, to_col2),
  1194                             dupe_multis, joins, nullable=True, reuse=can_reuse)
  1421                             dupe_multis, exclusions, nullable=True,
       
  1422                             reuse=can_reuse)
  1195                     joins.extend([int_alias, alias])
  1423                     joins.extend([int_alias, alias])
  1196                 else:
  1424                 else:
  1197                     # One-to-many field (ForeignKey defined on the target model)
  1425                     # One-to-many field (ForeignKey defined on the target model)
  1198                     if cached_data:
  1426                     if cached_data:
  1199                         (table, from_col, to_col, opts, target) = cached_data
  1427                         (table, from_col, to_col, opts, target) = cached_data
  1207                         target = opts.pk
  1435                         target = opts.pk
  1208                         orig_opts._join_cache[name] = (table, from_col, to_col,
  1436                         orig_opts._join_cache[name] = (table, from_col, to_col,
  1209                                 opts, target)
  1437                                 opts, target)
  1210 
  1438 
  1211                     alias = self.join((alias, table, from_col, to_col),
  1439                     alias = self.join((alias, table, from_col, to_col),
  1212                             dupe_multis, joins, nullable=True, reuse=can_reuse)
  1440                             dupe_multis, exclusions, nullable=True,
       
  1441                             reuse=can_reuse)
  1213                     joins.append(alias)
  1442                     joins.append(alias)
       
  1443 
       
  1444             for (dupe_opts, dupe_col) in dupe_set:
       
  1445                 try:
       
  1446                     self.update_dupe_avoidance(dupe_opts, dupe_col, int_alias)
       
  1447                 except NameError:
       
  1448                     self.update_dupe_avoidance(dupe_opts, dupe_col, alias)
  1214 
  1449 
  1215         if pos != len(names) - 1:
  1450         if pos != len(names) - 1:
  1216             raise FieldError("Join on field %r not permitted." % name)
  1451             raise FieldError("Join on field %r not permitted." % name)
  1217 
  1452 
  1218         return field, target, opts, joins, last
  1453         return field, target, opts, joins, last, extra_filters
  1219 
  1454 
  1220     def split_exclude(self, filter_expr, prefix):
  1455     def update_dupe_avoidance(self, opts, col, alias):
       
  1456         """
       
  1457         For a column that is one of multiple pointing to the same table, update
       
  1458         the internal data structures to note that this alias shouldn't be used
       
  1459         for those other columns.
       
  1460         """
       
  1461         ident = id(opts)
       
  1462         for name in opts.duplicate_targets[col]:
       
  1463             try:
       
  1464                 self.dupe_avoidance[ident, name].add(alias)
       
  1465             except KeyError:
       
  1466                 self.dupe_avoidance[ident, name] = set([alias])
       
  1467 
       
  1468     def split_exclude(self, filter_expr, prefix, can_reuse):
  1221         """
  1469         """
  1222         When doing an exclude against any kind of N-to-many relation, we need
  1470         When doing an exclude against any kind of N-to-many relation, we need
  1223         to use a subquery. This method constructs the nested query, given the
  1471         to use a subquery. This method constructs the nested query, given the
  1224         original exclude filter (filter_expr) and the portion up to the first
  1472         original exclude filter (filter_expr) and the portion up to the first
  1225         N-to-many relation field.
  1473         N-to-many relation field.
  1226         """
  1474         """
  1227         query = Query(self.model, self.connection)
  1475         query = Query(self.model, self.connection)
  1228         query.add_filter(filter_expr)
  1476         query.add_filter(filter_expr, can_reuse=can_reuse)
       
  1477         query.bump_prefix()
  1229         query.set_start(prefix)
  1478         query.set_start(prefix)
  1230         query.clear_ordering(True)
  1479         query.clear_ordering(True)
  1231         self.add_filter(('%s__in' % prefix, query), negate=True, trim=True)
  1480         self.add_filter(('%s__in' % prefix, query), negate=True, trim=True,
       
  1481                 can_reuse=can_reuse)
  1232 
  1482 
  1233     def set_limits(self, low=None, high=None):
  1483     def set_limits(self, low=None, high=None):
  1234         """
  1484         """
  1235         Adjusts the limits on the rows retrieved. We use low/high to set these,
  1485         Adjusts the limits on the rows retrieved. We use low/high to set these,
  1236         as it makes it more Pythonic to read and write. When the SQL query is
  1486         as it makes it more Pythonic to read and write. When the SQL query is
  1238 
  1488 
  1239         Any limits passed in here are applied relative to the existing
  1489         Any limits passed in here are applied relative to the existing
  1240         constraints. So low is added to the current low value and both will be
  1490         constraints. So low is added to the current low value and both will be
  1241         clamped to any existing high value.
  1491         clamped to any existing high value.
  1242         """
  1492         """
  1243         if high:
  1493         if high is not None:
  1244             if self.high_mark:
  1494             if self.high_mark is not None:
  1245                 self.high_mark = min(self.high_mark, self.low_mark + high)
  1495                 self.high_mark = min(self.high_mark, self.low_mark + high)
  1246             else:
  1496             else:
  1247                 self.high_mark = self.low_mark + high
  1497                 self.high_mark = self.low_mark + high
  1248         if low:
  1498         if low is not None:
  1249             if self.high_mark:
  1499             if self.high_mark is not None:
  1250                 self.low_mark = min(self.high_mark, self.low_mark + low)
  1500                 self.low_mark = min(self.high_mark, self.low_mark + low)
  1251             else:
  1501             else:
  1252                 self.low_mark = self.low_mark + low
  1502                 self.low_mark = self.low_mark + low
  1253 
  1503 
  1254     def clear_limits(self):
  1504     def clear_limits(self):
  1272         """
  1522         """
  1273         alias = self.get_initial_alias()
  1523         alias = self.get_initial_alias()
  1274         opts = self.get_meta()
  1524         opts = self.get_meta()
  1275         try:
  1525         try:
  1276             for name in field_names:
  1526             for name in field_names:
  1277                 field, target, u2, joins, u3 = self.setup_joins(
  1527                 field, target, u2, joins, u3, u4 = self.setup_joins(
  1278                         name.split(LOOKUP_SEP), opts, alias, False, allow_m2m,
  1528                         name.split(LOOKUP_SEP), opts, alias, False, allow_m2m,
  1279                         True)
  1529                         True)
  1280                 final_alias = joins[-1]
  1530                 final_alias = joins[-1]
  1281                 col = target.column
  1531                 col = target.column
  1282                 if len(joins) > 1:
  1532                 if len(joins) > 1:
  1284                     if col == join[RHS_JOIN_COL]:
  1534                     if col == join[RHS_JOIN_COL]:
  1285                         self.unref_alias(final_alias)
  1535                         self.unref_alias(final_alias)
  1286                         final_alias = join[LHS_ALIAS]
  1536                         final_alias = join[LHS_ALIAS]
  1287                         col = join[LHS_JOIN_COL]
  1537                         col = join[LHS_JOIN_COL]
  1288                         joins = joins[:-1]
  1538                         joins = joins[:-1]
  1289                 for join in joins[1:]:
  1539                 self.promote_alias_chain(joins[1:])
  1290                     # Only nullable aliases are promoted, so we don't end up
       
  1291                     # doing unnecessary left outer joins here.
       
  1292                     self.promote_alias(join)
       
  1293                 self.select.append((final_alias, col))
  1540                 self.select.append((final_alias, col))
  1294                 self.select_fields.append(field)
  1541                 self.select_fields.append(field)
  1295         except MultiJoin:
  1542         except MultiJoin:
  1296             raise FieldError("Invalid field name: '%s'" % name)
  1543             raise FieldError("Invalid field name: '%s'" % name)
  1297         except FieldError:
  1544         except FieldError:
  1360             # level.
  1607             # level.
  1361             self.distinct = False
  1608             self.distinct = False
  1362         self.select = [select]
  1609         self.select = [select]
  1363         self.select_fields = [None]
  1610         self.select_fields = [None]
  1364         self.extra_select = {}
  1611         self.extra_select = {}
  1365         self.extra_select_params = ()
       
  1366 
  1612 
  1367     def add_select_related(self, fields):
  1613     def add_select_related(self, fields):
  1368         """
  1614         """
  1369         Sets up the select_related data structure so that we only select
  1615         Sets up the select_related data structure so that we only select
  1370         certain related models (as opposed to all models, when
  1616         certain related models (as opposed to all models, when
  1383         """
  1629         """
  1384         Adds data to the various extra_* attributes for user-created additions
  1630         Adds data to the various extra_* attributes for user-created additions
  1385         to the query.
  1631         to the query.
  1386         """
  1632         """
  1387         if select:
  1633         if select:
  1388             # The extra select might be ordered (because it will be accepting
  1634             # We need to pair any placeholder markers in the 'select'
  1389             # parameters).
  1635             # dictionary with their parameters in 'select_params' so that
  1390             if (isinstance(select, SortedDict) and
  1636             # subsequent updates to the select dictionary also adjust the
  1391                     not isinstance(self.extra_select, SortedDict)):
  1637             # parameters appropriately.
  1392                 self.extra_select = SortedDict(self.extra_select)
  1638             select_pairs = SortedDict()
  1393             self.extra_select.update(select)
  1639             if select_params:
  1394         if select_params:
  1640                 param_iter = iter(select_params)
  1395             self.extra_select_params += tuple(select_params)
  1641             else:
       
  1642                 param_iter = iter([])
       
  1643             for name, entry in select.items():
       
  1644                 entry = force_unicode(entry)
       
  1645                 entry_params = []
       
  1646                 pos = entry.find("%s")
       
  1647                 while pos != -1:
       
  1648                     entry_params.append(param_iter.next())
       
  1649                     pos = entry.find("%s", pos + 2)
       
  1650                 select_pairs[name] = (entry, entry_params)
       
  1651             # This is order preserving, since self.extra_select is a SortedDict.
       
  1652             self.extra_select.update(select_pairs)
  1396         if where:
  1653         if where:
  1397             self.extra_where += tuple(where)
  1654             self.extra_where += tuple(where)
  1398         if params:
  1655         if params:
  1399             self.extra_params += tuple(params)
  1656             self.extra_params += tuple(params)
  1400         if tables:
  1657         if tables:
  1424         as friendly as add_filter(). Mostly useful for querying directly
  1681         as friendly as add_filter(). Mostly useful for querying directly
  1425         against the join table of many-to-many relation in a subquery.
  1682         against the join table of many-to-many relation in a subquery.
  1426         """
  1683         """
  1427         opts = self.model._meta
  1684         opts = self.model._meta
  1428         alias = self.get_initial_alias()
  1685         alias = self.get_initial_alias()
  1429         field, col, opts, joins, last = self.setup_joins(
  1686         field, col, opts, joins, last, extra = self.setup_joins(
  1430                 start.split(LOOKUP_SEP), opts, alias, False)
  1687                 start.split(LOOKUP_SEP), opts, alias, False)
       
  1688         self.unref_alias(alias)
  1431         alias = joins[last[-1]]
  1689         alias = joins[last[-1]]
  1432         self.select = [(alias, self.alias_map[alias][RHS_JOIN_COL])]
  1690         self.select = [(alias, self.alias_map[alias][RHS_JOIN_COL])]
  1433         self.select_fields = [field]
  1691         self.select_fields = [field]
  1434         self.start_meta = opts
  1692         self.start_meta = opts
  1435 
  1693 
  1472                 return cursor.fetchone()[:-len(results.ordering_aliases)]
  1730                 return cursor.fetchone()[:-len(results.ordering_aliases)]
  1473             return cursor.fetchone()
  1731             return cursor.fetchone()
  1474 
  1732 
  1475         # The MULTI case.
  1733         # The MULTI case.
  1476         if self.ordering_aliases:
  1734         if self.ordering_aliases:
  1477             return order_modified_iter(cursor, len(self.ordering_aliases),
  1735             result = order_modified_iter(cursor, len(self.ordering_aliases),
  1478                     self.connection.features.empty_fetchmany_value)
  1736                     self.connection.features.empty_fetchmany_value)
  1479         return iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
  1737         else:
  1480                 self.connection.features.empty_fetchmany_value)
  1738             result = iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
       
  1739                     self.connection.features.empty_fetchmany_value)
       
  1740         if not self.connection.features.can_use_chunked_reads:
       
  1741             # If we are using non-chunked reads, we return the same data
       
  1742             # structure as normally, but ensure it is all read into memory
       
  1743             # before going any further.
       
  1744             return list(result)
       
  1745         return result
  1481 
  1746 
  1482 # Use the backend's custom Query class if it defines one. Otherwise, use the
  1747 # Use the backend's custom Query class if it defines one. Otherwise, use the
  1483 # default.
  1748 # default.
  1484 if connection.features.uses_custom_query_class:
  1749 if connection.features.uses_custom_query_class:
  1485     Query = connection.ops.query_class(Query)
  1750     Query = connection.ops.query_class(Query)
  1512     """
  1777     """
  1513     for rows in iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
  1778     for rows in iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
  1514             sentinel):
  1779             sentinel):
  1515         yield [r[:-trim] for r in rows]
  1780         yield [r[:-trim] for r in rows]
  1516 
  1781 
  1517 def setup_join_cache(sender):
  1782 def setup_join_cache(sender, **kwargs):
  1518     """
  1783     """
  1519     The information needed to join between model fields is something that is
  1784     The information needed to join between model fields is something that is
  1520     invariant over the life of the model, so we cache it in the model's Options
  1785     invariant over the life of the model, so we cache it in the model's Options
  1521     class, rather than recomputing it all the time.
  1786     class, rather than recomputing it all the time.
  1522 
  1787 
  1523     This method initialises the (empty) cache when the model is created.
  1788     This method initialises the (empty) cache when the model is created.
  1524     """
  1789     """
  1525     sender._meta._join_cache = {}
  1790     sender._meta._join_cache = {}
  1526 
  1791 
  1527 dispatcher.connect(setup_join_cache, signal=signals.class_prepared)
  1792 signals.class_prepared.connect(setup_join_cache)
  1528 
  1793