3
W­E^p  ã               @   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d	d
gZG dd„ deƒZG dd„ deƒZG dd	„ d	eƒZG d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ÚDeleteQueryÚUpdateQueryÚInsertQueryÚAggregateQueryc               @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	S )
r	   zA DELETE SQL query.ZSQLDeleteCompilerc             C   s4   || j | i| _ || _| j|ƒjtƒ}|r0|jS dS )Nr   )Ú	alias_mapÚwhereÚget_compilerÚexecute_sqlr   Úrowcount)ÚselfÚtabler   ÚusingÚcursor© r   úA/tmp/pip-build-78s3knkw/Django/django/db/models/sql/subqueries.pyÚdo_query   s    zDeleteQuery.do_queryc          	   C   sx   d}| j ƒ j}xdtdt|ƒtƒD ]P}| jƒ | _| jtf |j	d |||t … iŽƒ || j
| j ƒ j| j|d�7 }q 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__in)r   )Úget_metaÚpkÚrangeÚlenr   Úwhere_classr   Úadd_qr   Zattnamer   Zdb_table)r   Úpk_listr   Znum_deletedÚfieldÚoffsetr   r   r   Údelete_batch   s    

 zDeleteQuery.delete_batchc                sÞ   |j ‰ ˆ jƒ  | jƒ  t‡ fdd„ˆ jD ƒƒ}| sB|t| jƒkrLˆ j| _nt|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S d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S r   )Zalias_refcount)Ú.0Út)Úinnerqr   r   ú
<listcomp>7   s    z)DeleteQuery.delete_qs.<locals>.<listcomp>r   T)Zflatr   )Úpk__in)ÚqueryZget_initial_aliasÚtupler   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_qs,   s&    


zDeleteQuery.delete_qsN)Ú__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‡ fdd„Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )r
   zAn UPDATE SQL query.ZSQLUpdateCompilerc                s   t ƒ j||Ž | jƒ  d S )N)ÚsuperÚ__init__Ú_setup_query)r   ÚargsÚkwargs)Ú	__class__r   r   r7   T   s    zUpdateQuery.__init__c             C   s   g | _ d| _i | _dS )z›
        Run on initialization and at the end of chaining. Any attributes that
        would normally be set in __init__() should go here instead.
        N)r/   Úrelated_idsÚrelated_updates)r   r   r   r   r8   X   s    zUpdateQuery._setup_queryc                s   t ƒ jƒ }| jjƒ |_|S )N)r6   Úcloner=   Úcopy)r   Úobj)r;   r   r   r>   a   s    
zUpdateQuery.clonec             C   s^   | j |ƒ xNtdt|ƒtƒD ]:}| jƒ | _| jt|||t … d�ƒ | j|ƒj	t
ƒ qW d S )Nr   )r'   )Úadd_update_valuesr   r   r   r   r   r   r   r   r   r   )r   r   r/   r   r!   r   r   r   Úupdate_batchf   s
    

zUpdateQuery.update_batchc             C   s    g }x�|j ƒ D ]„\}}| jƒ j|ƒ}|jo0|j  p:|j }|jjj}| sX|jrd|j	rdt
d| ƒ‚|| jƒ jk	r‚| j|||ƒ q|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).)Úitemsr   Ú	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   rA   m   s    
zUpdateQuery.add_update_valuesc             C   sD   x>|D ]6\}}}t |dƒr*|j| ddd�}| jj|||fƒ qW 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.
        Úresolve_expressionFT)Zallow_joinsZfor_saveN)ÚhasattrrL   r/   rF   )r   rH   r    r*   rJ   r   r   r   rG   ƒ   s    
zUpdateQuery.add_update_fieldsc             C   s   | j j|g ƒj|d|fƒ dS )zž
        Add (name, value) to an update query for an ancestor model.

        Update are coalesced so that only one update query per ancestor is run.
        N)r=   Ú
setdefaultrF   )r   r*   r    Úvaluer   r   r   rE   �   s    zUpdateQuery.add_related_updatec             C   s\   | j s
g S g }xH| j jƒ D ]:\}}t|ƒ}||_| jdk	rJ|jd| jfƒ |j|ƒ qW |S )zá
        Return 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=   rC   r
   r/   r<   Z
add_filterrF   )r   Úresultr*   r/   r(   r   r   r   Úget_related_updates—   s    
zUpdateQuery.get_related_updates)r1   r2   r3   r4   r5   r7   r8   r>   rB   rA   rG   rE   rQ   Ú__classcell__r   r   )r;   r   r
   O   s   	c                   s0   e Zd ZdZddœ‡ fdd„
Zddd„Z‡  ZS )	r   ZSQLInsertCompilerF)Úignore_conflictsc               s$   t ƒ j||Ž g | _g | _|| _d S )N)r6   r7   ÚfieldsÚobjsrS   )r   rS   r9   r:   )r;   r   r   r7   ¬   s    zInsertQuery.__init__c             C   s   || _ || _|| _d S )N)rT   rU   Úraw)r   rT   rU   rV   r   r   r   Úinsert_values²   s    zInsertQuery.insert_values)F)r1   r2   r3   r5   r7   rW   rR   r   r   )r;   r   r   ©   s   c               @   s   e Zd ZdZdZdd„ ZdS )r   zu
    Take another query as a parameter to the FROM clause and only select the
    elements in the provided list.
    ZSQLAggregateCompilerc             C   s$   d|_ |j|ƒjdd�\| _ | _d S )NT)Zwith_col_aliases)Zsubqueryr   Zas_sqlZ
sub_params)r   r(   r   r   r   r   Úadd_subqueryÀ   s    zAggregateQuery.add_subqueryN)r1   r2   r3   r4   r5   rX   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   Ú__all__r	   r
   r   r   r   r   r   r   Ú<module>   s   ?Z