o 3ap@sdZddlmZddlmZddlmZmZmZddl m Z gdZ Gddde Z Gd d d e Z Gd d d e ZGd dde ZdS)zR Query subclasses which provide extra functionality beyond simple data retrieval. ) FieldError)Q)CURSORGET_ITERATOR_CHUNK_SIZE NO_RESULTS)Query) DeleteQuery UpdateQuery InsertQueryAggregateQueryc@s$eZdZdZdZddZddZdS)rzA DELETE SQL query.SQLDeleteCompilercCsX||j|i|_||_||t}|r*| |jWdS1s%wYdS)Nr) alias_mapwhere get_compiler execute_sqlrrowcount)selftablerusingcursorrA/usr/lib/python3/dist-packages/django/db/models/sql/subqueries.pydo_querys zDeleteQuery.do_queryc Csxd}|j}tdt|tD]*}||_|tdi|j d|||ti||j |j |j|d7}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)rNr) get_metapkrangelenr where_classradd_qrattnamerdb_table)rpk_listr num_deletedfieldoffsetrrr delete_batchs   zDeleteQuery.delete_batchN)__name__ __module__ __qualname____doc__compilerrr&rrrrrs  rcs`eZdZdZdZfddZddZfddZd d Zd d Z d dZ ddZ ddZ Z S)r zAn UPDATE SQL query.SQLUpdateCompilercstj|i||dSN)super__init__ _setup_query)rargskwargs __class__rrr/4s zUpdateQuery.__init__cCsg|_d|_i|_dS)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_idsrelated_updates)rrrrr08s zUpdateQuery._setup_querycst}|j|_|Sr-)r.cloner7copy)robjr3rrr8As  zUpdateQuery.clonecCsZ||tdt|tD]}||_|t|||td|| t q dS)Nr)pk__in) add_update_valuesrrrrrrrrrr)rr"r5rr%rrr update_batchFs  zUpdateQuery.update_batchcCsg}|D]A\}}||}|jo|j p|j }|jjj}|r*|jr0|j r0t d|||jur?| |||q| |||fq| |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_field auto_createdconcretemodel_metaconcrete_model is_relation many_to_manyradd_related_updateappendadd_update_fields)rr5 values_seqnamevalr$directrBrrrr<Ms   zUpdateQuery.add_update_valuescCs@|D]\}}}t|dr|j|ddd}|j|||fqdS)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_joinsfor_saveN)hasattrrNr5rH)rrJr$rBrLrrrrIcs  zUpdateQuery.add_update_fieldscCs|j|g|d|fdS)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)r7 setdefaultrH)rrBr$valuerrrrGoszUpdateQuery.add_related_updatecCsX|jsgSg}|jD]\}}t|}||_|jdur$|d|jf||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. Nr;)r7r>r r5r6 add_filterrH)rresultrBr5queryrrrget_related_updatesws  zUpdateQuery.get_related_updates)r'r(r)r*r+r/r0r8r=r<rIrGrW __classcell__rrr3rr /s   r cs0eZdZdZddfdd ZdddZZS) r SQLInsertCompilerF)ignore_conflictscs(tj|i|g|_g|_||_dSr-)r.r/fieldsobjsrZ)rrZr1r2r3rrr/s zInsertQuery.__init__cCs||_||_||_dSr-)r[r\raw)rr[r\r]rrr insert_valuess zInsertQuery.insert_values)F)r'r(r)r+r/r^rXrrr3rr sr cs$eZdZdZdZfddZZS)r zu Take another query as a parameter to the FROM clause and only select the elements in the provided list. SQLAggregateCompilercs||_t|dSr-) inner_queryr.r/)rrBr`r3rrr/szAggregateQuery.__init__)r'r(r)r*r+r/rXrrr3rr sr N)r*django.core.exceptionsrdjango.db.models.query_utilsrdjango.db.models.sql.constantsrrrdjango.db.models.sql.queryr__all__rr r r rrrrs    Z