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__/query.cpython-310.pyc
o

��Ih��@sdZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
mZmZddl
mZmZmZddlmZddlmZmZddlmZmZmZdd	lmZdd
lmZddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%ddl&m'Z'dd
l(m)Z)ddl*m+Z+ddl,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4m5Z5ddl6m7Z7m8Z8m9Z9m:Z:ddl;m<Z<m=Z=m>Z>m?Z?m@Z@ddlAmBZBddlCmDZDddlEmFZFddlGmHZHddgZIeFd�ZJeFd�ZKdd�ZLdd�ZMe	dd �ZNGd!d�d�ZOGd"d�de�ZPd,d$d%�ZQd&d'�ZRd(d)�ZSGd*d+�d+�ZTdS)-aW
Create SQL statements for QuerySets.

The code in here encapsulates all of the SQL construction so that QuerySets
themselves do not have to (and could be backed by things other than SQL
databases). The abstraction barrier only works one way: this module has to know
all about the internals of models in order to get the information it needs.
�N)�Counter�
namedtuple)�Iterator�Mapping)�chain�count�product)�ascii_uppercase)�FieldDoesNotExist�
FieldError)�DEFAULT_DB_ALIAS�NotSupportedError�connections)�Count)�
LOOKUP_SEP)�BaseExpression�Col�Exists�F�OuterRef�Ref�ResolvedOuterRef)�Field)�MultiColSource)�Lookup)�Q�check_rel_lookup_compatibility�refs_expression)�INNER�LOUTER�	ORDER_DIR�
ORDER_PATTERN�SINGLE)�	BaseTable�Empty�Join�	MultiJoin)�AND�OR�
ExtraWhere�NothingNode�	WhereNode)�RemovedInDjango40Warning)�cached_property)�_lazy_re_compile)�Node�Query�RawQueryz['`\"\]\[;\s]|--|/\*|\*/z[\w\-]+cCstt�dd�|��D���S)Ncss*�|]}|jr
|j|jfn|jfVqdS�N)�concrete�name�attname��.0�f�r9�</usr/lib/python3/dist-packages/django/db/models/sql/query.py�	<genexpr>;s
��
�z,get_field_names_from_opts.<locals>.<genexpr>)�setr�
from_iterable�
get_fields)�optsr9r9r:�get_field_names_from_opts:s
�r@ccs2�|jD]}t|t�rt|�EdHq|VqdSr2)�children�
isinstancer/�get_children_from_q)�q�childr9r9r:rCAs�

�rC�JoinInfo)�final_field�targetsr?�joins�path�transform_functionc@s^eZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
dd��Zdd�Zdd�Z
dS)r1zA single raw SQL query.r9cCs4||_||_||_d|_d\|_|_i|_i|_dS)N�rN)�params�sql�using�cursor�low_mark�	high_mark�extra_select�annotation_select)�selfrNrOrMr9r9r:�__init__Rs
zRawQuery.__init__cCs
|�|�Sr2��clone�rUrOr9r9r:r^s
zRawQuery.chaincCst|j||jd�S)N)rM)r1rNrMrYr9r9r:rXa�zRawQuery.clonecs6|jdur	|��t|jjj��fdd�|jjD�S)Ncsg|]}�|d��qS�rr9)r7�column_meta��	converterr9r:�
<listcomp>hs�z(RawQuery.get_columns.<locals>.<listcomp>)rP�_execute_queryrrO�
introspection�identifier_converter�description�rUr9r]r:�get_columnsds

�zRawQuery.get_columnscCs6|��t|jjjst|j�}t|�S|j}t|�Sr2)r`rrO�features�can_use_chunked_reads�listrP�iter)rU�resultr9r9r:�__iter__ks
�zRawQuery.__iter__cCsd|jj|fS)Nz<%s: %s>)�	__class__�__name__rdr9r9r:�__repr__wszRawQuery.__repr__cCs"|jdurdSt|jt�rtStSr2)rMrBr�dict�tuplerdr9r9r:�params_typezs
zRawQuery.params_typecCs"|jdur|jS|j|�|j�Sr2)rqrNrMrdr9r9r:�__str__�s
zRawQuery.__str__cs�t|j}|j}|jj�|turt�fdd�|jD��}n|tur.�fdd�|j��D�}n
|dur5d}nt	d|��|�
�|_
|j
�|j|�dS)Nc3s�|]}�|�VqdSr2r9)r7�val��adapterr9r:r;���z*RawQuery._execute_query.<locals>.<genexpr>csi|]	\}}|�|��qSr9r9)r7�keyrsrtr9r:�
<dictcomp>�sz+RawQuery._execute_query.<locals>.<dictcomp>zUnexpected params type: %s)
rrOrq�ops�adapt_unknown_valuerprMro�items�RuntimeErrorrP�executerN)rU�
connectionrqrMr9rtr:r`�s

zRawQuery._execute_queryN)r9)rm�
__module__�__qualname__�__doc__rVrrXrerkrn�propertyrqrrr`r9r9r9r:r1Os

c@s`eZdZdZdZeeg�ZdZedfdd�Z	e
dd��Ze
d	d
��Ze
dd��Zd
d�Zdd�Zdd�Zd�dd�Zdd�Zdd�Zd�dd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd�d(d)�Zd*d+�Zd�d,d-�Zd.d/�Zd0d1�Z d�d3d4�Z!d5d6�Z"d�d8d9�Z#d:d;�Z$d<d=�Z%d>d?�Z&d@dA�Z'dBdC�Z(dDdE�Z)dFdG�Z*d�dHdI�Z+dJdK�Z,dLdM�Z-d�dNdO�Z.dPdQ�Z/dRdS�Z0d�dTdU�Z1dVdW�Z2dXdY�Z3dZd[�Z4d\d]�Z5d^d_�Z6d`da�Z7dbdc�Z8ddde�Z9	2		d�dfdg�Z:dhdi�Z;djdk�Z<dldm�Z=	2		d�dndo�Z>d�dpdq�Z?drds�Z@d�dtdu�ZA		2d�dvdw�ZBdxdy�ZCeDd�dzd{��ZEeDd|d}��ZFd�d~d�ZGd�d��ZHd�d��ZId�d��ZJd�d�d��ZKd�d��ZLe
d�d���ZMd�d��ZNd�d��ZOd�d��ZPd�d��ZQd�d��ZRd�d��ZSd�d��ZTd�d�d��ZUd�d��ZVd�d�d��ZWd�d�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]d�d��Z^d�d��Z_d�d��Z`d�d��Zad�d��Zbd�d��Zce
d�d���Zde
d�d���Zed�d��Zfd�d��ZgdS)�r0zA single SQL query.�T�SQLCompilerTcCs||_i|_i|_||_i|_i|_d|_d|_d|_t	�|_
d|_d|_d|_
|�|_||_d|_d|_d\|_|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_i|_d|_d|_ d|_!d|_"d|_#i|_$d|_%d|_&d|_'d|_(t)�df|_*i|_+d|_,d|_-i|_.dS)NTFr9rL�)/�model�alias_refcount�	alias_map�
alias_cols�external_aliases�	table_map�default_cols�default_ordering�standard_orderingr<�used_aliases�filter_is_sticky�subquery�select�where�where_class�group_by�order_byrQrR�distinct�distinct_fields�select_for_update�select_for_update_nowait�select_for_update_skip_locked�select_for_update_of�select_for_no_key_update�select_related�	max_depth�
values_select�annotations�annotation_select_mask�_annotation_select_cache�
combinator�combinator_all�combined_queries�extra�extra_select_mask�_extra_select_cache�extra_tables�extra_order_by�	frozenset�deferred_loading�_filtered_relations�
explain_query�explain_format�explain_options)rUr�r�r�r9r9r:rV�sX
zQuery.__init__cCsPt|j�dkr|jd}t|dd�p|jSt|j�dkr&tt|j����jSdS)N�r�target)	�lenr��getattr�fieldrT�nextri�values�output_field)rUr�r9r9r:r��s
�zQuery.output_fieldcCst|jp	|jp	|j�Sr2)�boolr�r�r�rdr9r9r:�has_select_fields�zQuery.has_select_fieldscCs|jD]}|SdSr2)r��rU�aliasr9r9r:�
base_tables
�zQuery.base_tablecCs|��\}}||S)a(
        Return the query as a string of SQL with the parameter values
        substituted in (use sql_with_params() to see the unsubstituted string).

        Parameter values won't necessarily be quoted correctly, since that is
        done by the database interface at execution time.
        )�sql_with_params)rUrNrMr9r9r:rr
sz
Query.__str__cCs|�t���S)zw
        Return the query as an SQL string and the parameters that will be
        substituted into the query.
        )�get_compilerr�as_sqlrdr9r9r:r�szQuery.sql_with_paramscCs|��}||t|�<|S)z4Limit the amount of work when a Query is deepcopied.)rX�id)rU�memorjr9r9r:�__deepcopy__szQuery.__deepcopy__NcCs:|dur|durtd��|rt|}|j�|j�|||�S)NzNeed either using or connection)�
ValueErrorrry�compiler)rUrOr~r9r9r:r�"s
zQuery.get_compilercCs|jjS)z�
        Return the Options instance (the model._meta) from which to start
        processing. Normally, this is self.model._meta, but it can be changed
        by subclasses.
        )r��_metardr9r9r:�get_meta)�zQuery.get_metacCsJt�}|j|_|j��|_|j��|_|j��|_|j��|_|j��|_|j�	�|_|j
��|_
|jdur:d|_n|j��|_tdd�|j
D��|_
d|_|j��|_|jdur]d|_n|j��|_|jdurld|_n|j��|_|jdur~t�|j�|_d|jvr�|j��|_|j��|_|j��|_z|`W|Sty�Y|Sw)zi
        Return a copy of the current Query. A lightweight alternative to
        to deepcopy().
        Ncss�|]}|��VqdSr2rW)r7�queryr9r9r:r;ErvzQuery.clone.<locals>.<genexpr>F�subq_aliases)r$rl�__dict__�copyr�r�r�r�r�rXr�r�rpr�r�r�r�r�r��deepcopyr�r�r�r��AttributeError)rU�objr9r9r:rX1sD




