HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/.local/lib/python3.10/site-packages/sqlalchemy/orm/__pycache__/context.cpython-310.pyc
o

���gM��@sPddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm	Z	dd	lm
Z
dd
lmZddlmZddlm
Z
dd
lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z#dd lm$Z$dd!lm%Z%dd"lm&Z&dd#lmZdd$l&m'Z'dd%l&m(Z(dd&l&m)Z)dd#l&mZ*dd'l&m+Z+dd(l,m-Z-dd)l,m.Z.dd*l,m/Z/dd+l,m0Z0dd,l1m2Z2dd-l1m3Z3dd.l1m4Z4dd/l1m5Z5dd0l1m6Z6dd1l1m7Z7dd2l8m9Z9dd3l:m;Z;dd4l:m<Z<dd5l=m>Z>dd6l=m?Z?dd7l=m@Z@dd8l=mAZAdd9l=mBZBdd:l=mCZCdd;l=mDZDdd<l=mEZEdd=lFmGZGe
�r�dd>lHmIZIdd?lHmJZJdd@lmKZKddAlLmMZMddBlNmOZOddClPmQZQddDlPmRZRddElSmTZTddFlUmVZVddGl,mWZWddHlXmYZYddIl8mZZZddJl:m[Z[ddKl=m\Z\ddLl=m]Z]ddMl=m^Z^ddNl=m_Z_ddOl=m`Z`ddPlambZbedQedR�ZcejdZee�f�ZgeCjhZhGdSdT�dT�Zie�fdUdVi�ZjGdWdX�dXe4�ZkGdYdZ�dZek�ZlGd[d\�d\ek�ZmGd]d^�d^�Zne&jj4�od_d`�Gdadb�dbem��ZpGdcdd�dde;e6eEe-�Zqe&jj4�od_de�Gdfdg�dgele>��Zre&jj4�od_dh�Gdidj�djemeD��Zs		kd�d�dtdu�Ztd�dydz�Zud�d{d|�Zv	d�d�d�d��ZwGd�d��d��ZxGd�d��d�ex�ZyGd�d��d�ex�ZzGd�d��d�ex�Z{Gd�d��d�e{�Z|Gd�d��d�e{�Z}Gd�d��d�e}�Z~dS)��)�annotationsN)�Any)�cast)�Dict)�Iterable)�List)�Optional)�Set)�Tuple)�Type)�
TYPE_CHECKING)�TypeVar)�Union�)�
attributes)�
interfaces)�loading)�_is_aliased_class)�ORMColumnDescription)�ORMColumnsClauseRole)�PathRegistry)�_entity_corresponds_to)�_ORMJoin)�_TraceAdaptRole)�AliasedClass)�Bundle)�
ORMAdapter)�ORMStatementAdapter�)�exc)�future)�inspect)�sql)�util)�	coercions)�
expression)�roles)�visitors)�_TP)�is_dml)�is_insert_update)�is_select_base)�_select_iterables)�CacheableOptions)�CompileState)�
Executable)�
Generative)�Options)�
UpdateBase)�GroupedElement)�
TextClause)�CompoundSelectState)�LABEL_STYLE_DISAMBIGUATE_ONLY)�LABEL_STYLE_NONE)�LABEL_STYLE_TABLENAME_PLUS_COL)�Select)�SelectLabelStyle)�SelectState)�TypedReturnsRows)�InternalTraversal)�_InternalEntityType)�OrmExecuteOptionsParameter)�PostLoad)�Mapper)�Query)�_BindArguments)�Session)�Result)�_CoreSingleExecuteParams)�_ColumnsClauseArgument)�SQLCompiler)�_DMLTableElement)�
ColumnElement)�_JoinTargetElement)�_LabelConventionCallable)�_SetupJoinsElement)�ExecutableReturnsRows)�
SelectBase)�
TypeEngine�_T)�boundc@sTeZdZUdZded<ded<ded<Gdd	�d	e�Z	
	
dddd�Zd dd�Zd
S)!�QueryContext)�top_level_context�
compile_state�query�user_passed_query�params�load_options�bind_arguments�execution_options�session�	autoflush�populate_existing�invoke_all_eagers�
version_check�
refresh_state�create_eager_joins�propagated_loader_optionsr�runid�partials�post_load_paths�identity_token�	yield_per�loaders_require_buffering�loaders_require_uniquing�intrdzDict[PathRegistry, PostLoad]rf�ORMCompileStaterUc@s<eZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdS)z!QueryContext.default_load_optionsFTN)�__name__�
__module__�__qualname__�_only_return_tuples�_populate_existing�_version_check�_invoke_all_eagers�
_autoflush�_identity_token�
_yield_per�_refresh_state�_lazy_loaded_from�_legacy_uniquing�_sa_top_level_orm_context�_is_user_refresh�r|r|�I/home/arjun/.local/lib/python3.10/site-packages/sqlalchemy/orm/context.py�default_load_options�sr~Nr.�	statement�&Union[Select[Any], FromStatement[Any]]rWrXrFr\rDrY�QUnion[Type[QueryContext.default_load_options], QueryContext.default_load_options]r[�$Optional[OrmExecuteOptionsParameter]rZ�Optional[_BindArguments]c	s�|�_|pt�_|pt�_|�_|�_|�_|�_d�_d�_	|�_
|j�_|j
j}	|j}
t�fdd�t|	|
�D���_t|j��_|j�_|j�_|j�_|j�_|j�_|j�_|j �_!dS)NFc3s&�|]\}}|jr|��|�VqdS�N)�propagate_to_loaders�'_adapt_cached_option_to_uncached_option)�.0�opt�uncached_opt��selfr|r}�	<genexpr>�s��

�z(QueryContext.__init__.<locals>.<genexpr>)"rY�_EMPTY_DICTr[rZrUrVrWr\rirjrXrzrT�select_statement�
_with_options�tuple�ziprc�dictrrtr]rqr^rsr_rrr`rwrarvrhrurg)r�rUrrWrXr\rYr[rZ�cached_options�uncached_optionsr|r�r}�__init__�s0



�zQueryContext.__init__�returncCs
|jp|Sr�)rTr�r|r|r}�_get_top_level_context�s
z#QueryContext._get_top_level_context)NN)rUr.rr�rWr�rXrFr\rDrYr�r[r�rZr�)r�rS)	rmrnro�	__slots__�__annotations__r1r~r�r�r|r|r|r}rSfs
�ArS� _result_disable_adapt_to_contextTcsTeZdZdZdd�Zed�fdd��Zed
d��Zeddd��Zedd��Z	�Z
S)�AbstractORMCompileStateFc	Cs�i|_|duri|_}|sJ�dS|j|_}|r0d|d<|r.|jD]
}|jr-|�|�q#dS|�dd�r8dS|jd}z|d}Wn	tyLYnw|jD]
}|j	r]|jr]|�|�qPd|d<dS)NT�toplevel_ormFr�
selectable)
r�global_attributes�_global_attributesr��_is_criteria_option�process_compile_state�get�stack�KeyError�_is_compile_state)	r�r�compiler�toplevel�process_criteria_for_toplevel�gar��stack_0�
toplevel_stmtr|r|r}�_init_global_attributes�s6


�
�

