o
    bp                     @   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
 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)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   sX   || j | i| _ || _| |t}|r*| |jW  d    S 1 s%w   Y  dS )Nr   )Z	alias_mapwhereget_compilerexecute_sqlr   Zrowcount)selftabler   usingcursor r   ^/var/www/secure340b-portal/env/lib/python3.10/site-packages/django/db/models/sql/subqueries.pydo_query   s    zDeleteQuery.do_queryc                 C   sx   d}|   j}tdt|tD ]*}|  | _| tdi |j	d |||t  i || 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   Z__in)r   Nr   )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
    	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.ZSQLUpdateCompilerc                    s   t  j|i | |   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 r%   )r&   cloner/   copy)r   objr+   r   r   r0   A   s   
zUpdateQuery.clonec                 C   sZ   |  | tdt|tD ]}|  | _| t|||t  d | |	t
 q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 }|  D ]A\}}|  |}|jo|j  p|j }|jjj}|r*|jr0|j	r0t
d| ||  jur?| ||| 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.
        zMCannot update model field %r (only non-relations and foreign keys permitted).)itemsr   	get_fieldZauto_createdZconcretemodel_metaZconcrete_modelZis_relationZmany_to_manyr   add_related_updateappendadd_update_fields)r   r-   
values_seqnamevalr   directr8   r   r   r   r4   M   s    

zUpdateQuery.add_update_valuesc                 C   s@   |D ]\}}}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)Zallow_joinsZfor_saveN)hasattrrA   r-   r;   )r   r=   r   r8   r?   r   r   r   r<   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/   
setdefaultr;   )r   r8   r   valuer   r   r   r:   o   s   zUpdateQuery.add_related_updatec                 C   sX   | j sg S g }| j  D ]\}}t|}||_| jdur$|d| jf || 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.
        Nr3   )r/   r6   r	   r-   r.   
add_filterr;   )r   resultr8   r-   queryr   r   r   get_related_updatesw   s   
zUpdateQuery.get_related_updates)r    r!   r"   r#   r$   r'   r(   r0   r5   r4   r<   r:   rH   __classcell__r   r   r+   r   r	   /   s    	r	   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|i | g | _g | _|| _d S r%   )r&   r'   fieldsobjsrJ   )r   rJ   r)   r*   r+   r   r   r'      s   
zInsertQuery.__init__c                 C   s   || _ || _|| _d S r%   )rK   rL   raw)r   rK   rL   rM   r   r   r   insert_values   s   
zInsertQuery.insert_values)F)r    r!   r"   r$   r'   rN   rI   r   r   r+   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.
    ZSQLAggregateCompilerc                    s   || _ t | d S r%   )inner_queryr&   r'   )r   r8   rO   r+   r   r   r'      s   zAggregateQuery.__init__)r    r!   r"   r#   r$   r'   rI   r   r   r+   r   r      s    r   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