B
    CVvgp  ã               @   sŒ   d 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)Ú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S )r   zA DELETE SQL query.ZSQLDeleteCompilerc          	   C   s@   || j | i| _ || _|  |¡ t¡}|r<|� |jS Q R X dS )Nr   )Z	alias_mapÚwhereÚget_compilerÚexecute_sqlr   Zrowcount)ÚselfÚtabler   ÚusingÚcursor© r   úC/tmp/pip-install-o3oxmrkh/Django/django/db/models/sql/subqueries.pyÚdo_query   s    zDeleteQuery.do_queryc          
   C   sx   d}|   ¡ j}xdtdt|ƒtƒD ]P}|  ¡ | _|  tf |j	d |||t … iŽ¡ || 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_batchN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úcompilerr   r   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||Ž |  ¡  d S )N)ÚsuperÚ__init__Ú_setup_query)r   ÚargsÚkwargs)Ú	__class__r   r   r&   4   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)ÚvaluesÚrelated_idsÚrelated_updates)r   r   r   r   r'   8   s    zUpdateQuery._setup_queryc                s   t ƒ  ¡ }| j ¡ |_|S )N)r%   Úcloner-   Úcopy)r   Úobj)r*   r   r   r.   A   s    
zUpdateQuery.clonec             C   s^   |   |¡ xNtdt|ƒtƒD ]:}|  ¡ | _|  t|||t … d�¡ |  |¡ 	t
¡ qW d S )Nr   )Úpk__in)Ú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Ž|  ¡ D ]‚\}}|  ¡  |¡}|jo0|j  p:|j }|jjj}|rV|jrb|j	rbt
d| ƒ‚||  ¡ jk	r€|  |||¡ q| |||f¡ qW |  |¡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concreteÚmodelZ_metaZconcrete_modelZis_relationZmany_to_manyr   Úadd_related_updateÚappendÚadd_update_fields)r   r+   Ú
values_seqÚnameÚvalr   Údirectr6   r   r   r   r2   M   s    
zUpdateQuery.add_update_valuesc             C   sD   x>|D ]6\}}}t |dƒr*|j| ddd�}| 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)Úhasattrr>   r+   r8   )r   r:   r   r6   r<   r   r   r   r9   c   s    
zUpdateQuery.add_update_fieldsc             C   s   | j  |g ¡ |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-   Ú
setdefaultr8   )r   r6   r   Úvaluer   r   r   r7   o   s    zUpdateQuery.add_related_updatec             C   s\   | j s
g S g }xH| j  ¡ D ]:\}}t|ƒ}||_| jdk	rJ| d| jf¡ | |¡ 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.
        Nr1   )r-   r4   r	   r+   r,   Z
add_filterr8   )r   Úresultr6   r+   Úqueryr   r   r   Úget_related_updatesw   s    
zUpdateQuery.get_related_updates)r    r!   r"   r#   r$   r&   r'   r.   r3   r2   r9   r7   rD   Ú__classcell__r   r   )r*   r   r	   /   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)r%   r&   ÚfieldsÚobjsrF   )r   rF   r(   r)   )r*   r   r   r&   Œ   s    zInsertQuery.__init__c             C   s   || _ || _|| _d S )N)rG   rH   Úraw)r   rG   rH   rI   r   r   r   Úinsert_values’   s    zInsertQuery.insert_values)F)r    r!   r"   r$   r&   rJ   rE   r   r   )r*   r   r
   ‰   s   c                   s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )r   zu
    Take another query as a parameter to the FROM clause and only select the
    elements in the provided list.
    ZSQLAggregateCompilerc                s   || _ tƒ  |¡ d S )N)Úinner_queryr%   r&   )r   r6   rK   )r*   r   r   r&       s    zAggregateQuery.__init__)r    r!   r"   r#   r$   r&   rE   r   r   )r*   r   r   ˜   s   N)r#   Zdjango.core.exceptionsr   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