�z/AbstractORMCompileState._init_global_attributesr�Union[Select, FromStatement]r��Optional[SQLCompiler]�kwrr�cst�j||fi|��S)a�Create a context for a statement given a :class:`.Compiler`.

        This method is always invoked in the context of SQLCompiler.process().

        For a Select object, this would be invoked from
        SQLCompiler.visit_select(). For the special FromStatement object used
        by Query to indicate "Query.from_statement()", this is called by
        FromStatement._compiler_dispatch() that would be called by
        SQLCompiler.process().
        )�super�create_for_statement��clsrr�r���	__class__r|r}r�sz,AbstractORMCompileState.create_for_statementcC�t��r���NotImplementedError)r�r\rrXr[rZ�is_pre_eventr|r|r}�orm_pre_session_exec�
z,AbstractORMCompileState.orm_pre_session_execrEcCs(|j||pi|d�}|�||||||�S)N)r[)�execute�orm_setup_cursor_result)r�r\rrXr[rZ�conn�resultr|r|r}�orm_execute_statement's

��z-AbstractORMCompileState.orm_execute_statementcCr�r�r��r�r\rrXr[rZr�r|r|r}r�=r�z/AbstractORMCompileState.orm_setup_cursor_result)rr�r�r�r�rr�r�)r�rE)rmrnro�is_dml_returningr��classmethodr�r�r�r��
__classcell__r|r|r�r}r��s'
r�c@s(eZdZdZedd��Zedd��ZdS)�AutoflushOnlyORMCompileStatez>ORM compile state that is a passthrough, except for autoflush.cCs4tj�ddh||j�\}}|s|jr|��||fS)N�_sa_orm_load_optionsr])rSr~�from_execution_options�_execution_optionsrt)r�r\rrXr[rZr�rYr|r|r}r�Ms���

z1AutoflushOnlyORMCompileState.orm_pre_session_execcCs|Sr�r|r�r|r|r}r�js
z4AutoflushOnlyORMCompileState.orm_setup_cursor_resultN)rmrnro�__doc__r�r�r�r|r|r|r}r�Js
r�c@seZdZUGdd�de�Zded<ded<ded<ded<d	ed
<ded<d
ed<ded<ded<ded<ded<ded<ded<ded<eZded<dZdd �Z	e
rZed?d'd(��Zd)d*�Z
ed@d.d/��Zed0d1��Zed2d3��Zed4d5��Zed6d7��Zd8d9�Zd:d;�Zed<d=��Zd>S)Arlc@s�eZdZdejfdejfdejfdejfdejfdejfdejfdejfd	ejfd
ejfdejfgZdZdZ	d
Z
eZd
Z
d
ZdZdZdZdZdZdS)z'ORMCompileState.default_compile_options�_use_legacy_query_style�_for_statement�_bake_ok�
_current_path�_enable_single_crit�_enable_eagerloads�_only_load_props�_set_base_alias�_for_refresh_state�_render_for_subquery�_is_starFTN)rmrnror=�
dp_boolean�dp_has_cache_key�dp_plain_obj�_cache_key_traversalr�r�r��_path_registryr�r�r�r�r�r�r�r�r|r|r|r}�default_compile_optionsxs0�r�zDict[Any, Any]rr�r�rr��List[_QueryEntity]�	_entitiesz%Dict[_InternalEntityType, ORMAdapter]�_polymorphic_adaptersz=Union[Type[default_compile_options], default_compile_options]�compile_optionszOptional[_QueryEntity]�_primary_entity�bool�use_legacy_query_stylerL�_label_conventionzList[ColumnElement[Any]]�primary_columns�secondary_columnszSet[ColumnElement[Any]]�dedupe_columnszList[Tuple[Any, ...]]rbr�current_pathFcOr�r�r�)r��argr�r|r|r}r���zORMCompileState.__init__r�r�r�r�rr�cK�dSr�r|r�r|r|r}r��sz$ORMCompileState.create_for_statementcCs*|j}||vr|�|�|�|�dSdSr�)r��add�append)r��obj�col_collection�deduper|r|r}�_append_dedupe_col_collection�s

�z-ORMCompileState._append_dedupe_col_collection�label_styler:�legacycCs|r	ddd�}|St�|�S)NcSs|r|St|d�S)N�key)�getattr)�col�col_namer|r|r}�name�s
z7ORMCompileState._column_naming_convention.<locals>.namer�)r;�_column_naming_convention)r�r�r�r�r|r|r}r��s

z)ORMCompileState._column_naming_conventioncCst|�Sr�)�_column_descriptions�r�rr|r|r}�get_column_descriptions��z'ORMCompileState.get_column_descriptionsc
Cs�tj�dhd�||j�\}}d|vr|d}|jj�|j|�}|s$t}n|�t�}|j	r4|�d|j	i�}t
|jdd�rSt|jj
�dkrS|�dd�durS|�dd	d
��}||d<z|jd}	WntyiJd��w|	rq|	j|d<|sz|jrz|��||fS)Nr�>r]rhrgr^�sa_top_level_orm_contextr�rhr��
�compiled_cacheTz#excess depth for ORM loader options)r��_cache_disable_reason�clause�plugin_subjectFz0statement had 'orm' plugin but no plugin_subject�mapper)rSr~r�r�rV�
merge_withr[�_orm_load_exec_options�unionrvr��_compile_options�lenr�r��_propagate_attrsr�rrt)
r�r\rrXr[rZr�rY�ctxrr|r|r}r��sT���
�����
�

z$ORMCompileState.orm_pre_session_execc	CsJ|j}|jj}|�dtj�}	|jjr|St||||||	||�}
t�	||
�S)Nr�)
�context�compiledrUr�rSr~r�r�r�	instances)r�r\rrXr[rZr��execution_contextrUrY�querycontextr|r|r}r�5s$
��
z'ORMCompileState.orm_setup_cursor_resultcCsdd�|jD�S)z�return all _MapperEntity objects in the lead entities collection.

        Does **not** include entities that have been replaced by
        with_entities(), with_only_columns()

        cS�g|]	}t|t�r|�qSr|��
isinstance�
_MapperEntity�r��entr|r|r}�
<listcomp>b�

��z9ORMCompileState._lead_mapper_entities.<locals>.<listcomp>)r�r�r|r|r}�_lead_mapper_entitiesZs�z%ORMCompileState._lead_mapper_entitiesc
CsL|js |jj|jvr"|j��D]}|�|ttj||j	|d��qdSdSdS)a5given MapperEntity or ORMColumnEntity, setup polymorphic loading
        if called for by the Mapper.

        As of #8168 in 2.0.0rc1, polymorphic adapters, which greatly increase
        the complexity of the query creation process, are not used at all
        except in the quasi-legacy cases of with_polymorphic referring to an
        alias and/or subquery. This would apply to concrete polymorphic
        loading, and joined inheritance where a subquery is
        passed to with_polymorphic (which is completely unnecessary in modern
        use).

        )�equivalentsr�N)
�is_aliased_classr�persist_selectabler��iterate_to_root�"_mapper_loads_polymorphically_withrr�WITH_POLYMORPHIC_ADAPTER�_equivalent_columns)r��ext_infor��mpr|r|r}� _create_with_polymorphic_adapterfs$�����z0ORMCompileState._create_with_polymorphic_adaptercCs:|jp|gD]}||j|<|��D]}||j|j<qqdSr�)�_with_polymorphic_mappersr�r�local_table)r�r�adapter�m2�mr|r|r}r�s
��z2ORMCompileState._mapper_loads_polymorphically_withcCstd��)Nz0this method only works for ORMSelectCompileStater�)r�rVr�r|r|r}�_create_entities_collection�s�z+ORMCompileState._create_entities_collectionN)rr�r�r�r�rr�rl)r�r:r�r�r�rL)rmrnror-r�r�r�r��_has_mapper_entitiesr�rr�r�r�r�r�r�r��propertyrr!rr'r|r|r|r}rlwsJ
%

