o
    þÞh  ã                   @   s|   d Z ddlmZ ddlmZmZmZ ddlmZ g d¢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)ÚGET_ITERATOR_CHUNK_SIZEÚ
NO_RESULTSÚ	ROW_COUNT)Ú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.ÚSQLDeleteCompilerc                 C   s&   || j | i| _ || _|  |¡ t¡S ©N)Ú	alias_mapÚwhereÚget_compilerÚexecute_sqlr   )ÚselfÚtabler   Úusing© r   ú_/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/db/models/sql/subqueries.pyÚdo_query   s   zDeleteQuery.do_queryc                 C   sl   d}|   ¡ j}tdt|ƒtƒD ]$}|  ¡  |  |j› d�|||t … ¡ || j|   ¡ j	| j
|d�7 }q|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   Ú__in)r   )Úget_metaÚpkÚrangeÚlenr   Úclear_whereÚ
add_filterÚattnamer   Údb_tabler   )r   Úpk_listr   Ú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
    r   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.ÚSQLUpdateCompilerc                    s   t ƒ j|i |¤Ž |  ¡  d S r   )ÚsuperÚ__init__Ú_setup_query)r   ÚargsÚkwargs©Ú	__class__r   r   r,   5   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-   9   s   
zUpdateQuery._setup_queryc                    s   t ƒ  ¡ }| j ¡ |_|S r   )r+   Úcloner4   Úcopy)r   Úobjr0   r   r   r5   B   s   
zUpdateQuery.clonec                 C   sT   |   |¡ tdt|ƒtƒD ]}|  ¡  |  d|||t … ¡ |  |¡ t¡ qd S )Nr   Úpk__in)	Úadd_update_valuesr   r   r   r   r   r   r   r   )r   r    r2   r   r#   r   r   r   Úupdate_batchG   s   
ÿûzUpdateQuery.update_batchc                 C   s´   g }|  ¡ D ]N\}}|  ¡  |¡}|jo|j  p|j }|jjj}|jdkr/|jj	r/t
dƒ‚|r7|jr=|jr=t
d| ƒ‚||  ¡ jurL|  |||¡ q| |||f¡ q|  |¡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.
        r   z:Composite primary key fields must be updated individually.zMCannot update model field %r (only non-relations and foreign keys permitted).)Úitemsr   Ú	get_fieldÚauto_createdÚconcreteÚmodelÚ_metaÚconcrete_modelÚnameÚis_composite_pkr   Úis_relationÚmany_to_manyÚadd_related_updateÚappendÚadd_update_fields)r   r2   Ú
values_seqrB   Úvalr"   Údirectr?   r   r   r   r9   P   s*   ÿ
ÿÿÿ
zUpdateQuery.add_update_valuesc                 C   sH   |D ]\}}}|j rqt|dƒr|j| ddd�}| j |||f¡ q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)Úallow_joinsÚfor_saveN)Ú	generatedÚhasattrrL   r2   rG   )r   rI   r"   r?   rJ   r   r   r   rH   l   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)r4   Ú
setdefaultrG   )r   r?   r"   Úvaluer   r   r   rF   {   s   zUpdateQuery.add_related_updatec                 C   sZ   | j sg S g }| j  ¡ D ]\}}t|ƒ}||_| jdur%| d| j| ¡ | |¡ q|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.
        Nr8   )r4   r;   r   r2   r3   r   rG   )r   Úresultr?   r2   Úqueryr   r   r   Úget_related_updatesƒ   s   
zUpdateQuery.get_related_updates)r%   r&   r'   r(   r)   r,   r-   r5   r:   r9   rH   rF   rU   Ú__classcell__r   r   r0   r   r   0   s    		r   c                       s4   e Zd ZdZddddœ‡ fdd„
Zd	dd„Z‡  ZS )
r	   ÚSQLInsertCompilerN)Úon_conflictÚupdate_fieldsÚunique_fieldsc                   s<   t ƒ j|i |¤Ž g | _g | _|| _|pg | _|pg | _d S r   )r+   r,   ÚfieldsÚobjsrX   rY   rZ   )r   rX   rY   rZ   r.   r/   r0   r   r   r,   ˜   s   
zInsertQuery.__init__Fc                 C   s   || _ || _|| _d S r   )r[   r\   Úraw)r   r[   r\   r]   r   r   r   Úinsert_values¢   s   
zInsertQuery.insert_values)F)r%   r&   r'   r)   r,   r^   rV   r   r   r0   r   r	   •   s
    ÿ
r	   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.
    ÚSQLAggregateCompilerc                    s   || _ tƒ  |¡ d S r   )Úinner_queryr+   r,   )r   r?   r`   r0   r   r   r,   °   s   zAggregateQuery.__init__)r%   r&   r'   r(   r)   r,   rV   r   r   r0   r   r
   ¨   s    r
   N)r(   Údjango.core.exceptionsr   Údjango.db.models.sql.constantsr   r   r   Údjango.db.models.sql.queryr   Ú__all__r   r   r	   r
   r   r   r   r   Ú<module>   s     e