ó
ø¢TQc           @   sS  d  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
 Td 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 d d d g 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 S(   sR   
Query subclasses which provide extra functionality beyond simple data retrieval.
iÿÿÿÿ(   t
   FieldError(   t   connections(   t
   LOOKUP_SEP(   t	   DateFieldt   FieldDoesNotExist(   t   *(   t   Date(   t   Query(   t   ANDt
   Constraint(   t
   SortedDict(   t   Promise(   t
   force_text(   t   sixt   DeleteQueryt   UpdateQueryt   InsertQueryt	   DateQueryt   AggregateQueryc           B   s2   e  Z d  Z d Z d „  Z d d „ Z d „  Z RS(   so   
    Delete queries are done through this class, since they are more constrained
    than general queries.
    t   SQLDeleteCompilerc         C   s/   | g |  _  | |  _ |  j | ƒ j d  ƒ d  S(   N(   t   tablest   wheret   get_compilert   execute_sqlt   None(   t   selft   tableR   t   using(    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   do_query   s    	c         C   s   | s |  j  j j } n  x~ t d t | ƒ t ƒ D]d } |  j ƒ  } | j t d | j
 | ƒ d | | | t !f t ƒ |  j |  j  j j | d | ƒq1 Wd S(   sº   
        Set up and execute delete queries for all the objects in pk_list.

        More than one physical query may be executed if there are a
        lot of values in pk_list.
        i    t   inR   N(   t   modelt   _metat   pkt   ranget   lent   GET_ITERATOR_CHUNK_SIZEt   where_classt   addR	   R   t   columnR   R   t   db_table(   R   t   pk_listR   t   fieldt   offsetR   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   delete_batch#   s    c   	      C   sb  | j  } | j ƒ  |  j ƒ  g  | j D] } | j | r' | ^ q' } | s\ | |  j k r{ t | j ƒ r{ | j |  _ nÍ | j j j	 } t
 | j j sÓ t | j d d t ƒƒ } | s¿ d S|  j | | ƒ d S| j ƒ  |  j ƒ  | j f g d g | _ | _ | } |  j ƒ  } | j t d | j | ƒ d | f t ƒ | |  _ |  j | ƒ j d ƒ d S(   sÌ   
        Delete the queryset in one SQL query (if possible). For simple queries
        this is done by copying the query.query.where to self.query, for
        complex queries by using subquery.
        R    t   flatNR   (   t   queryt   get_initial_aliasR   t   alias_refcountR"   t   havingR   R   R   R    R   t   featurest   update_can_self_selectt   listt   values_listt   TrueR+   t   clear_select_clauseR&   R   t   selectt   select_fieldsR$   R%   R	   R   R   R   (	   R   R-   R   t   innerqt   tt   innerq_used_tablesR    t   valuesR   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt	   delete_qs2   s,    	


((	N(   t   __name__t
   __module__t   __doc__t   compilerR   R   R+   R=   (    (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyR      s
   	c           B   s_   e  Z d  Z d Z d „  Z d „  Z d
 d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(   s+   
    Represents an "update" SQL query.
    t   SQLUpdateCompilerc         O   s'   t  t |  ƒ j | | Ž  |  j ƒ  d  S(   N(   t   superR   t   __init__t   _setup_query(   R   t   argst   kwargs(    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRD   ^   s    c         C   s1   g  |  _  d |  _ t |  d ƒ s- i  |  _ n  d S(   sÐ   
        Runs on initialization and after cloning. Any attributes that would
        normally be set in __init__ should go in here, instead, so that they
        are also set up after a clone() call.
        t   related_updatesN(   R<   R   t   related_idst   hasattrRH   (   R   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRE   b   s    		c         K   s(   t  t |  ƒ j | d |  j j ƒ  | S(   NRH   (   RC   R   t   cloneRH   t   copy(   R   t   klassRG   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRK   m   s    c         C   sž   |  j  j j } |  j | ƒ x{ t d t | ƒ t ƒ D]a } |  j ƒ  |  _ |  j j	 t
 d  | j | ƒ d | | | t !f t ƒ |  j | ƒ j d  ƒ q5 Wd  S(   Ni    R   (   R   R   R    t   add_update_valuesR!   R"   R#   R$   R   R%   R	   R   R&   R   R   R   (   R   R(   R<   R   t   pk_fieldR*   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   update_batchq   s    c   	      C   s©   g  } x“ t  j | ƒ D]‚ \ } } |  j j j | ƒ \ } } } } | sP | rc t d | ƒ ‚ n  | r‚ |  j | | | ƒ q n  | j | | | f ƒ q W|  j | ƒ S(   s±   
        Convert a dictionary of field name to value mappings into an update
        query. This is the entry point for the public update() method on
        querysets.
        sM   Cannot update model field %r (only non-relations and foreign keys permitted).(	   R   t	   iteritemsR   R   t   get_field_by_nameR    t   add_related_updatet   appendt   add_update_fields(	   R   R<   t
   values_seqt   namet   valR)   R   t   directt   m2m(    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRN   {   s    !c         C   sa   g  | D]@ } t  | d t ƒ rA | d | d t | d ƒ f n | ^ q } |  j j | ƒ d S(   sº   
        Turn a sequence of (field, model, value) triples into an update query.
        Used by add_update_values() as well as the "fast" update path when
        saving models.
        i   i    i   N(   t
   isinstanceR   R   R<   t   extend(   R   RV   t   value(    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRU   Œ   s    Jc         C   sR   y! |  j  | j | d | f ƒ Wn* t k
 rM | d | f g |  j  | <n Xd S(   s    
        Adds (name, value) to an update query for an ancestor model.

        Updates are coalesced so that we only run one update query per ancestor.
        N(   RH   RT   R   t   KeyError(   R   R   R)   R]   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRS   ˜   s    !c         C   s„   |  j  s g  Sg  } xj t j |  j  ƒ D]V \ } } t | ƒ } | | _ |  j d k	 ro | j d |  j f ƒ n  | j | ƒ q& W| S(   sâ   
        Returns a list of query objects: one for each update required to an
        ancestor model. Each query will have the same filtering conditions as
        the current query but will only update a single table.
        t   pk__inN(	   RH   R   RQ   R   R<   RI   R   t
   add_filterRT   (   R   t   resultR   R<   R-   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   get_related_updates£   s    		N(   R>   R?   R@   RA   RD   RE   R   RK   RP   RN   RU   RS   Rb   (    (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyR   W   s   			
			c           B   s/   e  Z d  Z d „  Z d d „ Z e d „ Z RS(   t   SQLInsertCompilerc         O   s/   t  t |  ƒ j | | Ž  g  |  _ g  |  _ d  S(   N(   RC   R   RD   t   fieldst   objs(   R   RF   RG   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRD   ·   s    	c         K   sL   i |  j  d 6|  j d 6|  j d 6} | j | ƒ t t |  ƒ j | |  S(   NRd   Re   t   raw(   Rd   Re   Rf   t   updateRC   R   RK   (   R   RM   RG   t   extras(    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyRK   ¼   s    c         C   s~   | |  _  x\ | D]T } xK | D]C } t | | j ƒ } t | t ƒ r t | | j t | ƒ ƒ q q Wq W| |  _ | |  _ d S(   s¢  
        Set up the insert query from the 'insert_values' dictionary. The
        dictionary gives the model field names and their target values.

        If 'raw_values' is True, the values in the 'insert_values' dictionary
        are inserted directly into the query, rather than passed as SQL
        parameters. This provides a way to insert NULL and DEFAULT keywords
        into the query, for example.
        N(	   Rd   t   getattrt   attnameR[   R   t   setattrR   Re   Rf   (   R   Rd   Re   Rf   R)   t   objR]   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   insert_valuesÅ   s    
	$	N(   R>   R?   RA   RD   R   RK   t   FalseRm   (    (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyR   ´   s   		c           B   s    e  Z d  Z d Z d d „ Z RS(   sã   
    A DateQuery is a normal query, except that it specifically selects a single
    date field. This requires some special handling when converting the results
    back to Python objects, so we put it in a separate class.
    t   SQLDateCompilert   ASCc         C   s*  y1 |  j  | j t ƒ |  j ƒ  |  j ƒ  t ƒ } Wn0 t k
 rc t d |  j j	 j
 | f ƒ ‚ n X| d } t | t ƒ s t d | j ƒ ‚ | d d } t | | j f | ƒ } |  j ƒ  | g d	 g |  _ |  _ t |  _ | d k r÷ d g pý d g |  _ | j r&|  j d | t f ƒ n  d	 S(
   sB   
        Converts the query into a date extraction query.
        s   %s has no field named '%s'i    s   %r isn't a DateField.i   iÿÿÿÿRp   i   s
   %s__isnullN(   t   setup_joinst   splitR   t   get_metaR.   Rn   R    R   R   R   t   object_nameR[   R   t   AssertionErrorRW   R   R&   R6   R   R7   R8   R5   t   distinctt   order_byt   nullR`   (   R   t
   field_namet   lookup_typet   orderRa   R)   t   aliasR7   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   add_date_selectâ   s(    		

	!	(   R>   R?   R@   RA   R}   (    (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyR   Ù   s   c           B   s   e  Z d  Z d Z d „  Z RS(   s‰   
    An AggregateQuery takes another query as a parameter to the FROM
    clause and only selects the elements in the provided list.
    t   SQLAggregateCompilerc         C   s+   | j  | ƒ j d t ƒ \ |  _ |  _ d  S(   Nt   with_col_aliases(   R   t   as_sqlR5   t   subqueryt
   sub_params(   R   R-   R   (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   add_subquery  s    (   R>   R?   R@   RA   Rƒ   (    (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyR   þ   s   N(    R@   t   django.core.exceptionsR    t	   django.dbR   t   django.db.models.constantsR   t   django.db.models.fieldsR   R   t   django.db.models.sql.constantst#   django.db.models.sql.datastructuresR   t   django.db.models.sql.queryR   t   django.db.models.sql.whereR   R	   t   django.utils.datastructuresR
   t   django.utils.functionalR   t   django.utils.encodingR   t   django.utilsR   t   __all__R   R   R   R   R   (    (    (    s8   ../Django//lib/python/django/db/models/sql/subqueries.pyt   <module>   s&   
	A]%%