https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/django/db/models/sql/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/django/db/models/sql/__pycache__/compiler.cpython-310.pyc
o

��Ihj%�@sJddlZddlZddlmZddlmZddlmZmZddl	m
Z
mZddlm
Z
ddlmZmZmZmZmZddlmZmZdd	lmZmZdd
lmZmZmZmZmZm Z ddl!m"Z"m#Z#ddl$m%Z%dd
l&m'Z'ddl(m)Z)ddl*m+Z+Gdd�d�Z,Gdd�de,�Z-Gdd�de,�Z.Gdd�de,�Z/Gdd�de,�Z0dd�Z1dS)�N)�partial)�chain)�EmptyResultSet�
FieldError)�
DatabaseError�NotSupportedError)�
LOOKUP_SEP)�F�OrderBy�RawSQL�Ref�Value)�Cast�Random)�Q�select_related_descend)�CURSOR�GET_ITERATOR_CHUNK_SIZE�MULTI�
NO_RESULTS�	ORDER_DIR�SINGLE)�Query�
get_order_dir)�TransactionManagementError)�cached_property)�
make_hashable)�_lazy_re_compilec@seZdZedejejB�Zdd�Zdd�Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd=dd�Zd>dd�Zdd �Z	!	d?d"d#�Zd$d%�Zd&d'�Z	(	d@d)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zdddefd3d4�Zd5d6�Z e!defd7d8�Z"d9d:�Z#d;d<�Z$dS)A�SQLCompilerz^(.*)\s(?:ASC|DESC).*cCs8||_||_||_ddi|_d|_d|_d|_d|_dS)N�*)�query�
connection�using�quote_cache�select�annotation_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__csHt�fdd��jjD��r�j�����\�_�_�_t�j��_	dS)Nc3s �|]}�jj|dkVqdS�rN�r �alias_refcount)�.0�a�r(r)r*�	<genexpr>,��z*SQLCompiler.setup_query.<locals>.<genexpr>)
�allr �	alias_map�get_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_having�having�get_extra_selectr$�bool�has_extra_select�get_group_by)r(�order_by�extra_select�group_byr)r)r*�
pre_sql_setup1s

zSQLCompiler.pre_sql_setupcCs\|jjdurgSg}|jjdur+|jjD]}t|d�s%|�|j�|��q|�|�qdd�|D�}|D]\}}}||vr>q4|��}|D]}|�|�qDq4|D]\}\}	}
}|s_|�|���qO|jrh|j��nd}|D]}|�|�qlg}
t�}|�	||�}|D]*}|�
|�\}	}
|�||	|
�\}	}
t|
�}|	|f|vr�|
�|	|
f�|�
|	|f�q�|
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".
        NT�as_sqlcSsh|]
}t|t�r|j�qSr))�
isinstancer�source�r/�exprr)r)r*�	<setcomp>us

��z+SQLCompiler.get_group_by.<locals>.<setcomp>r))r rE�hasattr�append�resolve_ref�get_group_by_cols�extendr>�set�collapse_group_by�compile�
select_formatr�add)r(r$rC�expressionsrK�ref_sources�_�cols�col�sql�params�is_ref�having_group_by�result�seen�params_hashr)r)r*rB?sJ%
����zSQLCompiler.get_group_bycs��jjjr>d}|D]}t|dd��jjjjkr%t|dd��jjkr%|}nq	|r<dd�|D��|g��fdd�|D�}|S�jjj	r]�fdd�|D��dd��D����fd	d�|D�}|S)
N�target�aliascSs$h|]}t|d�r|jjr|j�qS�rc)rMrc�primary_keyrdrJr)r)r*rL�s���z0SQLCompiler.collapse_group_by.<locals>.<setcomp>cs2g|]}|�vst|dd�dur|j�vr|�qS�rdN)�getattrrdrJ)r>�
pk_aliasesr)r*�
<listcomp>�s�z1SQLCompiler.collapse_group_by.<locals>.<listcomp>cs4h|]}t|d�r|jjr�jj�|jj�r|�qSre)rMrcrfr!�features�%allows_group_by_selected_pks_on_model�modelrJr1r)r*rL�s����cSsh|]}|j�qSr))rdrJr)r)r*rL�scs(g|]}|�vst|dd��vr|�qSrg)rhrJ)�aliases�pksr)r*rj�s")
r!rk�allows_group_by_pkrhr rm�_meta�pk�
base_table�allows_group_by_selected_pks)r(rWr>rrrKr))rnr>riror(r*rS�s2
���
�
��zSQLCompiler.collapse_group_byc	s�g}d}i}d}|jj��D]\}\}}|||<|�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
y�d\}}Yn
w|
�|||�\}}|
�|
||f|f�q�|
||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_fields�related_klass_infoscs6|dD]}|dr|d|d|d<�|�qdS)Nrw�from_parentrvr))r&�ki��get_select_from_parentr)r*r{�s�
�z6SQLCompiler.get_select.<locals>.get_select_from_parent)�0r))r rD�itemsrNrr$�default_cols�get_default_columnsrm�annotation_select�select_related�get_related_selectionsrTrrU)r(r$r&�annotations�
select_idxrdr\r]rZ�select_listr[�
annotationrw�retr)rzr*r7�sP