��zQuery.clonecCsF|��}|r|j|kr||_|jst�|_d|_t|d�r!|��|S)z�
        Return a copy of the current Query that's ready for another operation.
        The klass argument changes the type of the Query, e.g. UpdateQuery.
        F�_setup_query)rXrlr�r<r��hasattrr�)rU�klassr�r9r9r:rds
zQuery.chaincCs|��}|�|�|Sr2)rX�change_aliases)rU�
change_maprXr9r9r:�relabeled_cloness
zQuery.relabeled_clonecCs|jsd}|�||�Sr2�r��get_col)rUr�r�r�r9r9r:�_get_colxszQuery._get_colc	Cs�|��}g}|D]h}t|t�r|�|�qt|ttf�r*|�||�\}}|�|�q|j��D]\}}||ur>t||�}n-q/t|t	�sJ|j
rc|jsc|d7}d|}||j|<|�
|g�t||�}n|�||�\}}|�|�q|�|�||fS)Nr�z__col%d)�get_source_expressionsrBr�appendr+r�rewrite_colsrTr{r�contains_aggregate�
is_summaryr��append_annotation_mask�set_source_expressions)	rU�
annotation�col_cnt�
orig_exprs�	new_exprs�expr�new_expr�	col_alias�selected_annotationr9r9r:r�}s.

�

zQuery.rewrite_colscs|jsiS�fdd�|j��D�}t|jt�s"|js"|s"|js"|jr�ddl	m
}|��}d|_||j
|�}d|_d|_|�|j�|jdd�|jsgtdd	�|D��}|jrd|rd|j
jj�|���f|_d|_d
d�|jD�}d|d
<d}	t|j���D])\}
}|j}|jr�|�||	�\}}	|�|�|j|
<|j|
=|�|
�|�|j�q||jdkr�|js�|js�|j
jj�|���f|_n|}d|_d|_i|_ |jdd�|�!�d|_d|_|�"|�}
|
�#t$�}|d
ur�d
gt%|j�}|
�&|j�'��}t(|
�)|f|��}t*t+|j|��S)zU
        Return the dictionary with the values of the existing aggregations.
        csg|]
\}}|�vr|�qSr9r9)r7r�r���added_aggregate_namesr9r:r_�s
�z)Query.get_aggregation.<locals>.<listcomp>r)�AggregateQueryTF��forcecss �|]}t|dd�r|VqdS)r�TN)r�)r7r�r9r9r:r;�s�
�
�z(Query.get_aggregation.<locals>.<genexpr>cSsi|]}|d�qS)r�r9�r7�tr9r9r:rx��z)Query.get_aggregation.<locals>.<dictcomp>r�Nr9),rTr�r{rBr�rp�	is_slicedr�r��django.db.models.sql.subqueriesr�rXr�r�r�r��set_annotation_mask�clear_ordering�anyr�r��pkr��get_initial_aliasr�rhr�r�r�r��remover�r��clear_limitsr��execute_sqlr"r��get_convertersr�r��apply_convertersro�zip)rUrOr��existing_annotationsr��inner_query�outer_query�"has_existing_aggregate_annotations�relabelsr�r��
expressionr�r�rj�
convertersr9r�r:�get_aggregation�sl
����

�

zQuery.get_aggregationcCs>|��}|jtd�ddd�|�|dg�d}|durd}|S)zO
        Perform a COUNT() query using the current filter constraints.
        �*�__countT)r�r�Nr)rX�add_annotationrr)rUrOr��numberr9r9r:�	get_count	szQuery.get_countcCs|jSr2)r�rdr9r9r:�has_filtersszQuery.has_filterscs�|��}|js$|jdur |�dd�|jjjD�d�|jdd�|��|j	r@|j
dkr@t�jj
�t��fdd�|j	D��|_	|jdd�|rN|jd	d
�|�dd	iddddd�|�dg�|S)NTcs��|]}|jVqdSr2�r5r6r9r9r:r;��zQuery.exists.<locals>.<genexpr>F��
allow_aliases�unionc3s�|]
}|j��d�VqdS))�limitN)�exists)r7�combined_query��limit_combinedrOr9r:r;"�
��
�r�r�)�high�a)rXr�r��
add_fieldsr�r��concrete_fields�set_group_by�clear_select_clauser�r�rrf�%supports_slicing_ordering_in_compoundrpr��
set_limits�	add_extra�set_extra_mask)rUrOrrDr9rr:rs"