Y
$
rlc@s,eZdZdZdZdd�Zdd�Zdd�Zd	S)
�DMLReturningColFilterz�an adapter used for the DML RETURNING case.

    Has a subset of the interface used by
    :class:`.ORMAdapter` and is used for :class:`._QueryEntity`
    instances to set up their columns as used in RETURNING for a
    DML statement.

    )r�columns�__weakref__cCs:|dur|j|jur||_n||_t�|j�|_|_dSr�)r#rr#�WeakPopulateDict�adapt_check_presentr+)r��
target_mapper�immediate_dml_mapperr|r|r}r��s��zDMLReturningColFilter.__init__cCs.t�|�D]}|�|�}|dur|SqdSr�)�sql_util�
_find_columnsr.)r�r��	as_filter�cc�c2r|r|r}�__call__�s
�zDMLReturningColFilter.__call__cCs.|j}|j�|d�}|durdS|jj�|�Sr�)r�_columntopropertyr�r#�c�corresponding_column)r�r�r�propr|r|r}r.�s
z)DMLReturningColFilter.adapt_check_presentN)rmrnror�r�r�r6r.r|r|r|r}r*�s	r*�orm�orm_from_statementc@speZdZUdZdZded<ded<dZded<dZdZdZ	dZ
eZeZ
eddd��Zdd�Zdd�Zdd�ZdS)�ORMFromStatementCompileStateNF�
FromStatement�statement_containerz)Union[SelectBase, TextClause, UpdateBase]�requested_statementzOptional[_DMLTableElement]�	dml_tabler�r�r�r�rr�cKs�t|t�sJ�|dur|jrt�d��|�|�}d|_|jj|_	||_
|_|j|_
}|jr6|j|_d|_g|_i|_|j|_|j	r[t|tj�r[|js[|js[|jtur[|�t�|_n||_|�|jsi|jsi|jnt|j	�|_tj ||j!|jdd�|jj"|_#|j$||ddd�|j%r�|j%D]
}|j&r�|�'|�q�|j(r�|j(D]\}}||�q�g|_)g|_*t+�|_,g|_-g|_.d|_/t|jtj0�r�i|_1|jD]}	|	�2|�q�d|_3|_4d|_5|j)D]}
|j6|
d|j7d�q�|St8t9j:|j|j;d�|_<|S)Nz�The ORM FromStatement construct only supports being invoked as the topmost statement, as it is only intended to define how result rows should be returned.T��is_current_entitiesF)r�r�)�within_columns_clause�add_to_result_map)�adapt_on_names)=rr>r��sa_exc�CompileError�__new__r�rr�r�r?r��elementr@r)�tablerAr�r�r�r�r%rO�_is_textual�_label_styler7�set_label_styler8rr�r��_QueryEntity�to_compile_state�_raw_columnsr�r�r�r�r�r��_with_context_optionsr�r��setr�rb�_fallback_from_clauses�order_byr4�extra_criteria_entities�setup_compile_state�_ordered_columns�_textual_ordered_columns�_loose_column_name_matching�process�_add_to_result_maprr�ADAPT_FROM_STATEMENT�_adapt_on_names�_from_obj_alias)r�r?r�r�r�rr��fnr��entityr8r|r|r}r��s��
��
���
����	�
�

�


�
���z1ORMFromStatementCompileState.create_for_statementcCs|Sr�r|�r��cols�current_adapterr|r|r}�_adapt_col_listO�z,ORMFromStatementCompileState._adapt_col_listcCr�r�r|r�r|r|r}�_get_current_adapterRrfz1ORMFromStatementCompileState._get_current_adaptercCsV|jj�dd�}t||�}|jjrt|j�dkrt�	d��|jD]}|�
||�q dS)z�used by BulkORMInsert (and Update / Delete?) to set up a handler
        for RETURNING to return ORM objects and expressions

        rNr�sCan't generate ORM query that includes multiple expressions at the same time as '*'; query for '*' alone if present)rrr�r*r�r�rr�rGrH�!setup_dml_returning_compile_state)r��
dml_mapperr/r$rar|r|r}riUs�
�
�z>ORMFromStatementCompileState.setup_dml_returning_compile_state)r?r�r�r�r�rr�r=)rmrnror_r(r�rA�_has_orm_entities�multi_row_eager_loaders�eager_adding_joins�compound_eager_adapterr�rV�eager_joinsr�r�rergrir|r|r|r}r=�s$
r=cs�eZdZUdZdZejZejZ	dZ
ded<ded<dej
fdejfgejZed	ejfgZd
Z	
dd d
d�Zdd�Zedd��Zdd�Z�fdd�Zedd��Zedd��Zedd��Zedd��Z�ZS)!r>a*Core construct that represents a load of ORM objects from various
    :class:`.ReturnsRows` and other classes including:

    :class:`.Select`, :class:`.TextClause`, :class:`.TextualSelect`,
    :class:`.CompoundSelect`, :class`.Insert`, :class:`.Update`,
    and in theory, :class:`.Delete`.

    r<N�(Union[ExecutableReturnsRows, TextClause]rJr�r^rQrT�entities�%Iterable[_ColumnsClauseArgument[Any]]csf�fdd�t�|�D��_|�_|j�_|j�_|j�_|j�_|j�_t	|�r+|j
nd�_
|�_dS)Ncs g|]}tjtj|�dd��qS)T)�apply_propagate_attrs�post_inspect)r$�expectr&�ColumnsClauseRolerr�r|r}r�s���z*FromStatement.__init__.<locals>.<listcomp>)r#�to_listrQrJr)�	is_select�	is_delete�	is_insert�	is_updater+rMr^)r�rqrJr^r|r�r}r��s
�	�
zFromStatement.__init__cKs:|j||fi|��}|j}|r||_|j|jfi|��S)z�provide a fixed _compiler_dispatch method.

        This is roughly similar to using the sqlalchemy.ext.compiler
        ``@compiles`` extension.

        )�_compile_state_factoryr�rUr[r)r�r�r�rUr�r|r|r}�_compiler_dispatch�s
z FromStatement._compiler_dispatchcCsttt�|��j}||�S)a@Return a :term:`plugin-enabled` 'column descriptions' structure
        referring to the columns which are SELECTed by this statement.

        See the section :ref:`queryguide_inspection` for an overview
        of this feature.

        .. seealso::

            :ref:`queryguide_inspection` - ORM background

        )r�ORMSelectCompileStater;�get_plugin_classr�)r��methr|r|r}�column_descriptions�s
��z!FromStatement.column_descriptionscCs|Sr�r|r�r|r|r}�_ensure_disambiguated_names�rfz)FromStatement._ensure_disambiguated_namesc+s<�tj�dd�|jD��EdHt�jdi|��EdHdS)Ncs��|]}|jVqdSr�)�
_from_objects�r�rJr|r|r}r����
�z-FromStatement.get_children.<locals>.<genexpr>r|)�	itertools�chain�
from_iterablerQr��get_children)r�r�r�r|r}r��s
��zFromStatement.get_childrencC�|jjSr�)rJ�_all_selected_columnsr�r|r|r}r��r�z#FromStatement._all_selected_columnscC�t|j�r	|jjSdSr�)r)rJ�_return_defaultsr�r|r|r}r���zFromStatement._return_defaultscCr�r�)r)rJ�
_returningr�r|r|r}r��r�zFromStatement._returningcCr�r�)r*rJ�_inliner�r|r|r}r��r�zFromStatement._inline�T)rqrrrJrpr^r�) rmrnror��__visit_name__r=r�rr�r|�_for_update_argr�r=�dp_clauseelement_list�dp_clauseelementr/�_executable_traverse_internals�_traverse_internalsr�r��is_from_statementr�r}r)r�r�r�r�r�r�r�r�r|r|r�r}r>is@
	����



r>�compound_selectc@seZdZdS)�CompoundSelectCompileStateN)rmrnror|r|r|r}r��sr��selectc@sPeZdZdZeZdZdZdZdZ	dZ
dZdZdZ
dZdZedGdd��Zd
d�Zdd�Zedd��Zedd��Zedd��Zedd��Zedd��Zdd�Zdd�Zdd �Zd!d"�Zd#d$�ZdHd%d&�Zd'd(�Zd)d*�Z d+d,�Z!d-d.�Z"d/d0�Z#d1d2�Z$d3d4�Z%d5d6�Z&d7d8�Z'd9d:�Z(d;d<�Z)d=d>�Z*e+d?d@��Z,e+dAdB��Z-dCdD�Z.dEdF�Z/dS)Ir~r|NFrr�r�r�r�rr�c
s$|�|��|dur|j}nd}|}|j�|j�|_|jr)|���_t�	��j_n|�_|jj
�_|jj�_
g�_d�_i�_|j�_|sO�jddd�7_�j
rc�jjturc�js_t�_n	t�_n�jj�_|jrv�fdd�|jD��_��|j�j
��_tj�|j�jdd�|jj�_d�_ �j!|||dd	�|r�|j"s�|jr�|jD]}|j"D]}|j#r�|�$�d
d��j|D��q�q��jj"D]
}|j#r�|�%��q�|j&r�|j&D]\}}	|��q�g�_'g�_(t)��_*i�_+i�_,g�_-g�_.��/dd
�|j0D���_1��2�t3j4��j5|fi|���S)z@compiler hook, we arrive here from compiler.visit_select() only.NTF)r�r�c	s"i|]
}|tj�|jgdd��qS)FrB)rOrPrQ)r��memoized_entitiesr�r|r}�
<dictcomp>Bs���z>ORMSelectCompileState.create_for_statement.<locals>.<dictcomp>rBr|)r�r�cSrr|rrr|r|r}rss��z>ORMSelectCompileState.create_for_statement.<locals>.<listcomp>csr�r�)r�)r��infor|r|r}r��r�z=ORMSelectCompileState.create_for_statement.<locals>.<genexpr>)6rIr�r��
safe_mergerr��_cloner�r#�
immutabledictr��
for_statementr�r�r�r�r�r�rM�LABEL_STYLE_LEGACY_ORMr8r�r6�_memoized_select_entities�_memoized_entitiesr�r�rOrPrQr�r��eager_order_byr�r�r��'process_compile_state_replaced_entitiesr�rRr�r�rSr�rorVrbrT�_normalize_froms�	_from_obj�from_clauses�_setup_for_generater;r�r)
r�rr�r�r�r�r�r�r`r�r|r�r}r��s�
	
�

�
�
�

���
����
�����

�


�z*ORMSelectCompileState.create_for_statementcCs�td�td|j���|jD]P}t|t�r>|ddkr>tdt�|d����td|j|���td|j|j���qt|t�r_|ddkr_td	t�|d����td|j|���qdS)
Nz5
---------------------------------------------------
zcurrent path: r�loaderz
Loader:           rz    �path_with_polymorphicz
With Polymorphic: )�printr�rrr�r�coerce�__dict__)r�r�r|r|r}�_dump_option_struct�s
��z)ORMSelectCompileState._dump_option_structcs�|j}d|_d|_|jjr|��|jD]}|jr#|�|j|j	|�q|jr/|�|j|j
�|���|jrI|j|_�rIt
�fdd�|jD��|_�rW|jdvrW|�|j��n|j|_|jrkt
�fdd�|jD��|_�r||jdvr||�t�|j���n|jp�d|_|jr�|jdj}|�|j�|_|jr�|�|j��|_nd|_|j|_|jr�t
t�dd�|jD���|_n|j dur�t
t�dd�|j D���|_!n|j"s�d	|_|j#|_#|jj$r�t%|j
�d
kr�t&�'d��|j
D]}|�(|�q�|j)D]}|d}||g|d
d��R�q�|jj*�r|�+�|j,�s!|jj-�rJd
��t&�.d��|j�s+t/|j0�|_|jdu�r4d|_|j1�rF|j2�rF|j3�rF|�4�|_n|�5�|_|j6�rc|�7�}|du�re|j�8d|i�|_dSdSdS)Nr|c3s�|]}�|d�VqdS�TNr|�r��crit�rdr|r}r��s
��
�z<ORMSelectCompileState._setup_for_generate.<locals>.<genexpr>�NFc3s"�|]}�r�|d�n|VqdSr�r|r�r�r|r}r��s
��
�rcs�&�|]}|dur
t�|�ndVqdSr��r1�surface_selectables�r��sr|r|r}r���
��
�csr�r�r�r�r|r|r}r��r�r�rrhFz,no columns were included in _only_load_propsz4Query contains no columns with which to SELECT from.�
deepentity)9r�r�_join_entitiesr�r��_set_select_from_aliasr��_setup_joins�_joinr�r�rg�_where_criteriar��_order_by_clausesrerU�_having_criteria�_group_by_clausesr#�flatten_iterator�group_byr�r��_target_adapter�copy_and_process�_distinct_on�distinct_on�	_distinct�distinct�
_correlate�	correlate�_correlate_except�correlate_except�_auto_correlater�r�rrGrHrWrbr��_adjust_for_extra_criteriar�r��InvalidRequestError�listrTrlrm�_should_nest_selectable�_compound_eager_statement�_simple_statementr��_mapper_zero�	_annotate)r�rVr�r$ra�rec�strategy�ezeror|r�r}r��s�
��
�
�
��
�	�
����
��

���



����


��z)ORMSelectCompileState._setup_for_generatecCsF|�|�}g|_d|_i|_|�|j|�|_tj||j	|jdd�|S)aCreates a partial ORMSelectCompileState that includes
        the full collection of _MapperEntity and other _QueryEntity objects.

        Supports a few remaining use cases that are pre-compilation
        but still need to gather some of the column  / adaption information.

        NTrB)
rIr�r�r�r�rMr�rOrPrQ)r�rVr�r�r|r|r}r';s
	��z1ORMSelectCompileState._create_entities_collectioncCs|j}t|d�Sr�)r��_determine_last_joined_entity)r�r�setup_joinsr|r|r}�determine_last_joined_entityUs
z2ORMSelectCompileState.determine_last_joined_entityccsl�|jD]/}|jr+d|jvr+|jd}|js"|js"t|g�EdHqt|j�EdHqt|g�EdHqdS)N�entity_namespace)rQ�
is_selectable�_annotations�	is_mapperrr,�_all_column_expressions)r�rrJ�ensr|r|r}�all_selected_columns[s�
�

�z*ORMSelectCompileState.all_selected_columnscCs|�tj�dd�|jD���S)Ncss0�|]}d|jvr|jn|jd��gVqdS)�parententityN)r�r��__clause_element__r�r|r|r}r�ms�

����
�zAORMSelectCompileState.get_columns_clause_froms.<locals>.<genexpr>)r�r�r�r�rQr�r|r|r}�get_columns_clause_fromsjs

��z.ORMSelectCompileState.get_columns_clause_fromscCs>tjtj||d�}t|j|�}|jj|j|j	|j
|jd�|S)N)rs)r�rRr�r)r$rur&�ReturnsRowsRoler>rQr��updater�rRr�r)r�r�from_statement�stmtr|r|r}r�{s��z$ORMSelectCompileState.from_statementcCsX|j}|jjs	J�t|j�dksJ�|�|jd�}|r*|jddi7_||_dSdS)� used only for legacy Query casesrrr�FN)r�r�r�rr��_get_select_from_alias_from_objr_)r�rVr$r|r|r}r��s
�z,ORMSelectCompileState._set_select_from_aliascCst|}d|jvr|jd}t|d�r|jst�d��|jSt|jtjj	�r8|�
�}||jus/J�ttj
|j|d�SdS)r�r�rzPA selectable (FromClause) instance is expected when the base alias is being set.�rN)r��hasattrrrG�
ArgumentError�_adapterrr�r"�AliasedReturnsRows�_all_equivsrr�LEGACY_SELECT_FROM_ALIAS)r��from_objr��equivsr|r|r}r��s$


��z5ORMSelectCompileState._get_select_from_alias_from_objcCs|jdjS)z8return the Mapper associated with the first QueryEntity.r)r�rr�r|r|r}r���z"ORMSelectCompileState._mapper_zerocCsD|jD]}d|jvr|jdSq|jD]
}|jr|jSqdS)z�Return the 'entity' (mapper or AliasedClass) associated
        with the first QueryEntity, or alternatively the 'select from'
        entity if specified.r�N)r�r�r��entity_zero)r�r�qentr|r|r}�_entity_zero�s

�

�z"ORMSelectCompileState._entity_zerocCs$|j|jgkrt�d|��|jjS)Nz4%s() can only be used against a single mapped class.)r�r�rGr�r�)r��methnamer|r|r}�_only_full_mapper_zero�s��z,ORMSelectCompileState._only_full_mapper_zerocCs$t|j�dkrt�|pd��|��S)Nrz8This operation requires a Query against a single mapper.)rr�rGr�r�)r��	rationaler|r|r}�_only_entity_zero�s�z'ORMSelectCompileState._only_entity_zerocCs^i}|j��D]}dd�|D�D]	}|�|jj�qqdd�|jD�D]	}|�|jj�q#|S)NcSrr|rrr|r|r}r�s��z5ORMSelectCompileState._all_equivs.<locals>.<listcomp>cSrr|rrr|r|r}r�r)r��valuesr�rrr�)r�r�r�rr|r|r}r��s
��
�z!ORMSelectCompileState._all_equivscsJ�jrdd��jD�}t��j|�}ng}d}�j�j�fdd�|D��j�j�j�j�jf�j	�j
j�j
j�j
�jd��j��}|��}���}ttj||d��_tj|g�j�}�j|_�j	duro�j	jduro�j	|_	|}�j��D]
}t�|||j�}qv|j� ||�|r�|jj |g�j�!|��R�|jj |g�j"�R�|S)NcSs$g|]}t|tjj�r|jn|�qSr|)rr"�elements�_label_referencerJ)r��elemr|r|r}r�s���zCORMSelectCompileState._compound_eager_statement.<locals>.<listcomp>csg|]	}|�jvr|�qSr|)r�)r�r8r�r|r}r
s��
for_update�hints�statement_hintsr�r�r�)#rUr1� expand_column_list_from_order_byr��_select_statementr�r�r�r�r�r��_hints�_statement_hintsr�r��_select_args�aliasr�rr�COMPOUND_EAGER_STATEMENTrnr r�r�rM�ofror�splice_joins�stop_on�select_from�non_generativer�r�)r��unwrapped_order_by�order_by_col_expr�innerr�r�from_clause�
eager_joinr|r�r}r��sn�	����
��
�
����z/ORMSelectCompileState._compound_eager_statementcCs||j|j|jt|j�t|j���|j|j|j	|j
f|j|jj
|jj|j|jd�|j��}|jr<|j
j|g|j�R�|S)Nr)rr�r�r�r�rorr�r�r�rUr�r�r
rr�r�rr�r)r�rr|r|r}r�Es&
���z'ORMSelectCompileState._simple_statementcCstj|||d�}|r
||_|r||_|r|jt|�7_|r*|jj|g|�R�n|r2|j�|�|r=|jt|�7_||_	|
|_
||_||_||_
||_|rT||_|rY||_||_|ra||_|	rf|	|_|
rr|jj|g|
�R�|dur�|jj|g|�R�|S)N)rQr�rM)r9�_create_raw_selectr�r�r�r�r�rr��
_limit_clause�_offset_clause�
_fetch_clause�_fetch_clause_options�_independent_ctes�_independent_ctes_opts�	_prefixes�	_suffixesr�r
rr�r�)r��raw_columnsr��where_criteria�having_criteriar�rUrr	r
r�r��limit_clause�
offset_clause�fetch_clause�fetch_clause_optionsr�r��prefixes�suffixesr��independent_ctes�independent_ctes_optsrr|r|r}rYsR���z'ORMSelectCompileState._select_statementcCsxd|jvr|jd}|j�|d�}|r|�|�St|tj�r!|}nt|d�r*|j}ndS|j�|d�}|r:|�|�SdS)Nr�rK)	r�r�r��adapt_clauserr%�
FromClauser�rK)r�rJ�searchrr|r|r}�_adapt_polymorphic_element�s




�z0ORMSelectCompileState._adapt_polymorphic_elementcs�r�fdd�|D�S|S)Ncsg|]}�|d��qSr�r|)r��or�r|r}r�sz9ORMSelectCompileState._adapt_col_list.<locals>.<listcomp>r|rbr|r�r}re�sz%ORMSelectCompileState._adapt_col_listcsJg�|jr��d|jjf�|jr��d|jf��sdS�fdd�}|S)NTFcs�fdd�}t�|i|�S)NcsFd|jvp	d|jv}�D]\}}|s|r ||�}|dur |SqdS)N�
_orm_adaptr�)r�)r�is_orm_adapt�always_adaptr$�e��adaptersr|r}�replace�s
���zRORMSelectCompileState._get_current_adapter.<locals>._adapt_clause.<locals>.replace)r'�replacement_traverse)rr3r;r9r|r}�
_adapt_clause�szAORMSelectCompileState._get_current_adapter.<locals>._adapt_clause)r_r�r;r�r3)r�r=r|r9r}rg�s
��
z*ORMSelectCompileState._get_current_adapterc
Cs�|D]�\}}}}|d}|d}t|�}|durt|�}t|tj�r0|dur+t�d��|}d}n
d|jvr:|jd}|durK|jsKt|d�sKt�	d��d}	t|tj�r�t
|dd�}	|dur|	rb|	}n|j}z|j}Wnt
y~}
zt�	d|�|
�d}
~
ww|j}|j}t|tj�s�|}|||jf|jvr�q|dur�||ur�|j�dd�|ur�t�d	|j�d|�|f��n
|dur�d}|}nd}}|�|||||||�qdS)
N�isouter�fullzUNo 'on clause' argument may be passed when joining to a relationship path as a targetr�rz9Expected mapped entity or selectable/table as join target�_of_typez0Join target %s does not refer to a mapped entityzMexplicit from clause %s does not match left side of relationship attribute %s)r!rr�PropComparatorrGr�r�r�r�r�r�r)ra�AttributeError�
_parententityr�QueryableAttributer��_already_joined_edgesr��_join_left_to_right)
r��args�entities_collection�right�onclause�from_�flagsr>r?�of_type�err�leftr:r|r|r}r��s��

�
������������zORMSelectCompileState._joinc
	Cs|dur|dus
J�|�||||�\}}}	n|�||�\}}	||ur+t�d||f��|�||||�\}
}}|
js?|�|
�}nd}|durh|j|}|jd|�t||||||d�g|j|dd�|_dS|	dur{t	||	t
�suJ�||	j}n|}|jt||
||||d�g|_dS)z�given raw "left", "right", "onclause" parameters consumed from
        a particular key within _join(), add a real ORMJoin object to
        our _from_obj list (or augment an existing one)

        Nz>Can't construct a join from %s to %s, they are the same entityr|)r>r?�_extra_criteriar)�"_join_determine_implicit_left_side�_join_place_explicit_left_siderGr�� _join_check_and_adapt_right_sider��_get_extra_criteriar�rrrr�)
r�rHrOrIrJr:�	outerjoinr?�replace_from_obj_index�use_entity_index�r_info�extra_criteria�left_clauser|r|r}rFMsr��
���
�
��������z)ORMSelectCompileState._join_left_to_rightcCsJt|�}d}}|jr7t�|j|j|�}t|�dkr$|d}|j|}n|t|�dkr/t�d��t�d|f��|r�i}	t|�D])\}
}|j	}|durKq?t|�}
|
|urTq?t
|t�ra|
|f|	|j<q?d|f|	|
j<q?t|	�
��}t�||j|�}t|�dkr�|	||d\}}nt|�dkr�t�d��t�d|f��t�d��|||fS)z�When join conditions don't express the left side explicitly,
        determine if an existing FROM or entity in this query
        can serve as the left hand side.

        NrraCan't determine which FROM clause to join from, there are multiple FROMS which can join to this entity. Please use the .select_from() method to establish an explicit left side, as well as providing an explicit ON clause if not present already to help resolve the ambiguity.z�Don't know how to join to %r. Please use the .select_from() method to establish an explicit left side, as well as providing an explicit ON clause if not present already to help resolve the ambiguity.zgNo entities to join from; please use select_from() to establish the left entity/selectable of this join)r!r�r1�find_left_clause_to_join_fromr�rrGr��	enumerate�entity_zero_or_selectablerrr��keys)r�rHrOrIrJrXrVrW�indexes�	potential�entity_indexrra�ent_info�all_clausesr|r|r}rQ�sb
��	��
��	���
z8ORMSelectCompileState._join_determine_implicit_left_sidec	Cs�d}}t|�}|jr$t�|j|j�}t|�dkrt�d��|r$|d}|durI|rIt|d�rIt	|�D]\}}t
|t�rH|�|�rH|}||fSq3||fS)z�When join conditions express a left side explicitly, determine
        where in our existing list of FROM clauses we should join towards,
        or if we need to make a new join, and if so is it from one of our
        existing entities.

        NrzrCan't identify which entity in which to assign the left side of this join.   Please use a more specific ON clause.rr)
r!r�r1�#find_left_clause_that_matches_givenr�rrGr�r�r\rr�corresponds_to)	r�rHrOrVrW�l_infor_�idxrr|r|r}rRs0��
���z4ORMSelectCompileState._join_place_explicit_left_sidec	Cszt|�}t|�}d}t|dd�}|r8|jst|jtj�r8|jp"|jgD]}	t	�
|j|	�r7t	�
|	|j�r7d}nq#|rH|j|jurHt�d|j��t|dd�|jt|dd�}}
}|rl|rl|�
|j�slt�d||f��t|d�ry|j|f7_d}|jr�|r�|j}|
jr�|��}
|
dur�|
|d�}n2|r�|
�|j�s�t�d|
j|jjf��t|
tj�r�t�tj|
�}
d}t||
�}tjd	|d
dd�|o�|o�|}|s�|r�t|dd
�}d}tjd|dd�|r�|s�J�tt j!t|�|j"d�}|�#||�n|j�s|�s|j$�r|�#|tt j%||j|j"d��t|tj&��r'|��}
|
�r'|
|d�}|�r6|j'|||j(ff7_'t|�||fS)z�transform the "right" side of the join as well as the onclause
        according to polymorphic mapping translations, aliasing on the query
        or on the join, special cases where the right and left side have
        overlapping tables.

        FrNTz*Can't join table/selectable '%s' to itselfrzIJoin target %s does not correspond to the right side of join condition %sz(Selectable '%s' is not derived from '%s'z�An alias is being generated automatically against joined entity %s for raw clauseelement, which is deprecated and will be removed in a later release. Use the aliased() construct explicitly, see the linked example.z1.4�xaj1)�code)�flatz�An alias is being generated automatically against joined entity %s due to overlapping tables.  This is a legacy pattern which may be deprecated in a later release.  Use the aliased(<entity>, flat=True) construct explicitly, see the linked example.�xaj2r�)r�r))r!r��with_polymorphicrrr%�Joinr�r�r1�selectables_overlaprGr��
common_parentrr�r��is_clause_element�_is_lateralrg�is_derived_from�descriptionrOr$rur&�FromClauseRolerr#�warn_deprecated�warnrr� DEPRECATED_JOIN_ADAPT_RIGHT_SIDErr�#_has_aliased_polymorphic_fromclause�#WITH_POLYMORPHIC_ADAPTER_RIGHT_JOIN�
ClauseElementrEr�)r�rOrIrJr:rfrX�overlap�right_mapperr��right_selectable�right_is_aliased�need_adapterrd�aliased_entityr$r|r|r}rS\s���������


���
���

�����
�
����
�
�����
z6ORMSelectCompileState._join_check_and_adapt_right_sidecCsF|jj|jj|j|j|jj|jj|jpd|jj|jj	|jj
|jjd�S)N)r(r)r�r�r,r-r�r*r+r.r/)r�rrr�r�r#r$r�rr r!r"r�r|r|r}r	s�z"ORMSelectCompileState._select_argscCsF|j}|�d�dup"|�d�dup"|�dd�p"|�dd�p"|�dd�S)Nr(r)r�Fr�r|r�)rr�)r��kwargsr|r|r}r�-	s�
�
�
�z-ORMSelectCompileState._should_nest_selectablecs8d�jf�jvrt��fdd��jd�jfD��SdS)N�additional_entity_criteriac3s4�|]}|js|j�ur|���r|���VqdSr�)�include_aliasesra�_should_include�_resolve_where_criteria)r��ae�rr�r|r}r�=	s��
���z<ORMSelectCompileState._get_extra_criteria.<locals>.<genexpr>r|)rr�r�)r�rr|r�r}rT8	s����	z)ORMSelectCompileState._get_extra_criteriac	Cs
|jD]+}|j�dd�}|r.|jjdusd|jf|jvr.||jvr.||jr(|jndf|j|<qt	|j�
��}|D]J\}}||jvrBq8|jj}|jj
rMg}n|�|�}|dur[||f7}|��}|D] }|rj|�|�}|ryt�|ddi�}||d�}|j|f7_qaq8dS)a�Apply extra criteria filtering.

        For all distinct single-table-inheritance mappers represented in
        the columns clause of this query, as well as the "select from entity",
        add criterion to the WHERE
        clause of the given QueryContext such that only the appropriate
        subtypes are selected from the total results.

        Additionally, add WHERE criteria originating from LoaderCriteriaOptions
        associated with the global context.

        r�Nr�r5TF)r�r�r�r�_single_table_criterionr�rVrr�rSrr�r�r�rTrg�traverser1�_deep_annotater�)	r��
fromclauserr2r$�single_critr�rdr�r|r|r}r�H	sB
��

��




��z0ORMSelectCompileState._adjust_for_extra_criteria)rr�r�r�r�rr�r~r�)0rmrnrorEr�r�r_r(rkrlrmrnr�r�r�r�r�r�r�r�r'r�r�r�r�r�r�r�r�rrr�r�r�rr3rergr�rFrQrRrSr)rr�rTr�r|r|r|r}r~�sl'






	[M0_cjB>


r~F�query_or_select_stmt�#Union[Query, Select, FromStatement]rU�Optional[ORMSelectCompileState]r�r�r��List[ORMColumnDescription]cCs8|durtj||d�}|}dd�dd�|jD�D�}|S)N)r�c
SsJg|]!\}}|j|jt|dd�|j|jdur|jst|dd�ndd��qS)rFNra)r��type�aliased�exprra)�_label_namer�r�r�r�rp)r�r�insp_entr|r|r}r�	s
�

����z(_column_descriptions.<locals>.<listcomp>cSsg|]}||jf�qSr|�r�)r��_entr|r|r}r�	s
�)r~r'r�)r�rUr�r	�dr|r|r}r��	s�
��r��query_or_augmented_select�Union[Query[Any], Select[Any]]�"Optional[_InternalEntityType[Any]]cCsJ|}|jr|j}|dur|S|jr!d|jdjvr!|jdjdSt|�S)Nr�r)r��_last_joined_entityr�r��_entity_from_pre_ent_zero)r�r�r�r|r|r}�_legacy_filter_by_entity_zero�	sr�cCsT|}|jsdS|jd}d|jvr|jdSt|t�r|jSd|jvr(|jdS|S)Nrr��bundle)rQr�rrra)r�r�rr|r|r}r��	s





r�r��Tuple[_SetupJoinsElement, ...]r��=Optional[Union[_InternalEntityType[Any], _JoinTargetElement]]cCs.|sdS|d\}}}}t|tj�r|jS|S)N���)rrrDra)r�r��targetrJrKrLr|r|r}r��	s�r�c@sreZdZUdZdZded<dZdZdZded<ded	<d
ed<ded
<ddd�Z	ddd�Z
dd�Zedd��Z
dS)rOz:represent an entity column returned within a Query result.r|r��supports_single_entityFz
Optional[str]r�z!Union[Type[Any], TypeEngine[Any]]r�z.Union[_InternalEntityType, ColumnElement[Any]]r�zOptional[_InternalEntityType]r�rUrlr��NonecCr�r�r��r�rUr|r|r}rW�	r�z _QueryEntity.setup_compile_stater$r*cCr�r�r��r�rUr$r|r|r}ri�	sz._QueryEntity.setup_dml_returning_compile_statecCr�r�r�)r�r
r�r|r|r}�
row_processor�	r�z_QueryEntity.row_processorcCs�t|�D]f\}}|jr|jr|�||j||�q|j}|jra|jr9d|jvr.t||||�qt	�
||j|||�q|j�dd�rHt
||||�q|jrVt	�
||j|||�qt	�
||g|||�q|jrjt
|||�q|S)Nr�r�F)r\�_is_lambda_element�_is_sequencerP�	_resolvedrpr�r�r�
_ColumnEntity�_for_columns�_select_iterabler��
_BundleEntity�_is_clause_list�	is_bundle)r�rUrqrHrCrgrar|r|r}rP�	sj�
������z_QueryEntity.to_compile_stateN)rUrlr�r��rUrlr$r*r�r�)rmrnror�r�r��_non_hashable_value�_null_column_type�use_id_for_hashrWrir�r�rPr|r|r|r}rO�	s 


rOc@s�eZdZUdZdZded<ded<ded<ded	<d
ed<ded
<dd�ZdZdZdZ	e
dd��Ze
dd��Zdd�Z
dd�Zdd�Zd&d!d"�Zd#d$�Zd%S)'rz mapper/class/AliasedClass entity)
r�rr�r�path�_extra_entitiesr�r"r��_polymorphic_discriminatorr>r�zMapper[Any]rr�r�rrr��strr�cCs�|�|�|r|jdur||_d|_d|_|jd}|j|}|_|j}||_|j	|_	}|jf|_
|jr;|j|_
n|jj|_
|j|_|j|_|j|_|j|_|j|_|jr`|�||j�dSdS)NTr�)r�r�r(rkr��
_post_inspectr�rar�rr�rr�r��class_rmr�r�r��with_polymorphic_mappersr"�polymorphic_onr�� _should_select_with_poly_adapterr!)r�rUrarHrCrrr|r|r}r�J
s4






��z_MapperEntity.__init__TcCr�r�)rr�r�r|r|r}r�t
r�z_MapperEntity.typecCs|jSr�r�r�r|r|r}r]x
sz'_MapperEntity.entity_zero_or_selectablecCst|j|�Sr�)rr��r�rar|r|r}re|
sz_MapperEntity.corresponds_tocCsVd}|js|jr|j�|jd�}n|jj}|r&|jr"|�|j�}|S|}|S|j}|Sr�)rr�r�rr�r�r_�wrap)r�rUr$�retr|r|r}�_get_entity_clauses
s ����z!_MapperEntity._get_entity_clausescCs�|j}|�|�}|jr|r|�|j�}n|s|j}|j|ur&|jj}|j}nd}}tj	||j
|||j||||jd�	}||j
|jfS)N)�only_load_propsra�polymorphic_discriminator)rUr�rnr�r�r�r�rar�_instance_processorrr�r�r�r�)r�r
r�rUr$r�ra�	_instancer|r|r}r��
s,