�

�
zSQLCompiler.get_selectc	CsF|jjr	|jj}n$|jjs|jj}n|jjr|jj}n|j��jr+|j��j}||_ng}|jjr8td\}}ntd\}}g}|D�]}t	|d�rrt
|t�rUt||j
�}t
|t�s^|��}|jjsj|��}|��|�|df�qB|dkr�|�tt��df�qBt||�\}}|dk}||jjvr�|�tt||jj|�|d�df�qB||jjvr�|jjr�|jr�t|�}	n|jj|}	t
|	t�r�t|	|	j
�}	|�t|	|d�df�qBd|vr�|�dd	�\}
}|�ttd
|�|
�|fg�|d�df�qB|jj�r||jjv�r(|jj�r|j�r|�tt|�|d�df�qB|�|j ||j��|d��qB||jj!v�rA|�tt|jj|�|d�df�qB|�tt|t|jj|��|d�df�qBg}t"�}|D]�\}	}
|	j#|jddd
�}|jj�r�|j�r�|�$�d}|	�$�d}t%|j�D]:\}\}}}|
�r�||j&k�r�|j'}n|�r�t
|t��r�||j(k�s��q�||k�r�|�)td|d	d�g�n5�q�|�r�t*d��t+|jj�d	}d|��}|jj,D]	}|�-||��q�|j�.||�|�)t|�d�g�|�/|�\}}|j0�1|�d	}t2|�}||f|v�r�q]|�3||f�|�||||
ff��q]|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).
        �ASC�DESC�resolve_expressionF�?��
descendingT�.ru�%s.%s)�
default_orderN)�allow_joins�reuserz%dr)z:ORDER BY term does not match any column in the result set.�__orderbycol)4r �extra_order_by�default_orderingrC�get_meta�orderingr'�standard_orderingrrMrHr
r�output_fieldr
�asc�copy�reverse_orderingrNrrr�rr��
combinatorr$r	�splitr�quote_name_unless_alias�extrarQ�find_ordering_namerDrRr��get_source_expressions�	enumerate�refsrI�name�set_source_expressionsrr8�combined_queries�add_annotation�add_select_colrT�ordering_parts�searchrrV)r(r�r��descrC�fieldr[�orderr�rK�tabler`rar^�resolved�src�expr_src�idx�sel_exprrY�	col_alias�order_by_idx�col_name�qr\r]�without_orderingrbr)r)r*r;s�	






�

��
����
�
zSQLCompiler.get_order_byc
Csng}|jjr5|jjs5dd�|D�}|D]!\}\}}}|j�|�d}	|s4|	|f|vr4|�||	|fdf�q|S)NcS�g|]}|d�qS)rur)�r/�tr)r)r*rj��z0SQLCompiler.get_extra_select.<locals>.<listcomp>ru)r �distinct�distinct_fieldsr�r�rN)
r(rCr$rD�
select_sqlrKr\r]r^r�r)r)r*r?�s�zSQLCompiler.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_maprD�external_aliases�getr!�ops�
quote_name)r(r��rr)r)r*r��s

�

z#SQLCompiler.quote_name_unless_aliascCsJt|d|jjd�}|r|||j�\}}||fS|�||j�\}}||fS)N�as_)rhr!�vendorrG)r(�node�vendor_implr\r]r)r)r*rT�s�zSQLCompiler.compilec	s��jj}�fdd��jjD�}|js0t�jj|�D]\}}|js#|jr'td��|�	�r/td��qd}|D][}zE|jj
sX�jj
rX|j��|_|j�g�jj
��jj
��jj�R�|��\}}	|jjrs|jskd�|�}n|jssd�|�}|||	ff7}Wq4ty�|dks�|d	kr�|r�Yq4�w|s�t��jjj|}
|r�|dkr�|
d
7}
|jr�dnd�t�fdd
�|D��\}}d�|
��|�g}
g}|D]}|�|�q�|
|fS)Ncs$g|]}|��s|��j�j��qSr))�is_empty�get_compilerr"r!)r/r r1r)r*rj�s
��z2SQLCompiler.get_combinator_sql.<locals>.<listcomp>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"�|]\}}��|�|fVqdS�N)�format)r/r\�args)�bracesr)r*r2�s� z1SQLCompiler.get_combinator_sql.<locals>.<genexpr>z {} )r!rkr r��%supports_slicing_ordering_in_compound�zip�low_mark�	high_markrr;�
values_select�clone�
set_valuesrDr�rGr�� supports_parentheses_in_compoundr�rr��
set_operators�joinrQ)r(r�r4rk�	compilersr �compiler�parts�part_sql�	part_args�combinator_sql�	sql_parts�
args_partsr`r]�partr))r�r(r*�get_combinator_sql�s^
�����
�zSQLCompiler.get_combinator_sqlTFc-	Cs|jj��}�z{|��\}}}d}|o|jjdup|jj}|jj}	|jj}
|	rBt	|
d�
|	��s6td�
|	���|�|	|jj
�\}}�nc|��\}
}|��\}}|jdurY|�|j�ndgf\}}|jdurj|�|j�ndgf\}}dg}g}|jjr�|jj�|
|�\}}||7}||7}g}d}|j|D]-\}\}}}|r�d||jj�|�f}n|r�d|d|f}|d7}|�|�|�|�q�|d	�|�d
g|�7}|�|�|jj�rB|jjj�rB|j��r�td��|r�|jjjs�td��|jj }|jj!}|jj"}|jj#}|�r|jjj$�std
��|�r|jjj%�std��|�r(|jjj&�s(td��|�r5|jjj'�s5td��|jjj(|||�)�|d�}|�rP|jjj*�rP|�|�|�r_|�d|�|�|�g} |D]\}!}"| �|!�|�|"��qc| �r�|
�r}t+d��|�p�|jj�,�}|�dd	�| ��|j-�r�d}|�r�|�d|�|�|�|jj.�r�|�/d|jjj0|jj1fi|jj2���|�r�g}#|D]\}\}$}%}|#�|$�|�|%��q�|�dd	�|#��|�r�|�|jj�3|jj|jj��|�r|jjj*�s|�|�|jj4�rs|�rsg}&g}'t5|jdd�D]E\}(\})}}|�s%|�r%d|(}|�r=|&�d|jj�d�|jj�|�f��q|)�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.z<FOR NO KEY UPDATE is not supported on this database backend.)�nowait�skip_locked�of�no_key�WHERE %sz1annotate() + distinct(fields) is not implemented.zGROUP BY %sz	HAVING %srzORDER BY %s)�startzcol%dr��subquery�SELECT %s FROM (%s) subquery� );r r.r�rFr�r�r�r!rkrhr�rr��combinator_all�get_distinct�get_from_clauser<rTr>r�r��distinct_sqlr$r�rQrNr��select_for_update�has_select_for_update�get_autocommitr�%supports_select_for_update_with_limit�select_for_update_nowait�select_for_update_skip_locked�select_for_update_of�select_for_no_key_update�has_select_for_update_nowait�!has_select_for_update_skip_locked�has_select_for_update_of�has_select_for_no_key_update�for_update_sql�"get_select_for_update_of_arguments�for_update_after_from�NotImplementedError�force_no_orderingr'�
explain_query�insert�explain_query_prefix�explain_format�explain_options�limit_offset_sqlr�r��relabeled_clonerdrG�tuple�reset_refcounts)-r(�with_limits�with_col_aliases�refcounts_beforerDrCrE�for_update_part�with_limit_offsetr�rkr`r]r��distinct_params�from_�f_paramsr<�w_paramsr>�h_params�distinct_result�out_cols�col_idxrY�s_sql�s_paramsrdr�r�r�r��grouping�g_sql�g_paramsr��o_sql�o_params�sub_selects�
sub_params�indexr$�select_clone�	subselect�	subparamsr)r)r*rG�s�""�


���




��

�
�
��zSQLCompiler.as_sqlNcCs�g}|dur|j��}|��}|p|j��}d|i}|jD]?}|jjj}||jkr+d}|r;|dur;t|jj|jj�r;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 r��deferred_to_columnsr6�concrete_fieldsrmrq�concrete_model�
issubclass�attname�join_parent_model�get_colrN)r(�start_alias�optsrxr`�	only_load�seen_modelsr�rmrd�columnr)r)r*r�s,



