ó
ø¢TQc           @   si  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 d  d l m Z m Z m Z 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 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d „  ƒ  YZ! d e f d „  ƒ  YZ" d „  Z# d S(   iÿÿÿÿ(   t   zip(   t
   FieldError(   t   transaction(   t   truncate_name(   t
   LOOKUP_SEP(   t   select_related_descend(   t   SINGLEt   MULTIt	   ORDER_DIRt   GET_ITERATOR_CHUNK_SIZE(   t   EmptyResultSet(   t   SQLEvaluator(   t   get_order_dirt   Query(   t   DatabaseError(   t   sixt   SQLCompilerc        
   B   sì   e  Z d  „  Z d „  Z d „  Z e e d „ Z d „  Z e d „ Z	 e d d d e e d „ Z d „  Z d „  Z d d	 d d
 „ Z d „  Z d „  Z d „  Z d „  Z d d d d d d d d d d „	 Z d „  Z d „  Z e d „ Z RS(   c         C   s(   | |  _  | |  _ | |  _ i  |  _ d  S(   N(   t   queryt
   connectiont   usingt   quote_cache(   t   selfR   R   R   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   __init__   s    			c         C   s—   |  j  j s7 |  j  j d |  j  j j j d d f ƒ n  |  j  j rm |  j  j rm |  j  j	 rm |  j  j
 ƒ  n  |  j  j r“ |  j  j r“ |  j ƒ  n  d S(   sr  
        Does any necessary class setup immediately prior to producing SQL. This
        is for things that can't necessarily be done in __init__ because we
        might not have all the pieces in place at that time.
        # TODO: after the query has been executed, the altered state should be
        # cleaned. We are not using a clone() of the query here.
        N(   R   t   tablest   joint   Nonet   modelt   _metat   db_tablet   selectt   default_colst   included_inherited_modelst   setup_inherited_modelst   select_relatedt   related_select_colst   fill_related_selections(   R   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   pre_sql_setup   s    +c         C   s‡   | |  j  k r |  j  | S| |  j j k r> | |  j j k sP | |  j j k ra | |  j  | <| S|  j j j | ƒ } | |  j  | <| S(   sÛ   
        A wrapper around connection.ops.quote_name that doesn't quote aliases
        for table names. This avoids problems with some SQL dialects that treat
        quoted strings specially (e.g. PostgreSQL).
        (   R   R   t	   alias_mapt	   table_mapt   extra_selectR   t   opst
   quote_name(   R   t   namet   r(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   quote_name_unless_alias(   s    $c         C   sµ  | r( |  j  j |  j  j k r( d d f S|  j ƒ  |  j  j j ƒ  |  _ |  j | ƒ } |  j ƒ  \ } } |  j	 ƒ  } |  j
 ƒ  \ } } |  j }	 |  j  j j d |	 d |  j ƒ \ }
 } |  j  j j d |	 d |  j ƒ \ } } g  } x. t j |  j  j ƒ D] } | j | d ƒ qù Wd g } |  j  j rH| j |  j j j | ƒ ƒ n  | j d j | |  j  j ƒ ƒ | j d ƒ | j | ƒ | j | ƒ |
 r¶| j d |
 ƒ | j | ƒ n  |  j | ƒ \ } } | r+| ræt d	 ƒ ‚ n  | s|  j j j ƒ  } n  | j d
 d j | ƒ ƒ | j | ƒ n  | rR| j d | ƒ | j | ƒ n  | ru| j d d j | ƒ ƒ n  | r|  j  j d k	 r±| j d |  j  j |  j  j ƒ n  |  j  j r|  j  j d k rþ|  j j j ƒ  } | rþ| j d | ƒ qþn  | j d |  j  j ƒ qn  |  j  j r‰|  j j  j! r‰|  j  j" } | rg|  j j  j# rgt$ d ƒ ‚ n  | j |  j j j% d | ƒ ƒ n  |  j  j& |  j ƒ d j | ƒ t' | ƒ f S(   sÏ   
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.

        If 'with_limits' is False, any limit/offset information is not included
        in the query.
        t    t   qnR   i   t   SELECTs   , t   FROMs   WHERE %ss.   annotate() + distinct(fields) not implemented.s   GROUP BY %ss	   HAVING %ss   ORDER BY %ss   LIMIT %ds	   OFFSET %ds1   NOWAIT is not supported on this database backend.t   nowaitt    (    N((   R   t   low_markt	   high_markR$   t   alias_refcountt   copyt   refcounts_beforet   get_columnst   get_orderingt   get_distinctt   get_from_clauseR,   t   wheret   as_sqlR   t   havingR   t
   itervaluesR'   t   extendt   distinctt   appendR(   t   distinct_sqlR   t   ordering_aliasest   get_groupingt   NotImplementedErrort   force_no_orderingR   t   no_limit_valuet   select_for_updatet   featurest   has_select_for_updatet   select_for_update_nowaitt   has_select_for_update_nowaitR   t   for_update_sqlt   reset_refcountst   tuple(   R   t   with_limitst   with_col_aliasest   out_colst   orderingt   ordering_group_byt   distinct_fieldst   from_t   f_paramsR.   R<   t   w_paramsR>   t   h_paramst   paramst   valt   resultt   groupingt	   gb_paramsR1   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR=   8   sl    

	''	 $"c         C   s`   |  j  j ƒ  } | j d k r= | j d k r= | j t ƒ n  | j ƒ  | j d |  j	 ƒ j
 ƒ  S(   sV  
        Perform the same functionality as the as_sql() method, returning an
        SQL string and parameters. However, the alias prefixes are bumped
        beforehand (in a copy -- the current query isn't changed), and any
        ordering is removed if the query is unsliced.

        Used when nesting this query inside another.
        i    R   N(   R   t   cloneR3   R4   R   t   clear_orderingt   Truet   bump_prefixt   get_compilerR   R=   (   R   t   obj(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   as_nested_sql’   s
    	
c         C   sÒ  |  j  } |  j j j } g  t j |  j j ƒ D]& \ } } d | d | | ƒ f ^ q. } t |  j j j	 ƒ  ƒ } | r‡ | j
 ƒ  } n	 t ƒ  } |  j j rL|  j ƒ  }	 xâ|  j j D]} t | t t f ƒ r÷| \ } }
 |  j j | j } | |	 k r|
 |	 | k rqµ n  d | | ƒ | |
 ƒ f } | rÉ| d | k r‡d t | ƒ } | j d | | f ƒ | j | ƒ | j | ƒ qô| j d | | | d ƒ f ƒ | j | ƒ | j | d ƒ qE| j | ƒ | j | ƒ | j | d ƒ qµ | j | j | |  j ƒ ƒ t | d ƒ rµ | j | j ƒ | j | j ƒ qµ qµ WnA |  j j r|  j | | ƒ \ } } | j | ƒ | j | ƒ n  |  j j j ƒ  } | j g  |  j j j ƒ  D]P \ } } d | j | |  j ƒ | d k	 rþd	 | t  | | ƒ ƒ pd
 f ^ q¸ƒ x³ |  j j! D]¥ \ } } d | | ƒ | | ƒ f } | rš| | k ršd t | ƒ } | j d | | f ƒ | j | ƒ | j | ƒ q| j | ƒ | j | ƒ | j | ƒ qW| |  _" | S(   s‡  
        Returns the list of columns to use in the select statement. If no
        columns have been specified, returns all columns relating to fields in
        the model.

        If 'with_aliases' is true, any column names that are duplicated
        (without the table names) are given unique aliases. This is needed in
        some cases to avoid ambiguity with nested queries.
        s
   (%s) AS %si    s   %s.%si   s   Col%ds   %s AS %st   aliass   %s%ss    AS %sR-   N(#   R,   R   R(   R)   R   t	   iteritemsR   R'   t   sett   keysR6   R   t   deferred_to_columnst
   isinstancet   listRP   R%   t
   table_namet   lenRB   t   addR=   t   hasattrRg   R   t   get_default_columnsR@   t   updatet   max_name_lengtht   aggregate_selectt   itemsR   R   R"   t   _select_aliases(   R   t   with_aliasesR.   t   qn2Rg   t   colR]   t   aliasest   col_aliasest	   only_loadt   columnt   tableR+   t   c_aliast   colst   new_aliasesRt   t	   aggregate(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR8   ¢   sj    
	B	!	g	c      	   C   sf  g  } | d k r$ |  j j j } n  |  j } |  j j j }	 t ƒ  }
 |  j	 ƒ  } | rg i | d 6} n  xò| j
 ƒ  D]ä\ } } | | j k r˜ d } n  | r° | d k	 r° qt n  | r$y | | } Wq4t k
 r | j | ƒ } |  j j | | j j | j | j j j f ƒ } | | | <q4Xn |  j j | } |  j j | j } | | k rl| j | | k rlqt n  | r›| j | | j f ƒ |
 j | ƒ qt n  | r| j | k rd t | ƒ } | j d | | ƒ |	 | j ƒ | f ƒ | j | ƒ |
 j | ƒ qt d | | ƒ |	 | j ƒ f } | j | ƒ |
 j | ƒ | rt | j | j ƒ qt qt W| |
 f S(   sW  
        Computes the default columns for selecting every field in the base
        model. Will sometimes be called to pull in related models (e.g. via
        select_related), in which case "opts" and "start_alias" will be given
        to provide a starting point for the traversal.

        Returns a list of strings, quoted appropriately for use in SQL
        directly, as well as a set of aliases used in the select statement (if
        'as_pairs' is True, returns a list of (alias, col_name) pairs instead
        of strings as the first component and None as the second component).
        s   Col%ds   %s.%s AS %ss   %s.%sN(   R   R   R   R   R,   R   R(   R)   Ri   Rk   t   get_fields_with_modelt   concrete_modelt   KeyErrort   get_ancestor_linkR   R   R~   t   pkR   R%   Rn   RB   Rp   Ro   (   R   Rx   R|   t   start_aliast   optst   as_pairst
   local_onlyR]   R.   Ry   R{   R}   t   seent   fieldR   Rg   t
   link_fieldR   R€   R+   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyRr   ò   sT    			c         C   s¶   |  j  } |  j j j } g  } |  j j j } x‚ |  j j D]t } | j t	 ƒ } |  j
 | | d ƒ \ } } }	 }
 }
 |  j | |	 ƒ \ } }	 | j d | |	 ƒ | | ƒ f ƒ q: W| S(   sÙ   
        Returns a quoted list of fields to use in DISTINCT ON part of the query.

        Note that this method can alter the tables in the query, and thus it
        must be called before get_from_clause().
        s   %s.%sN(   R,   R   R(   R)   R   R   R   RV   t   splitR   t   _setup_joinsR   t   _final_join_removalRB   (   R   R.   Ry   R]   RŠ   R*   t   partsRŽ   Rz   Rg   t   _(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR:   2  s    	$'c      	   C   s¹  |  j  j r |  j  j } n? |  j  j s6 |  j  j } n$ |  j  j pW |  j  j j j pW g  } |  j } |  j j	 j
 } |  j  j } |  j } g  } g  } g  } |  j  j r¸ t d \ }	 }
 n t d \ }	 }
 t ƒ  } xÏ| D]Ç} | d k r	| j |  j j	 j ƒ  ƒ qØ n  t | t ƒ rp| d k  r4|
 } | } n |	 } | j d | | f ƒ | j t | ƒ g  f ƒ qØ n  t | |	 ƒ \ } } | |  j  j k rº| j d | | ƒ | f ƒ qØ n  d | k r_| j d d ƒ \ } } | | f | k rŸd | | ƒ | f } | j | | f ƒ | s,| | k r\| j d | | f ƒ | j | g  f ƒ q\qŸqØ t | ƒ d |  j  j k rCx!|  j | |  j  j j d	 |	 ƒD]Ÿ \ } } } | | f | k rd | | ƒ | | ƒ f } | j | | f ƒ | r| | k r| j | ƒ n  | j d | | f ƒ | j | g  f ƒ qqWqØ | | ƒ } | rq| | k rq| j | ƒ n  | j d | | f ƒ | j |  j  j | ƒ qØ W| |  j  _ | | f S(
   sè  
        Returns a tuple containing a list representing the SQL elements in the
        "order by" clause, and the list of SQL elements that need to be added
        to the GROUP BY clause as a result of the ordering.

        Also sets the ordering_aliases attribute on this instance to a list of
        extra aliases needed in the select.

        Determining the ordering SQL can change the tables we need to include,
        so this should be run *before* get_from_clause().
        t   ASCt   DESCt   ?i    s   %s %st   .i   s   %s.%st   default_order(   R   t   extra_order_byt   default_orderingt   order_byR   R   RT   R,   R   R(   R)   RA   Rw   t   standard_orderingR   Ri   RB   t   random_function_sqlRl   t   intt   strR   Ru   R   Rp   R'   t   find_ordering_nameRD   (   R   RT   R.   Ry   RA   t   select_aliasesR]   t   group_byRD   t   asct   desct   processed_pairsRŽ   t   orderRz   R   t   elt(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR9   F  sv    			
%R•   c      
   C   s:  t  | | ƒ \ } } | j t ƒ } |  j | | | ƒ \ } }	 } }
 } | j rt |
 ƒ d k r| j r| s~ t ƒ  } n  t g  |
 D] } |  j	 j
 | j ^ qˆ ƒ } | | k rÅ t d ƒ ‚ n  | j | ƒ g  } x3 | j D]( } | j |  j | | | | | ƒ ƒ qâ W| S|  j |	 | ƒ \ }	 } | |	 | f g S(   sã   
        Returns the table alias (the name might be ambiguous, the alias will
        not be) and column name for ordering by the given 'name' parameter.
        The 'name' is of the form 'field1__field2__...__fieldN'.
        i   s!   Infinite loop caused by ordering.(   R   R   R   R‘   t   relRo   RT   Ri   RP   R   R%   Rn   R   Rp   R@   R¡   R’   (   R   R*   RŠ   Rg   R™   t   already_seenR§   t   piecesRŽ   Rz   t   joinst   jt
   join_tuplet   resultst   item(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR¡   ›  s"    $$,c         C   sÆ   | s |  j  j ƒ  } n  |  j  j | | | t ƒ \ } } } } } } g  | D]" } |  j  j | d k  rL | ^ qL }	 | d } | j }
 | j s£ |  j  j | ƒ n  |  j  j |	 ƒ | |
 | | | f S(   s‚  
        A helper method for get_ordering and get_distinct. This method will
        call query.setup_joins, handle refcounts and then promote the joins.

        Note that get_ordering and get_distinct must produce same target
        columns on same input, as the prefixes of get_ordering and get_distinct
        must match. Executing SQL where this is not true is an error.
        i   iÿÿÿÿ(	   R   t   get_initial_aliast   setup_joinst   FalseR5   R~   R©   t	   ref_aliast   promote_joins(   R   R«   RŠ   Rg   RŽ   t   targetR¬   R”   R­   t   joins_to_promoteRz   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR‘   ¹  s    	!/
		c         C   s^   | rT xK |  j  j | } | | j k r, Pn  |  j  j | ƒ | j } | j } q	 n  | | f S(   sT  
        A helper method for get_distinct and get_ordering. This method will
        trim extra not-needed joins from the tail of the join chain.

        This is very similar to what is done in trim_joins, but we will
        trim LEFT JOINS here. It would be a good idea to consolidate this
        method and query.trim_joins().
        (   R   R%   t   rhs_join_colt   unref_aliast	   lhs_aliast   lhs_join_col(   R   Rz   Rg   R   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR’   ×  s    		c         C   sÔ  g  } |  j  } |  j j j } t } x|  j j D]} |  j j | sM q1 n  y) |  j j | \ } } } } }	 }
 } Wn t	 k
 rŒ q1 n X| | k r£ d | p¦ d } | rý | rý | j
 d | | | ƒ | | | ƒ | |	 ƒ | | ƒ | |
 ƒ f ƒ n3 | r