�z_MapperEntity.row_processorrUrlr$r*r�r�cCs.tj||j||j||j|j|jj|jd�	dS)N�rlr�r�)	r�_setup_entity_queryrr�r�r"r�r�r�r�r|r|r}ri�
s
�z/_MapperEntity.setup_dml_returning_compile_statecCs�|�|�}|jj}|dusd|jf|jvr%|j}||jr|jndf|j|<tj	||j||j
||j|j|j
j|jd�	|j�|j�dS)Nr�r�)r�rr�r�r�rr�rVrr�r�r�r"r�r�r�rTr�r�)r�rUr$�single_table_critrr|r|r}rW�
s,
�
��z!_MapperEntity.setup_compile_stateNr�)rmrnror�r�r�r�r�r�r�r)r�r]rer�r�rirWr|r|r|r}r3
s,

%


rc@s�eZdZUdZdZded<ded<ded<d	ed
<ded<ded
<		d&dd�Zedd��Zedd��Z	dd�Z
edd��Zdd�Zd'd"d#�Z
d$d%�ZdS)(r�r|)r�r�r�r�r�r�r�r�rr�z	Type[Any]r�r�r�r�r�r�TNc	Cs�d|_|jd}|r|j�|�n|�|�t|tjtjf�r$|�	�}n|}||_
|_t|�|_|j
|_g|_|rj|jD],}d|jvrNt|||||d�q=t|t�r]t|||||d�q=tj||g|d||d�q=|j
j|_dS)NTr���
parent_bundle)rkr�r�r�rrrDrrAr�r�r�r�r�r��exprsr�r�_ORMColumnEntityr��
single_entityr�)r�rUr�rHrC�setup_entitiesr�r�r|r|r}r��
sR	

