
 XF                 @   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 Gd d   d e  Z Gd d	   d	 e  Z Gd d
   d
 e  Z Gd d   d e  Z d S)zR
Query subclasses which provide extra functionality beyond simple data retrieval.
    )
FieldError)connections)Q)CURSORGET_ITERATOR_CHUNK_SIZE
NO_RESULTS)Query)sixDeleteQueryUpdateQueryInsertQueryAggregateQueryc               @   sC   e  Z d  Z d Z d Z d d   Z d d d  Z d d	   Z d S)
r
   zo
    Delete queries are done through this class, since they are more constrained
    than general queries.
    ZSQLDeleteCompilerc             C   s>   | g |  _  | |  _ |  j |  j t  } | r: | j Sd S)Nr   )tableswhereget_compilerexecute_sqlr   rowcount)selftabler   usingcursor r   M/home/ubuntu/projects/ifolica/build/django/django/db/models/sql/subqueries.pydo_query   s    	zDeleteQuery.do_queryNc             C   s   d } | s |  j    j } n  x t d t |  t  D]l } |  j   |  _ |  j t i | | | t  | j	 d 6   | |  j
 |  j    j |  j d | 7} q7 W| S)z
        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.
        r   Z__inr   )get_metapkrangelenr   where_classr   add_qr   Zattnamer   Zdb_table)r   pk_listr   fieldZnum_deletedoffsetr   r   r   delete_batch   s    	&,zDeleteQuery.delete_batchc                s+  | j      j   |  j     f d d     j D } | sO | |  j k r^   j |  _ n | j j j } t | j j	 s t
 | j d d d  } | s d S|  j | |  S  j   | j |  j    g   _   } |  j   |  _ |  j t d |   |  j |  j t  } | r'| j Sd S)z
        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.
        c                s#   g  |  ] }   j  | r |  q Sr   )Zalias_refcount).0t)innerqr   r   
<listcomp><   s   	 z)DeleteQuery.delete_qs.<locals>.<listcomp>r   ZflatTr   pk__in)queryZget_initial_aliasr   r   model_metar   r   featuresZupdate_can_self_selectlistZvalues_listr#   Zclear_select_clauseZget_colselectr   r   r   r   r   r   r   )r   r)   r   Zinnerq_used_tablesr   valuesr   r   )r&   r   	delete_qs1   s&    	


zDeleteQuery.delete_qs)__name__
__module____qualname____doc__compilerr   r#   r0   r   r   r   r   r
      s
   c                   s   e  Z d  Z d Z d Z   f d d   Z d d   Z d   f d d	  Z d
 d   Z d d   Z	 d d   Z
 d d   Z d d   Z   S)r   z+
    Represents an "update" SQL query.
    ZSQLUpdateCompilerc                s'   t  t |   j | |   |  j   d  S)N)superr   __init___setup_query)r   argskwargs)	__class__r   r   r7   \   s    zUpdateQuery.__init__c             C   s1   g  |  _  d |  _ t |  d  s- i  |  _ n  d S)z
        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.
        Nrelated_updates)r/   related_idshasattrr<   )r   r   r   r   r8   `   s    		zUpdateQuery._setup_queryNc                s(   t  t |   j | d |  j j   | S)Nr<   )r6   r   cloner<   copy)r   klassr:   )r;   r   r   r?   k   s    zUpdateQuery.clonec             C   s}   |  j  |  xi t d t |  t  D]O } |  j   |  _ |  j t d | | | t    |  j |  j	 t
  q& Wd  S)Nr   r(   )add_update_valuesr   r   r   r   r   r   r   r   r   r   )r   r    r/   r   r"   r   r   r   update_batchn   s
    $zUpdateQuery.update_batchc             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)z
        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.
        zMCannot update model field %r (only non-relations and foreign keys permitted).)r	   	iteritemsr   	get_fieldZauto_createdZconcreter*   r+   Zconcrete_modelZis_relationZmany_to_manyr   add_related_updateappendadd_update_fields)r   r/   
values_seqnamevalr!   directr*   r   r   r   rB   u   s    zUpdateQuery.add_update_valuesc             C   s   |  j  j |  d S)z
        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)r/   extend)r   rI   r   r   r   rH      s    zUpdateQuery.add_update_fieldsc             C   s)   |  j  j | g   j | d | f  d S)z
        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)r<   
setdefaultrG   )r   r*   r!   valuer   r   r   rF      s    zUpdateQuery.add_related_updatec             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)z
        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.
        Nr(   )r<   r	   rD   r   r/   r=   Z
add_filterrG   )r   resultr*   r/   r)   r   r   r   get_related_updates   s    		zUpdateQuery.get_related_updates)r1   r2   r3   r4   r5   r7   r8   r?   rC   rB   rH   rF   rQ   r   r   )r;   r   r   U   s   c                   sL   e  Z d  Z d Z   f d d   Z d   f d d  Z d d d	  Z   S)
r   ZSQLInsertCompilerc                s/   t  t |   j | |   g  |  _ g  |  _ d  S)N)r6   r   r7   fieldsobjs)r   r9   r:   )r;   r   r   r7      s    	zInsertQuery.__init__Nc                s^   i |  j  d  d   d 6|  j d  d   d 6|  j d 6} | j |  t t |   j | |  S)NrR   rS   raw)rR   rS   rT   updater6   r   r?   )r   rA   r:   extras)r;   r   r   r?      s    zInsertQuery.cloneFc             C   s   | |  _  | |  _ | |  _ d S)a  
        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)rR   rS   rT   )r   rR   rS   rT   r   r   r   insert_values   s    
		zInsertQuery.insert_values)r1   r2   r3   r5   r7   r?   rW   r   r   )r;   r   r      s   	c               @   s(   e  Z d  Z d Z d Z d d   Z d S)r   z
    An AggregateQuery takes another query as a parameter to the FROM
    clause and only selects the elements in the provided list.
    ZSQLAggregateCompilerc             C   s1   | j  |  j d d d d  \ |  _ |  _ d  S)NZwith_col_aliasesTsubquery)r   Zas_sqlrX   Z
sub_params)r   r)   r   r   r   r   add_subquery   s    zAggregateQuery.add_subqueryN)r1   r2   r3   r4   r5   rY   r   r   r   r   r      s   N)r4   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.db.models.query_utilsr   Zdjango.db.models.sql.constantsr   r   r   Zdjango.db.models.sql.queryr   Zdjango.utilsr	   __all__r
   r   r   r   r   r   r   r   <module>   s   DX 