��
zSQLCompiler.get_default_columnscCs�g}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 r�r�r�r�_setup_joins�
trim_joinsr�rNrT)r(r`r]r4r�r�rY�targetsrd�joins�path�transform_functionrcr��pr)r)r*r��s


�zSQLCompiler.get_distinctr�c
s8t||�\}}|dk�|�t�}��||��\}}	�}
}}�|jr�|jr�t|dd�|dkr�|dkr�|p6t�}t�fdd�|
D��}||vrJt	d��|�
|�g}
|jD]/}t|d	�rjt|t
�sj�rf|��n|��}t|t
�rw|
�|d
f�qT|
���||�||��qT|
S�j�|	|
|�\}	�}���fdd�|	D�S)
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'.
        r�r0N���rrc3s$�|]
}t�jj|dd�VqdS)�	join_colsN)rhr r5)r/�jr1r)r*r2�s�"z1SQLCompiler.find_ordering_name.<locals>.<genexpr>z!Infinite loop caused by ordering.r�Fcs"g|]
}t�|���d�df�qS)r�F)r
r�)rdr�r=r)r*rjs"z2SQLCompiler.find_ordering_name.<locals>.<listcomp>)rr�rr8�is_relationr�rhrRrrrVrMrHr
r�r�rNrQr�r r9)r(r�r4rdr��already_seenr��piecesr�r:r;r<�
join_tuple�results�itemrYr))rdr�r(r=r*r��s8
��



�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(rDr4rdr�r:r;r<r=r)r)r*r8szSQLCompiler._setup_joinsc		Cs�g}g}t|jj�D],}|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_paramsr�rYr)r)r*r�s&�
�zSQLCompiler.get_from_clauseruc"	s\��fdd�}g}|s|�jjkr|S�s�j����j��}�j��}	t�}
|dur8t�jjt�}|r8�jj}dd�}�j	D]���j
jj}|
�
�j�|ro|��ji�}
�jsn|
s_�j|vrntd�jd�|��pjdf��nd	}
t�|||	�|��s|q?�jj
�d	�j�jr��jjnd
d�d	d�}|�|�g}�j��jg�|�\}}}}}}|d
}�j|�jj
jd�}|D]}|�t|��|�|df�q�||d<��|�jj
j||d|
|�}|||�q?|�r,dd��jD�}|D]�\�}t�|||	�|�dd��sq���}|
�
|��j�|g�|�}|jd
}t|�j
��o#|�j
u}|�d�jj�j|d�}|�|�g}�j||j�j
d�}|D]}|�t|��|�|df��qC||d<|����i�}
��||j||d|
|�}|||�q�fdd�}dd�}t |�D]�}|dk�r�n~|�jj!v�r|
�
|��j�|g�|�\�}}}}}|j
}|d
}t|�j
��o�|�j
u}|�d|t"||�|d�}|�|�g}�j||j�j
d�}|D]}|�t|��|�|df��q�||d<|�|i�}�j||j||d||d�}|||��q�t|��#|
�} | �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).
        cs0dd��jD�}dd��jD�}t||�jj�S)Ncss�|]	}|jr|jVqdSr�)rBr��r/�fr)r)r*r2;s�zQSQLCompiler.get_related_selections.<locals>._get_field_choices.<locals>.<genexpr>css"�|]}|jjr|j��VqdSr�)r��unique�related_query_namerOr)r)r*r2<s��
�)�fields�related_objectsrr �_filtered_relations)�direct_choices�reverse_choices)r4r(r)r*�_get_field_choices:s
�z>SQLCompiler.get_related_selections.<locals>._get_field_choicesNcSs||d<dS)Nrwr))r&rwr)r)r*�get_related_klass_infosTszCSQLCompiler.get_related_selections.<locals>.get_related_klass_infoszCNon-relational field given in select_related: '%s'. Choices are: %sr�z(none)FcSsdSr�r))�x�yr)r)r*�<lambda>ssz4SQLCompiler.get_related_selections.<locals>.<lambda>)rmr��reverse�local_setter�
remote_setterrxr?)r3r4rvrucSs&g|]}|jjr|js|j|jf�qSr))r�rQ�many_to_many�
related_model)r/�or)r)r*rj�s��
�z6SQLCompiler.get_related_selections.<locals>.<listcomp>T)r])r3r4rxcs|r�j�||�dSdSr�)�remote_field�set_cached_value)�obj�from_obj)rPr)r*r^�s�z8SQLCompiler.get_related_selections.<locals>.local_settercSst|||�dSr�)�setattr)r�rerfr)r)r*r_�sz9SQLCompiler.get_related_selections.<locals>.remote_setter)r4�
root_alias�	cur_depth�	requested�
restrictedcss�|]}d|VqdS)z'%s'Nr)�r/�sr)r)r*r2���z5SQLCompiler.get_related_selections.<locals>.<genexpr>zBInvalid field name(s) given in select_related: %s. Choices are: %s)$r �	max_depthr�r6�get_loaded_field_namesrRrHr��dictrSrmrqr.rVr�r�rBrr�rrcrdrQrNrHrr8r�rTrRr;r/�listrUrr�)"r(r$r4rhrirjrkrXrwr5�fields_foundrY�field_model�nextr&rvrYr;rd�columnsr[�next_klass_infos�related_fieldsrm�related_field_name�	join_inforxr^r_r��	join_opts�next_requested�fields_not_found�invalid_fieldsr))rPr4r(r*r�2s




������

���
�

�

��

�
��
����z"SQLCompiler.get_related_selectionscs4�fdd���fdd�}��fdd�}g}g}�jjD]k}�j}|dkr)||�}n@|�t�D],}g|�dg���|��R}	|	D]}
|
d	}|
d
rL|j}|j|krU|
}nq?d}nq.|dure|�|�q||�}|dur��j	j
jr}|���|�d�q|���
|j��q|r�td
d�|�d�|��f��|S)zk
        Return a quoted list of arguments for the SELECT FOR UPDATE OF part of
        the query.
        c3sX�|djj}|jj��D]\�}�j����|d���fdd�|dD�d�Vq
dS)NrmFcs<g|]}�j|djj�ks�j|djj�vr|�qS�r)r$rcrm)r/�select_index)�parent_list�parent_modelr(r)r*rj�s�zbSQLCompiler.get_select_for_update_of_arguments.<locals>._get_parent_klass_info.<locals>.<listcomp>rv)rmr�r]rv)rqr.�parentsr}�get_parent_list)r&r.�parent_linkr1)r�r�r*�_get_parent_klass_info�s�
�
��zNSQLCompiler.get_select_for_update_of_arguments.<locals>._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.r�r1r)r*�"_get_first_selected_col_from_model�s��zZSQLCompiler.get_select_for_update_of_arguments.<locals>._get_first_selected_col_from_modelc3s��t�d�jfg�}|rU|��\}}|durg�dVn|d}|dr'|j}||jg�t���V|��fdd��|�D��|��fdd�|�	dg�D��|sdSdS)	z<Yield all allowed field paths in breadth-first search order.Nr(r�r]c3��|]}�|fVqdSr�r)�r/r&�r<r)r*r2�
��
�z]SQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choices.<locals>.<genexpr>c3r�r�r)r�r�r)r*r2r�rw)
�collections�dequer&�popleftrcr�rr�rQr�)�queue�parent_pathr&r��r�r(r�r*rXs&��
��zJSQLCompiler.get_select_for_update_of_arguments.<locals>._get_field_choicesr(rwr�r]Nrz�Invalid field name(s) given in select_for_update(of=(...)): %s. Only relational fields followed in the query are allowed. Choices are: %s.r�)r r�r&r�rr�rcr�rNr!rk�select_for_update_of_columnrTr�rdrr�)r(r�rXr`�
invalid_namesr�r&r[r��klass_infos�related_klass_infor�r)r�r*r�sX


��
��

�
���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_data�get_loaded_field_names_cb)r(rvr)r)r*r,GszSQLCompiler.deferred_to_columnscCsPi}t|�D]\}}|r%|jj�|�}|�|j�}|s|r%|||f||<q|Sr�)r�r!r��get_db_converters)r(rW�
converters�i�
expression�backend_converters�field_convertersr)r)r*�get_convertersQs�zSQLCompiler.get_convertersc
csf�|j}t|���}tt|�D]!}|D]\}\}}||}|D]}	|	|||�}q|||<q|VqdSr�)r!rrr}�map)
r(�rowsr�r!�row�pos�convsr��value�	converterr)r)r*�apply_converters[s�
�zSQLCompiler.apply_converterscCsh|dur|jt||d�}dd�|jd|j�D�}|�|�}t�|�}|r2|�||�}|r2tt	|�}|S)z>Return an iterator over the results from executing this query.N)�
chunked_fetch�
chunk_sizecSr�rr)rlr)r)r*rjkr�z,SQLCompiler.results_iter.<locals>.<listcomp>r)
�execute_sqlrr$r9r�r�
from_iterabler�r�r)r(rF�tuple_expectedr�r�rSr�r�r)r)r*�results_iterfs