�



�
��	z_BundleEntity.__init__cCs|j}|dur
|jSdSr�)r�r)r�r�r|r|r}r-sz_BundleEntity.mappercC�&|jD]
}|j}|dur|SqdSr�)r�r��r�rr�r|r|r}r�5�
�z_BundleEntity.entity_zerocC�dSr�r|r�r|r|r}re>sz_BundleEntity.corresponds_tocCr�r�)r�r]r�r|r|r}r]Cr�z'_BundleEntity.entity_zero_or_selectablecCs|jD]}|�|�qdSr�)r�rW)r�rUrr|r|r}rWLs
�z!_BundleEntity.setup_compile_staterUrlr$r*r�r�cC�
|�|�Sr��rWr�r|r|r}riP�
z/_BundleEntity.setup_dml_returning_compile_statecs@t��fdd�|jD��\}}}|j��j||�}||j|jfS)Ncsg|]}|�����qSr|)r�r�r
r�r|r}rYsz/_BundleEntity.row_processor.<locals>.<listcomp>)r�r�r��create_row_processorrVr�r�)r�r
r��procs�labels�extra�procr|r�r}r�Ws

�z_BundleEntity.row_processorr�r�)rmrnror�r�r�r�r)rr�rer]rWrir�r|r|r|r}r��
s,
	
