ó
i4Vdc           @   sà   d  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
 d d l m Z d d l m Z 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 S(   sR   
Query subclasses which provide extra functionality beyond simple data retrieval.
iÿÿÿÿ(   t
   FieldError(   t   connections(   t   Q(   t   CURSORt   GET_ITERATOR_CHUNK_SIZEt
   NO_RESULTS(   t   Query(   t   sixt   DeleteQueryt   UpdateQueryt   InsertQueryt   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 t ƒ } | r: | j Sd S(   Ni    (   t   tablest   wheret   get_compilert   execute_sqlR   t   rowcount(   t   selft   tableR   t   usingt   cursor(    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   do_query   s    	c         C   s¨   d } | s |  j  ƒ  j } n  xƒ t d t | ƒ t ƒ D]i } |  j ƒ  |  _ |  j t i | | | t !| j	 d 6  ƒ | |  j
 |  j  ƒ  j |  j d | ƒ7} q7 W| 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   (   t   get_metat   pkt   ranget   lenR   t   where_classR   t   add_qR   t   attnameR   t   db_table(   R   t   pk_listR   t   fieldt   num_deletedt   offset(    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   delete_batch   s    	#,c   	      C   s8  | j  } | j ƒ  |  j ƒ  g  | j D] } | j | r' | ^ q' } | s\ | |  j k rk | j |  _ n¤ | j j j } t | j	 j
 s¿ t | j d d t ƒƒ } | s¯ d S|  j | | ƒ S| j ƒ  | j |  j ƒ  ƒ g | _ | } |  j ƒ  |  _ |  j t d | ƒ ƒ |  j | ƒ j t ƒ } | r4| j Sd 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   flati    t   pk__in(   t   queryt   get_initial_aliasR   t   alias_refcountR   t   modelt   _metaR   R   t   featurest   update_can_self_selectt   listt   values_listt   TrueR$   t   clear_select_clauset   get_colt   selectR   R   R   R   R   R   R   (	   R   R'   R   t   innerqt   tt   innerq_used_tablesR   t   valuesR   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt	   delete_qs1   s(    	


N(   t   __name__t
   __module__t   __doc__t   compilerR   t   NoneR$   R8   (    (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/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(    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyR@   \   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(   R7   R=   t   related_idst   hasattrRD   (   R   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyRA   `   s    		c         K   s(   t  t |  ƒ j | d |  j j ƒ  | S(   NRD   (   R?   R	   t   cloneRD   t   copy(   R   t   klassRC   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyRG   k   s    c         C   sz   |  j  | ƒ xf t d t | ƒ t ƒ D]L } |  j ƒ  |  _ |  j t d | | | t !ƒ ƒ |  j | ƒ j	 t
 ƒ q& Wd  S(   Ni    R&   (   t   add_update_valuesR   R   R   R   R   R   R   R   R   R   (   R   R    R7   R   R#   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   update_batcho   s
    !c         C   så   g  } xÏ t  j | ƒ D]¾ \ } } |  j ƒ  j | ƒ } | j oG | j pR | j } | j j j } | s} | j	 r | j
 r t d | ƒ ‚ n  | |  j ƒ  j k	 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   t	   get_fieldt   auto_createdt   concreteR*   R+   t   concrete_modelt   is_relationt   many_to_manyR    t   add_related_updatet   appendt   add_update_fields(   R   R7   t
   values_seqt   namet   valR!   t   directR*   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyRJ   v   s    c         C   s   |  j  j | ƒ d S(   só   
        Append a sequence of (field, model, value) triples to the internal list
        that will be used to generate the UPDATE query. Might be more usefully
        called add_update_targets() to hint at the extra information here.
        N(   R7   t   extend(   R   RV   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyRU   Œ   s    c         C   s)   |  j  j | g  ƒ j | d | f ƒ d 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(   RD   t
   setdefaultRT   R=   (   R   R*   R!   t   value(    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/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.
        R&   N(	   RD   R   RL   R	   R7   RE   R=   t
   add_filterRT   (   R   t   resultR*   R7   R'   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   get_related_updatesœ   s    		N(   R9   R:   R;   R<   R@   RA   R=   RG   RK   RJ   RU   RS   R_   (    (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyR	   U   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(   R?   R
   R@   t   fieldst   objs(   R   RB   RC   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyR@   ±   s    	c         K   sL   i |  j  d 6|  j d 6|  j d 6} | j | ƒ t t |  ƒ j | |  S(   NRa   Rb   t   raw(   Ra   Rb   Rc   t   updateR?   R
   RG   (   R   RI   RC   t   extras(    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyRG   ¶   s    c         C   s   | |  _  | |  _ | |  _ 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(   Ra   Rb   Rc   (   R   Ra   Rb   Rc   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   insert_values¿   s    
		N(   R9   R:   R<   R@   R=   RG   t   FalseRf   (    (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/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   s1   | j  | ƒ j d t d t ƒ \ |  _ |  _ d  S(   Nt   with_col_aliasest   subquery(   R   t   as_sqlR0   Rj   t
   sub_params(   R   R'   R   (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   add_subqueryÖ   s    (   R9   R:   R;   R<   Rm   (    (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyR   Î   s   N(   R;   t   django.core.exceptionsR    t	   django.dbR   t   django.db.models.query_utilsR   t   django.db.models.sql.constantsR   R   R   t   django.db.models.sql.queryR   t   django.utilsR   t   __all__R   R	   R
   R   (    (    (    sm   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.pyt   <module>   s   DY 