�zQuery.existscCs|�|�}|j|d�}|��S)N�rO)rr��has_results)rUrOrDr�r9r9r:r$-s
zQuery.has_resultscKs`|��}|D]}t�|�rd|vrtd|�d���qd|_||_||_|j|d�}d�|���S)Nz--zInvalid option name: �.Tr#�
)	rX�EXPLAIN_OPTIONS_PATTERN�	fullmatchr�r�r�r�r��join)rUrO�format�optionsrD�option_namer�r9r9r:�explain2s��z
Query.explainc
s*�j|jks
Jd���jrJd���j|jksJd���j|jks%Jd��i�|tk}|r0t�nt�j�}���t|dd�}|�	�fdd��jD��t�}t
|j�d	d
�}|D]4}|j|}	|	���}	�j|	|d�}
|	j
tkrw|�|
�|�|
�||
kr�|
�|<|j|s���|
�qZ|�	|�|����j|jO_|j��}|����j�||�|jr��fdd
�|jD��nd�_|tkrԈjr�|jr�td���j�|j�t�}�jd
ur�|��j�|jd
ur�|�|j�|r���|��j |j 7_ |j!�p	�j!�_!|j"�p�j"�_"d
S)aN
        Merge the 'rhs' query into the current one (with any 'rhs' effects
        being applied *after* (that is, "to the right of") anything in the
        current query. 'rhs' is not modified during a call to this function.

        The 'connector' parameter describes how to connect filters from the
        'rhs' query.
        z4Cannot combine queries on two different base models.z3Cannot combine queries once a slice has been taken.z6Cannot combine a unique query with a non-unique query.z6Cannot combine queries with different distinct fields.�Fc3s$�|]
}�j|jtkr|VqdSr2�r��	join_typer)r7�jrdr9r:r;gs��z Query.combine.<locals>.<genexpr>r�N)�reusec�g|]}|����qSr9�r��r7�col�r�r9r:r_��z!Query.combine.<locals>.<listcomp>r9zSWhen merging querysets using 'or', you cannot have extra(select=...) on both sides.)#r�r�r�r�r'r<r�r��JoinPromoter�	add_votesrhr�r)r0r�add�discardr��unref_alias�update_join_typesr�r�rX�relabel_aliasesr��
set_selectr(r�r��updater�r"r�r�r�)
rU�rhs�	connector�conjunctionr2�joinpromoter�	rhs_votes�
rhs_tablesr�r)�	new_alias�wr�r9)r�rUr:�combine@sr	�����






�






z
Query.combinecCs|j\}}|s	dS|��}i}|j|jhi}|D]u}|�t�}	|jjj}
|}|	dd�D]5}|
}
||jvr;|j|j	}|�
|�}t|�rH|j}
n|j
j}
|
j}t|�sZ||
�|�t||
|j�q,|�
|	d�}|joo|j}|ru|jn|j}|jj}||jkr�|
}t|�s�t|||�q|r�i}|��D]\}}|jjD]}||vr�|jjj}t|||�q�q�|��D]\}}||vr�||�|�q�|��D]
\}}||||�q�dS|��D]\}}||vr�||�|�q�|||<q�|��D]	}|�|t��q�|��D]\}}||||��qdS)a
        Convert the self.deferred_loading data structure to an alternate data
        structure, describing the field that *will* be loaded. This is used to
        compute the columns to select from the database and also by the
        QuerySet class to work out which fields are being initialized on each
        model. Models that have all their fields included aren't mentioned in
        the result, only those that have field restrictions in place.

        The "target" parameter is the instance that is populated (in place).
        The "callback" is a function that is called whenever a (model, field)
        pair need to be added to "target". It accepts three parameters:
        "target", and the model and list of fields being added for that model.
        N���)r�r��concrete_modelr��splitrr�r�r��
relation_name�	get_field�is_reverse_o2o�
related_model�remote_fieldr;�add_to_dict�auto_createdr3r{�local_fieldsrA�get_parent_list�
setdefaultr<)rUr��callback�field_names�defer�	orig_opts�seen�must_include�
field_name�parts�	cur_modelr?r4�	old_model�sourcer��is_reverse_objectr��worksetr��mr9r9r:�deferred_to_data�sp





�
����
�zQuery.deferred_to_dataFcCs�|j�|�}|s|r|d}|j|d7<|dfS|r/d|jt|j�df}|�|�n|dur6|jn|}|g|j|<d|j|<|dfS)a
        Return a table alias for the given table_name and whether this is a
        new alias or not.

        If 'create' is true, a new alias is always created. Otherwise, the
        most recently created alias for the table (if one exists) is reused.
        rr�F�%s%dNT)r��getr��alias_prefixr�r�r�r�)rU�
table_name�create�filtered_relation�
alias_listr�r9r9r:�table_alias�s
zQuery.table_aliascCs|j|d7<dS)z-Increases the reference count for this alias.r�N�r�r�r9r9r:�	ref_aliasr�zQuery.ref_aliasr�cCs|j||8<dS)z-Decreases the reference count for this alias.Nro)rUr��amountr9r9r:r=r�zQuery.unref_aliascs�t����r]��d���j�jdurq�j�jdusJ��j�j}|o-�j|jtk}�j�jtk}�j�js>|rY|sY�j����j�<�����fdd��jD���sdSdS)a 
        Promote recursively the join type of given aliases and its children to
        an outer join. If 'unconditional' is False, only promote the join if
        it is nullable or the parent join is an outer join.

        The children promotion is done to avoid join chains that contain a LOUTER
        b INNER c. So, if we have currently a INNER b INNER c and a->b is promoted,
        then we must also promote b->c automatically, or otherwise the promotion
        of a->b doesn't actually change anything in the query results.
        rNc3s,�|]}�j|j�kr|�vr|VqdSr2)r��parent_alias)r7r)�r��aliasesrUr9r:r;=s���z&Query.promote_joins.<locals>.<genexpr>)	rh�popr�r0rrr�nullable�promote�extend)rUrtrr�
parent_louter�already_louterr9rsr:�
promote_joins s"
���zQuery.promote_joinscCslt|�}|r4|�d�}|j|jtkr0|j|��|j|<|j|j}|j|jtkr0|�|�|sdSdS)a�
        Change join type from LOUTER to INNER for all joins in aliases.

        Similarly to promote_joins(), this method must ensure no join chains
        containing first an outer, then an inner join are generated. If we
        are demoting b->c join in chain a LOUTER b LOUTER c then we must
        demote a->b automatically, or otherwise the demotion of b->c doesn't
        actually change anything in the query results. .
        rN)	rhrur�r0r�demoterrrr�)rUrtr�rrr9r9r:�demote_joinsBs


�zQuery.demote_joinscCs8|j����D]\}}||�|d�}|�||�qdS)zp
        Reset reference counts for aliases so that they match the value passed
        in `to_counts`.
        rN)r�r�r{rhr=)rU�	to_countsr��cur_refcount�unref_amountr9r9r:�reset_refcountsUs�zQuery.reset_refcountscs*t�������sJ�|j���t|jt�r$t�fdd�|jD��|_t�fdd�|jD��|_|j	o?�fdd�|j	�
�D�|_	��
�D]@\}}||jvrOqE|j|���}||j|<|j
||j
|<|j
|=|j|=|j|j}t|�D]\}}||kr�|||<nqvqE�fdd�|j�
�D�|_dS)z�
        Change the aliases in change_map (which maps old-alias -> new-alias),
        relabelling any references to them in select columns and the where
        clause.
        cr3r9r4r5r7r9r:r_jr8z(Query.change_aliases.<locals>.<listcomp>cr3r9r4r5r7r9r:r_kr8csi|]
\}}||����qSr9r4)r7rwr6r7r9r:rxls�z(Query.change_aliases.<locals>.<dictcomp>cs&i|]\}}��||�|p|�v�qSr9�rh)r7r��aliasedr7r9r:rxs��N)r<�
isdisjointr�r�r?rBr�rpr�r�r{r�r�r�r�rj�	enumerater�)rUr��	old_aliasrH�
alias_data�
table_aliases�posr�r9r7r:r�^s4�

��
�zQuery.change_aliasescs��fdd�}�j|jkrdSt��d}t|��D]\}}|�jvr'|�_n	||kr/td��q�j��jg��_|j��j�|_���fdd�t�j�D��dS)a
        Change the alias prefix to the next letter in the alphabet in a way
        that the outer query's aliases and this query's aliases will not
        conflict. Even tables that previously had no alias will get an alias
        after this call.
        c3sl�t}tt�j�d�}|Vtd�D] }|r ||�|�d�n|}t||d�D]}d�|�Vq(d}qdS)a
            Generate a sequence of characters in alphabetical order:
                -> 'A', 'B', 'C', ...

            When the alphabet is finished, the sequence will continue with the
            Cartesian product:
                -> 'AA', 'AB', 'AC', ...
            r�N)�repeat�)r	�chr�ordrir�indexrr))�alphabet�prefix�n�seq�srdr9r:�
prefix_gen�s�	�z%Query.bump_prefix.<locals>.prefix_genN�z6Maximum recursion depth exceeded: too many subqueries.cs i|]\}}|d�j|f�qS)rg)ri)r7r�r�rdr9r:rx�s��z%Query.bump_prefix.<locals>.<dictcomp>)	ri�sys�getrecursionlimitr�r��RecursionErrorrr�r�)rUrr��local_recursion_limitr�r�r9rdr:�bump_prefix�s$
���zQuery.bump_prefixcCs4|jr
|j}|�|�|S|�t|��jd��}|S)zf
        Return the first alias for this query, after increasing its reference
        count.
        N)r�r�rpr)r#r��db_tabler�r9r9r:r��s
�zQuery.get_initial_aliascCstdd�|j��D��S)z�
        Return the number of tables in this query with a non-zero reference
        count. After execution, the reference counts are zeroed, so tables
        added in compiler will not be seen by this method.
        cSsg|]}|rd�qS�r�r9)r7rr9r9r:r_��z-Query.count_active_tables.<locals>.<listcomp>)r�r�r�rdr9r9r:�count_active_tables�szQuery.count_active_tablesc	s�|r�r��fdd�|j��D�}n
��fdd�|j��D�}|r5�j|vr*�j}n|d}|�|�|S|j�jd�jd�\}}�jrX|j�jjtksP�j	rSt}nt
}|�_|�_�|j|<|S)a�
        Return an alias for the 'join', either reusing an existing alias for
        that join or creating a new one. 'join' is either a
        sql.datastructures.BaseTable or Join.

        The 'reuse' parameter can be either None which means all joins are
        reusable, or it can be a set containing the aliases that can be reused.

        The 'reuse_with_filtered_relation' parameter is used when computing
        FilteredRelation instances.

        A join is always created as LOUTER if the lhs alias is LOUTER to make
        sure chains like t1 LOUTER t2 INNER t3 aren't generated. All new
        joins are created as LOUTER if the join is nullable.
        cs*g|]\}}|�vr|j�dd�r|�qS)F)�with_filtered_relation)�equals�r7rr1�r)r2r9r:r_�s
�zQuery.join.<locals>.<listcomp>cs,g|]\}}�dus|�vr|�kr|�qSr2r9r�r�r9r:r_�s
�rKT)rkrl)r�r{rnrprjrlr0rrrrvr)	rUr)r2�reuse_with_filtered_relation�
reuse_aliases�reuse_aliasr��_r0r9r�r:r)�s,��


z
Query.joinc
Cs�||vr||S|�|�}|s|S|}|D]1}||vr#|j}||}q|j|s,|j}q|�|�}|�|jg||�}	|j}|	jd}||<q|pL|dS)a}
        Make sure the given 'model' is joined in the query. If 'model' isn't
        a parent of 'opts' or if it is None this method is a no-op.

        The 'alias' is the root alias for starting the join, 'seen' is a dict
        of model -> alias of existing joins. It must also contain a mapping
        of None -> some alias. This will be returned in the no-op case.
        rKN)�get_base_chainr��parents�get_ancestor_link�setup_joinsr4rI)
rUr?r�r�r\r�	curr_opts�	int_model�
link_field�	join_infor9r9r:�join_parent_model�s&	


zQuery.join_parent_modelcCst�|�r	td��dS)NzbColumn aliases cannot contain whitespace characters, quotation marks, semicolons, or SQL comments.)�FORBIDDEN_ALIAS_PATTERN�searchr�r�r9r9r:�check_aliass

��zQuery.check_aliascCsT|�|�|j|dd|d�}|r|�|g�n|�t|j��|h��||j|<dS)z0Add a single annotation expression to the Query.TN)�allow_joinsr2�	summarize)r��resolve_expressionr�r�r<rT�
differencer�)rUr�r�r�r�r9r9r:r	"s

�zQuery.add_annotationc
Os�|��}|�|�d|_|jdkr"|jdur"|js"|js"|jdd�|jj	|g|�Ri|��|j
��D]!\}}|j	|g|�Ri|��}t|d�rP|j
�|j
�||j
|<q4|j��D]\}}	t|	t�rl|	jjjj|kpvt|	t�ov|	j|	jk|j
|<q[|S)NTrr�r�)rXr�r�rQrRr�r�r�r�r�r�r{r�r�rAr�rBr%�
join_fieldrQr�r�r#rjrn)
rUr��args�kwargsrXrw�value�resolvedr��tabler9r9r:r�-s(
��

�zQuery.resolve_expressioncs0t�j���jj�}�fdd��j|dd�D�S)Ncsg|]
}|j�jvr|�qSr9)r�r�r5rdr9r:r_Hs
�z+Query.get_external_cols.<locals>.<listcomp>T��include_external)rr�r�r�rA�	_gen_cols)rU�exprsr9rdr:�get_external_colsFs
�zQuery.get_external_colscCs4|rt||�gS|��}tdd�|D��r|gS|S)Ncsr
r2)�possibly_multivaluedr5r9r9r:r;Qrz*Query.get_group_by_cols.<locals>.<genexpr>)rr�r�)rUr��
external_colsr9r9r:�get_group_by_colsMszQuery.get_group_by_colscCs*|j|d���\}}|jrd|}||fS)N)r~z(%s))r�r�r�)rUr�r~rNrMr9r9r:r�UszQuery.as_sqlcsht|d�r|j���d�}|St|ttf�r2���fdd�|D�}t|�}t|d�r.||�S||�S|S)Nr�)r2r�c3s�|]
}��|���VqdSr2)�resolve_lookup_value)r7�	sub_value�r��	can_reuserUr9r:r;crz-Query.resolve_lookup_value.<locals>.<genexpr>�_make)r�r�rBrhrp�type)rUr�r�r�r��type_r9r�r:r�[s
���
zQuery.resolve_lookup_valuec	Cs�|�t�}|jrt||j�\}}|r|d|fS|�||���\}}}}|dt|�t|��}t|�dkrC|sCtd||��jj	f��||dfS)zX
        Solve the lookup type from the lookup (e.g.: 'foobar__id__icontains').
        r9rr�z"Invalid lookup "%s" for model %s".F)
rMrr�r�
names_to_pathr�r�rr�rm)	rU�lookup�lookup_splittedr�expression_lookupsr�r��lookup_parts�field_partsr9r9r:�solve_lookup_typems

��
zQuery.solve_lookup_typecCs4t|d�rt|jj||�std||jf��dSdS)z�
        Check whether the object passed while querying is of the correct type.
        If not, raise a ValueError specifying the wrong object.
        r�z)Cannot query "%s": Must be "%s" instance.N)r�rr�r�r��object_name)rUr�r?r�r9r9r:�check_query_object_types
���zQuery.check_query_object_typecCs�|jr=t|t�r|jst|j||�std|jjj|jf��t	|d�r,|�
|||�dSt	|d�r?|D]
}|�
|||�q3dSdSdS)z3Check the type of object passed to query relations.z6Cannot use QuerySet for "%s": Use a QuerySet for "%s".r�rkN)�is_relationrBr0r�rr�r�r�r�r�r�)rUr�r�r?�vr9r9r:�check_related_objects�s"���

�zQuery.check_related_objectscCsPt|d�rt|dd�st|jjd��t|d�r$|��D]	}|�|�qdSdS)z>Raise an error if expression cannot be used in a WHERE clause.r��
filterableTz$ is disallowed in the filter clause.r�N)r�r�r
rlrmr��check_filterable)rUrr�r9r9r:r��s�
�
�
�zQuery.check_filterablec	Cs�|pdg�^}}|D]}|�||�}q
|�|�}|s6|jjr%td�|���|�||�}d}|�|�}|s6dS|||�}|jdurS|jsS|dvrKtd��|�d�|d�St	t
jjrj|dkrj|jdkrj|�d�|d�S|S)	a1
        Try to extract transforms and lookup from given lhs.

        The lhs value is something that works like SQLExpression.
        The rhs value is what the lookup is going to compare against.
        The lookups is a list of names to extract using get_lookup()
        and get_transform().
        �exact�$Related Field got invalid lookup: {}N)r��iexactz Cannot use None as a query value�isnullTr�)
�
try_transform�
get_lookupr�r�rr*rB�can_use_none_as_rhsr�rrrf�!interprets_empty_strings_as_nulls)	rU�lookups�lhsrB�
transforms�lookup_namer4�lookup_classr�r9r9r:�build_lookup�s*



zQuery.build_lookupcCsZ|�|�}|r||�S|jj}t�||���}|r!dd�|�}nd}td||j|f��)z�
        Helper method for build_lookup(). Try to fetch and initialize
        a transform for name parameter from lhs.
        z, perhaps you meant %s?z or r%zCUnsupported lookup '%s' for %s or join on the field not permitted%s)	�
get_transformr�rl�difflib�get_close_matches�get_lookupsr)rrm)rUr�r4�transform_classr��suggested_lookups�
suggestionr9r9r:r��s

��zQuery.try_transformc	
s�t|t�r	td��t|t�r|j|||||||d�St|d�rDt|dd�s)td��|�dg|j	||d�d	�}	|�
�}
|
�|	t�|
gfS|\}}|sPtd
|��|�
|�\}
}}|r_|�|�|skt|�dkrktd��|j���|�|||�}�fd
d�|j��D�}|r�|�|�|�
�}
|r�|�|
||�}	|
�|	t�|
gfS|��}|��}|p�|}z#|j||||||d�}t|t�r�t|�}|�|j||j�|j|_Wnty�}z|�|||j �WYd}~Sd}~ww|�!|j�|�"|j#|j|j$�\}}}|du�r
|�!|�|jj%�r=t|
�}|dk�r!td�&|
d���t|�dk�r3|�'|d|j|�}nt(|||j#|j�}n
|�'|d|j|�}|�|
||�}	|	j)}|
�|	t�|dk�od|	j*d	u�od|}|�r�|dk�ss|	j*du�r�|	j*du�r�d	}|dk�r�|�+|d��s�|j,|dj-t.k�r�|d�/d�}|�'|d|j#d|�}|
�||d�t�t|t0��r�|�+|j1��r�|j1�/d�}|
�||d�t�|
|�s�|fSdfS)a�
        Build a WhereNode for a single filter clause but don't add it
        to this Query. Query.add_q() will then add this filter to the where
        Node.

        The 'branch_negated' tells us if the current branch contains any
        negations. This will be used to determine if subqueries are needed.

        The 'current_negated' is used to determine if the current filter is
        negated or not and this will be used to determine if IS NULL filtering
        is needed.

        The difference between current_negated and branch_negated is that
        branch_negated is set on first negation, but current_negated is
        flipped for each negation.

        Note that add_filter will not do any negating itself, that is done
        upper in the code by add_q().

        The 'can_reuse' is a set of reusable joins for multijoins.

        If 'reuse_with_filtered_relation' is True, then only joins in can_reuse
        will be reused.

        The method will create a filter clause that can be added to the current
        query. However, if the filter isn't added to the query then the caller
        is responsible for unreffing the joins used.
        z"Cannot parse keyword query as dict)�branch_negated�current_negatedr�r��
split_subqr�r��conditionalFz3Cannot filter against a non-conditional expression.r��r�TzCannot parse keyword query %rr��7Joined field references are not permitted in this querycs$h|]\}}|��|d�kr|�qSr[r��r7�kr���	pre_joinsr9r:�	<setcomp>.s$z%Query.build_filter.<locals>.<setcomp>)r��
allow_manyr�Nr�rr�rKr9)2rBrorr�_add_qr�r��	TypeErrorr�r�r�r;r'r�r�r�r�r�r�r{r�r�r�rrhr�rGr?rI�
_lookup_joinsr&�
split_exclude�names_with_pathrA�
trim_joinsrHrJr�r*r�rr�rB�is_nullabler�r0rr�rr�)rU�filter_exprr�r�r�r�r�r�r��	condition�clause�argr�r�r_�reffed_expression�
used_joinsr?r�r�r��erH�	join_list�num_lookupsr6�lookup_type�
require_outerr�r9r�r:�build_filter�s�

�
	�



�
��



(
�zQuery.build_filtercCs$|�tdi|d|di���dS)Nrr�r9)�add_qr)rU�
filter_clauser9r9r:�
add_filter�s$zQuery.add_filtercsF�fdd��jD�}��|�j�\}}|r�j�|t���|�dS)zo
        A preprocessor for the internal _add_q(). Responsible for doing final
        join promotion.
        cs h|]}�j|jtkr|�qSr9r/)r7rrdr9r:r��s zQuery.add_q.<locals>.<setcomp>N)r�r�r�r�r;r'r})rU�q_object�existing_innerrr�r9rdr:r�s
zQuery.add_qcCs|j|dd�dS)NFr�r)r
)rUrr9r9r:�build_where�rZzQuery.build_wherec
Cs�|j}||jA}|p|j}|j||jd�}	t|jt|j�|�}
|jD]}|j|||||||d�\}}
|
�|
�|r?|	�||�q"|
�	|�}
|	|
fS)z%Add a Q-object to the current filter.�rC�negated)r�r�r�r�r�r�)
rCrr�r9r�rAr
r:r;r>)rUrr�r�r�r�r�r�rC�
target_clauserErE�child_clause�needed_innerr9r9r:r��s&

�

�
�
zQuery._add_qc

Cs�|j}||jN}|p|j}|j||jd�}|jD]%}t|t�r)|j||||d�}n|j||||dddd�\}}	|�||�q|S)z4Add a FilteredRelation object to the current filter.r)r2r�r�TF)r�r�r�r�r�r�)	rCrr�rArBr/�build_filtered_relation_qr
r;)
rUrr2r�r�rCrrErr�r9r9r:r�s$



�
�zQuery.build_filtered_relation_qcCs�||_tt|j��}|�|j�\}}}|rtd|j��t|�D]>}|�|�\}}	}|s.dnd}
|	d|
�}t|�D]"\}}
t	|�|krU|||
krTtd|j|f��q;td||jf��q ||j
|j<dS)NzAFilteredRelation's relation_name cannot contain lookups (got %r).r.r�zOFilteredRelation's condition doesn't support relations outside the %r (got %r).zlFilteredRelation's condition doesn't support nested relations deeper than the relation_name (got %r for %r).)r�rorCrr�rNr�rr�r�r�)rUrlr�r��relation_lookup_parts�relation_field_partsr�r�r��lookup_field_parts�shift�lookup_field_path�idx�lookup_field_partr9r9r:�add_filtered_relation�s:��������zQuery.add_filtered_relationc
Cs�gg}}t|�D�];\}}|gf}	|dkr|jj}d}
d}z|�|�}
WnJtyo||jvr8|j|j}
n5||jvrm|dkrm|j|}t|j	vrg|j	�
t�}|�||||�\}
}
}}|�|
dd��n|�|j	�}
Ynw|
dur�|
j
r�|
js�td|��z|
jjj}Wn0ty�d}Yn&w|d8}|dks�|r�tgt|��|j�|j��}td|d�|�f��n�||jur�|�|�}|r�|�|�|	d�|�|dj}t|
d	��r(|
�|�}|�s	t|�D]!\}}|j�r|	d�|d|d��|�|	�t|d|��q�|d}|�|�|j}|j}|j}|	d�|�|�|	�q	|
}|
f}|�rE|dt |�k�rEtd
||d|f��|||||dd�fS)a�
        Walk the list of names and turns them into PathInfo tuples. A single
        name in 'names' can generate multiple PathInfos (m2m, for example).

        'names' is the path of names to travel, 'opts' is the model Options we
        start the name resolving from, 'allow_many' is as for setup_joins().
        If fail_on_missing is set to True, then a name that can't be resolved
        will generate a FieldError.

        Return a list of PathInfo tuples. In addition return the final field
        (the last used join field) and target (which is a field guaranteed to
        contain the same value as the final field). Finally, return those names
        that weren't found (which are likely transforms and the final lookup).
        r�NrrKz�Field %r does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.r�z7Cannot resolve keyword '%s' into field. Choices are: %s�, �
get_path_infozACannot resolve keyword %r into field. Join on '%s' not permitted.)!r�r�r4rOr
rTr�r�rrNrMr�rxr�rQrr�r�rLr��sortedr@r)�get_path_to_parent�to_optsr�r#�m2mr�r&r��
target_fieldsr�)rU�namesr?r��fail_on_missingrJr�r�r4�cur_names_with_pathr�rlr_�filtered_relation_pathr�r��	available�path_to_parent�	pathinfos�	inner_pos�p�lastrGrHr9r9r:r��s�



���
�������





�
��zQuery.names_to_pathcsn|g}�fdd�}d�tt|�dd�D]5}	z�j|d|	�||dd�\}
�}}WntyA}
z
|	dkr5�|
�WYd}
~
qd}
~
ww||	d�}|D]}���fd	d
�}tj|||d�}qK|
D]M}|jrn|j��}|j}nd}d}|j	}|j
r��|j�}nd}t
|j||t|j||d�}|js�|r�|nd}�j|||d
�}|�|�|r�|dd�|_q`t�||||
|�S)a
        Compute the necessary table joins for the passage through the fields
        given in 'names'. 'opts' is the Options class for the current model
        (which gives the table we are starting from), 'alias' is the alias for
        the table to start the joining from.

        The 'can_reuse' defines the reverse foreign key joins we can reuse. It
        can be None in which case all joins are reusable or a set of aliases
        that can be reused. Note that non-reverse foreign keys are always
        reusable when using setup_joins().

        The 'reuse_with_filtered_relation' can be used to force 'can_reuse'
        parameter and force the relation on the given connections.

        If 'allow_many' is False, then any reverse foreign key seen will
        generate a MultiJoin exception.

        Return the final field involved in the joins, the target field (used
        for any 'where' constraint), the final 'opts' value, the joins, the
        field path traveled to generate the joins, and a transform function
        that takes a field and alias and is equivalent to `field.get_col(alias)`
        in the simple case but wraps field transforms if they were included in
        names.

        The target field is the field containing the concrete value. Final
        field can be something different, for example foreign key pointing to
        that value. Final field is needed for example in some value
        conversions (convert 'obj' in fk__id=obj to pk val using the foreign
        key field for example).
        cs�jsd}|�|�Sr2r�)r�r�rdr9r:�final_transformerms