�;



r�c@sLeZdZdZe	d
dd��Zedd��Zedd��Zed	d
��Z	dd�Z
dS)r�)�
_fetch_column�_row_processor�raw_column_index�translate_raw_columnNc

Cs�|D]=}|j}d|vr|d}	nt�|d�}	|	r5d|jvr)t||||	|||d�qt||||	|||d�qt||||||d�qdS)Nr�rgr�)r�r1�extract_first_column_annotation�_IdentityTokenEntityr��_RawColumnEntity)
r�rUr+rHr�rCr��columnr�_entityr|r|r}r�isH

�
�
�
��z_ColumnEntity._for_columnscCr�r�)r�r�r�r|r|r}r��r�z_ColumnEntity.typecCs|jjjSr�)r�r��hashabler�r|r|r}r��r�z!_ColumnEntity._non_hashable_valuecCs
|jjjSr�)r�r��_isnullr�r|r|r}r��s
z_ColumnEntity._null_column_typec	Cs�|j}|jdur |j\}}}|jr||jj|jf7}|||fS|jdur)|j}n|j}|jr5|jj	|}|j
r<|��}|jrE|jj	|}|�
|�}||j|jf}||_|jrh|j|jj|jf}||j|fS|Sr�)rUr�r�rVrQr�r�r�r_r+r��_deannotatern�_getterr�r�)	r�r
r�rU�getter�
label_name�extra_entitiesr�r�r|r|r}r��s4
�


�z_ColumnEntity.row_processorr�)rmrnror�r�r�r)r�r�r�r�r|r|r|r}r�as�1


r�c@sBeZdZdZdZdZdZ	ddd�Zdd�Zddd�Z	dd�Z
dS)r�NF)r�r�r�r]r�cCs�||_||_|du|_|jr|jddi7_|r|jr d|_n
|r'|j|_n|�|�|_|r6|j	�
|�n|�
|�||_|jjrH|jjdnd|_
|j|jf|_d|_|_dS)Nr�Tr)r�r�r�r�r��_is_text_clauser��
_proxy_keyr�r�r�r�r�r]r�r�r�)r�rUr�rHr�rCr�r|r|r}r��s$	



�z_RawColumnEntity.__init__cCr�r�r|r�r|r|r}rerfz_RawColumnEntity.corresponds_torUrlr$r*r�r�cCr�r�r�r�r|r|r}rir�z2_RawColumnEntity.setup_dml_returning_compile_statecCs\|��}|r||jd�}|durdSn|j}|jr|��}|j�|�|j�|�||_dSr�)	rgr�r�r�r�r�r�r�r�)r�rUrdr�r|r|r}rWs�
z$_RawColumnEntity.setup_compile_stater�r�)rmrnror�rr�r�r�rerirWr|r|r|r}r��s
�$
r�c@s>eZdZdZdZdZ	ddd�Zdd�Zddd�Zdd�Z	dS)r�zColumn/expression based entity.F)r�rr�r�r]r�r�NcCs|j}|}	|�dd�}
|�d|	�}|
rt|j|
�|_d|_n||_|du|_||_|r?|r6|
r1|
n|j|_n|j	||
d�|_nd|_|	j
|	|_|_}|	j
|_
}
|r\|j�|�n|�|�d|_||_d|_|_|j|jf|_|
jr�|�||j�dSdS)N�	proxy_key�proxy_ownerF)r�T)r�r�r�rar�r�r�r�r�r�r�r�r]rr�r�rkr�r�r�r�r�r!r�)r�rUr�rHr�r�rCr�rr��orm_keyr�r�rr|r|r}r�7s@


�
��z_ORMColumnEntity.__init__cCs*t|�r	||juSt|j�o|�|j�Sr�)rr�ror�r|r|r}rezs
�
�z_ORMColumnEntity.corresponds_torUrlr$r*r�r�cCs<|j|_||jd�}|dur|j�|�|j�|�dSdSr�)r�r�r�r�r�r�)r�rUr$r�r|r|r}ri�s�z2_ORMColumnEntity.setup_dml_returning_compile_statecCs�|��}|r||jd�}|dur|jsJ�|j|_dSn|j}|j}|jj}|dus2d|jf|jvr?||jr9|j	ndf|j
|<|jrI|jsI|�
�}t|jj��|jj�r[|j�|j�|j�|�|j�|�||_dS)NFr�)rgr�r�r�r�rr�r�rr�rVr��_expression_labelr�rSr��intersectionr�rTr�r�r�r�)r�rUrdr�r�r�r|r|r}rW�s6
��
��
z$_ORMColumnEntity.setup_compile_stater�r�)
rmrnror�r�r�r�rerirWr|r|r|r}r�(s
�C
	r�c@s eZdZdZdd�Zdd�ZdS)r�FcCr�r�r|r�r|r|r}rW�rfz(_IdentityTokenEntity.setup_compile_statecs�fdd�}||j|jfS)Ncs�jjSr�)rYru)�row�r
r|r}r��sz2_IdentityTokenEntity.row_processor.<locals>.getter)r�r�)r�r
r�r�r|r�r}r��sz"_IdentityTokenEntity.row_processorN)rmrnror�rWr�r|r|r|r}r��sr�r�)r�r�rUr�r�r�r�r�)r�r�r�r�r�)r�r�r�r�r�r�)�
__future__rr��typingrrrrrrr	r
rrr
r�rrr�baserrr�
path_registryrr#rrrrrrrrrGr r!r"r$r%r&r1r'�sql._typingr(r)r*r+�sql.baser,r-r.r/r0r1�sql.dmlr2�sql.elementsr3r4�sql.selectabler5r6r7r8r9r:r;r<�sql.visitorsr=�_typingr>r?r@rrArVrBr\rCrD�enginerE�engine.interfacesrFrG�sql.compilerrHrIrJrKrLrMrNrO�sql.type_apirPrQ�rootr�r�r�r�rSrr�r�rlr*�
plugin_forr=r>r�r~r�r�r�r�rOrr�r�r�r�r�r|r|r|r}�<module>ss�l-+-x
�&�

�_-yN