zSQLCompiler.results_itercCst|�t��S)z�
        Backends (e.g. NoSQL) can override this in order to use optimized
        versions of "query has any results."
        )r@r�rr1r)r)r*�has_resultstszSQLCompiler.has_resultsc	CsD|pt}z|��\}}|st�Wnty$|tkr!tg�YSYdSw|r-|j��}n|j��}z|�||�Wnt	yF|�
��w|tkrM|S|tkrpz|�
�}|rd|d|j�W|�
�S|W|�
�S|�
�w|tkrz|�
�dSt||jjj|jr�|jnd|�}|r�|jjjs�z	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)rrGrr�iterr!�chunked_cursor�cursor�execute�	Exception�closerr�fetchoner9�cursor_iterrk�empty_fetchmany_valuerA�can_use_chunked_readsrr)	r(�result_typer�r�r\r]r��valr`r)r)r*r�{sV
��
�
�
�zSQLCompiler.execute_sqlc
Cs�|j}|jjj}t|jj�D]&\}}|�|�\}}	d||�|||�f}
|jj�	t
d||
f|	�d�q|��\}}d||fS)Nr��%s = %s�ANDzEXISTS (%s))r�r!r�r�r�r r$rTr<rVrrG)
r(rdrvr��qn�qn2r(�
select_col�lhs_sql�
lhs_params�rhsr\r]r)r)r*�as_subquery_condition�s
�z!SQLCompiler.as_subquery_conditionccsH�t|���}|dD]}t|t�sd�dd�|D��Vq|VqdS)Nrr�css�|]}t|�VqdSr�)�str)r/�cr)r)r*r2�rnz,SQLCompiler.explain_query.<locals>.<genexpr>)rrr�rHr�r�)r(r`r�r)r)r*r	�s�
�zSQLCompiler.explain_query)TF)NNN)Nr�N)NNruNN)%�__name__�
__module__�__qualname__r�re�	MULTILINE�DOTALLr�r+r:rFrBrSr7r;r?r�rTr�rGrr�r�r8r�r�rr,r�r�rr�r�rr�r�r	r)r)r)r*rsR
�V2H