z,Query.setup_joins.<locals>.final_transformerNrrKT)r*r�cs<z|||�}��||�WStyt�t�r�r���wr2)r�rrBr)r�r�r4�previous�wrapped�rG�last_field_exceptionrUr9r:�	transform�s
�z$Query.setup_joins.<locals>.transform)r4r4)rl)r2r�)�ranger�r�r�	functools�partialrlrXr�r&�directrr�r%r�rr'r)r�rJrF)rUr)r?r�r�r�r�rIr3�pivotrJrH�rest�excr�r4r8r)rlrnrvr~r2r9r6r:r�GsV ���


��
�zQuery.setup_joinscs�|dd�}tt|��D]H\}}t|�dks|jsn:|jr n5dd�|jjD�}dd�|D����|�s7n�fdd�|jjD��t	�fdd	�|D��}|�
|���q||d
|fS)a�
        The 'target' parameter is the final field being joined to, 'joins'
        is the full list of join aliases. The 'path' contain the PathInfos
        used to create the joins.

        Return the final target field and table alias and the new active
        joins.

        Always trim any direct join if the target column is already in the
        previous table. Can't trim reverse joins as it's unknown if there's
        anything on the other side of the join.
        Nr�cS�h|]}|j�qSr9��columnr�r9r9r:r��r�z#Query.trim_joins.<locals>.<setcomp>cSr@r9rAr�r9r9r:r��r�cs*i|]}|dj�vr|dj|d�qS)r�rrA�r7�r)�cur_targetsr9r:rx�s*z$Query.trim_joins.<locals>.<dictcomp>c3s�|]}�|jVqdSr2rAr�)�targets_dictr9r:r;���z#Query.trim_joins.<locals>.<genexpr>rK)r��reversedr�r<rlr��foreign_related_fields�issubset�related_fieldsrpr=ru)rUrHrIrJr��info�join_targetsr9)rErFr:r�s

