
TQc           @   s  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z m Z m Z m Z m Z d d l m Z m  Z  m! Z! d d l" m# Z# d d l$ m% Z% m& Z& m' Z' m( Z( m) Z) m* Z* d d l+ m, Z, d d g Z- d e. f d     YZ/ d e. f d     YZ0 d d  Z1 d   Z2 e j3 j4 e2  d   Z5 d   Z6 d S(   sW  
Create SQL statements for QuerySets.

The code in here encapsulates all of the SQL construction so that QuerySets
themselves do not have to (and could be backed by things other than SQL
databases). The abstraction barrier only works one way: this module has to know
all about the internals of models in order to get the information it needs.
iN(   t
   SortedDict(   t
   force_text(   t   Node(   t   six(   t   connectionst   DEFAULT_DB_ALIAS(   t   signals(   t
   LOOKUP_SEP(   t   ExpressionNode(   t   FieldDoesNotExist(   t
   aggregates(   t   QUERY_TERMSt	   ORDER_DIRt   SINGLEt   ORDER_PATTERNt   JoinInfo(   t   EmptyResultSett   Emptyt	   MultiJoin(   t   SQLEvaluator(   t	   WhereNodet
   Constraintt   EverythingNodet
   ExtraWheret   ANDt   OR(   t
   FieldErrort   Queryt   RawQueryc           B   sP   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   s    
    A single raw SQL query
    c         C   sR   | p	 d |  _  | |  _ | |  _ d  |  _ d \ |  _ |  _ i  |  _ i  |  _ d  S(   Ni    (    (   i    N(	   t   paramst   sqlt   usingt   Nonet   cursort   low_markt	   high_markt   extra_selectt   aggregate_select(   t   selfR   R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __init__&   s    				c         C   s   t  |  j | d |  j S(   NR   (   R   R   R   (   R&   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   clone2   s    c         C   s   | j  j | |  S(   s   Convert the database-returned value into a type that is consistent
        across database backends.

        By default, this defers to the underlying backend operations, but
        it can be overridden by Query classes for specific backends.
        (   t   opst   convert_values(   R&   t   valuet   fieldt
   connection(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR*   5   s    c         C   sV   |  j  d  k r |  j   n  t |  j j j } g  |  j  j D] } | | d  ^ q< S(   Ni    (   R!   R    t   _execute_queryR   R   t   introspectiont   table_name_convertert   description(   R&   t	   convertert   column_meta(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   get_columns>   s
    c         C   sB   |  j    t |  j j j s/ t |  j  } n	 |  j } t |  S(   N(   R.   R   R   t   featurest   can_use_chunked_readst   listR!   t   iter(   R&   t   result(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __iter__E   s
    
	c         C   s   d |  j  t |  j  S(   Ns   <RawQuery: %r>(   R   t   tupleR   (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __repr__Q   s    c         C   s3   t  |  j j   |  _ |  j j |  j |  j  d  S(   N(   R   R   R!   t   executeR   R   (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR.   T   s    N(   t   __name__t
   __module__t   __doc__R    R'   R(   R*   R4   R:   R<   R.   (    (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   !   s   						c           B   s  e  Z d  Z d Z d Z d Z e Z e Z	 d Z
 e d  Z d   Z d   Z d   Z d	   Z d
   Z d   Z dE dE d  Z d   Z dE dE d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e d  Z  d   Z! d d  Z" e d  Z# d   Z$ d   Z% d   Z& dF d  Z' d    Z( d!   Z) e dG e e e dE d"  Z* d#   Z+ d$   Z, d%   Z- d&   Z. e/ e e dE e0 e d'  Z1 dE e d(  Z2 e0 e dE e e0 d)  Z3 e d*  Z4 d+   Z5 d,   Z6 dE dE d-  Z7 d.   Z8 d/   Z9 d0   Z: d1   Z; d2   Z< e0 d3  Z= d4   Z> e d5  Z? d6   Z@ d7   ZA d8   ZB d9   ZC d:   ZD d;   ZE d<   ZF d=   ZG d>   ZH d?   ZI d@   ZJ dA   ZK eL eK  ZM dB   ZN eL eN  ZO dC   ZP dD   ZQ RS(H   s   
    A single SQL query.
    s
   INNER JOINs   LEFT OUTER JOINt   Tt   SQLCompilerc         C   s  | |  _  i  |  _ i  |  _ i  |  _ i  |  _ t |  _ t |  _ t |  _ g  |  _	 g  |  _
 i  |  _ t   |  _ t |  _ i  |  _ g  |  _ g  |  _ g  |  _ |   |  _ | |  _ d  |  _ |   |  _ g  |  _ d \ |  _ |  _ t |  _ g  |  _ t |  _ t |  _ t |  _  g  |  _! t"   |  _# d  |  _$ d  |  _% d |  _& t"   |  _' d  |  _( d  |  _) d |  _* d |  _+ t   t f |  _, d  S(   Ni    i   (   i    N(    (    (-   t   modelt   alias_refcountt	   alias_mapt	   table_mapt   join_mapt   Truet   default_colst   default_orderingt   standard_orderingt   ordering_aliasest   related_select_fieldst   dupe_avoidancet   sett   used_aliasest   Falset   filter_is_stickyt   included_inherited_modelst   selectt   select_fieldst   tablest   wheret   where_classR    t   group_byt   havingt   order_byR"   R#   t   distinctt   distinct_fieldst   select_for_updatet   select_for_update_nowaitt   select_relatedt   related_select_colsR    R
   t   aggregate_select_maskt   _aggregate_select_cachet	   max_deptht   extrat   extra_select_maskt   _extra_select_cachet   extra_tablest   extra_order_byt   deferred_loading(   R&   RC   RW   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR'   h   sN    																																c         C   s   |  j    \ } } | | S(   s)  
        Returns the query as a string of SQL with the parameter values
        substituted in (use sql_with_params() to see the unsubstituted string).

        Parameter values won't necessarily be quoted correctly, since that is
        done by the database interface at execution time.
        (   t   sql_with_params(   R&   R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __str__   s    c         C   s   |  j  t  j   S(   sw   
        Returns the query as an SQL string and the parameters that will be
        subsituted into the query.
        (   t   get_compilerR   t   as_sql(   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyRk      s    c         C   s&   |  j  d |  } | | t |   <| S(   Nt   memo(   R(   t   id(   R&   Ro   R9   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __deepcopy__   s    c         C   s]   |  j  j   } g  | d <g  | d <g  | d D]! } | d k	 rI | j pL d ^ q. | d <| S(   s#   
        Pickling support.
        RM   Ra   RU   N(   t   __dict__t   copyR    t   name(   R&   t   obj_dictt   f(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __getstate__   s    

3c         C   s]   | d j  } g  | d D]' } | d k	 r9 | j |  p< d ^ q | d <|  j j |  d S(   s%   
        Unpickling support.
        RC   RU   N(   t   _metaR    t	   get_fieldRr   t   update(   R&   Ru   t   optsRt   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   __setstate__   s    9c         C   s   |  S(   N(    (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   prepare   s    c         C   s   | d  k r' | d  k r' t d   n  | r: t | } n  x- |  j j   D] \ } } | j j |  qJ W| j j |  j  |  | |  S(   Ns   Need either using or connection(   R    t
   ValueErrorR   R%   t   itemsR)   t   check_aggregate_supportt   compiler(   R&   R   R-   t   aliast	   aggregate(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyRm      s    c         C   s
   |  j  j S(   s   
        Returns the Options instance (the model._meta) from which to start
        processing. Normally, this is self.model._meta, but it can be changed
        by subclasses.
        (   RC   Rx   (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   get_meta   s    c         K   s2  t    } | p |  j | _ |  j | _ |  j j   | _ |  j j   | _ |  j j   | _ |  j j   | _ |  j | _ |  j	 | _	 |  j
 | _
 |  j j   | _ g  | _ |  j | _ |  j | _ |  j j   | _ |  j | _ |  j | _ t j |  j d | | _ |  j | _ |  j d k r6d | _ n |  j | _ t j |  j d | | _ |  j | _ |  j |  j | _ | _ |  j | _ |  j | _ |  j | _ |  j | _ |  j | _ g  | _  t j |  j! d | | _! |  j" d k r d | _" n |  j" j   | _" d | _# |  j$ | _$ |  j% j   | _% |  j& d k rTd | _& n |  j& j   | _& |  j' d k rd | _' n |  j' j   | _' |  j( | _( |  j) | _) t j |  j* d | | _* |  j+ r|  j, r|  j, j   | _, n t-   | _, t. | _+ | j/ j0 |  t1 | d  r.| j2   n  | S(   s   
        Creates a copy of the current instance. The 'kwargs' parameter can be
        used by clients to update attributes after copying has taken place.
        Ro   t   _setup_queryN(3   R   t	   __class__RC   RD   Rs   RE   RF   RG   RI   RJ   RK   RS   RL   RU   RM   RN   RT   RV   t   deepcopyRW   RX   RY   R    RZ   R[   R"   R#   R\   R]   R^   R_   R`   Ra   R
   Rb   Rc   Rd   Re   Rf   Rg   Rh   Ri   Rj   RR   RP   RO   RQ   Rr   Rz   t   hasattrR   (   R&   t   klassRo   t   kwargst   obj(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR(      sn    					c         C   s   | j  j | |  S(   s   Convert the database-returned value into a type that is consistent
        across database backends.

        By default, this defers to the underlying backend operations, but
        it can be overridden by Query classes for specific backends.
        (   R)   R*   (   R&   R+   R,   R-   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR*   :  s    c         C   s]   | d k r | j r d S| S| j r0 t |  S| j rC t |  S|  j | | j |  Sd S(   s   Resolve the value of aggregates returned by the database to
        consistent (and reasonable) types.

        This is required because of the predisposition of certain backends
        to return Decimal and long types when they are not needed.
        i    N(   R    t
   is_ordinalt   intt   is_computedt   floatR*   R,   (   R&   R+   R   R-   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   resolve_aggregateC  s    		
	
c   
   
   C   s  |  j  s i  S|  j d k	 r d d l m } | |  j  } |  j   } x@ |  j  j   D]/ \ } } | j rW | | j  | <| j  | =qW qW Wy | j	 | |  Wq t
 k
 r t d   | j  D  SXn+ |  } g  |  _ t |  _ i  |  _ |  j   | j t  | j   t | _ t | _ g  | _ g  | _ | j |  j t  } | d k rxg  | j  j   D] } d ^ qc} n  t g  t | j  j   |  D]4 \ \ } } }	 | |  j |	 | d t | f ^ q S(   sV   
        Returns the dictionary with the values of the existing aggregations.
        i(   t   AggregateQueryc         s   s   |  ] } | d  f Vq d  S(   N(   R    (   t   .0R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pys	   <genexpr>t  s   R-   N(   R%   RY   R    t   django.db.models.sql.subqueriesR   RC   R(   R   t
   is_summaryt   add_subqueryR   t   dictRT   RQ   RI   Re   t   remove_inherited_modelst   clear_orderingRH   t   clear_limitsR^   R`   Ra   RM   Rm   t   execute_sqlR   t   zipR   R   (
   R&   R   R   t   queryR   R   R   R9   t   qt   val(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   get_aggregationY  sB    					

				%c         C   s  |  j    } t |  j  d k s< |  j s< |  j r |  j r d d l m } | } | j t	  | j
   | | j  } y | j | d | Wq t k
 r d SXn  | j   | j d |  d } t d | |  j  } |  j d k	 rt | |  j |  j  } n  | S(   sP   
        Performs a COUNT() query using the current filter constraints.
        i   i(   R   R   i    N(   R(   t   lenRT   R%   R\   R]   R   R   R   RH   R   RC   R   R   t   add_count_columnR   R    t   maxR"   R#   t   min(   R&   R   R   R   t   subqueryt   number(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt	   get_count  s"    0

c         C   s   |  j    } | j   | j i d d 6d  d  d  d  d   | j d g  | j t  | j d d  | j d |  } t	 | j
 t   S(   Ni   t   at   highR   (   R(   t   clear_select_clauset	   add_extraR    t   set_extra_maskR   RH   t
   set_limitsRm   t   boolR   R   (   R&   R   R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   has_results  s    
#c         C   sH  |  j  | j  k s t d   |  j   s6 t d   |  j | j k sT t d   |  j | j k sr t d   |  j   i  } t   } | t k } t } x | j	 D] } | j
 | s q n  | j | \ } }	 }
 } } } }	 |
 |  j k } | j | |  } |  j | | | | f | o"| | | |  } | j |  | | | <t } q W| s%t |  j	  } t | j	  } xK | D]C } | | k r~| j
 | r| j |  | j | |  qq~q~W| | B| | @} xK | D]@ } |  j
 j |  s| j
 j |  r|  j | g t  qqWn  | j rrt j | j  } | j |  |  j s|  j j t   t  qn7 |  j r|  j   } | j t   t  n |  j   } |  j j | |  g  |  _ x | j D]x } t | t t f  r|  j j | j | d | d  | d f  qt j |  } | j |  |  j j |  qW| j |  _ | t k r|  j  r| j  rt! d   qn  |  j  j" | j   t   } |  j# d k	 r| j" |  j#  n  | j# d k	 r| j" | j#  n  | r|  j% |  n  |  j& | j& 7_& | j' r#| j' p)|  j' |  _' | j( p>|  j( |  _( d S(	   sN  
        Merge the 'rhs' query into the current one (with any 'rhs' effects
        being applied *after* (that is, "to the right of") anything in the
        current query. 'rhs' is not modified during a call to this function.

        The 'connector' parameter describes how to connect filters from the
        'rhs' query.
        s4   Cannot combine queries on two different base models.s3   Cannot combine queries once a slice has been taken.s6   Cannot combine a unique query with a non-unique query.s6   Cannot combine queries with different distinct fields.i    i   sS   When merging querysets using 'or', you cannot have extra(select=...) on both sides.N()   RC   t   AssertionErrort
   can_filterR\   R]   R   RO   R   RH   RV   RD   RE   t   LOUTERt   gett   joint   addRQ   t   removet   promote_joinsRW   Rs   R   t   relabel_aliasesR   RX   RT   t
   isinstanceR7   R;   t   appendRU   R   Re   R~   Rz   Rf   R    R   Rh   R[   Ri   (   R&   t   rhst	   connectort
   change_mapt   usedt   conjunctiont   firstR   t   tablet   _t	   join_typet   lhst   lhs_colt   colt   promotet	   new_aliast   l_tablest   r_tablest   outer_tablest   wt   itemRf   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   combine  s    					
	"

$				1	c         C   s  |  j  \ } } | s d S|  j j } i  } i t | j g  | j 6} x| D]} | j t  }	 |  j }
 | } x |	 d  D] } |
 } | j |  d } t	 |  r | j }
 n | j
 j }
 |
 j } t	 |  s | | j |  n  t | |
 | j  q} W| j |	 d  \ } } } } | d k r4|
 } n  t	 |  sN t | | |  qN qN W| r>i  } xe t j |  D]T \ } } xE | j j   D]4 \ } } | | k rqn  t | | p| |  qWqsWx= t j |  D], \ } } | | k r| | j |  qqWx t j |  D] \ } } | | | |  qWn xG t j |  D]6 \ } } | | k rz| | j |  qN| | | <qNWx0 | j   D]" } | | k rt   | | <qqWx- t j |  D] \ } } | | | |  qWd S(   s  
        Converts the self.deferred_loading data structure to an alternate data
        structure, describing the field that *will* be loaded. This is used to
        compute the columns to select from the database and also by the
        QuerySet class to work out which fields are being initialised on each
        model. Models that have all their fields included aren't mentioned in
        the result, only those that have field restrictions in place.

        The "target" parameter is the instance that is populated (in place).
        The "callback" is a function that is called whenever a (model, field)
        pair need to be added to "target". It accepts three parameters:
        "target", and the model and list of fields being added for that model.
        Nii    (   Rj   RC   Rx   RO   t   pkt   concrete_modelt   splitR   t   get_field_by_namet   is_reverse_o2ot   relt   toR   t   add_to_dictR    R   t	   iteritemst   get_fields_with_modelRz   t   get_parent_list(   R&   t   targett   callbackt   field_namest   defert	   orig_optst   seent   must_includet
   field_namet   partst	   cur_modelR{   Rt   t	   old_modelt   sourceR,   RC   R   t   worksett   valuest   m(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   deferred_to_data1  s\    			c         C   sQ   | j  j } | | k r( t   | | <n  x" | D] } | | j | j  q/ Wd S(   sr   
        Callback used by deferred_to_columns(). The "target" parameter should
        be a set instance.
        N(   Rx   t   db_tableRO   R   t   column(   R&   R   RC   t   fieldsR   R,   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   deferred_to_columns_cb  s
    c         C   s   |  j  j |  } | rF | rF | d } |  j | c d 7<| t f S| r| d |  j t |  j  d f } | j |  n | } | g |  j  | <d |  j | <|  j j |  | t	 f S(   s	  
        Returns a table alias for the given table_name and whether this is a
        new alias or not.

        If 'create' is true, a new alias is always created. Otherwise, the
        most recently created alias for the table (if one exists) is reused.
        i    i   s   %s%d(
   RF   R   RD   RQ   t   alias_prefixR   RE   R   RV   RH   (   R&   t
   table_namet   createt   currentR   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   table_alias  s    

 c         C   s   |  j  | c d 7<d S(   s/    Increases the reference count for this alias. i   N(   RD   (   R&   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt	   ref_alias  s    i   c         C   s   |  j  | c | 8<d S(   s/    Decreases the reference count for this alias. N(   RD   (   R&   R   t   amount(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   unref_alias  s    c            s  t     x  r j d     j  j d k r@ q n    j  j } | ol   j | j   j k }   j  j   j k } | s   j  j s | r | r   j  j	 d   j  } |   j  < j
     f d     j j   D  q q Wd S(   sM  
        Promotes recursively the join type of given aliases and its children to
        an outer join. If 'unconditional' is False, the join is only promoted if
        it is nullable or the parent join is an outer join.

        Note about join promotion: When promoting any alias, we make sure all
        joins which start from that alias are promoted, too. When adding a join
        in join(), we make sure any join added to already existing LOUTER join
        is generated as LOUTER. This ensures we don't ever have broken join
        chains which contain first a LOUTER join, then an INNER JOIN, that is
        this kind of join should never be generated: a LOUTER b INNER c. The
        reason for avoiding this type of join chain is that the INNER after
        the LOUTER will effectively remove any effect the LOUTER had.
        i    R   c         3   s7   |  ]- }  j  | j   k r |  k r | Vq d  S(   N(   RE   t	   lhs_alias(   R   R   (   R   R&   t   aliases(    s3   ../Django//lib/python/django/db/models/sql/query.pys	   <genexpr>  s    N(   R7   t   popRE   t   rhs_join_colR    R   R   R   t   nullablet   _replacet   extendt   keys(   R&   R   t   unconditionalt   parent_aliast   parent_loutert   already_loutert   data(    (   R&   R   R   s3   ../Django//lib/python/django/db/models/sql/query.pyR     s    	c         C   sP   xI |  j  j   j   D]2 \ } } | | j | d  } |  j | |  q Wd S(   s   
        This method will reset reference counts for aliases so that they match
        the value passed in :param to_counts:.
        i    N(   RD   Rs   R   R   R   (   R&   t	   to_countsR   t   cur_refcountt   unref_amount(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   reset_refcounts  s    "c         C   sZ   xS |  j  D]H } | | k r
 | | k s? |  j | | | k r
 |  j | g  q
 q
 Wd S(   s}  
        Given a "before" copy of the alias_refcounts dictionary (as
        'initial_refcounts') and a collection of aliases that may have been
        changed or created, works out which aliases have been created since
        then and which ones haven't been used and promotes all of those
        aliases, plus any children of theirs in the alias tree, to outer joins.
        N(   RV   RD   R   (   R&   t   initial_refcountsRP   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   promote_unused_aliases  s    c         C   sZ  t  | j    j t  | j     t    k s6 t  |  j j |  |  j j |  x |  j |  j	 pk g  g D]r } xi t
 |  D][ \ } } t | t t f  r | d } | j | |  | d f | | <q | j |  q Wqo Wx |  j g D]r } xi | j   D][ \ } } t | t t f  rS| d } | j | |  | d f | | <q| j |  qWq WxU |  j j   D]D \ } }	 t g  |	 D] }
 | j |
 |
  ^ q }	 |	 |  j | <qxWx t j |  D] \ } } |  j | } | j d |  } |  j | |  j | <|  j | =| |  j | <|  j | =|  j | j } x4 t
 |  D]& \ } } | | k rM| | | <PqMqMWx: t
 |  j  D]) \ } } | | k r| |  j | <PqqWqWx= |  j j   D], \ } } | | k r| | |  j | <qqWx[ t j |  j  D]G \ } } | j } | | k r| j d | |  } | |  j | <qqWd S(   s   
        Changes the aliases in change_map (which maps old-alias -> new-alias),
        relabelling any references to them in select columns and the where
        clause.
        i    i   t	   rhs_aliasR   N(   RO   R   t   intersectionR   R   RW   R   RZ   RT   RY   t	   enumerateR   R7   R;   R   R
   R   RG   R   R   RE   R   RD   RF   R   RV   RS   R   (   R&   R   t   columnst   posR   t	   old_aliast   mappingt   keyt   kR   R   R   t
   alias_datat   table_aliasesR   R  R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   change_aliases  sT    6
#
#+


	c         C   s   t  |  j  } | t  d  k  s' t  t | d  } | |  _ t   } xV t |  j  D]E \ } } | | k rw qY n  d | | f } | | | <| |  j | <qY W|  j |  d S(   s@  
        Changes the alias prefix to the next letter in the alphabet and
        relabels all the aliases. Even tables that previously had no alias will
        get an alias after this call (it's mostly used for nested queries and
        the outer query will already be using the non-aliased table name).

        Subclasses who create their own prefix should override this method to
        produce a similar result (a new prefix and relabelled aliases).

        The 'exceptions' parameter is a container that holds alias names which
        should not be changed.
        t   Zi   s   %s%dN(   t   ordR   R   t   chrR    R
  RV   R  (   R&   t
   exceptionsR   t   prefixR   R  R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   bump_prefix)  s    		
c         C   sN   |  j  r& |  j  d } |  j |  n$ |  j d |  j j j d d f  } | S(   sg   
        Returns the first alias for this query, after increasing its reference
        count.
        i    N(   RV   R   R   R    RC   Rx   R   (   R&   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   get_initial_aliasC  s
    	$c         C   s,   t  g  |  j j   D] } | r d ^ q  S(   s   
        Returns the number of tables in this query with a non-zero reference
        count. Note that after execution, the reference counts are zeroed, so
        tables added in compiler will not be seen by this method.
        i   (   R   RD   R   (   R&   t   count(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   count_active_tablesO  s    c         C   s!  | \ } }	 }
 } | |  j  k r4 |  j  | j } n | } | r | r |	 |  j k r t |  j |	  j |  j t |   } t } n  | |	 |
 | f } | sYx |  j j | d  D] } | | k r | r |  j	 |  j  | j
 r q n  |  j  | j
 | k r	q n  |  j |  | s;| rN|  j  | j |  j k rN|  j | g  n  | Sq Wn  |  j |	 t  \ } } | sd } n: | s| s|  j  | j |  j k r|  j } n	 |  j } t |	 | | | |
 | |  } | |  j  | <| |  j k r|  j | c | f 7<n | f |  j | <| S(   s  
        Returns an alias for the join in 'connection', either reusing an
        existing alias for that join or creating a new one. 'connection' is a
        tuple (lhs, table, lhs_col, col) where 'lhs' is either an existing
        table alias or a table name. The join correspods to the SQL equivalent
        of::

            lhs.lhs_col = table.col

        If 'always_create' is True and 'reuse' is None, a new alias is always
        created, regardless of whether one already exists or not. If
        'always_create' is True and 'reuse' is a set, an alias in 'reuse' that
        matches the connection will be returned, if possible.  If
        'always_create' is False, the first existing alias that matches the
        'connection' is returned, if any. Otherwise a new join is created.

        If 'exclusions' is specified, it is something satisfying the container
        protocol ("foo in exclusions" must work) and specifies a list of
        aliases that should not be returned, even if they satisfy the join.

        If 'promote' is True, the join type for the alias will be LOUTER (if
        the alias previously existed, the join type will be promoted from INNER
        to LOUTER, if necessary).

        If 'outer_if_first' is True and a new join is created, it will have the
        LOUTER join type. This is used when joining certain types of querysets
        and Q-objects together.

        A join is always created as LOUTER if the lhs alias is LOUTER to make
        sure we do not generate chains like a LOUTER b INNER c.

        If 'nullable' is True, the join can potentially involve NULL values and
        is a candidate for promotion (to "left outer") when combining querysets.
        (    N(   RE   R   RF   RO   t
   differencet   unionRQ   RG   R   RD   R   R   R   R   R   R   RH   R    t   INNERR   (   R&   R-   t   always_createt
   exclusionsR   t   outer_if_firstR   t   reuseR   R   R   R   t	   lhs_tablet   t_identR   R   R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   W  s@    $+	%		c         C   s   |  j  j j j } |  j d } i | d 6} xi | j   D][ \ } } | | k r9 | j |  } |  j | | j j | j	 | j j
 j	 f  | | <q9 q9 W| |  _ d S(   s  
        If the model that is the basis for this QuerySet inherits other models,
        we need to ensure that those other models have their tables included in
        the query.

        We do this as a separate step so that subclasses know which
        tables are going to be active in the query, without needing to compute
        all the select columns (this method is called from pre_sql_setup(),
        whereas column determination is a later part, and side-effect, of
        as_sql()).
        i    N(   RC   Rx   R   RV   R    R   t   get_ancestor_linkR   R   R   R   RS   (   R&   R{   t
   root_aliasR   R,   RC   t
   link_field(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   setup_inherited_models  s    &c         C   sC   x3 |  j  j   D]" \ } } | r |  j |  q q Wi  |  _  d S(   s   
        Undoes the effects of setup_inherited_models(). Should be called
        whenever select columns (self.select) are set explicitly.
        N(   RS   R   R   (   R&   R  R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR     s    c         C   sa   xZ | j  D]O } t | t  r5 |  j |  rY t Sq
 | d j t  d |  j k r
 t Sq
 Wt S(   s|   
        Returns whether or not all elements of this q_object need to be put
        together in the HAVING clause.
        i    (	   t   childrenR   R   t   need_force_havingRH   R   R   R
   RQ   (   R&   t   q_objectt   child(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR+    s     c         C   s  | j  } | j j t  } t |  d k r | j |  j k r | d } | } |  j | }	 | sht d | j | | f   qhn t |  d k s | d g  | j D] }
 |
 j ^ q k s |  j	 d k s | rI|  j | | |  j   t  \ } }	 } } } } |  j |	 | | t  \ } } } |  j | t  | d | f } n | d } | j |  }	 | } | j |  | d | d |	 d | d S(	   sA   
        Adds a single aggregate expression to the Query
        i   i    s-   Cannot compute %s('%s'): '%s' is an aggregateiR   R   R   N(   Rx   t   lookupR   R   R   R
   R   Rt   R   RY   R    t   setup_joinsR  RQ   t
   trim_joinsR   RH   Ry   t   add_to_query(   R&   R   RC   R   R   R{   t
   field_listR   R   R   t   iR,   t	   join_listt   lastR   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   add_aggregate  s,    	$
)*!
c   )      C   s  | \ } }	 | j  t  }
 |
 s4 t d |   n  d } t |
  } t |
  d k r|
 d |  j k r| |  j k r|  j } x t |
  D] \ } } y | j j	 |  } Wn t
 k
 r |
 j   } Pn X| d | k  r y | j j } Wqt k
 r|
 j   } PqXq q Wn  t } |	 d
 k rZ| d k rKt d   n  d } t }	 nH t |	  rr|	   }	 n0 t |	 t  rt |	 |  d | }	 |	 j } n  t t j j r| d k r|	 d k rt }	 d } n  x |  j j   D]z \ } } | |
 d	 t j |
  f k r|  j   } | j | | |	 f t   | rL| j!   n  |  j" j | |  d
 SqW|  j#   } |  j$   } | p| } yI |  j% |
 | | t | d t d | d | d | \ } } } } } } Wn7 t& k
 r} |  j' | t j |
 | j(   |  d
 SXt } t } | d k rb|	 t k rb| rbt |  d k rb|  j) |  t } n  | d k ow|	 t k } |  j* | | | | |  \ }  } } | t+ k rt, |  }! t, |  j-  }" t. |!  t. |"  f t }# x |! D] }$ t. |"  }% |# p|  j/ |$ j0 |  j1 k }# |$ |% k r>|  j2 |$ d k r>qn  | pV|  j) |$ g |#  } |% |$ k rz|  j) |% g  } n  PqW|  j) |! |  |  j) |" | p|  n  | s| r| |  f |  j3 k r|  j3 j4 | |  f  n  |  j" j t5 | |  |  | |	 f |  n( |  j6 j t5 | |  |  | |	 f |  | rG|  j) |  | d k rGt |  d k rx | D] }& |  j/ |& j0 |  j1 k rm|  j/ |& j7 }' |' d
 k	 st8  |  j   } | j t5 |& |' d
  d t f t   | j!   |  j6 j | t   PqmqmWn  |  j9 |  rD|  j6 j t5 | |  d
  d t f t   qDqGn  | d
 k	 rc| j: |  n  | rx0 | D]% }( |  j; |( d | d | d t qpWn  d
 S(   s2  
        Add a single filter to the query. The 'filter_expr' is a pair:
        (filter_string, value). E.g. ('name__contains', 'fred')

        If 'negate' is True, this is an exclude() filter. It's important to
        note that this method does not negate anything in the where-clause
        object when inserting the filter constraints. This is because negated
        filters often require multiple calls to add_filter() and the negation
        should only happen once. So the caller is responsible for this (the
        caller will normally be add_q(), so that as an example).

        If 'trim' is True, we automatically trim the final join group (used
        internally when constructing nested queries).

        If 'can_reuse' is a set, we are processing a component of a
        multi-component filter (e.g. filter(Q1, Q2)). In this case, 'can_reuse'
        will be a set of table aliases that can be reused in this filter, even
        if we would otherwise force the creation of new aliases for a join
        (needed for nested Q-filters). The set is updated by this method.

        If 'process_extras' is set, any extra filters returned from the table
        joining process will be processed. This parameter is set to False
        during the processing of extra filters to avoid infinite recursion.
        s   Cannot parse keyword query %rt   exacti   is    Cannot use None as a query valuet   isnullR#  t    i    Nt   allow_explicit_fkt	   can_reuset   negatet   process_extras(<   R   R   R   R   t   query_termsR
   RC   R
  Rx   Ry   R	   R   R   R   t   AttributeErrorRQ   R    R~   RH   t   callableR   R   R   t   contains_aggregateR   R   R5   t!   interprets_empty_strings_as_nullsR   R   RX   R   R   R<  RZ   R   R  R/  R   t   split_excludet   levelR   R0  R   R8   RV   t   nextRE   R   R   RD   RY   R   R   RW   R   R   t   is_nullableRz   t
   add_filter()   R&   t   filter_exprR   R<  t   trimR;  R=  t   force_havingt   argR+   R   t   lookup_typet	   num_partst   lookup_modelt   counterR   t   lookup_fieldt   having_clauseR   R   t   entryR{   t
   allow_manyR,   R   R4  R5  t   extra_filterst   et   table_promotet   join_promotet   nonnull_comparisonR   t   join_itt   table_itR   R   R   t   j_aliast   j_colt   filter(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyRG    s    %				!
!
	1c         C   s  | d k r |  j } n  t | d  r: | j |  |  n|  j r} | j t k r} t |  d k r} |  j j t  t	 } n t
 } t } | j t k r | r |  j |  } n  x | j D] } | t k r |  j j   } n  | r |  j j |  n |  j j |  t | t  r0|  j | | d | n" |  j | | | j d | d | | rh|  j j   n |  j j   | t k r|  j | |  n  | j } q W| j r|  j j   n  | r|  j j   n  |  j r| |  _ n  d S(   s   
        Adds a Q-object to the current filter.

        Can also be used to add anything that has an 'add_to_query()' method.
        R1  i   RJ  R;  N(   R    RP   R   R1  RW   R   R   R   t   start_subtreeRH   RQ   R   R+  R*  RD   Rs   RZ   R   R   t   add_qRG  t   negatedt   end_subtreeR  R<  RR   (   R&   R,  RP   RJ  t   subtreeR   R-  t   refcounts_before(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR_    sB    *			c
   ,      C   s  | g }
 d g } t    } t    } g  } d } xt |  D]\ } } | d k	 re | j |  n  | j |  | j t |
   | d k r | j j } n  y | j |  \ } } } } Wn t	 k
 rUx | j
 D]= } | r | | j k r | j | j  \ } } } } Pq q W| j   t |  j  } t d | d j |  f   n X| r| sj| rx |
 D] } |  j |  qqWt | d   n  | r| j } x| j |  D] } | | k r| j } q| j | j } | | j k } | r;| j |  j j t |  | f d   | j | | f  n  | j } |  j | | j | | j j f d | } |
 j |  | j |  x' | D] \ } } |  j | | |  qWqWn  | j j |  } | } | r| j p| j  j } | | j k } | s| rN| r#| j | | f  n  | j |  j j t |  | f d   n  |	 rt! | d  r| j" | j# | | |   n  | r| r| r| \ } }  }! }" }# }$ } }% n | j$   } | j | j%    d j }  | j&   }! | j' j( j } | j }" | j)   }# | j | j*    d j }$ | j }% | |  |! |" |# |$ | |% f | j | <|  j | | |  |! f | | d t+ d	 | } | |" k r|# |$ k r|
 j |  | } q|  j | |" |# |$ f | | d t+ d	 | } |
 j" | | g  q| j' r| r| \ }& }' }( } }% nU | j' j( j } | j' j,   }% | j }& | j }' |% j }( |& |' |( | |% f | j | <|  j | |& |' |( f d | d |  j- |  } |
 j |  q| }% Pn0| }) | j  } | r| r| \ } }  }! }" }# }$ } }% n | j$   } | j | j*    d j }  | j)   }! |) j. } | j }" | j&   }# | j | j%    d j }$ | j }% | |  |! |" |# |$ | |% f | j | <|  j | | |  |! f | | d t+ d	 | } |  j | |" |# |$ f | | d t+ d	 | } |
 j" | | g  n | r!| \ }& }' }( } }% n | j | j' j/  d }* |) j. } | j }& |* j }' | j }( |) j0 |* j0 k r| j | j' j/  d }% n	 | j }% |& |' |( | |% f | j | <|  j | |& |' |( f | | d t+ d	 | } |
 j |  xB | D]: \ } } | d k r| }+ n | }+ |  j | | |+  qWq= W| t |  d k r| t |  d
 k rt d | | | d f   qt d |   n  | |% | |
 | | f S(   s  
        Compute the necessary table joins for the passage through the fields
        given in 'names'. 'opts' is the Options class for the current model
        (which gives the table we are joining to), 'alias' is the alias for the
        table we are joining to. If dupe_multis is True, any many-to-many or
        many-to-one joins will always create a new alias (necessary for
        disjunctive filters). If can_reuse is not None, it's a list of aliases
        that can be reused in these joins (nothing else can be reused in this
        case). Finally, 'negate' is used in the same sense as for add_filter()
        -- it indicates an exclude() filter, or something similar. It is only
        passed in here so that it can be passed to a field's extra_filter() for
        customized behavior.

        Returns the final field involved in the join, the target database
        column (used for any 'where' constraint), the final 'opts' value and the
        list of tables joined.
        i    R   s5   Cannot resolve keyword %r into field. Choices are: %ss   , i   R!  RT  R   R#  i   sH   Join on field %r not permitted. Did you misspell %r for the lookup type?s   Join on field %r not permitted.N(    (    (1   RO   R    R
  R   R   R   R   Rt   R   R	   R   t   attnamet   get_all_field_namesR7   R%   R   R   R   R   R   t   get_base_chainRx   t   parentsR   t   duplicate_targetsRz   RN   R   Rp   R   t   update_dupe_avoidancet   _join_cacheR,   R   R   RT  t   m2m_db_tablet   m2m_target_field_namet   m2m_column_nameR   R   t   m2m_reverse_namet   m2m_reverse_target_field_nameRH   t   get_related_fieldRF  R{   R   RC   (,   R&   t   namesR{   R   t   dupe_multisRS  R:  R;  R<  R=  t   joinsR5  t   dupe_setR!  RT  t	   int_aliasR  Rt   R,   RC   t   directt   m2mRv   t   proxied_modelt	   int_modelR   t   dedupet	   dupe_optst   dupe_colt   cached_dataR   t   table1t	   from_col1t   to_col1t   table2t	   from_col2t   to_col2R   R   t   from_colt   to_colt
   orig_fieldt   local_fieldt   to_avoid(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR/    s4   						!											!													!c         C   sY  t  |  } | j   } | | k r3 | j   } n  | r | d k r | | } | |  } | } | j   } |  j | d j }	 x' | D] }
 |  j |
  q Wn	 | j }	 | d }
 x | d k rK|  j |
 } |	 | j k s | j |  j k s | r Pn  |  j |
  | j	 }
 | j }	 | j   | d 8} | | k r | j   } q q W|	 |
 | f S(   s  
        Sometimes joins at the end of a multi-table sequence can be trimmed. If
        the final join is against the same column as we are comparing against,
        and is an inner join, we can go back one step in a join chain and
        compare against the LHS of the join instead (and then repeat the
        optimization). The result, potentially, involves fewer table joins.

        The 'target' parameter is the final field being joined to, 'join_list'
        is the full list of join aliases.

        The 'last' list contains offsets into 'join_list', corresponding to
        each component of the filter. Many-to-many relations, for example, add
        two tables to the join list and we want to deal with both tables the
        same way, so 'last' has an entry for the first of the two tables and
        then the table immediately after the second table, in that case.

        The 'trim' parameter forces the final piece of the join list to be
        trimmed before anything. See the documentation of add_filter() for
        details about this.

        The 'nonnull_check' parameter is True when we are using inner joins
        between tables explicitly to exclude NULL entries. In that case, the
        tables shouldn't be trimmed, because the very action of joining to them
        alters the result set.

        Returns the final active column and table alias and the new active
        join_list.
        i   i    i(
   R   R   RE   t   lhs_join_colR   R   R   R   R  R   (   R&   R   R4  R5  RI  t   nonnull_checkt   finalt   penultimateRe   R   R   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR0    s6    

	
!		

c         C   sv   t  |  } xc | j | D]T } y |  j | | f j |  Wq t k
 rm t | g  |  j | | f <q Xq Wd S(   s   
        For a column that is one of multiple pointing to the same table, update
        the internal data structures to note that this alias shouldn't be used
        for those other columns.
        N(   Rp   Rh  RN   R   t   KeyErrorRO   (   R&   R{   R   R   t   identRt   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyRi    s    c   	      C   s  t  |  j  } | j |  | j   | j t  | j |  | j d \ } } | j j	 t
 | | d	  d t f t  |  j d | | f d t d t d | t g  | j j   D] } | r | ^ q  } | d k r
|  j d | t f d t d t d | n  d	 S(
   s  
        When doing an exclude against any kind of N-to-many relation, we need
        to use a subquery. This method constructs the nested query, given the
        original exclude filter (filter_expr) and the portion up to the first
        N-to-many relation field.
        i    R8  s   %s__inR<  RI  R;  i   s
   %s__isnullN(   R   RC   RG  R  R   RH   t	   set_startRT   RW   R   R   R    RQ   R   R   RD   R   (	   R&   RH  R  R;  R   R   R   R  t   active_positions(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyRC  !  s    
("	(c         C   s   | d k	 rM |  j d k	 r: t |  j |  j |  |  _ qM |  j | |  _ n  | d k	 r |  j d k	 r t |  j |  j |  |  _ q |  j | |  _ n  d S(   s  
        Adjusts the limits on the rows retrieved. We use low/high to set these,
        as it makes it more Pythonic to read and write. When the SQL query is
        created, they are converted to the appropriate offset and limit values.

        Any limits passed in here are applied relative to the existing
        constraints. So low is added to the current low value and both will be
        clamped to any existing high value.
        N(   R    R#   R   R"   (   R&   t   lowR   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   D  s    
c         C   s   d \ |  _ |  _ d S(   s-   
        Clears any existing limits.
        i    N(   i    N(   R    R"   R#   (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   Y  s    c         C   s   |  j  o |  j d k S(   s   
        Returns True if adding filters to this instance is still possible.

        Typically, this means no limits or offsets have been put on the results.
        N(   R"   R#   R    (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   _  s    c         C   sB   g  |  _  g  |  _ t |  _ t |  _ |  j d  |  j d  d S(   s8   
        Removes all fields from SELECT clause.
        N(    (    (   RT   RU   RQ   RI   R`   R   t   set_aggregate_mask(   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   g  s    				c         C   s   g  |  _  g  |  _ d S(   s   
        Clears the list of fields to select (but not extra_select columns).
        Some queryset types completely replace any existing list of select
        columns.
        N(   RT   RU   (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   clear_select_fieldsr  s    	c         G   s   | |  _  t |  _ d S(   sY   
        Adds and resolves the given fields to the query's "distinct on" clause.
        N(   R]   RH   R\   (   R&   R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   add_distinct_fields{  s    	c         C   s  |  j    } |  j   } y x | D] } |  j | j t  | | t | t  \ } } } }	 }
 } |	 d } | j } t |	  d k r |  j	 | } | | j
 k r |  j |  | j } | j } |	 d  }	 q n  |  j |	 d  |  j j | | f  |  j j |  q" WWn t k
 r0t d |   nn t k
 rt | k rO  qt | j   t |  j  t |  j   } t d | d j |  f   n X|  j   d S(   s|   
        Adds the given (model) fields to the select set. The field names are
        added in the order specified.
        ii   s   Invalid field name: '%s's5   Cannot resolve keyword %r into field. Choices are: %ss   , N(   R  R   R/  R   R   RQ   RH   R   R   RE   R   R   R   R  R   RT   R   RU   R   R   t   sortedRe  R7   Re   R%   R   R   (   R&   R   t	   allow_m2mR   R{   Rt   R,   R   t   u2Rs  t   u3t   u4t   final_aliasR   R   Rq  (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt
   add_fields  s:    
			)c         G   su   g  } x- | D]% } t  j |  s | j |  q q W| rO t d |   n  | rh |  j j |  n	 t |  _ d S(   sk  
        Adds items from the 'ordering' sequence to the query's "order by"
        clause. These items are either field names (not column names) --
        possibly with a direction prefix ('-' or '?') -- or ordinals,
        corresponding to column positions in the 'select' list.

        If 'ordering' is empty, all ordering is cleared from the query.
        s   Invalid order_by arguments: %sN(   R   t   matchR   R   R[   R   RQ   RJ   (   R&   t   orderingt   errorsR   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   add_ordering  s    	c         C   s(   g  |  _  d |  _ | r$ t |  _ n  d S(   s   
        Removes any ordering settings. If 'force_empty' is True, there will be
        no ordering in the resulting query (not even the model's default).
        N(    (   R[   Ri   RQ   RJ   (   R&   t   force_empty(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR     s    		c         C   s1   g  |  _  x! |  j D] } |  j  j |  q Wd S(   s@  
        Expands the GROUP BY clause required by the query.

        This will usually be the set of all non-aggregate fields in the
        return data. If the database backend supports grouping by the
        primary key, and the query would be equivalent, the optimization
        will be made automatically.
        N(   RY   RT   R   (   R&   t   sel(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   set_group_by  s    		c         C   s>  |  j  sq |  j s- |  j j d d t } qt |  j  d k sU t d |  j   |  j j |  j d  } n |  j j } |  j s |  j j |  j	 d | j d d f  | j j f d t d t } n@ t |  j  d k s t d   |  j j |  j d d t } t |  _  i | d 6|  _ |  j d  d |  _ d S(	   sn   
        Converts the query to do count(...) or count(distinct(pk)) in order to
        get its size.
        t   *R   i   s7   Cannot add count col with multiple cols in 'select': %ri    R\   s4   Cannot add count col with multiple cols in 'select'.N(   R\   RT   t   aggregates_modulet   CountRH   R   R   RC   Rx   R   R    R   R   R   RQ   R
   R  RY   (   R&   R  R{   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR     s"    			3		c         C   sh   i  } x@ | D]8 } | } x) | j  t  D] } | j | i   } q) Wq W| |  _ g  |  _ g  |  _ d S(   s   
        Sets up the select_related data structure so that we only select
        certain related models (as opposed to all models, when
        self.select_related=True).
        N(   R   R   t
   setdefaultR`   Ra   RM   (   R&   R   t
   field_dictR,   t   dt   part(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   add_select_related  s    		c         C   s,  | r t    } | r$ t |  } n t g   } x | j   D]y \ }	 }
 t |
  }
 g  } |
 j d  } x9 | d k r | j t |   |
 j d | d  } qm W|
 | f | |	 <q= W|  j j |  n  | s | r |  j	 j
 t | |  t  n  | r|  j t |  7_ n  | r(| |  _ n  d S(   sn   
        Adds data to the various extra_* attributes for user-created additions
        to the query.
        s   %sii   N(   R    R8   R   R   t   findR   RE  Re   Rz   RW   R   R   R   Rh   R;   Ri   (   R&   RT   t   select_paramsRW   R   RV   R[   t   select_pairst
   param_iterRt   RR  t   entry_paramsR  (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR     s(    	c         C   s   t    t f |  _ d S(   sB   
        Remove any fields from the deferred loading set.
        N(   RO   RH   Rj   (   R&   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   clear_deferred_loading&  s    c         C   sL   |  j  \ } } | r0 | j |  t f |  _  n | j |  t f |  _  d S(   sp  
        Add the given list of model field names to the set of fields to
        exclude from loading from the database when automatic column selection
        is done. The new field names are added to any existing field names that
        are deferred (or removed from any existing field names that are marked
        as the only ones for immediate loading).
        N(   Rj   R  RH   R  RQ   (   R&   R   t   existingR   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   add_deferred_loading,  s    c         C   s   |  j  \ } } t |  } d | k rP | j d  | j |  j j j j  n  | rq | j |  t	 f |  _  n | t	 f |  _  d S(   s  
        Add the given list of model field names to the set of fields to
        retrieve when the SQL is executed ("immediate loading" fields). The
        field names replace any existing immediate loading field names. If
        there are field names already specified for deferred loading, those
        names are removed from the new field_names before storing the new names
        for immediate loading. (That is, immediate loading overrides any
        existing immediate values, but respects existing deferrals.)
        R   N(
   Rj   RO   R   R   RC   Rx   R   Rt   R  RQ   (   R&   R   R  R   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   add_immediate_loading@  s    
c         C   sF   y |  j  SWn4 t k
 rA i  } |  j | |  j  | |  _  | SXd S(   sI  
        If any fields are marked to be deferred, returns a dictionary mapping
        models to a set of names in those fields that will be loaded. If a
        model is not in the returned dictionary, none of it's fields are
        deferred.

        If no fields are marked for deferral, returns an empty dictionary.
        N(   t   _loaded_field_names_cacheR?  R   t   get_loaded_field_names_cb(   R&   t
   collection(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   get_loaded_field_namesX  s    	c         C   s*   t  g  | D] } | j ^ q
  | | <d S(   s>   
        Callback used by get_deferred_field_names().
        N(   RO   Rt   (   R&   R   RC   R   Rv   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR  k  s    c         C   s4   | d k r d |  _ n t |  |  _ d |  _ d S(   sG   Set the mask of aggregates that will actually be returned by the SELECTN(   R    Rb   RO   Rc   (   R&   Rq  (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR  q  s    c         C   s4   | d k r d |  _ n t |  |  _ d |  _ d S(   s   
        Set the mask of extra select items that will be returned by SELECT,
        we don't actually remove them from the Query since they might be used
        later
        N(   R    Rf   RO   Rg   (   R&   Rq  (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   y  s    c         C   s}   |  j  d k	 r |  j  S|  j d k	 rr t g  |  j j   D]' \ } } | |  j k r8 | | f ^ q8  |  _  |  j  S|  j Sd S(   s   The SortedDict of aggregate columns that are not masked, and should
        be used in the SELECT clause.

        This result is cached for optimization purposes.
        N(   Rc   R    Rb   R    R
   R   (   R&   R  t   v(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   _aggregate_select  s    'c         C   s}   |  j  d  k	 r |  j  S|  j d  k	 rr t g  |  j j   D]' \ } } | |  j k r8 | | f ^ q8  |  _  |  j  S|  j Sd  S(   N(   Rg   R    Rf   R    Re   R   (   R&   R  R  (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   _extra_select  s    'c         C   s	  |  j  j } |  j   } |  j | j t  | | t  \ } } } } } } |  j | d j }	 | }
 x | D] } |  j	 |  ql Wxc | d D]W } |  j | } | j |	 k s | j
 |  j k r Pn  |  j	 |
  | j }
 | j }	 q W|
 |	 f g |  _ |  j   d S(   s  
        Sets the table from which to start joining. The start position is
        specified by the related attribute from the base model. This will
        automatically set to the select column to be the column linked from the
        previous table.

        This method is primarily for internal use and the error checking isn't
        as friendly as add_filter(). Mostly useful for querying directly
        against the join table of many-to-many relation in a subquery.
        i   N(   RC   Rx   R  R/  R   R   RQ   RE   R  R   R   R  R  R   RT   R   (   R&   t   startR{   R   R,   R   Rs  R5  Re   t
   select_colt   select_aliast	   join_info(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR    s$    -	c         C   s(   t  t j j r | j r t S| j Sd S(   s%  
        A helper to check if the given field should be treated as nullable.

        Some backends treat '' as null and Django treats such fields as
        nullable for those backends. In such situations field.null can be
        False even if we should treat the field as nullable.
        N(   R   R   R5   RB  t   empty_strings_allowedRH   t   null(   R&   R,   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyRF    s    	N(    (    (R   R>   R?   R@   R  R   R   R   R>  t   base_aggregates_moduleR  R   R   R'   Rl   Rk   Rq   Rw   R|   R}   R    Rm   R   R(   R*   R   R   R   R   R   R   R   RQ   R   R   R   R   R  R  R  R  R  R  R   R)  R   R+  R6  R   RH   RG  R_  R/  R0  Ri  RC  R   R   R   R   R  R  R  R  R   R  R   R  R   R  R  R  R  R  R  R   R  t   propertyR%   R  R$   R  RF  (    (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR   Y   s   >							H				6	#	
	u	S		&				;		T		
		/	3	:		#						'	
		"		 										%t   ASCc         C   s:   t  | } |  d d k r, |  d | d f S|  | d f S(   s  
    Returns the field name and direction for an order specification. For
    example, '-foo' is returned as ('foo', 'DESC').

    The 'default' param is used to indicate which way no prefix (or a '+'
    prefix) should sort. The '-' prefix always sorts the opposite way.
    i    t   -i   (   R   (   R,   t   defaultt   dirn(    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   get_order_dir  s    
c         K   s   i  |  j  _ d S(   s   
    The information needed to join between model fields is something that is
    invariant over the life of the model, so we cache it in the model's Options
    class, rather than recomputing it all the time.

    This method initialises the (empty) cache when the model is created.
    N(   Rx   Rj  (   t   senderR   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   setup_join_cache  s    c         C   s7   | |  k r  |  | j  |  n t | g  |  | <d S(   ss   
    A helper function to add "value" to the set of values for "key", whether or
    not "key" already exists.
    N(   R   RO   (   R  R  R+   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR     s    c         C   s   t  |  d  o |  j j S(   s   
    A little helper to check if the given field is reverse-o2o. The field is
    expected to be some sort of relation field or related object.
    R   (   R   R,   t   unique(   R,   (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyR      s    (7   R@   Rs   t   django.utils.datastructuresR    t   django.utils.encodingR   t   django.utils.treeR   t   django.utilsR   t	   django.dbR   R   t   django.db.modelsR   t   django.db.models.constantsR   t   django.db.models.expressionsR   t   django.db.models.fieldsR	   t   django.db.models.sqlR
   R  t   django.db.models.sql.constantsR   R   R   R   R   t#   django.db.models.sql.datastructuresR   R   R   t    django.db.models.sql.expressionsR   t   django.db.models.sql.whereR   R   R   R   R   R   t   django.core.exceptionsR   t   __all__t   objectR   R   R  R  t   class_preparedt   connectR   R   (    (    (    s3   ../Django//lib/python/django/db/models/sql/query.pyt   <module>   s>   (.8       	
	