7
 ,
�)
"
�1e


�C
rc@sHeZdZdZe�Zdd�Zdd�Zdd�Zdd	�Z	d
d�Z
ddd
�ZdS)�SQLInsertCompilerNcCsv|dur
|g}}n%t|d�r|�|�\}}nt|d�r)|�|||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.
        NrG�get_placeholder�%s)rMrTr�r!r��modify_insert_params)r(r�r�r\r]r)r)r*�field_as_sql�s	

zSQLInsertCompiler.field_as_sqlcCszt|d�r3|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().
        r�FT�r��for_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!)rMr�r �contains_column_references�
ValueError�contains_aggregaterr��contains_over_clause�get_db_prep_saver!)r(r�r�r)r)r*�
prepare_value�s.
�������zSQLInsertCompiler.prepare_valuecCs"|jjr
t||j�S|j|dd�S)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 �rawrhr0�pre_save)r(r�rer)r)r*�pre_save_valszSQLInsertCompiler.pre_save_valcsP|sggfS��fdd�|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(�|]}�fdd�t�|�D�VqdS)c3s �|]\}}��||�VqdSr�)r�)r/r��vr1r)r*r22r3z>SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>.<genexpr>N�r��r/r��rSr(r)r*r21s
��
�z4SQLInsertCompiler.assemble_as_sql.<locals>.<genexpr>css�|]}t|�VqdSr�r�r�r)r)r*r28rncSsg|]	}dd�|D��qS)cSsg|]	}|D]}|�qqSr)r)�r/�psr>r)r)r*rj?�z@SQLInsertCompiler.assemble_as_sql.<locals>.<listcomp>.<listcomp>r)r�r)r)r*rj?r�z5SQLInsertCompiler.assemble_as_sql.<locals>.<listcomp>r�)r(rS�
value_rows�rows_of_fields_as_sql�sql_and_param_pair_rows�placeholder_rows�
param_rowsr)r�r*�assemble_as_sql s�z!SQLInsertCompiler.assemble_as_sqlc
s��jjj��j��}�jjj�jjd�}d|�|j�fg��jjp%|j	g���
dd��fdd��D����jjrH��fdd��jjD�}n�fd	d��jjD�}dg��j
o^�jjj}���|�\}}�jjj�jjd�}�j
rɈjjjrɈjjjr���
�jj��|��|}n��
d
d�|d��|dg}|r���
|��jj��j
�\}	�_|	r���
|	�|�jg7}d���tt�|��fgS|r��
�jj��|��|r݈�
|�d���td
d�|D��fgS|r��
|��fdd�t||�D�S)N)�ignore_conflictsz%s %sz(%s)r�c3s�|]}�|j�VqdSr�)r7rO)r�r)r*r2Ks�z+SQLInsertCompiler.as_sql.<locals>.<genexpr>cs g|]���fdd��D��qS)c	s g|]}��|��|����qSr))r�r�)r/r�)rer(r)r*rjOs z7SQLInsertCompiler.as_sql.<locals>.<listcomp>.<listcomp>r))r/r�)rer*rjNs��z,SQLInsertCompiler.as_sql.<locals>.<listcomp>csg|]	}�jj��g�qSr))r!r��pk_default_value)r/rYr1r)r*rjTr��VALUES (%s)rr�css�|]
}|D]}|VqqdSr�r)r�r)r)r*r2ws�c	s.g|]\}}d��dd�|�g�|f�qS)r�r�r�)r�)r/r>�vals)r`r)r*rj{s��)r!r�r�r r��insert_statementr��db_tablerSrrrNr��objs�returning_fieldsrk�has_bulk_insertr��ignore_conflicts_suffix_sql�can_return_columns_from_insert� can_return_rows_from_bulk_insert�bulk_insert_sql�return_insert_columns�returning_paramsrrr�r�)
r(r4r�r��can_bulkr�r�r�r]�r_sqlr))rSr�r`r(r*rGCsP

"��






�zSQLInsertCompiler.as_sqlcCs*|rt|jj�dkr|jjjsJ�||_|j���r}|��D]
\}}|�	||�q|js5gWd�S|jjjrPt|jj�dkrP|jj
�|�Wd�S|jjjrpt|jj�dks_J�|jj
�
||j�gWd�S|jj
�||j��j|j��jj�fgWd�S1s�wYdS)Nru)r8r r�r!rkr�r�r�rGr�r��fetch_returned_insert_rowsr��fetch_returned_insert_columnsr��last_insert_idr�r�rrr7)r(r�r�r\r]r)r)r*r��s.����
�
�$�zSQLInsertCompiler.execute_sqlr�)r�r�r�r�rr�r�r�r�r�rGr�r)r)r)r*r��s 	#=r�c@s@eZdZedd��Zedd��Zedd��Zdd�Zd	d
�Z	dS)�SQLDeleteCompilercs(�j��t�fdd��jjD��dkS)Nc3s �|]}�jj|dkVqdSr,r-r�r1r)r*r2�r3z1SQLDeleteCompiler.single_alias.<locals>.<genexpr>ru)r r6�sumr5r1r)r1r*�single_alias�s
zSQLDeleteCompiler.single_aliascs>t|t�r
|j�kSt|d�sdSt��fdd�|��D��S)Nr�Fc3s�|]	}��|��VqdSr�)�_expr_refs_base_model)r/�source_expr��
base_model�clsr)r*r2�s
�
�
�z:SQLDeleteCompiler._expr_refs_base_model.<locals>.<genexpr>)rHrrmrM�anyr�)rrKrr)rr*r�s