zQuery.trim_joinsccs^�|D])}t|t�r|Vq|r tt|dd��r |��EdHq|j|��|d�EdHqdS)Nr�r�)rBr�callabler�r�r�r�)�clsr�r�r�r9r9r:r��s�
��zQuery._gen_colsccs �dd�|�|�D�EdHdS)Ncsr
r2�r�)r7r�r9r9r:r;�rz)Query._gen_col_aliases.<locals>.<genexpr>)r�)rOr�r9r9r:�_gen_col_aliases�s�zQuery._gen_col_aliasesc
Cs:|j�|�}|dur8|s!|�|g�D]}t|j|t�r td��q|r6||jvr.td|��t||j|�S|S|�	t
�}|j�|d�}|durZ|dd�D]}|�||�}qO|S|j||�
�|��|d�}	|�|	j|	j|	j�\}
}}|s�t|�dkr�td��t|
�dkr�td��|	�|
d|�}|dur�|�|�|S)Nr�zCCannot aggregate over the '%s' alias. Use annotate() to promote it.rr�)r�z?Referencing multicolumn fields with F() objects isn't supported)r�rhrQrBr�r%rrTrrMrr�r�r�r�rrHrIrJr�rKrA)
rUr4r�r2r�r�r��
field_listr8r�rH�final_aliasr	r9r9r:�resolve_ref�sD��
��

zQuery.resolve_refcCsN|\}}t|t�r|t|�f}nt|t�r|t|j�f}t|j�}|j|_|�|�|jdd�|�	|�\}}|j
d}	|	j}
|	j}||vrp|
jj
j}|�|�|
�d�}
|
|�|j
dj�|�|��}|j�|t�d|j|<|
�d�}
|
|	t|��}|j�|t�|�t|��\}}|r�|jd|dfdd|d�\}}|�|t�||fS)a�
        When doing an exclude against any kind of N-to-many relation, we need
        to use a subquery. This method constructs the nested query, given the
        original exclude filter (filter_expr) and the portion up to the first
        N-to-many relation field.

        For example, if the origin filter is ~Q(child__name='foo'), filter_expr
        is ('child__name', 'foo') and can_reuse is a set of joins usable for
        filters in the original query.

        We will turn this into equivalent of:
            WHERE NOT EXISTS(
                SELECT 1
                FROM child
                WHERE name = 'foo' AND child.parent_id = parent.id
                LIMIT 1
            )
        Tr�rr�z
%s__isnull)r�r�r�)rBrrr4r0r�r�rr��
trim_startr�r�r�r�r�r�r�r�r�r;r'r�rr
rr()rUrr�r��
filter_lhs�
filter_rhsr��trimmed_prefix�contains_louterr6�select_fieldr�r�r�r�rr�or_null_conditionr�r9r9r:r�sB







�



�zQuery.split_excludecCs(|j�t�t�|jD]}|��qdSr2)r�r;r*r'r��	set_empty)rUr�r9r9r:r\Ks

�zQuery.set_emptycCstdd�|jjD��S)Ncss�|]}t|t�VqdSr2)rBr*)r7�cr9r9r:r;QrGz!Query.is_empty.<locals>.<genexpr>)r�r�rArdr9r9r:�is_emptyPszQuery.is_emptycCs�|dur|jdurt|j|j|�|_n|j||_|dur4|jdur.t|j|j|�|_n|j||_|j|jkr@|��dSdS)a�
        Adjust the limits on the rows retrieved. Use low/high to set these,
        as it makes it more Pythonic to read and write. When the SQL query is
        created, convert them to the appropriate offset and limit values.

        Apply any limits passed in here to the existing constraints. Add low
        to the current low value and clamp both to any existing high value.
        N)rR�minrQr\)rU�lowrr9r9r:r Ss	

