o xgj%@sJddlZddlZddlmZddlmZddlmZmZddl m Z m Z ddl m Z ddlmZmZmZmZmZddlmZmZdd lmZmZdd lmZmZmZmZmZm Z dd l!m"Z"m#Z#dd l$m%Z%dd l&m'Z'ddl(m)Z)ddl*m+Z+GdddZ,Gddde,Z-Gddde,Z.Gddde,Z/Gddde,Z0ddZ1dS)N)partial)chain)EmptyResultSet FieldError) DatabaseErrorNotSupportedError) LOOKUP_SEP)FOrderByRawSQLRefValue)CastRandom)Qselect_related_descend)CURSORGET_ITERATOR_CHUNK_SIZEMULTI NO_RESULTS ORDER_DIRSINGLE)Query get_order_dir)TransactionManagementError)cached_property) make_hashable)_lazy_re_compilec@seZdZedejejBZddZddZ ddZ dd Z d d Z d d Z ddZddZddZddZddZd=ddZd>ddZdd Z ! d?d"d#Zd$d%Zd&d'Z ( d@d)d*Zd+d,Zd-d.Zd/d0Zd1d2Zdddefd3d4Zd5d6Z e!defd7d8Z"d9d:Z#d;d<Z$dS)A SQLCompilerz^(.*)\s(?:ASC|DESC).*cCs8||_||_||_ddi|_d|_d|_d|_d|_dS)N*)query connectionusing quote_cacheselectannotation_col_map klass_info_meta_ordering)selfr r!r"r)?/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py__init__s  zSQLCompiler.__init__csHtfddjjDrj\___tj_ dS)Nc3s |] }jj|dkVqdSrNr alias_refcount).0ar(r)r* ,z*SQLCompiler.setup_query..) allr alias_mapget_initial_alias get_selectr$r&r%len col_countr1r)r1r* setup_query+s zSQLCompiler.setup_querycCsX||}|jj\|_|_|||j}t||_ | |j||}|||fS)z Do any necessary class setup immediately prior to producing SQL. This is for things that can't necessarily be done in __init__ because we might not have all the pieces in place at that time. ) r: get_order_byr where split_havinghavingget_extra_selectr$boolhas_extra_select get_group_by)r(order_by extra_selectgroup_byr)r)r* pre_sql_setup1s  zSQLCompiler.pre_sql_setupcCs\|jjdurgSg}|jjdur+|jjD]}t|ds%||j|q||qdd|D}|D]\}}}||vr>q4|}|D]}||qDq4|D]\}\} } } | s_||qO|jrh|jnd} | D]}||qlg} t}| || }|D]*}| |\} } | || | \} } t | }| |f|vr| | | f| | |fq| S)z Return a list of 2-tuples of form (sql, params). The logic of what exactly the GROUP BY clause contains is hard to describe in other words than "if it passes the test suite, then it is correct". NTas_sqlcSsh|] }t|tr|jqSr)) isinstancer sourcer/exprr)r)r* us  z+SQLCompiler.get_group_by..r))r rEhasattrappend resolve_refget_group_by_colsextendr>setcollapse_group_bycompile select_formatradd)r(r$rC expressionsrK ref_sources_colscolsqlparamsis_refhaving_group_byresultseen params_hashr)r)r*rB?sJ %        zSQLCompiler.get_group_bycsjjjr>d}|D]}t|ddjjjjkr%t|ddjjkr%|}nq |r.cs2g|]}|vst|dddur|jvr|qSrdN)getattrrdrJ)r> pk_aliasesr)r* s z1SQLCompiler.collapse_group_by..cs4h|]}t|dr|jjrjj|jjr|qSre)rMrcrfr!features%allows_group_by_selected_pks_on_modelmodelrJr1r)r*rLscSsh|]}|jqSr))rdrJr)r)r*rLscs(g|]}|vst|ddvr|qSrg)rhrJ)aliasespksr)r*rjs") r!rkallows_group_by_pkrhr rm_metapk base_tableallows_group_by_selected_pks)r(rWr>rrrKr))rnr>riror(r*rSs2   zSQLCompiler.collapse_group_byc sg}d}i}d}|jjD]\}\}}|||<|t|||f|d7}q|jjr1|jjr1J|jjr:|}n|jj}|r^g} |D]} | ||| df|d7}qD|jj| d}|jj D]\}} |||<|| |f|d7}qd|jj r| |} | |d<fdd|g} |D].\} }z | | \}}Wn t yd\}}Yn w| |||\}}| | ||f|fq| ||fS) a Return three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations The (sql, params) is what the expression will produce, and alias is the "AS alias" for the column (possibly None). The klass_info structure contains the following information: - The base model of the query. - Which columns for that model are present in the query (by position of the select clause). - related_klass_infos: [f, klass_info] to descent into The annotations is a dictionary of {'attname': column position} values. Nr)rm select_fieldsrelated_klass_infoscs6|dD]}|dr|d|d|d<|qdS)Nrw from_parentrvr))r&kiget_select_from_parentr)r*r{s  z6SQLCompiler.get_select..get_select_from_parent)0r))r rDitemsrNr r$ default_colsget_default_columnsrmannotation_selectselect_relatedget_related_selectionsrTrrU)r(r$r& annotations select_idxrdr\r]rZ select_listr[ annotationrwretr)rzr*r7sP           zSQLCompiler.get_selectc CsF|jjr |jj}n$|jjs|jj}n|jjr|jj}n|jjr+|jj}||_ng}|jjr8td\}}ntd\}}g}|D]}t |drrt |t rUt ||j }t |ts^|}|jjsj|}|||dfqB|dkr|ttdfqBt||\}}|dk}||jjvr|tt||jj||ddfqB||jjvr|jjr|jrt|} n|jj|} t | t rt | | j } |t| |ddfqBd|vr|dd \} }|ttd || |fg|ddfqB|jjr||jjvr(|jjr|jr|tt||ddfqB||j ||j|d qB||jj!vrA|tt|jj||ddfqB|tt|t|jj||ddfqBg} t"} |D]\} } | j#|jdd d }|jjr|jr|$d}| $d}t%|jD]:\}\}}}| r||j&kr|j'}n|rt |tr||j(ksq||kr|)td|d dgn5q|rt*dt+|jjd }d|}|jj,D] }|-||q|j.|||)t|dg|/|\}}|j01|d }t2|}||f| vrq]| 3||f| |||| ffq]| S)a+ Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the ORDER BY clause. The order_by clause can alter the select clause (for example it can add aliases to clauses that do not yet have one, or it can add totally new select clauses). ASCDESCresolve_expressionF? descendingT.ru%s.%s) default_orderN) allow_joinsreuserz%dr)z:ORDER BY term does not match any column in the result set. __orderbycol)4r extra_order_bydefault_orderingrCget_metaorderingr'standard_orderingrrMrHr r output_fieldr asccopyreverse_orderingrNrrrr r combinatorr$r splitr quote_name_unless_aliasextrarQfind_ordering_namerDrRrget_source_expressions enumeraterefsrInameset_source_expressionsrr8combined_queriesadd_annotationadd_select_colrTordering_partssearchrrV)r(rrdescrCfieldr[orderrrKtabler`rar^resolvedsrcexpr_srcidxsel_exprrY col_alias order_by_idxcol_nameqr\r]without_orderingrbr)r)r*r;s                          zSQLCompiler.get_order_byc Csng}|jjr5|jjs5dd|D}|D]!\}\}}}|j|d} |s4| |f|vr4||| |fdfq|S)NcSg|]}|dqS)rur)r/tr)r)r*rjz0SQLCompiler.get_extra_select..ru)r distinctdistinct_fieldsrrrN) r(rCr$rD select_sqlrKr\r]r^rr)r)r*r?szSQLCompiler.get_extra_selectcCs|||jvr |j|S||jjvr||jjvs)||jjvs)|jj|r0||jjvr0||j|<|S|jj |}||j|<|S)z A wrapper around connection.ops.quote_name that doesn't quote aliases for table names. This avoids problems with some SQL dialects that treat quoted strings specially (e.g. PostgreSQL). ) r#r r5 table_maprDexternal_aliasesgetr!ops quote_name)r(rrr)r)r*rs       z#SQLCompiler.quote_name_unless_aliascCsJt|d|jjd}|r|||j\}}||fS|||j\}}||fS)Nas_)rhr!vendorrG)r(node vendor_implr\r]r)r)r*rTs zSQLCompiler.compilec sjj}fddjjD}|js0tjj|D]\}}|js#|jr'td| r/tdqd}|D][}zE|jj sXjj rX|j |_|j gjj jj jjR|\}} |jjrs|jskd|}n|jssd|}||| ff7}Wq4ty|dks|d kr|rYq4w|stjjj|} |r|dkr| d 7} |jrdnd tfd d |D\} } d| | g} g}| D]}||q| |fS)Ncs$g|]}|s|jjqSr))is_empty get_compilerr"r!)r/r r1r)r*rjs z2SQLCompiler.get_combinator_sql..z>LIMIT/OFFSET not allowed in subqueries of compound statements.z:ORDER BY not allowed in subqueries of compound statements.r)zSELECT * FROM ({})z({})union differencez ALLz{}c3s"|] \}}||fVqdSN)format)r/r\args)bracesr)r*r2s z1SQLCompiler.get_combinator_sql..z {} )r!rkr r%supports_slicing_ordering_in_compoundziplow_mark high_markrr; values_selectclone set_valuesrDrrGr supports_parentheses_in_compoundrrr set_operatorsjoinrQ)r(rr4rk compilersr compilerpartspart_sql part_argscombinator_sql sql_parts args_partsr`r]partr))rr(r*get_combinator_sqls^         zSQLCompiler.get_combinator_sqlTFc- Cs|jj}z{|\}}}d}|o|jjdup|jj}|jj} |jj} | rBt | d | s6t d | | | |jj \} } nc|\} }|\}}|jdurY||jndgf\}}|jdurj||jndgf\}}dg} g} |jjr|jj| |\}}| |7} | |7} g}d}|j|D]-\}\}}}|rd||jj|f}n|rd|d|f}|d7}| |||q| d |d g|7} | ||jjrB|jjjrB|jrtd |r|jjjst d |jj }|jj!}|jj"}|jj#}|r|jjj$st d |r|jjj%st d|r(|jjj&s(t d|r5|jjj's5t d|jjj(|||)|d}|rP|jjj*rP| ||r_| d|| |g} |D]\}!}"| |!| |"qc| r| r}t+d|p|jj,}| dd | |j-rd}|r| d|| ||jj.r| /d|jjj0|jj1fi|jj2|rg}#|D]\}\}$}%}|#|$| |%q| dd |#|r| |jj3|jj|jj|r|jjj*s| ||jj4rs|rsg}&g}'t5|jddD]E\}(\})}}|s%|r%d|(}|r=|&d|jjd|jj|fq|)6|)j7di}*|*8||j\}+},|&|+|'|,qdd |&d| ft9|'| fW|j:|Sd| t9| fW|j:|S|j:|w)z Create the SQL for this query. Return the SQL string and list of parameters. If 'with_limits' is False, any limit/offset information is not included in the query. Nzsupports_select_{}z-{} is not supported on this database backend.SELECTruz%s AS %szCol%d, FROMz:select_for_update cannot be used outside of a transaction.zNLIMIT/OFFSET is not supported with select_for_update on this database backend.z1NOWAIT is not supported on this database backend.z6SKIP LOCKED is not supported on this database backend.z8FOR UPDATE OF is not supported on this database backend.zrr distinct_sqlr$rrQrNrselect_for_updatehas_select_for_updateget_autocommitr%supports_select_for_update_with_limitselect_for_update_nowaitselect_for_update_skip_lockedselect_for_update_ofselect_for_no_key_updatehas_select_for_update_nowait!has_select_for_update_skip_lockedhas_select_for_update_ofhas_select_for_no_key_updatefor_update_sql"get_select_for_update_of_argumentsfor_update_after_fromNotImplementedErrorforce_no_orderingr' explain_queryinsertexplain_query_prefixexplain_formatexplain_optionslimit_offset_sqlrrrelabeled_clonerdrGtuplereset_refcounts)-r( with_limitswith_col_aliasesrefcounts_beforerDrCrEfor_update_partwith_limit_offsetrrkr`r]rdistinct_paramsfrom_f_paramsr<w_paramsr>h_paramsdistinct_resultout_colscol_idxrYs_sqls_paramsrdrrrrgroupingg_sqlg_paramsro_sqlo_params sub_selects sub_paramsindexr$ select_clone subselect subparamsr)r)r*rGs   ""                  zSQLCompiler.as_sqlNc Csg}|dur |j}|}|p|j}d|i}|jD]?}|jjj}||jkr+d}|r;|dur;t|jj|jjr;q|j|vrI|j ||jvrIq|j ||||} | | } | | q|S)aT Compute the default columns for selecting every field in the base model. Will sometimes be called to pull in related models (e.g. via select_related), in which case "opts" and "start_alias" will be given to provide a starting point for the traversal. Return a list of strings, quoted appropriately for use in SQL directly, as well as a set of aliases used in the select statement (if 'as_pairs' is True, return a list of (alias, col_name) pairs instead of strings as the first component and None as the second component). N) r rdeferred_to_columnsr6concrete_fieldsrmrqconcrete_model issubclassattnamejoin_parent_modelget_colrN) r( start_aliasoptsrxr` only_load seen_modelsrrmrdcolumnr)r)r*rs,         zSQLCompiler.get_default_columnscCsg}g}|j}|jjD]E}|t}|||d\}}}} } }} |j|| | \}}}|D]"} ||jjvr=||q/| | | |\} }|| ||q/q ||fS)z Return a quoted list of fields to use in DISTINCT ON part of the query. This method can alter the tables in the query, and thus it must be called before get_from_clause(). N) r rrrr _setup_joins trim_joinsrrNrT)r(r`r]r4rrrYtargetsrdjoinspathtransform_functionrcrpr)r)r*rs       zSQLCompiler.get_distinctrc s8t||\}}|dk|t}||\}} } } }|jr|jrt|dd|dkr|dkr|p6t}tfdd| D} | |vrJt d| | g} |jD]/}t |d rjt |t sjrf|n|}t |t rw| |d fqT| ||||qT| Sj| | | \} }fd d | DS) z Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given 'name' parameter. The 'name' is of the form 'field1__field2__...__fieldN'. rr0Nrrc3s$|] }tjj|ddVqdS) join_colsN)rhr r5)r/jr1r)r*r2s"z1SQLCompiler.find_ordering_name..z!Infinite loop caused by ordering.rFcs"g|] }t|ddfqS)rF)r r)rdrr=r)r*rjs"z2SQLCompiler.find_ordering_name..)rrrr8 is_relationrrhrRrrrVrMrHr rrrNrQrr r9)r(rr4rdr already_seenrpiecesrr:r;r< join_tupleresultsitemrYr))rdrr(r=r*rs8     zSQLCompiler.find_ordering_namec CsD|p|j}|j|||\}}}}}}|d}|||||||fS)a$ Helper method for get_order_by() and get_distinct(). get_ordering() and get_distinct() must produce same target columns on same input, as the prefixes of get_ordering() and get_distinct() must match. Executing SQL where this is not true is an error. r?)r r6 setup_joins) r(rDr4rdrr:r;r<r=r)r)r*r8szSQLCompiler._setup_joinsc Csg}g}t|jjD],}|jj|sq z|jj|}Wn ty$Yq w||\}}||||q |jjD]"}|j |\}}||jjvsS|jj|dkr]|d| |q;||fS)a Return a list of strings that are joined together to go after the "FROM" part of the query, as well as a list any extra parameters that need to be included. Subclasses, can override this to create a from-clause via a "select". This should only be called after any SQL construction methods that might change the tables that are needed. This means the select columns, ordering, and distinct must be done first. ruz, %s) rr r5r.KeyErrorrTrNrQ extra_tables table_aliasr) r(r`r]rd from_clause clause_sql clause_paramsrrYr)r)r*rs&      zSQLCompiler.get_from_clauseruc" s\fdd}g}|s|jjkr|Ssjj}j} t} |dur8tjjt}|r8jj}dd} j D]j j j } | j|ro|ji} jsn| s_j|vrntdjd|pjdfnd } t||| | s|q?jj d jjrjjnd d d d }||g}jjg|\}}}}}}|d }j|jj j d}|D]}|t|||dfq||d<|jj j ||d| |}| ||q?|r,ddjD}|D]\}t||| |ddsq}| |j|g|}|jd }t|j o#|j u}|djjj|d }||g}j||j j d}|D]}|t|||dfqC||d<|i} ||j ||d| |}| ||qfdd}dd}t |D]}|dkrn~|jj!vr| |j|g|\}}}}}|j }|d }t|j o|j u}|d|t"|||d }||g}j||j j d}|D]}|t|||dfq||d<||i}j||j ||d||d}| ||qt|#| } | r,dd| D}!tdd|!d|p(df|S)a Fill in the information needed for a select_related query. The current depth is measured as the number of connections away from the root model (for example, cur_depth=1 means we are looking at models with direct connections to the root model). cs0ddjD}ddjD}t||jjS)Ncss|] }|jr|jVqdSr)rBrr/fr)r)r*r2;szQSQLCompiler.get_related_selections.._get_field_choices..css"|] }|jjr|jVqdSr)runiquerelated_query_namerOr)r)r*r2<s  )fieldsrelated_objectsrr _filtered_relations)direct_choicesreverse_choices)r4r(r)r*_get_field_choices:s z>SQLCompiler.get_related_selections.._get_field_choicesNcSs ||d<dS)Nrwr))r&rwr)r)r*get_related_klass_infosTs zCSQLCompiler.get_related_selections..get_related_klass_infoszCNon-relational field given in select_related: '%s'. Choices are: %srz(none)FcSsdSrr))xyr)r)r*ssz4SQLCompiler.get_related_selections..)rmrreverse local_setter remote_setterrxr?)r3r4rvrucSs&g|]}|jjr|js|j|jfqSr))rrQ many_to_many related_model)r/or)r)r*rjs z6SQLCompiler.get_related_selections..T)r])r3r4rxcs|r j||dSdSr) remote_fieldset_cached_value)objfrom_obj)rPr)r*r^sz8SQLCompiler.get_related_selections..local_settercSst|||dSr)setattr)rrerfr)r)r*r_sz9SQLCompiler.get_related_selections..remote_setter)r4 root_alias cur_depth requested restrictedcss|]}d|VqdS)z'%s'Nr)r/sr)r)r*r2z5SQLCompiler.get_related_selections..zBInvalid field name(s) given in select_related: %s. Choices are: %s)$r max_depthrr6get_loaded_field_namesrRrHrdictrSrmrqr.rVrrrBrrrrcrdrQrNrHrr8rrTrRr;r/listrUrr)"r(r$r4rhrirjrkrXrwr5 fields_foundrY field_modelnextr&rvrYr;rdcolumnsr[next_klass_infosrelated_fieldsrmrelated_field_name join_inforxr^r_r join_optsnext_requestedfields_not_foundinvalid_fieldsr))rPr4r(r*r2s                       z"SQLCompiler.get_related_selectionsc s4fddfdd}fdd}g}g}jjD]k}j}|dkr)||}n@|tD],}g|dg|R} | D]} | d } | d rL| j} | j|krU| }nq?d }nq.|d ure||q||}|d urj j j r}| |d q| |jq|rtd d|d|f|S)zk Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query. c3sX|djj}|jjD]\}j|dfdd|dDdVq dS)NrmFcs<g|]}j|djjksj|djjvr|qSr)r$rcrm)r/ select_index) parent_list parent_modelr(r)r*rjs zbSQLCompiler.get_select_for_update_of_arguments.._get_parent_klass_info..rv)rmrr]rv)rqr.parentsr}get_parent_list)r&r. parent_linkr1)rrr*_get_parent_klass_infos   zNSQLCompiler.get_select_for_update_of_arguments.._get_parent_klass_infocsF|djj}|dD]}j|djj|kr j|dSq dS)z Find the first selected column from a model. If it doesn't exist, don't lock a model. select_fields is filled recursively, so it also contains fields from the parent models. rmrvrN)rqr.r$rcrm)r&r.rr1r)r*"_get_first_selected_col_from_models  zZSQLCompiler.get_select_for_update_of_arguments.._get_first_selected_col_from_modelc3stdjfg}|rU|\}}|durgdVn|d}|dr'|j}||jgtV|fdd|D|fdd| dgD|s dSdS) z._get_field_choices..c3rrr)rrr)r*r2rrw) collectionsdequer&popleftrcrrrrQr)queue parent_pathr&rrr(rr*rXs&     zJSQLCompiler.get_select_for_update_of_arguments.._get_field_choicesr(rwrr]NrzInvalid field name(s) given in select_for_update(of=(...)): %s. Only relational fields followed in the query are allowed. Choices are: %s.r)r rr&rrrrcrrNr!rkselect_for_update_of_columnrTrrdrr) r(rrXr` invalid_namesrr&r[r klass_infosrelated_klass_inforr)rr*rsX          z.SQLCompiler.get_select_for_update_of_argumentscCsi}|j||jj|S)z Convert the self.deferred_loading data structure to mapping of table names to sets of column names which are to be loaded. Return the dictionary. )r deferred_to_dataget_loaded_field_names_cb)r(rvr)r)r*r,GszSQLCompiler.deferred_to_columnscCsPi}t|D]\}}|r%|jj|}||j}|s|r%|||f||<q|Sr)rr!rget_db_converters)r(rW convertersi expressionbackend_convertersfield_convertersr)r)r*get_convertersQs zSQLCompiler.get_convertersc csf|j}t|}tt|D]!}|D]\}\}}||}|D]} | |||}q|||<q|VqdSr)r!rrr}map) r(rowsrr!rowposconvsrvalue converterr)r)r*apply_converters[s  zSQLCompiler.apply_converterscCsh|dur |jt||d}dd|jd|jD}||}t|}|r2|||}|r2tt |}|S)z>Return an iterator over the results from executing this query.N) chunked_fetch chunk_sizecSrrr)rlr)r)r*rjkrz,SQLCompiler.results_iter..r) execute_sqlrr$r9rr from_iterablerrr)r(rFtuple_expectedrrrSrrr)r)r* results_iterfs    zSQLCompiler.results_itercCst|tS)z Backends (e.g. NoSQL) can override this in order to use optimized versions of "query has any results." )r@rrr1r)r)r* has_resultstszSQLCompiler.has_resultsc CsD|pt}z |\}}|stWnty$|tkr!tgYSYdSw|r-|j}n|j}z|||Wn t yF| w|t krM|S|t krpz| }|rd|d|jW| S|W| S| w|tkrz| dSt||jjj|jr|jnd|}|r|jjjsz t|W| S| w|S)a Run the query against the database and return the result(s). The return value is a single data item if result_type is SINGLE, or an iterator over the results if the result_type is MULTI. result_type is either MULTI (use fetchmany() to retrieve all rows), SINGLE (only retrieve a single row), or None. In this last case, the cursor is returned if any query is executed, since it's used by subclasses such as InsertQuery). It's possible, however, that no query is needed, as the filters describe an empty set. In that case, None is returned, to avoid any unnecessary database interaction. Nr)rrGrriterr!chunked_cursorcursorexecute Exceptioncloserrfetchoner9 cursor_iterrkempty_fetchmany_valuerAcan_use_chunked_readsrr) r( result_typerrr\r]rvalr`r)r)r*r{sV         zSQLCompiler.execute_sqlc Cs|j}|jjj}t|jjD]&\}}||\}} d|||||f} |jj t d|| f| dq| \} } d| | fS)Nr%s = %sANDz EXISTS (%s)) rr!rrrr r$rTr<rVr rG) r(rdrvrqnqn2r( select_collhs_sql lhs_paramsrhsr\r]r)r)r*as_subquery_conditions   z!SQLCompiler.as_subquery_conditionccsHt|}|dD]}t|tsddd|DVq |Vq dS)Nrrcss|]}t|VqdSr)str)r/cr)r)r*r2rnz,SQLCompiler.explain_query..)rrrrHrr)r(r`rr)r)r*r s   zSQLCompiler.explain_query)TF)NNN)NrN)NNruNN)%__name__ __module__ __qualname__rre MULTILINEDOTALLrr+r:rFrBrSr7r;r?rrTrrGrrrr8rrrr,rrrrrrrrr r)r)r)r*rsR V2H  7 , ) " 1e    C rc@sHeZdZdZeZddZddZddZdd Z d d Z dd d Z dS)SQLInsertCompilerNcCsv|dur |g}}n%t|dr||\}}nt|dr)||||j|g}}nd|g}}|jj||}||fS)at Take a field and a value intended to be saved on that field, and return placeholder SQL and accompanying params. Check for raw values, expressions, and fields with get_placeholder() defined in that order. When field is None, consider the value raw and use it as the placeholder, with no corresponding parameters returned. NrGget_placeholder%s)rMrTrr!rmodify_insert_params)r(rrr\r]r)r)r* field_as_sqls    zSQLInsertCompiler.field_as_sqlcCszt|dr3|j|jddd}|jrtd||f|jr%td|j|f|jr1td|j|f|S|j ||j d}|S) z Prepare a value to be used in a query by resolving it if it is an expression and otherwise calling the field's get_db_prep_save(). rFTrfor_savezbFailed to insert expression "%s" on %s. F() expressions can only be used to update, not to insert.:Aggregate functions are not allowed in this query (%s=%r).9Window expressions are not allowed in this query (%s=%r).r!) rMrr contains_column_references ValueErrorcontains_aggregaterrcontains_over_clauseget_db_prep_saver!)r(rrr)r)r* prepare_values. zSQLInsertCompiler.prepare_valuecCs"|jjr t||jS|j|ddS)z Get the given field's value off the given obj. pre_save() is used for things like auto_now on DateTimeField. Skip it if this is a raw query. T)rV)r rawrhr0pre_save)r(rrer)r)r* pre_save_vals zSQLInsertCompiler.pre_save_valcsP|sggfSfdd|D}dd|D}t|\}}dd|D}||fS)a+ Take a sequence of N fields and a sequence of M rows of values, and generate placeholder SQL and parameters for each field and value. Return a pair containing: * a sequence of M rows of N SQL placeholder strings, and * a sequence of M rows of corresponding parameter values. Each placeholder string may contain any number of '%s' interpolation strings, and each parameter row will contain exactly as many params as the total number of '%s's in the corresponding placeholder row. c3s(|]}fddt|DVqdS)c3s |] \}}||VqdSr)r)r/rvr1r)r*r22r3z>SQLInsertCompiler.assemble_as_sql...Nrr/rrSr(r)r*r21s  z4SQLInsertCompiler.assemble_as_sql..css|]}t|VqdSrrrr)r)r*r28rncSsg|] }dd|DqS)cSsg|] }|D]}|qqSr)r)r/psr>r)r)r*rj?z@SQLInsertCompiler.assemble_as_sql...r)rr)r)r*rj?rz5SQLInsertCompiler.assemble_as_sql..r)r(rS value_rowsrows_of_fields_as_sqlsql_and_param_pair_rowsplaceholder_rows param_rowsr)rr*assemble_as_sql s   z!SQLInsertCompiler.assemble_as_sqlc sjjjj}jjjjjd}d||jfgjjp%|j g dd fddDjjrHfddjj D}nfd djj D}dgj o^jjj}|\}}jjjjjd}j rɈjjjrɈjjjr jj||}n d d |d |d g}|r |jjj \} _| r | |jg7}d tt|fgS|r jj||r݈ |d td d|DfgS|r |fddt||DS)N)ignore_conflictsz%s %sz(%s)rc3s|]}|jVqdSr)r7rO)rr)r*r2Ksz+SQLInsertCompiler.as_sql..cs g|] fddDqS)c s g|] }||qSr))rr)r/r)rer(r)r*rjOs z7SQLInsertCompiler.as_sql...r))r/r)rer*rjNsz,SQLInsertCompiler.as_sql..csg|] }jjgqSr))r!rpk_default_value)r/rYr1r)r*rjTr VALUES (%s)rrcss|] }|D]}|VqqdSrr)rr)r)r*r2wsc s.g|]\}}ddd|g|fqS)rrr)r)r/r>vals)r`r)r*rj{s)r!rrr rinsert_statementrdb_tablerSrrrNrobjsreturning_fieldsrkhas_bulk_insertrignore_conflicts_suffix_sqlcan_return_columns_from_insert can_return_rows_from_bulk_insertbulk_insert_sqlreturn_insert_columnsreturning_paramsrrrr) r(r4rrcan_bulkrrrr]r_sqlr))rSrr`r(r*rGCsP  "         zSQLInsertCompiler.as_sqlcCs*|rt|jjdkr|jjjsJ||_|jr}|D] \}}| ||q|js5gWdS|jjjrPt|jjdkrP|jj |WdS|jjj rpt|jjdks_J|jj ||jgWdS|jj ||jj|jjjfgWdS1swYdS)Nru)r8r rr!rkrrrrGrrfetch_returned_insert_rowsrfetch_returned_insert_columnsrlast_insert_idrrrrr7)r(rrr\r]r)r)r*rs.    $zSQLInsertCompiler.execute_sqlr) rrrrrrrrrrrGrr)r)r)r*rs  #=rc@s@eZdZeddZeddZeddZddZd d Z d S) SQLDeleteCompilercs(jtfddjjDdkS)Nc3s |] }jj|dkVqdSr,r-rr1r)r*r2r3z1SQLDeleteCompiler.single_alias..ru)r r6sumr5r1r)r1r* single_aliass zSQLDeleteCompiler.single_aliascs>t|tr |jkSt|dsdStfdd|DS)NrFc3s|] }|VqdSr)_expr_refs_base_model)r/ source_expr base_modelclsr)r*r2s  z:SQLDeleteCompiler._expr_refs_base_model..)rHrrmrManyr)rrKrr)rr*rs   z'SQLDeleteCompiler._expr_refs_base_modelcs*tfddtjjjjjDS)Nc3s |] }|jjVqdSr)rr rmrJr1r)r*r2s  zESQLDeleteCompiler.contains_self_reference_subquery..)rrr rvaluesr<childrenr1r)r1r* contains_self_reference_subquerys z2SQLDeleteCompiler.contains_self_reference_subquerycCsFd||jg}||j\}}|r|d|d|t|fS)NzDELETE FROM %srr)rrsrTr<rNrr)r(r r`r<r]r)r)r*_as_sqls zSQLDeleteCompiler._as_sqlcCs|jr |js ||jS|j}t|_||jjj j }| |j g|_ t|jj}|j|_|jjjsK|j|jd\}}td||}|t|d||S)f Create the SQL for this query. Return the SQL string and list of parameters. rzSELECT * FROM (%s) subquery)pk__in)rr r r rr __class__clear_select_clausermrqrrr2r6r$ where_classr<r!rkupdate_can_self_selectrrGr add_qr)r(innerqrrouterqr\r]r)r)r*rGs        zSQLDeleteCompiler.as_sqlN) rrrrr classmethodrr r rGr)r)r)r*rs    rcs0eZdZddZfddZfddZZS)SQLUpdateCompilercCs||jjs dS|j}gg}}|jjD]\}}}t|drB|j|jddd}|jr5td|j|f|j rAtd|j|fn't|dra|j rV|j | ||j d }ntd |||jjf|j ||j d }t|d rw||||j }nd }|j}t|d r||\} } |d|||| f|| q|dur|d|||f||q|d||q|jj} d|| d|g} ||jj\} } | r| d| d| t|| fS)r )rr)rFTrrrprepare_database_saverzSTried to update field %s with a model instance, %r. Use a value compatible with %s.rrrGrNz %s = NULLz UPDATE %s SETrrr)rFr rrrMrrrrrrcrrr! TypeErrorrrrr7rTrNrQrsrr<r)r(rr update_paramsrrmr placeholderrr\r]rr`r<r)r)r*rGsj         zSQLUpdateCompiler.as_sqlcszt|}z|r |jnd}|du}W|r|n|r!|ww|jD]}||j|}|r:|r:|}d}q'|S)a Execute the specified update. Return the number of rows affected by the primary update query. The "primary update query" is the first non-empty query that is executed. Row counts for any subsequent, related queries are not available. rNF)superrrowcountrr get_related_updatesrr")r(rrrrr aux_rowsrr)r*rs"   zSQLUpdateCompiler.execute_sqlcs|jj}|j|j}|jjs|dkrdS|jjtd}d|_|j ddi|_ g|_ | | jjgt|dkoH|jjj }|j|j_|jjsV|r{g}||jtD] }|dd|Dqa|jd |f||j_n|jd |f|j|dS) aO If the update depends on results from other tables, munge the "where" conditions to match the format required for (portable) SQL updates. If multiple updates are required, pull out the id values to update at this point so that they don't change as a result of the progressive updates. ruN)klassFT)forcecss|]}|dVqdSr,r)r/rr)r)r*r2Grnz2SQLUpdateCompiler.pre_sql_setup..r )r r.rr6count_active_tablesrelated_updatesrrrclear_orderingrr$ add_fieldsrrrrrrFr!rkrrr<rr"rrrQ add_filter related_idsr)r(rcountr must_pre_selectidentsrrr)r*rF%s,      zSQLUpdateCompiler.pre_sql_setup)rrrrGrrF __classcell__r)r)rr*rs ?rc@seZdZddZdS)SQLAggregateCompilercCsgg}}|jjD]}||\}}||||\}}||||q t|jj|_d |}t |}|jj |j jdd\}}d||f}||}||fS)r rT)rr)r rrrTrUrNrQr8r9rr inner_queryrr"rG)r(r\r]rann_sql ann_paramsinner_query_sqlinner_query_paramsr)r)r*rGQs"     zSQLAggregateCompiler.as_sqlN)rrrrGr)r)r)r*r-Ps r-c#sVz$tfdd|D]}dur|nfdd|DVq WdSw)z[ Yield blocks of rows from a cursor and ensure the cursor is closed when done. cs Sr) fetchmanyr))ritersizer)r*r\ns zcursor_iter..Ncsg|]}|dqSrr)r")r9r)r*rjoszcursor_iter..)rr)rsentinelr9r4rr))r9rr4r*rhs "r)2rr functoolsr itertoolsrdjango.core.exceptionsrr django.dbrrdjango.db.models.constantsrdjango.db.models.expressionsr r r r r django.db.models.functionsrrdjango.db.models.query_utilsrrdjango.db.models.sql.constantsrrrrrrdjango.db.models.sql.queryrrdjango.db.transactionrdjango.utils.functionalrdjango.utils.hashablerdjango.utils.regex_helperrrrrrr-rr)r)r)r*sB        I@;