�z'SQLDeleteCompiler._expr_refs_base_modelcs*t�fdd�t�jj���jjj�D��S)Nc3s �|]}��|�jj�VqdSr�)rr rmrJr1r)r*r2�s
��
�zESQLDeleteCompiler.contains_self_reference_subquery.<locals>.<genexpr>)rrr r��valuesr<�childrenr1r)r1r*� contains_self_reference_subquery�s�z2SQLDeleteCompiler.contains_self_reference_subquerycCsFd|�|j�g}|�|j�\}}|r|�d|�d�|�t|�fS)NzDELETE FROM %sr�r�)r�rsrTr<rNr�r)r(r r`r<r]r)r)r*�_as_sql�s�zSQLDeleteCompiler._as_sqlcCs�|jr|js|�|j�S|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.
        r�zSELECT * FROM (%s) subquery)�pk__in)rr
rr r�r�	__class__�clear_select_clausermrqrrr2r6r$�where_classr<r!rk�update_can_self_selectr�rGr�add_qr)r(�innerqrr�outerqr\r]r)r)r*rG�s
�

zSQLDeleteCompiler.as_sqlN)
r�r�r�rr�classmethodrr
rrGr)r)r)r*r��s



	r�cs0eZdZdd�Z�fdd�Z�fdd�Z�ZS)�SQLUpdateCompilercCs�|��|jjs
dS|j}gg}}|jjD]�\}}}t|d�rB|j|jddd�}|jr5td|j|f��|j	rAtd|j|f��n't|d�ra|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)r�r)r�FTr�r�r��prepare_database_saver�zSTried to update field %s with a model instance, %r. Use a value compatible with %s.r�r�rGr�Nz	%s = NULLz
UPDATE %s SETr�r�r�)rFr rr�rMr�r�rr�r�rcr�rr!�	TypeErrorrr�r�r7rTrNrQrsr�r<r)r(r�r�
update_paramsr�rmr��placeholderr�r\r]r�r`r<r)r)r*rG�sj