�zQuery.set_limitscCsd\|_|_dS)zClear any existing limits.rLN�rQrRrdr9r9r:r�j�zQuery.clear_limitscCs|jdkp	|jduS)Nrrardr9r9r:r�nszQuery.is_slicedcCs|jduo|j|jdkS)Nr�)rRrQrdr9r9r:�
has_limit_onerszQuery.has_limit_onecCs|jS)z�
        Return True if adding filters to this instance is still possible.

        Typically, this means no limits or offsets have been put on the results.
        )r�rdr9r9r:�
can_filterur�zQuery.can_filtercCs*d|_d|_d|_|�d�|�d�dS)z%Remove all fields from SELECT clause.r9FN)r�r�r�r"r�rdr9r9r:r}s

zQuery.clear_select_clausecCsd|_d|_dS)z�
        Clear the list of fields to select (but not extra_select columns).
        Some queryset types completely replace any existing list of select
        columns.
        r9N�r�r�rdr9r9r:�clear_select_fields�s
zQuery.clear_select_fieldscCs$|j|f7_|j|f7_dSr2re)rUr6r4r9r9r:�add_select_col�szQuery.add_select_colcCsd|_t|�|_dS�NF)r�rpr�)rU�colsr9r9r:r@�szQuery.set_selectcGs||_d|_dS)zW
        Add and resolve the given fields to the query's "distinct on" clause.
        TN)r�r�)rUrYr9r9r:�add_distinct_fields�s
zQuery.add_distinct_fieldsc
	Cs|��}|��}z;g}|D])}|j|�t�|||d�}|�|j|j|j�\}}	}
|D]}|�	|�
||	��q*q
|rA|�|�WdSWdStyPt
d|��t
y�t|vr[�||jvrft
d|��tgt|��|j�|j�|j��}t
d|d�|�f��w)zu
        Add the given (model) fields to the select set. Add the field names in
        the order specified.
        )r�zInvalid field name: '%s'z;Cannot select the '%s' alias. Use annotate() to promote it.z5Cannot resolve keyword %r into field. Choices are: %sr"N)r�r�r�rMrrrHrIrJr�rKr@r&rr�r$r@r�rTr�r))
rUrY�	allow_m2mr�r?rir4r�rHrSrIr�r)r9r9r:r�sR
���
��������zQuery.add_fieldscGs�g}|D]\}t|t�rJd|vrt�|�rtjd|tdd�q|dkr$q|�d�r/|dd�}||jvr5q|j	r>||j	vr>q|�
|�t�|j
j�n
t|d	�sT|�|�t|d
d�r`td|��q|ritd
|��|rt|j|7_dSd|_dS)a8
        Add items from the 'ordering' sequence to the query's "order by"
        clause. These items are either field names (not column names) --
        possibly with a direction prefix ('-' or '?') -- or OrderBy
        expressions.

        If 'ordering' is empty, clear all ordering from the query.
        r%z~Passing column raw column aliases to order_by() is deprecated. Wrap %r in a RawSQL expression before passing it to order_by().�)�category�
stacklevel�?�-r�Nr�r�Fz[Using an aggregate in order_by() without also including it in annotate() is not allowed: %szInvalid order_by arguments: %s)rB�strr!�match�warnings�warnr,�
startswithr�r�r�rMrr�r�r�r�r�rr�r�)rU�ordering�errors�itemr9r9r:�add_ordering�sF	
��



���
zQuery.add_orderingcCs8|s
|js|js|jr
dSd|_d|_|rd|_dSdS)a
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting
        query (not even the model's default).
        Nr9F)r�r�r�r�r�r�)rUr��
clear_defaultr9r9r:r��s
�zQuery.clear_orderingc
Cs�|r/t�}t�}t|j���dd�D]}|jj}||vr.|�dd�|jjD��|�	|�qt|j
�}|jrv|j��D]9\}}t
�|j�}	d|	jvrb|j}
d|
j|
jf}tj|td�|��}n|rh||vrjd}|j|d�}|�|�q<t|�|_dS)	a?
        Expand the GROUP BY clause required by the query.

        This will usually be the set of all non-aggregate fields in the
        return data. If the database backend supports grouping by the
        primary key, and the query would be equivalent, the optimization
        will be made automatically.
        r�NcSr@r9rA)r7r�r9r9r:r�s��z%Query.set_group_by.<locals>.<setcomp>r�zI`alias=None` must be added to the signature of %s.%s.get_group_by_cols().)rmrP)r<rhr�r�r�rQrAr��local_concrete_fieldsr;r�rTr{�inspect�	signaturer��
parametersrlrr�rsrtr,rxrpr�)
rUr�column_names�seen_modelsr)r�r�r�r�r}�annotation_class�msg�
group_by_colsr9r9r:rs6

�
�


�
zQuery.set_group_bycCsLt|jt�r	i}n|j}|D]}|}|�t�D]}|�|i�}qq||_dS)z�
        Set up the select_related data structure so that we only select
        certain related models (as opposed to all models, when
        self.select_related=True).
        N)rBr�r�rMrrW)rU�fields�
field_dictr��d�partr9r9r:�add_select_related-s�
zQuery.add_select_relatedc
Cs�|rWi}|rt|�}ntg�}|��D]=\}	}
|�|	�t|
�}
g}|
�d�}|dkrJ|dks7|
|ddkr>|�t|��|
�d|d�}|dks+|
|f||	<q|j�|�|s[|re|j	�
t||�t�|rp|j
t|�7_
|rw||_dSdS)zm
        Add data to the various extra_* attributes for user-created additions
        to the query.
        z%srKrr��%r.N)rir{r�rq�findr�r�r�rAr�r;r)r'r�rpr�)
rUr��
select_paramsr�rM�tablesr��select_pairs�
param_iterr4�entry�entry_paramsr�r9r9r:r!=s0


�
�zQuery.add_extracCst�df|_dS)z0Remove any fields from the deferred loading set.TN)r�r�rdr9r9r:�clear_deferred_loading^rbzQuery.clear_deferred_loadingcCs6|j\}}|r|�|�df|_dS|�|�df|_dS)aj
        Add the given list of model field names to the set of fields to
        exclude from loading from the database when automatic column selection
        is done. Add the new field names to any existing field names that
        are deferred (or removed from any existing field names that are marked
        as the only ones for immediate loading).
        TFN)r�rr��rUrY�existingrZr9r9r:�add_deferred_loadingbs
zQuery.add_deferred_loadingcCs`|j\}}t|�}d|vr|�d�|�|��jj�|r'|�|�df|_dSt|�df|_dS)a

        Add the given list of model field names to the set of fields to
        retrieve when the SQL is executed ("immediate loading" fields). The
        field names replace any existing immediate loading field names. If
        there are field names already specified for deferred loading, remove
        those names from the new field_names before storing the new names
        for immediate loading. (That is, immediate loading overrides any
        existing immediate values, but respects existing deferrals.)
        r�FN)	r�r<r�r;r�r�r4r�r�r�r9r9r:�add_immediate_loadingvs


zQuery.add_immediate_loadingcCs8z|jWStyi}|�||j�||_|YSw)aF
        If any fields are marked to be deferred, return a dictionary mapping
        models to a set of names in those fields that will be loaded. If a
        model is not in the returned dictionary, none of its fields are
        deferred.

        If no fields are marked for deferral, return an empty dictionary.
        )�_loaded_field_names_cacher�rf�get_loaded_field_names_cb)rU�
collectionr9r9r:�get_loaded_field_names�s�zQuery.get_loaded_field_namescCsdd�|D�||<dS)z,Callback used by get_deferred_field_names().cSr@r9rr6r9r9r:r��r�z2Query.get_loaded_field_names_cb.<locals>.<setcomp>Nr9)rUr�r�r�r9r9r:r��r�zQuery.get_loaded_field_names_cbcC�$|durd|_nt|�|_d|_dS)z@Set the mask of annotations that will be returned by the SELECT.N)r�r<r��rUr)r9r9r:r��s