d pd } | j
 d | | | ƒ | f ƒ t } q1 Wx |  j j D] } |  j j | ƒ \ } } | |  j j k s|  j j | d k rG| ršd pd } | j
 d | | | ƒ f ƒ t } qGqGW| g  f S(   sÔ  
        Returns a list of strings that are joined together to go after the
        "FROM" part of the query, as well as a list any extra parameters that
        need to be included. Sub-classes, can override this to create a
        from-clause via a "select".

        This should only be called after any SQL construction methods that
        might change the tables we need. This means the select columns,
        ordering and distinct must be done first.
        s    %sR-   s   %s %s%s ON (%s.%s = %s.%s)s   , s   %s%s%si   s   %s%s(   R,   R   R(   R)   Rb   R   R   R5   R%   R†   RB   R³   t   extra_tablest   table_alias(   R   R]   R.   Ry   t   firstRg   R*   t	   join_typet   lhst   lhs_colRz   t   nullablet	   alias_strt	   connectort   tt   unused(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR;   ê  s4    	)	& 
(c         C   s<  |  j  } g  g  } } |  j j d k	 r2|  j j |  j j } t |  j j j j	 ƒ t |  j j ƒ k r° |  j
 j j r° |  j j j j |  j j j j j f g |  j _ g  } n  t ƒ  } |  j j | } xª | D]¢ } t | t t f ƒ rd | | d ƒ | | d ƒ f }	 n7 t | d ƒ r9| j | |  j
 ƒ }	 n d t | ƒ }	 |	 | k rÐ | j |	 ƒ | j |	 ƒ qÐ qÐ W| râ|  j
 j j râxS | D]H \ }
 } |
 | k s±| r“| j |
 ƒ | j | ƒ | j |
 ƒ q“q“Wn  xM |  j j j ƒ  D]6 \ } } d t | ƒ }	 | j |	 ƒ | j | ƒ qõWn  | | f S(   sY   
        Returns a tuple representing the SQL elements in the "group by" clause.
        s   %s.%si    i   R=   s   (%s)N(   R,   R   R£   R   R   R"   Ro   R   R   t   fieldsR   RJ   t   allows_group_by_pkR   Rˆ   R~   Ri   Rl   Rm   RP   Rq   R=   R    RB   Rp   R@   R'   t   values(   R   RU   R.   R]   R[   t   select_colsR   R   Rz   t   sqlR§   t   order_paramsR'   t   extra_params(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyRE     s<    	*0		'i   c
   !      C   s°  | r) |  j  j r) | |  j  j k r) d S| sh |  j  j ƒ  } |  j  j ƒ  } g  |  j  _ g  |  j  _ n  | sz t ƒ  } n  | d k r’ t ƒ  } n  |	 d k rª t ƒ  }	 n  | }
 |  j  j ƒ  } | d k rþ t	 |  j  j
 t ƒ rõ |  j  j
 } t } qþ t } n  xg| j ƒ  D]Y\ } } | p#| j } t | | | | j | ƒ ƒ sJqn  |	 j ƒ  } |
 j ƒ  } | j j j j } | p€| j } | r°| } | } g  } x| j | ƒ D]þ } | j | sÍ| j } q«n  | j | j } | | j k } | r3| j |  j  j j t | ƒ | f d ƒ ƒ | j | | f ƒ n  | j } |  j  j  | | j | | j! j f d | d | ƒ} | j" | ƒ x* | D]" \ } } |  j  j# | | | ƒ qƒWq«Wn | } | j | j k } | sÔ| r$| j |  j  j j t | ƒ | j f d ƒ ƒ | r$| j | | j f ƒ q$n  |  j  j  | | | j | j j$ ƒ  j f d | j% | ƒ d | ƒ} | j | ƒ |  j& d | d | j j j d t ƒ \ } } |  j  j j' | ƒ |  j  j j' | j j j j( ƒ | rð| j | j) i  ƒ } n t } | j p| } x* | D]" \ } } |  j  j# | | | ƒ qW|  j* | j j j | | d | | | | | | ƒ	 qW| r¬g  | j+ ƒ  D]$ } | j, j- r{| j, | j f ^ q{} x| D]ù\ } } t | | | | j | ƒ d t ƒsâq¬n  |	 j ƒ  } |
 j ƒ  } | j j } | } | } g  } | j | j j ƒ }  |  d k	 r¹x|  D]} | j | sb| j } q@n  | j | j } | | j k } | rÈ| j |  j  j j t | ƒ | ƒ d f ƒ | j | | f ƒ n  | j } |  j  j  | | j | | j! j f d | d t d	 | ƒ} | j" | ƒ x* | D]" \ } } |  j  j# | | | ƒ qWq@W| j | j k } | sf| r¹| j |  j  j j t | ƒ | j f d ƒ ƒ | r¶| j | | j f ƒ q¶q¹n  |  j  j  | | | j j$ ƒ  j | j f d | j% | ƒ d t ƒ} | j | ƒ |  j& d | d | j d t d
 t ƒ \ } } |  j  j j' | ƒ |  j  j j' | j j( ƒ | j | j. ƒ  i  ƒ } t } |  j* | j | | d | | | | ƒ q¬Wn  d S(   s  
        Fill in the information needed for a select_related query. The current
        depth is measured as the number of connections away from the root model
        (for example, cur_depth=1 means we are looking at models with direct
        connections to the root model).
        Nt
   exclusionst   promoteR‰   RŠ   R‹   i   t   reverset   reuseRŒ   (    (    (    (    (/   R   t	   max_deptht   get_metaR±   R"   t   related_select_fieldsRi   R   t   get_loaded_field_namesRl   R!   t   dictRb   R³   R„   R   R   t   getR6   R©   t   toR   R   t   nullt   get_base_chaint   parentsR~   t   duplicate_targetsRs   t   dupe_avoidancet   idRp   R   Rˆ   RB   t   update_dupe_avoidancet   get_related_fieldt   unionRr   R@   RÇ   R*   R#   t   get_all_related_objectsRŽ   t   uniquet   related_query_name(!   R   RŠ   t
   root_aliast	   cur_deptht   usedt	   requestedt
   restrictedRÂ   t   dupe_sett	   avoid_sett   orig_dupe_setR}   t   fR   t   field_modelt   avoidR   RÏ   t   int_optsRg   t   alias_chaint	   int_modelRÁ   t   dedupet	   dupe_optst   dupe_colt   columnsR{   t   nextt   new_nullablet   ot   related_fieldst   chain(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR#   D  sö    	%			!
		!.$	!
		.	!	c         C   s#   i  } |  j  j | |  j  j ƒ | S(   sµ   
        Converts the self.deferred_loading data structure to mapping of table
        names to sets of column names which are to be loaded. Returns the
        dictionary.
        (   R   t   deferred_to_datat   deferred_to_columns_cb(   R   Rö   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyRk   ñ  s    c         c   s  t  |  d ƒ } d } t |  j j ƒ } |  j j rX t j |  j ƒ rX t j	 |  j ƒ n  x¬|  j
 t ƒ D]›} x’| D]Š} | rA| d k r,|  j j r¨ |  j j } n |  j j j j } | |  j j } |  j ƒ  } | r,|  j j j j } g  | D]+ } | | k rõ | j | | k rõ | ^ qõ } q,n  |  j | | ƒ } n  | rút |  j j ƒ t |  j j ƒ }	 |	 t |  j j ƒ }
 t | |	  ƒ t g  t |  j j j ƒ  | |	 |
 !ƒ D]- \ \ } } } |  j j | | |  j ƒ ^ q²ƒ t | |
 ƒ } n  | Vqu Wqh Wd S(   sQ   
        Returns an iterator over the results from executing this query.
        t   resolve_columnsN(   Rq   R   t   boolR   Ru   RI   R   t
   is_managedR   t	   set_dirtyt   execute_sqlR   t   select_fieldsR   R   RÇ   RÔ   Rk   R   R~   Rþ   Ro   R'   R   RP   R    Rv   t   resolve_aggregateR   (   R   Rþ   RÇ   t   has_aggregate_selectt   rowst   rowR}   R   Rí   t   aggregate_startt   aggregate_endRg   Rƒ   t   value(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   results_iterû  s0    %"{c            s-  y% |  j  ƒ  \ } } | s$ t ‚ n  Wn+ t k
 rR | t k rK t g  ƒ Sd Sn X|  j j ƒ  ‰  ˆ  j | | ƒ | s| ˆ  S| t k r¹ |  j j	 r¯ ˆ  j
 ƒ  t |  j j	 ƒ  Sˆ  j
 ƒ  S|  j j	 rï t ˆ  t |  j j	 ƒ |  j j j ƒ } n! t ‡  f d †  |  j j j ƒ } |  j j j s)t | ƒ S| S(   sž  
        Run the query against the database and returns the result(s). The
        return value is a single data item if result_type is SINGLE, or an
        iterator over the results if the result_type is MULTI.

        result_type is either MULTI (use fetchmany() to retrieve all rows),
        SINGLE (only retrieve a single row), or None. In this last case, the
        cursor is returned if any query is executed, since it's used by
        subclasses such as InsertQuery). It's possible, however, that no query
        is needed, as the filters describe an empty set. In that case, None is
        returned, to avoid any unnecessary database interaction.
        Nc              s   ˆ  j  t ƒ S(   N(   t	   fetchmanyR	   (    (   t   cursor(    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   <lambda>V  s    (   R=   R
   R   t   iterR   R  t   executeR   R   RD   t   fetchoneRo   t   order_modified_iterRJ   t   empty_fetchmany_valuet   can_use_chunked_readsRm   (   R   t   result_typeRË   R[   R]   (    (   R  s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  0  s0    


N(   t   __name__t
   __module__R   R$   R,   Rb   R³   R=   Rf   R8   R   Rr   R:   R9   R¡   R‘   R’   R;   RE   R#   Rk   R  R   R  (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR      s,   			Z	P?		U			,	.	«	
	5t   SQLInsertCompilerc           B   s&   e  Z d  „  Z d „  Z e d „ Z RS(   c         C   s:   | d  k r | St | d ƒ r2 | j | |  j ƒ Sd Sd  S(   Nt   get_placeholders   %s(   R   Rq   R  R   (   R   RŽ   R\   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   placeholdera  s
    c      
   C   st  |  j  j j } |  j j j } d | | j ƒ g } t |  j j ƒ } | rX |  j j n	 | j	 g } | j
 d d j g  | D] } | | j ƒ ^ qz ƒ ƒ | rg  |  j j D][ } g  | D]H } | j |  j j rä t | | j ƒ n | j | t ƒ d |  j  ƒ^ qº ^ q­ } }	 n@ g  |  j j D] } |  j  j j ƒ  g ^ q"}	 g  g } d  g } t d „  | Dƒ ƒ o‚|  j o‚|  j  j j }
 |
 r¤d g t | ƒ g } n_ g  |	 D]: } g  t | | ƒ D] \ } } |  j | | ƒ ^ qÁ^ q«} |  j  j j | | ƒ } |  j rÀ|  j  j j rÀ| d } d | | j ƒ | | j	 j ƒ f } | j
 d	 d j | d ƒ ƒ |  j  j j ƒ  \ } } | r¤| j
 | | ƒ | | 7} n  d
 j | ƒ t  | ƒ f g S|
 r'| j
 |  j  j j! | t |	 ƒ ƒ ƒ d
 j | ƒ t  g  |	 D] } | D] } | ^ qqƒ f g Sg  t | | ƒ D]5 \ } } d
 j | d	 d j | ƒ g ƒ | f ^ q7Sd  S(   Ns   INSERT INTO %ss   (%s)s   , R   c         s   s   |  ] } t  | d  ƒ Vq d S(   R  N(   Rq   (   t   .0RŽ   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pys	   <genexpr>„  s    s   %si    s   %s.%ss   VALUES (%s)R2   ("   R   R(   R)   R   R   R   R   Rÿ   RÇ   Rˆ   RB   R   R~   t   objst   get_db_prep_savet   rawt   getattrt   attnamet   pre_saveRb   t   pk_default_valueR   t   anyt	   return_idRJ   t   has_bulk_insertRo   R    R  t   modify_insert_paramst   can_return_id_from_insertt   return_insert_idRP   t   bulk_insert_sql(   R   R.   RŠ   R]   t
   has_fieldsRÇ   Rí   Re   R[   RÉ   t   can_bulkt   placeholdersR\   RŽ   t   vRz   t   r_fmtt   r_paramst   pt   vals(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR=   m  sB    6r.		D
%%<c         C   sÐ   | o t  |  j j ƒ d k s% t ‚ | |  _ |  j j ƒ  } x* |  j ƒ  D] \ } } | j | | ƒ qJ W| os | sz d  S|  j j	 j
 rœ |  j j j | ƒ S|  j j j | |  j j j j |  j j j j j ƒ S(   Ni   (   Ro   R   R  t   AssertionErrorR$  R   R  R=   R  RJ   R'  R(   t   fetch_returned_insert_idt   last_insert_idR   R   R   Rˆ   R~   (   R   R$  R  RË   R[   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  ¤  s    %	(   R  R  R  R=   R³   R  (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  `  s   		7t   SQLDeleteCompilerc           B   s   e  Z d  „  Z RS(   c         C   s¤   t  |  j j ƒ d k s$ t d ƒ ‚ |  j } d | |  j j d ƒ g } |  j j j d | d |  j ƒ \ } } | r‹ | j d | ƒ n  d j	 | ƒ t
 | ƒ f S(	   sh   
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.
        i   s)   Can only delete from one table at a time.s   DELETE FROM %si    R.   R   s   WHERE %sR2   (   Ro   R   R   R2  R,   R<   R=   R   RB   R   RP   (   R   R.   R]   R<   R[   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR=   ³  s    		'(   R  R  R=   (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR5  ²  s   t   SQLUpdateCompilerc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sJ  |  j  ƒ  |  j j s  d d f S|  j j d } |  j } d | | ƒ g } | j d ƒ g  g  } } x]|  j j D]O\ } } } t | d ƒ r£ | j | ƒ } n | j | d |  j	 ƒ} t | d ƒ râ | j
 | |  j	 ƒ }	 n d }	 t | d	 ƒ rt | |  j d
 t ƒ} n  | j }
 t | d ƒ rr| j | |  j	 ƒ \ } } | j d | |
 ƒ | f ƒ | j | ƒ qs | d k	 r«| j d | |
 ƒ |	 f ƒ | j | ƒ qs | j d | |
 ƒ ƒ qs W| sÖd d f S| j d j | ƒ ƒ |  j j j d | d |  j	 ƒ \ } } | r-| j d | ƒ n  d j | ƒ t | | ƒ f S(   sh   
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.
        R-   i    s	   UPDATE %st   SETt   prepare_database_saveR   R  s   %st   evaluatet   allow_joinsR=   s   %s = %ss	   %s = NULLs   , R.   s   WHERE %sR2   (    N(    (   R$   R   RÉ   R   R,   RB   Rq   R8  R  R   R  R   R³   R~   R=   R@   R   R   R<   RP   (   R   R   R.   R]   RÉ   t   update_paramsRŽ   R   R\   R  R*   RË   R[   R<   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR=   Â  sB    

		
'c         C   sŠ   t  t |  ƒ j | ƒ } | r' | j p* d } | d k } ~ xG |  j j ƒ  D]6 } | j |  j ƒ j | ƒ } | rL | } t	 } qL qL W| S(   s  
        Execute the specified update. Returns the number of rows affected by
        the primary update query. The "primary update query" is the first
        non-empty query that is executed. Row counts for any subsequent,
        related queries are not available.
        i    N(
   t   superR6  R  t   rowcountR   R   t   get_related_updatesRd   R   R³   (   R   R  R  R  t   is_emptyR   t   aux_rows(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  ï  s    c      
   C   sÎ  t  |  j _ |  j j t ƒ t t |  ƒ j ƒ  |  j j ƒ  } |  j j	 r[ | d k r[ d S|  j j
 d t ƒ } | j ƒ  i  | _ g  | _ | j | j j j j g ƒ | j ƒ  } |  j j	 rÑ | d k rÑ d S| d k oê |  j j j } |  j j ƒ  |  j _ |  j j	 s| r‰g  } xG | j |  j ƒ j t ƒ D]* } | j g  | D] } | d ^ qIƒ q6W|  j j d | f ƒ | |  j _ n |  j j d | f ƒ x( |  j j  d D] } d |  j j! | <q°Wd S(   sœ  
        If the update depends on results from other tables, we need to do some
        munging of the "where" conditions to match the format required for
        (portable) SQL updates. That is done here.

        Further, if we are going to be running multiple updates, we pull out
        the id values to update at this point so that they don't change as a
        result of the progressive updates.
        i   Nt   klassi    t   pk__in("   R³   R   R!   Ra   Rb   R<  R6  R$   t   count_active_tablest   related_updatesR`   R   Rc   t   extraR   t
   add_fieldsR   R   Rˆ   R*   R   RJ   t   update_can_self_selectt   where_classR<   Rd   R   R  R   R@   t
   add_filtert   related_idsR   R5   (   R   t   countR   t   must_pre_selectt   identsR  R+   Rg   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR$     s2    

		"((   R  R  R=   R  R$   (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR6  Á  s   	-	t   SQLAggregateCompilerc           B   s   e  Z d d  „ Z RS(   c         C   s{   | d k r |  j } n  d d j g  |  j j j ƒ  D] } | j | |  j ƒ ^ q4 ƒ |  j j f } |  j j	 } | | f S(   sh   
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.
        s   SELECT %s FROM (%s) subquerys   , N(
   R   R,   R   R   Ru   RÉ   R=   R   t   subqueryt
   sub_params(   R   R.   Rƒ   RË   R[   (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR=   4  s    1N(   R  R  R   R=   (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyRN  3  s   t   SQLDateCompilerc           B   s   e  Z d  „  Z RS(   c   
      c   sÝ   t  |  d ƒ } | r4 d d l m } | ƒ  g } n d d l m } |  j j j } t |  j	 j
 ƒ } xq |  j t ƒ D]` } xW | D]O } | | }	 | r± |  j | | ƒ | }	 n | rÌ | t |	 ƒ ƒ }	 n  |	 Vq‚ Wqu Wd S(   sQ   
        Returns an iterator over the results from executing this query.
        Rþ   iÿÿÿÿ(   t   DateTimeField(   t   typecast_timestampN(   Rq   t   django.db.models.fieldsRR  t   django.db.backends.utilRS  R   RJ   t   needs_datetime_string_castRo   R   R'   R  R   Rþ   R    (
   R   Rþ   RR  RÇ   RS  t   needs_string_castt   offsetR  R  t   date(    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  G  s    
(   R  R  R  (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyRQ  F  s   c         #   sD   x= t  ‡  f d †  | ƒ D]# } g  | D] } | |  ^ q& Vq Wd S(   s+  
    Yields blocks of rows from a cursor. We use this iterator in the special
    case when extra output columns have been added to support ordering
    requirements. We must trim those extra columns before anything else can use
    the results, since they're only needed to make the SQL valid.
    c              s   ˆ  j  t ƒ S(   N(   R  R	   (    (   R  (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  e  s    N(   R  (   R  t   trimt   sentinelR  R+   (    (   R  s6   ../Django//lib/python/django/db/models/sql/compiler.pyR  ^  s    N($   t   django.utils.six.movesR    t   django.core.exceptionsR   t	   django.dbR   RU  R   t   django.db.models.constantsR   t   django.db.models.query_utilsR   t   django.db.models.sql.constantsR   R   R   R	   t#   django.db.models.sql.datastructuresR
   t    django.db.models.sql.expressionsR   t   django.db.models.sql.queryR   R   t   django.db.utilsR   t   django.utilsR   t   objectR   R  R5  R6  RN  RQ  R  (    (    (    s6   ../Django//lib/python/django/db/models/sql/compiler.pyt   <module>   s*   "ÿ ÿ ÿ RRr