�����
���


�zSQLUpdateCompiler.as_sqlcszt��|�}z|r|jnd}|du}W|r|��n|r!|��ww|j��D]}|�|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)�superr��rowcountr�r �get_related_updatesr�r")r(r�r�r�r�r �aux_rows�rr)r*r�s"
��
��zSQLUpdateCompiler.execute_sqlcs|jj��}|j��|j��}|jjs|dkrdS|jjtd�}d|_|j	dd�i|_
g|_|�|�
�jjg�t���|dkoH|jjj}|j��|j_|jjsV|r{g}|�|j��t�D]}|�dd�|D��qa|j�d	|f�||j_n|j�d	|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/r�r)r)r*r2Grnz2SQLUpdateCompiler.pre_sql_setup.<locals>.<genexpr>r
)r r.r�r6�count_active_tables�related_updatesrrr��clear_orderingr�r$�
add_fieldsr�rrr�rrFr!rkrrr<r�r"r�rrQ�
add_filter�related_idsr)r(r�countr �must_pre_select�identsr�rr)r*rF%s,	



zSQLUpdateCompiler.pre_sql_setup)r�r�r�rGr�rF�
__classcell__r)r)rr*r�s?rc@seZdZdd�ZdS)�SQLAggregateCompilercCs�gg}}|jj��D]}|�|�\}}|�|||�\}}|�|�|�|�qt|jj�|_d�	|�}t
|�}|jj�|j
�jdd�\}}d||f}||}||fS)rr�T)rr�)r r�rrTrUrNrQr8r9r�r�inner_queryr�r"rG)r(r\r]r��ann_sql�
ann_params�inner_query_sql�inner_query_paramsr)r)r*rGQs"


�
�zSQLAggregateCompiler.as_sqlN)r�r�r�rGr)r)r)r*r-Psr-c#sV�z$t��fdd�|�D]}�dur|n�fdd�|D�VqW���dS���w)z[
    Yield blocks of rows from a cursor and ensure the cursor is closed when
    done.
    cs
����Sr�)�	fetchmanyr))r��itersizer)r*r\ns
zcursor_iter.<locals>.<lambda>Ncsg|]}|d���qSr�r)r")r9r)r*rjoszcursor_iter.<locals>.<listcomp>)r�r�)r��sentinelr9r4r�r))r9r�r4r*r�hs�"�r�)2r�r��	functoolsr�	itertoolsr�django.core.exceptionsrr�	django.dbrr�django.db.models.constantsr�django.db.models.expressionsr	r
rrr
�django.db.models.functionsrr�django.db.models.query_utilsrr�django.db.models.sql.constantsrrrrrr�django.db.models.sql.queryrr�django.db.transactionr�django.utils.functionalr�django.utils.hashabler�django.utils.regex_helperrrr�r�rr-r�r)r)r)r*�<module>sB I@;

https://t.me/RX1948 - 2025