zQuery.set_annotation_maskcCs$|jdur|�|j�|��dSdSr2)r�r�rr�r9r9r:r��s
�zQuery.append_annotation_maskcCr�)z�
        Set the mask of extra select items that will be returned by SELECT.
        Don't remove them from the Query since they might be used later.
        N)r�r<r�r�r9r9r:r"�s

zQuery.set_extra_maskc
Csxd|_|��|��|r^|D]}|�|�qg}g}g}|js(|js(t|�}n#d|_|D]}||jvr:|�	|�q-||j
vrE|�	|�q-|�	|�q-|�|�|�|�t
|||�}ndd�|jjjD�}t
|�}|jdur�|�dd�|jjjD�d�|jdd�|��n%|jr�g}|jD]}	t|	t�r�|	j|vr�|j|	j}	|�	|	�q�t|�|_t|�|_|�|d�dS)NFcSsg|]}|j�qSr9rr6r9r9r:r_�r�z$Query.set_values.<locals>.<listcomp>Tcsr
r2rr6r9r9r:r;�rz#Query.set_values.<locals>.<genexpr>r)r�r�rfr�r�r�rhr�rSr�rTr"r�r�r�r�rr�rrrBr�refsrpr�)
rUr�r�rY�extra_names�annotation_namesr8�selectedr�r�r9r9r:�
set_values�sH









zQuery.set_valuescsJ�jdur�jS�js
iS�jdur"�fdd��j��D��_�jS�jS)z�
        Return the dictionary of aggregate columns that are not masked and
        should be used in the SELECT clause. Cache this result for performance.
        Nc� i|]\}}|�jvr||�qSr9)r�r�rdr9r:rx��

�z+Query.annotation_select.<locals>.<dictcomp>)r�r�r�r{rdr9rdr:rT�s


�zQuery.annotation_selectcsJ�jdur�jS�js
iS�jdur"�fdd��j��D��_�jS�jS)Ncr�r9)r�r�rdr9r:rx		r�z&Query.extra_select.<locals>.<dictcomp>)r�r�r�r{rdr9rdr:rS	s


�zQuery.extra_selectcs�g}|D]	\}}|�|�qd}�fdd��jD�}t|�D] \}}|jr'n�j||djtkr5d}||}	��|	�q|jj}
|}g}|D]\}
}|t	|�dkrWn|�
|
�|t	|�8}qI|�
|
jdj�t
�|�}�j||d}|jtkr�|js�dd�|
jD�}||d���||�|
��jd||d�}|r��j�|t�nd	d�|
jD�}||��jD]}�j|dkr�t�j|j|��j|<nq����fd
d�|D��||fS)a�
        Trim joins from the start of the join path. The candidates for trim
        are the PathInfos in names_with_path structure that are m2m joins.

        Also set the select column so the start matches the join.

        This method is meant to be used for generating the subquery joins &
        cols in split_exclude().

        Return a lookup usable for doing outerq.filter(lookup=self) and a
        boolean indicating if the joins in the prefix contain a LEFT OUTER join.
        _Fcs$g|]}|�jvs|�jkr|�qSr9)r�r�r�rdr9r:r_%	s
�z$Query.trim_start.<locals>.<listcomp>r�TrcS�g|]}|d�qSr[r9rCr9r9r:r_E	r�NcSr�r�r9rCr9r9r:r_P	r�cr3r9)r�r6)�select_aliasr9r:r_X	r8)rxr�r�r'r0rr=r�r�r�r�rIr4rr)rlrK�get_extra_restrictionr�r�r;r'r�r#rjr@)rUr��	all_pathsr��pathsrY�
lookup_tables�
trimmed_pathsrJr�r��paths_in_prefixrXr4�
first_join�
select_fields�extra_restrictionr�r9)r�rUr:rU	s\

�

�
��
�zQuery.trim_startcCsttjjo|jp|jS)a
        Check if the given field should be treated as nullable.

        Some backends treat '' as null and Django treats such fields as
        nullable for those backends. In such situations field.null can be
        False even if we should treat the field as nullable.
        )rrrfr��empty_strings_allowed�null)rUr�r9r9r:r[	s
��zQuery.is_nullable)NNr2)T)FNr�rh)FT)FFNTTFT)FFTTT)FF)TF)NTF)F)TNF)hrmrr�r�rir�r�r�r+rVr�r�r�r-r�rrr�r�r�r�rXrr�r�r�rrrrr$r-rJrfrnrpr=r{r}r�r�r�r�r�r)r�r�r	r�r�r�r�r�r�r�r�r�r�r�r
rrrr�rr!r�r�r�classmethodr�rQrTr�r\r^r r�r�rcrdrrfrgr@rjrryr�rr�r!r�r�r�r�r�r�r�r"r�rTrSrUrr9r9r9r:r0�s�
X




34X

i
V
"	'1
0!

-
�
�

g
�h

,C

	
+
.
)!3

J�ASCcCs4t|}|ddkr|dd�|dfS||dfS)a
    Return the field name and direction for an order specification. For
    example, '-foo' is returned as ('foo', 'DESC').

    The 'default' param is used to indicate which way no prefix (or a '+'
    prefix) should sort. The '-' prefix always sorts the opposite way.
    rrpr�N)r )r��default�dirnr9r9r:�
get_order_dirn	sr�cCs(||vr
||�|�dS|h||<dS)z^
    Add "value" to the set of values for "key", whether or not "key" already
    exists.
    N)r;)�datarwr�r9r9r:rS|	srScCs|jo	|jo	|jS)z�
    Check if the given field is reverse-o2o. The field is expected to be some
    sort of relation field or related object.
    )r��
one_to_oner3)r�r9r9r:rP�	srPc@s(eZdZdZdd�Zdd�Zdd�ZdS)	r9z]
    A class to abstract away join promotion problems for complex filter
    conditions.
    cCsD||_||_|jr|tkrt|_nt|_n|j|_||_t�|_dSr2)rCrr'r(�effective_connector�num_childrenr�votes)rUrCr�rr9r9r:rV�	szJoinPromoter.__init__cCs|j�|�dS)z`
        Add single vote per item to self.votes. Parameter can be any
        iterable.
        N)r�rA)rUr�r9r9r:r:�	szJoinPromoter.add_votescCs~t�}t�}|j��D]'\}}|jdkr||jkr|�|�|jdks-|jdkr2||jkr2|�|�q|�|�|�|�|S)a
        Change join types so that the generated query is as efficient as
        possible, but still correct. So, change as many joins as possible
        to INNER, but don't make OUTER joins INNER if that could remove
        results from the query.
        r(r')r<r�r{r�r�r;r{r})rUr��
to_promote�	to_demoter�r�r9r9r:r>�	s


�

zJoinPromoter.update_join_typesN)rmrr�r�rVr:r>r9r9r9r:r9�	s
r9)r�)Ur�r�r�r:r|r�rs�collectionsrr�collections.abcrr�	itertoolsrrr�stringr	�django.core.exceptionsr
r�	django.dbrr
r�django.db.models.aggregatesr�django.db.models.constantsr�django.db.models.expressionsrrrrrrr�django.db.models.fieldsr�'django.db.models.fields.related_lookupsr�django.db.models.lookupsr�django.db.models.query_utilsrrr�django.db.models.sql.constantsrrr r!r"�#django.db.models.sql.datastructuresr#r$r%r&�django.db.models.sql.wherer'r(r)r*r+�django.utils.deprecationr,�django.utils.functionalr-�django.utils.regex_helperr.�django.utils.treer/�__all__r�r'r@rCrFr1r0r�rSrPr9r9r9r9r:�<module>sv$�J
f

https://t.me/RX1948 - 2025