o
    gM                    @  sP  d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d d	lm
Z
 d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z# dd lm$Z$ dd!lm%Z% dd"lm&Z& dd#lmZ dd$l&m'Z' dd%l&m(Z( dd&l&m)Z) dd#l&mZ* dd'l&m+Z+ dd(l,m-Z- dd)l,m.Z. dd*l,m/Z/ dd+l,m0Z0 dd,l1m2Z2 dd-l1m3Z3 dd.l1m4Z4 dd/l1m5Z5 dd0l1m6Z6 dd1l1m7Z7 dd2l8m9Z9 dd3l:m;Z; dd4l:m<Z< dd5l=m>Z> dd6l=m?Z? dd7l=m@Z@ dd8l=mAZA dd9l=mBZB dd:l=mCZC dd;l=mDZD dd<l=mEZE dd=lFmGZG erdd>lHmIZI dd?lHmJZJ dd@lmKZK ddAlLmMZM ddBlNmOZO ddClPmQZQ ddDlPmRZR ddElSmTZT ddFlUmVZV ddGl,mWZW ddHlXmYZY ddIl8mZZZ ddJl:m[Z[ ddKl=m\Z\ ddLl=m]Z] ddMl=m^Z^ ddNl=m_Z_ ddOl=m`Z` ddPlambZb edQedRZcejdZeef ZgeCjhZhG dSdT dTZiefdUdViZjG dWdX dXe4ZkG dYdZ dZekZlG d[d\ d\ekZmG d]d^ d^Zne&jj4od_d`G dadb dbemZpG dcdd dde;e6eEe- Zqe&jj4od_deG dfdg dgele>Zre&jj4od_dhG didj djemeDZs		kdddtduZtddydzZudd{d|Zv	ddddZwG dd dZxG dd dexZyG dd dexZzG dd dexZ{G dd de{Z|G dd de{Z}G dd de}Z~dS )    )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                   @  sT   e Zd ZU dZded< ded< ded< G dd	 d	eZ	
	
ddddZd ddZd
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intrd   zDict[PathRegistry, PostLoad]rf   ORMCompileStaterU   c                   @  s<   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdS )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|   X/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/orm/context.pydefault_load_options   s    r~   Nr.   	statement&Union[Select[Any], FromStatement[Any]]rW   rX   rF   r\   rD   rY   QUnion[Type[QueryContext.default_load_options], QueryContext.default_load_options]r[   $Optional[OrmExecuteOptionsParameter]rZ   Optional[_BindArguments]c	                   s   | _ |pt _|pt _| _| _| _| _d _d _	| _
|j _|jj}	|j}
t fddt|	|
D  _t|j _|j _|j _|j _|j _|j _|j _|j  _!d S )NFc                 3  s&    | ]\}}|j r| |V  qd S 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[   rZ   rU   rV   rW   r\   ri   rj   rX   rz   rT   select_statement_with_optionstupleziprc   dictr   rt   r]   rq   r^   rs   r_   rr   r`   rw   ra   rv   rh   ru   rg   )r   rU   r   rW   rX   r\   rY   r[   rZ   cached_optionsuncached_optionsr|   r   r}   __init__   s0   


zQueryContext.__init__returnc                 C  s
   | j p| S r   )rT   r   r|   r|   r}   _get_top_level_context   s   
z#QueryContext._get_top_level_context)NN)rU   r.   r   r   rW   r   rX   rF   r\   rD   rY   r   r[   r   rZ   r   )r   rS   )	rm   rn   ro   	__slots____annotations__r1   r~   r   r   r|   r|   r|   r}   rS   f   s   
 ArS    _result_disable_adapt_to_contextTc                      sT   e Zd ZdZdd Zed fddZedd ZedddZedd Z	  Z
S )AbstractORMCompileStateFc          	      C  s   i | _ |d u ri  | _}|sJ d S |j | _}|r0d|d< |r.|jD ]
}|jr-||  q#d S |ddr8d S |jd }z|d }W n	 tyL   Y nw |jD ]}|j	r]|jr]||  qPd|d< d S )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]kwr   r   c                   s   t  j||fi |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clsr   r   r   	__class__r|   r}   r     s   z,AbstractORMCompileState.create_for_statementc                 C     t  r   NotImplementedError)r   r\   r   rX   r[   rZ   is_pre_eventr|   r|   r}   orm_pre_session_exec     
z,AbstractORMCompileState.orm_pre_session_execrE   c                 C  s(   |j ||pi |d}| ||||||S )N)r[   )executeorm_setup_cursor_result)r   r\   r   rX   r[   rZ   connresultr|   r|   r}   orm_execute_statement'  s   

z-AbstractORMCompileState.orm_execute_statementc                 C  r   r   r   r   r\   r   rX   r[   rZ   r   r|   r|   r}   r   =  r   z/AbstractORMCompileState.orm_setup_cursor_result)r   r   r   r   r   r   r   r   )r   rE   )rm   rn   ro   is_dml_returningr   classmethodr   r   r   r   __classcell__r|   r|   r   r}   r      s    '
r   c                   @  s(   e Zd ZdZedd Zedd ZdS )AutoflushOnlyORMCompileStatez>ORM compile state that is a passthrough, except for autoflush.c                 C  s4   t jddh||j\}}|s|jr|  ||fS )N_sa_orm_load_optionsr]   )rS   r~   from_execution_options_execution_optionsrt   )r   r\   r   rX   r[   rZ   r   rY   r|   r|   r}   r   M  s   

z1AutoflushOnlyORMCompileState.orm_pre_session_execc                 C  s   |S r   r|   r   r|   r|   r}   r   j  s   
z4AutoflushOnlyORMCompileState.orm_setup_cursor_resultN)rm   rn   ro   __doc__r   r   r   r|   r|   r|   r}   r   J  s    
r   c                   @  s  e Zd ZU G dd deZded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< ded< eZded< dZdd  Z	e
rZed?d'd(Zd)d* Zed@d.d/Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zd8d9 Zd:d; Zed<d= Zd>S )Arl   c                   @  s   e Zd Zdejfdejfdejfdejfdejfdejfdejfdejfd	ejfd
ejfdejfgZdZdZ	dZ
eZdZdZdZdZdZdZdZdS )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)rm   rn   ro   r=   
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_optionsx  s0    r   zDict[Any, Any]r   r   r   r   r   List[_QueryEntity]	_entitiesz%Dict[_InternalEntityType, ORMAdapter]_polymorphic_adaptersz=Union[Type[default_compile_options], default_compile_options]compile_optionszOptional[_QueryEntity]_primary_entitybooluse_legacy_query_stylerL   _label_conventionzList[ColumnElement[Any]]primary_columnssecondary_columnszSet[ColumnElement[Any]]dedupe_columnszList[Tuple[Any, ...]]rb   r   current_pathFc                 O  r   r   r   )r   argr   r|   r|   r}   r        zORMCompileState.__init__r   r   r   r   r   r   c                 K     d S r   r|   r   r|   r|   r}   r     s   z$ORMCompileState.create_for_statementc                 C  s*   | j }||vr|| || d S d S r   )r   addappend)r   objcol_collectiondeduper|   r|   r}   _append_dedupe_col_collection  s
   
z-ORMCompileState._append_dedupe_col_collectionlabel_styler:   legacyc                 C  s   |r	ddd}|S t |S )Nc                 S  s   |r|S t | 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_conventionc                 C  s   t |S r   )_column_descriptionsr   r   r|   r|   r}   get_column_descriptions     z'ORMCompileState.get_column_descriptionsc           
      C  s   t jdh d||j\}}d|v r|d }|jj|j|}|s$t}n|t}|j	r4|d|j	i}t
|jdd rSt|jjdkrS|ddd urS|d d	d
}||d< z|jd }	W n tyi   J dw |	rq|	j|d< |sz|jrz|  ||fS )Nr   >   r]   rh   rg   r^   sa_top_level_orm_contextr   rh   r   
   compiled_cacheTz#excess depth for ORM loader options)r   _cache_disable_reasonclauseplugin_subjectFz0statement had 'orm' plugin but no plugin_subjectmapper)rS   r~   r   r   rV   
merge_withr[   _orm_load_exec_optionsunionrv   r   _compile_optionslenr   r   _propagate_attrsr   r  rt   )
r   r\   r   rX   r[   rZ   r   rY   ctxr  r|   r|   r}   r     sT   



z$ORMCompileState.orm_pre_session_execc              	   C  sJ   |j }|jj}|dtj}	|jjr|S t||||||	||}
t	||
S )Nr   )
contextcompiledrU   r   rS   r~   r   r   r   	instances)r   r\   r   rX   r[   rZ   r   execution_contextrU   rY   querycontextr|   r|   r}   r   5  s$   

z'ORMCompileState.orm_setup_cursor_resultc                 C  s   dd | j D S )zreturn all _MapperEntity objects in the lead entities collection.

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

        c                 S     g | ]	}t |tr|qS r|   
isinstance_MapperEntityr   entr|   r|   r}   
<listcomp>b  
    
z9ORMCompileState._lead_mapper_entities.<locals>.<listcomp>)r   r   r|   r|   r}   _lead_mapper_entitiesZ  s   z%ORMCompileState._lead_mapper_entitiesc              
   C  sL   |j s |jj| jvr"|j D ]}| |ttj||j	|d qdS dS dS )a5  given 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_withr   r   WITH_POLYMORPHIC_ADAPTER_equivalent_columns)r   ext_infor   mpr|   r|   r}    _create_with_polymorphic_adapterf  s$   z0ORMCompileState._create_with_polymorphic_adapterc                 C  s:   |j p|gD ]}|| j|< | D ]}|| j|j< qqd S r   )_with_polymorphic_mappersr   r  local_table)r   r  adapterm2mr|   r|   r}   r    s   
z2ORMCompileState._mapper_loads_polymorphically_withc                 C  s   t d)Nz0this method only works for ORMSelectCompileStater   )r   rV   r   r|   r|   r}   _create_entities_collection  s   z+ORMCompileState._create_entities_collectionN)r   r   r   r   r   r   r   rl   )r   r:   r   r   r   rL   )rm   rn   ro   r-   r   r   r   r   _has_mapper_entitiesr   r   r   r   r   r   r   r   r   propertyr  r!  r  r'  r|   r|   r|   r}   rl   w  sJ   
 %

Y
$
rl   c                   @  s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
DMLReturningColFilterzan 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__c                 C  s:   |d ur|j |j ur|| _n|| _t| j | _| _d S r   )r#  r  r#   WeakPopulateDictadapt_check_presentr+  )r   target_mapperimmediate_dml_mapperr|   r|   r}   r     s   zDMLReturningColFilter.__init__c                 C  s.   t |D ]}| |}|d ur|  S qd S r   )sql_util_find_columnsr.  )r   r   	as_filterccc2r|   r|   r}   __call__  s   
zDMLReturningColFilter.__call__c                 C  s.   | j }|j|d }|d u rd S |jj|S r   )r  _columntopropertyr   r#  ccorresponding_column)r   r   r  propr|   r|   r}   r.    s
   z)DMLReturningColFilter.adapt_check_presentN)rm   rn   ro   r   r   r   r6  r.  r|   r|   r|   r}   r*    s    	r*  ormorm_from_statementc                   @  sp   e Zd ZU dZdZded< ded< dZded< dZdZdZ	dZ
eZeZedddZdd Zdd Zdd ZdS )ORMFromStatementCompileStateNFFromStatementstatement_containerz)Union[SelectBase, TextClause, UpdateBase]requested_statementzOptional[_DMLTableElement]	dml_tabler   r   r   r   r   r   c                 K  s  t |tsJ |d ur|jrtd| | }d |_|jj|_	| |_
|_|j |_}|jr6|j|_d|_g |_i |_|j|_|j	r[t |tjr[|js[|js[|jtu r[|t|_n||_||jsi|jsi|jnt|j	|_tj ||j!|jdd |jj"|_#|j$||ddd |j%r|j%D ]
}|j&r|'| q|j(r|j(D ]\}}|| qg |_)g |_*t+ |_,g |_-g |_.d |_/t |jtj0ri |_1|jD ]}	|	2| qd |_3|_4d|_5|j)D ]}
|j6|
d|j7d q|S t8t9j:|j|j;d|_<|S )NzThe 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)=r  r>  r   sa_excCompileError__new__r   r  r   r   r?  r   elementr@  r)   tablerA  r   r   r   r   r%   rO   _is_textual_label_styler7   set_label_styler8   r   r   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_mapr   r   ADAPT_FROM_STATEMENT_adapt_on_names_from_obj_alias)r   r?  r   r   r   r   r   fnr   entityr8  r|   r|   r}   r     s   


	






z1ORMFromStatementCompileState.create_for_statementc                 C  s   |S r   r|   r   colscurrent_adapterr|   r|   r}   _adapt_col_listO     z,ORMFromStatementCompileState._adapt_col_listc                 C  r   r   r|   r   r|   r|   r}   _get_current_adapterR  rf  z1ORMFromStatementCompileState._get_current_adapterc                 C  sV   | j jdd}t||}| jjrt| jdkrt	d| jD ]}|
| | q dS )zused by BulkORMInsert (and Update / Delete?) to set up a handler
        for RETURNING to return ORM objects and expressions

        r  Nr   sCan't generate ORM query that includes multiple expressions at the same time as '*'; query for '*' alone if present)r   r  r   r*  r   r   r  r   rG  rH  !setup_dml_returning_compile_state)r   
dml_mapperr/  r$  ra  r|   r|   r}   ri  U  s   

z>ORMFromStatementCompileState.setup_dml_returning_compile_state)r?  r   r   r   r   r   r   r=  )rm   rn   ro   r_  r(  r   rA  _has_orm_entitiesmulti_row_eager_loaderseager_adding_joinscompound_eager_adapterr   rV  eager_joinsr   r   re  rg  ri  r|   r|   r|   r}   r=    s$   
  r=  c                      s   e Zd ZU dZdZejZejZ	dZ
ded< ded< dejfdejfgej Zed	ejfg Zd
Z	
dd ddZdd Zedd Zdd Z fddZedd Zedd Zedd Zedd Z  ZS )!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]rJ  r   r^  rQ  r  Tentities%Iterable[_ColumnsClauseArgument[Any]]c                   sf    fddt |D  _| _|j _|j _|j _|j _|j _t	|r+|j
nd  _
| _d S )Nc                   s    g | ]}t jtj| d dqS )T)apply_propagate_attrspost_inspect)r$   expectr&   ColumnsClauseRoler  r   r|   r}   r    s    z*FromStatement.__init__.<locals>.<listcomp>)r#   to_listrQ  rJ  r)   	is_select	is_delete	is_insert	is_updater+   rM  r^  )r   rq  rJ  r^  r|   r   r}   r     s   
	
zFromStatement.__init__c                 K  s:   | j | |fi |}|j }|r||_|j|jfi |S )zprovide a fixed _compiler_dispatch method.

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

        )_compile_state_factoryr   rU   r[  r   )r   r   r   rU   r   r|   r|   r}   _compiler_dispatch  s
   z FromStatement._compiler_dispatchc                 C  s   t tt| 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_descriptionsc                 C  s   | S r   r|   r   r|   r|   r}   _ensure_disambiguated_names  rf  z)FromStatement._ensure_disambiguated_namesc                 +  s<    t jdd | jD E d H  t jdi |E d H  d S )Nc                 s      | ]}|j V  qd S r   )_from_objectsr   rJ  r|   r|   r}   r         
z-FromStatement.get_children.<locals>.<genexpr>r|   )	itertoolschainfrom_iterablerQ  r   get_children)r   r   r   r|   r}   r    s
   zFromStatement.get_childrenc                 C     | j jS r   )rJ  _all_selected_columnsr   r|   r|   r}   r    r   z#FromStatement._all_selected_columnsc                 C     t | jr	| jjS d S r   )r)   rJ  _return_defaultsr   r|   r|   r}   r       zFromStatement._return_defaultsc                 C  r  r   )r)   rJ  
_returningr   r|   r|   r}   r    r  zFromStatement._returningc                 C  r  r   )r*   rJ  _inliner   r|   r|   r}   r    r  zFromStatement._inlineT)rq  rr  rJ  rp  r^  r   ) rm   rn   ro   r   __visit_name__r=  r   r  r   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>  i  s@   
 	



r>  compound_selectc                   @  s   e Zd ZdS )CompoundSelectCompileStateN)rm   rn   ro   r|   r|   r|   r}   r    s    r  selectc                   @  sP  e Zd ZdZeZdZdZdZdZ	dZ
dZdZdZdZdZedGddZdd Zdd Zedd Zedd Zedd Zedd Zedd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdHd%d&Zd'd( Zd)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/dS )Ir~  r|   NFr   r   r   r   r   r   r   c           
        s$  |  |  |dur|j }nd}|}| j|j|_|jr)|  _t	  j_n| _|jj
 _|jj _g  _d _i  _|j _|sO  jddd7  _ jrc jjtu rc js_t _n	t _n jj _|jrv fdd|jD  _ |j j _tj |j jdd |jj _d _  j!|||dd	 |r|j"s|jr|jD ]}|j"D ]}|j#r|$ d
d  j| D  qq jj"D ]
}|j#r|%  q|j&r|j&D ]\}}	|  qg  _'g  _(t)  _*i  _+i  _,g  _-g  _. /dd |j0D  _1 2  t3j4  j5|fi |  S )z@compiler hook, we arrive here from compiler.visit_select() only.NTF)r   r   c              	     s"   i | ]}|t j |jg d dqS )FrB  )rO  rP  rQ  )r   memoized_entitiesr   r|   r}   
<dictcomp>B  s    z>ORMSelectCompileState.create_for_statement.<locals>.<dictcomp>rB  r|   )r   r   c                 S  r  r|   r  r  r|   r|   r}   r  s  s    z>ORMSelectCompileState.create_for_statement.<locals>.<listcomp>c                 s  r  r   )r   )r   infor|   r|   r}   r     r  z=ORMSelectCompileState.create_for_statement.<locals>.<genexpr>)6rI  r   r   
safe_merger  r   _cloner   r#   immutabledictr   for_statementr   r   r   r   r   r   rM  LABEL_STYLE_LEGACY_ORMr8   r   r6   _memoized_select_entities_memoized_entitiesr   r   rO  rP  rQ  r   r   eager_order_byr   r   r   'process_compile_state_replaced_entitiesr   rR  r   r   rS  r   ro  rV  rb   rT  _normalize_froms	_from_objfrom_clauses_setup_for_generater;   r   r   )
r   r   r   r   r   r   r  r   r`  r   r|   r   r}   r     s   
	












z*ORMSelectCompileState.create_for_statementc                 C  s   t d t d| j  | jD ]P}t|tr>|d dkr>t dt|d   t d| j|   t d| j| j  qt|tr_|d dkr_t d	t|d   t d| j|   qd S )
Nz5
---------------------------------------------------
zcurrent path: r   loaderz
Loader:           r   z    path_with_polymorphicz
With Polymorphic: )printr   r   r  r   r   coerce__dict__)r   r   r|   r|   r}   _dump_option_struct  s   
z)ORMSelectCompileState._dump_option_structc                   s  | j }d | _d| _| jjr|   |jD ]}|jr#| |j| j	|  q|jr/| |j| j
 |   |jrI|j| _ rIt fdd| jD | _ rW|jdvrW| |j n|j| _|jrkt fdd|jD | _ r||jdvr|| t|j n|jpd | _| jr| jd j}|| j| _|jr| |j | _nd| _|j| _|jrttdd |jD | _n|j d urttdd |j D | _!n|j"sd	| _|j#| _#| jj$rt%| j
d
krt&'d| j
D ]}|(|  q| j)D ]}|d }|| g|d
d  R   q| jj*r| +  | j,s!| jj-rJ dt&.d| js+t/| j0| _| jdu r4d | _| j1rF| j2rF| j3rF| 4 | _n| 5 | _| j6rc| 7 }|d ure| j8d|i| _d S d S d S )Nr|   c                 3  s    | ]} |d V  qdS TNr|   r   critrd  r|   r}   r     s
    
z<ORMSelectCompileState._setup_for_generate.<locals>.<genexpr>NFc                 3  s"    | ]} r |d n|V  qdS r  r|   r  r  r|   r}   r     s
    
r   c                 s  &    | ]}|d urt |nd V  qd S r   r1  surface_selectablesr   sr|   r|   r}   r     
    
c                 s  r  r   r  r  r|   r|   r}   r     r  r   r   rh  Fz,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_clausesre  rU  _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   r  rG  rH  rW  rb   r   _adjust_for_extra_criteriar   r   InvalidRequestErrorlistrT  rl  rm  _should_nest_selectable_compound_eager_statement_simple_statementr  _mapper_zero	_annotate)r   rV   r  r$  ra  recstrategyezeror|   r  r}   r    s   




	










z)ORMSelectCompileState._setup_for_generatec                 C  sF   |  | }g |_d|_i |_||j||_tj||j	|jdd |S )a  Creates 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  )
rI  r   r   r   r   rM  r   rO  rP  rQ  )r   rV   r   r   r|   r|   r}   r'  ;  s   
	z1ORMSelectCompileState._create_entities_collectionc                 C  s   |j }t|d S r   )r  _determine_last_joined_entity)r   r   setup_joinsr|   r|   r}   determine_last_joined_entityU  s   
z2ORMSelectCompileState.determine_last_joined_entityc                 c  sl    |j D ]/}|jr+d|jv r+|jd }|js"|js"t|gE d H  qt|jE d H  qt|gE d H  qd S )Nentity_namespace)rQ  is_selectable_annotations	is_mapperr  r,   _all_column_expressions)r   r   rJ  ensr|   r|   r}   all_selected_columns[  s   


z*ORMSelectCompileState.all_selected_columnsc                 C  s   |  tjdd |jD S )Nc                 s  s0    | ]}d |j vr|jn|j d   gV  qdS )parententityN)r  r  __clause_element__r  r|   r|   r}   r   m  s    


zAORMSelectCompileState.get_columns_clause_froms.<locals>.<genexpr>)r  r  r  r  rQ  r   r|   r|   r}   get_columns_clause_fromsj  s
   
z.ORMSelectCompileState.get_columns_clause_fromsc                 C  s>   t jtj||d}t|j|}|jj|j|j	|j
|jd |S )N)rs  )r   rR  r   r  )r$   ru  r&   ReturnsRowsRoler>  rQ  r  updater   rR  r   r  )r   r   from_statementstmtr|   r|   r}   r  {  s   z$ORMSelectCompileState.from_statementc                 C  sX   | j }| jjs	J t|jdksJ | |jd }|r*|  jddi7  _|| _dS dS ) used only for legacy Query casesr   r   r   FN)r   r   r   r  r  _get_select_from_alias_from_objr_  )r   rV   r$  r|   r|   r}   r    s   
z,ORMSelectCompileState._set_select_from_aliasc                 C  st   |}d|j v r|j d }t|dr|jstd|jS t|jtjj	r8| 
 }||ju s/J ttj|j|dS dS )r  r  r  zPA selectable (FromClause) instance is expected when the base alias is being set.r  N)r  hasattrr  rG  ArgumentError_adapterr  r   r"   AliasedReturnsRows_all_equivsr   r   LEGACY_SELECT_FROM_ALIAS)r   from_objr  equivsr|   r|   r}   r    s$   


z5ORMSelectCompileState._get_select_from_alias_from_objc                 C  s   | j d jS )z8return the Mapper associated with the first QueryEntity.r   )r   r  r   r|   r|   r}   r       z"ORMSelectCompileState._mapper_zeroc                 C  sD   | j D ]}d|jv r|jd   S q| jD ]
}|jr|j  S qdS )zReturn 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_zeroc                 C  s$   | j | jgkrtd| | jjS )Nz4%s() can only be used against a single mapped class.)r   r   rG  r  r  )r   methnamer|   r|   r}   _only_full_mapper_zero  s   z,ORMSelectCompileState._only_full_mapper_zeroc                 C  s$   t | jdkrt|pd|  S )Nr   z8This operation requires a Query against a single mapper.)r  r   rG  r  r  )r   	rationaler|   r|   r}   _only_entity_zero  s   z'ORMSelectCompileState._only_entity_zeroc                 C  s^   i }| j  D ]}dd |D D ]	}||jj qqdd | jD D ]	}||jj q#|S )Nc                 S  r  r|   r  r  r|   r|   r}   r    s    z5ORMSelectCompileState._all_equivs.<locals>.<listcomp>c                 S  r  r|   r  r  r|   r|   r}   r    r  )r  valuesr  r  r  r   )r   r  r  r  r|   r|   r}   r    s   

z!ORMSelectCompileState._all_equivsc                   sJ   j rdd  j D }t j|}ng }d } j j fdd|D   j j j j j f j	 j
j j
j j jd j}| }  }ttj||d _tj|g j  } j|_ j	d uro j	jd u ro j	|_	|} j D ]
}t|||j}qv|j || |r|j j |g j!|R   |j j |g j"R   |S )Nc                 S  s$   g | ]}t |tjjr|jn|qS r|   )r  r"   elements_label_referencerJ  )r   elemr|   r|   r}   r    s    zCORMSelectCompileState._compound_eager_statement.<locals>.<listcomp>c                   s   g | ]	}| j vr|qS r|   )r   )r   r8  r   r|   r}   r  
  s    
for_updatehintsstatement_hintsr  r  r  )#rU  r1   expand_column_list_from_order_byr   _select_statementr  r  r  r   r  r   _hints_statement_hintsr  r  _select_argsaliasr  r   r   COMPOUND_EAGER_STATEMENTrn  r    r  r   rM  ofro  r  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_statementc                 C  s|   | j | j| j t| jt| j  | j| j| j	| j
f| j| jj| jj| j| jd| j}| jr<|j
j|g| jR   |S )Nr  )r  r   r   r   r  ro  r  r  r  r   rU  r  r   r  r  r  r  r  r  r  )r   r   r|   r|   r}   r  E  s&   
z'ORMSelectCompileState._simple_statementc                 C  s  t j|||d}|r||_|r||_|r| jt|7  _|r*|jj|g|R   n|r2|j| |r=| jt|7  _||_	||_
||_||_||_||_|rT||_|rY||_||_|ra||_|	rf|	|_|
rr|jj|g|
R   |d ur|jj|g|R   |S )N)rQ  r  rM  )r9   _create_raw_selectr  r  r  r   r  r  r  _limit_clause_offset_clause_fetch_clause_fetch_clause_options_independent_ctes_independent_ctes_opts	_prefixes	_suffixesr  r  r  r  r  )r   raw_columnsr  where_criteriahaving_criteriar   rU  r  r	  r
  r  r  limit_clauseoffset_clausefetch_clausefetch_clause_optionsr  r  prefixessuffixesr  independent_ctesindependent_ctes_optsr   r|   r|   r}   r  Y  sR   z'ORMSelectCompileState._select_statementc                 C  sx   d|j v r|j d }| j|d }|r||S t|tjr!|}nt|dr*|j}nd S | j|d }|r:||S d S )Nr  rK  )	r  r   r   adapt_clauser  r%   
FromClauser  rK  )r   rJ  searchr  r|   r|   r}   _adapt_polymorphic_element  s   




z0ORMSelectCompileState._adapt_polymorphic_elementc                   s    r fdd|D S |S )Nc                   s   g | ]} |d qS r  r|   )r   or  r|   r}   r    s    z9ORMSelectCompileState._adapt_col_list.<locals>.<listcomp>r|   rb  r|   r  r}   re    s   z%ORMSelectCompileState._adapt_col_listc                   sJ   g  | j r d| j jf | jr d| jf  sd S  fdd}|S )NTFc                   s    fdd}t | i |S )Nc                   sF   d| j v p	d| j v } D ]\}}|s|r || }|d ur |  S qd S )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)r   r3  r;  r9  r|   r}   _adapt_clause  s   zAORMSelectCompileState._get_current_adapter.<locals>._adapt_clause)r_  r   r;  r   r3  )r   r=  r|   r9  r}   rg    s   

z*ORMSelectCompileState._get_current_adapterc                 C  s  |D ]\}}}}|d }|d }t |}|d urt |}t|tjr0|d ur+td|}d }n
d|jv r:|jd }|d u rK|jsKt|dsKt	dd }	t|tjrt
|dd }	|d u r|	rb|	}n|j}z|j}W n ty~ }
 zt	d| |
d }
~
ww |j}|j}t|tjs|}|||jf| jv rq|d ur||ur|jdd |urtd	|jd||f n|d urd }|}nd  }}| ||||||| qd S )
NisouterfullzUNo 'on clause' argument may be passed when joining to a relationship path as a targetr  r  z9Expected 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!   r  r   PropComparatorrG  r  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   


zORMSelectCompileState._joinc              	   C  s  |du r|du s
J |  ||||\}}}	n| ||\}}	||u r+td||f | ||||\}
}}|
js?| |
}nd}|durh| j| }| jd| t||||||dg | j|d d  | _dS |	dur{t	||	 t
suJ ||	 j}n|}| jt||
||||dg | _dS )zgiven 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_siderG  r   _join_check_and_adapt_right_sider  _get_extra_criteriar  r   r  r  r   )r   rH  rO  rI  rJ  r:  	outerjoinr?  replace_from_obj_indexuse_entity_indexr_infoextra_criterialeft_clauser|   r|   r}   rF  M  sr   


z)ORMSelectCompileState._join_left_to_rightc                 C  sJ  t |}d }}| jr7t| j|j|}t|dkr$|d }| j| }n|t|dkr/tdtd|f |ri }	t|D ])\}
}|j	}|du rKq?t |}||u rTq?t
|tra|
|f|	|j< q?d|f|	|j< q?t|	 }t||j|}t|dkr|	||d   \}}nt|dkrtdtd|f td|||fS )zWhen 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.

        Nr   r   a  Can'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.zDon'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   r  rG  r  	enumerateentity_zero_or_selectabler  r  r  keys)r   rH  rO  rI  rJ  rX  rV  rW  indexes	potentialentity_indexr  ra  ent_infoall_clausesr|   r|   r}   rQ    sb   
	
	
z8ORMSelectCompileState._join_determine_implicit_left_sidec           	      C  s   d }}t |}| jr$t| j|j}t|dkrtd|r$|d }|du rI|rIt|drIt	|D ]\}}t
|trH||rH|} ||fS q3||fS )zWhen 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.

        Nr   zrCan't identify which entity in which to assign the left side of this join.   Please use a more specific ON clause.r   r  )r!   r  r1  #find_left_clause_that_matches_givenr   r  rG  r  r  r\  r  r  corresponds_to)	r   rH  rO  rV  rW  l_infor_  idxr  r|   r|   r}   rR    s0   
 z4ORMSelectCompileState._join_place_explicit_left_sidec              	   C  sz  t |}t |}d}t|dd}|r8|jst|jtjr8| jp"|jgD ]}	t	
|j|	r7t	
|	|jr7d} nq#|rH|j|ju rHtd|j t|dd|jt|dd}}
}|rl|rl||jsltd||f t|dry|  j|f7  _d}|jr|r|j}|
jr|  }|dur||d}n2|r|
|jstd|
j|jjf t|
tjrttj|
}
d}t||
}tjd	| d
dd |o| o|}|s|rt|dd}d}tjd| dd |r|sJ tt j!t ||j"d}| #|| n|js|s|j$r| #|tt j%||j|j"d t|tj&r'|  }|r'||d}|r6|  j'|||j(ff7  _'t |||fS )ztransform 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.

        Fr  NTz*Can't join table/selectable '%s' to itselfr  zIJoin target %s does not correspond to the right side of join condition %sz(Selectable '%s' is not derived from '%s'zAn 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.z1.4xaj1)code)flatzAn 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_polymorphicr  r  r%   Joinr  r   r1  selectables_overlaprG  r  common_parentr  r  r  is_clause_element_is_lateralrg  is_derived_fromdescriptionrO   r$   ru  r&   FromClauseRoler   r#   warn_deprecatedwarnr   r    DEPRECATED_JOIN_ADAPT_RIGHT_SIDEr  r  #_has_aliased_polymorphic_fromclause#WITH_POLYMORPHIC_ADAPTER_RIGHT_JOINClauseElementrE  r   )r   rO  rI  rJ  r:  rf  rX  overlapright_mapperr  right_selectableright_is_aliasedneed_adapterrd  aliased_entityr$  r|   r|   r}   rS  \  s   










z6ORMSelectCompileState._join_check_and_adapt_right_sidec                 C  sF   | j j| j j| j| j| j j| j j| jpd | j j| j j	| j j
| j jdS )N)r(  r)  r  r  r,  r-  r  r*  r+  r.  r/  )r   r  r  r  r  r#  r$  r  r  r   r!  r"  r   r|   r|   r}   r  	  s   z"ORMSelectCompileState._select_argsc                 C  sF   | j }|dd up"|dd up"|ddp"|ddp"|ddS )Nr(  r)  r  Fr  r|   r  )r  r   )r   kwargsr|   r|   r}   r  -	  s   


z-ORMSelectCompileState._should_nest_selectablec                   s8   d j fjv rt fddjd j f D S dS )Nadditional_entity_criteriac                 3  s4    | ]}|j s|j u r|r| V  qd S r   )include_aliasesra  _should_include_resolve_where_criteria)r   aer  r   r|   r}   r   =	  s    
z<ORMSelectCompileState._get_extra_criteria.<locals>.<genexpr>r|   )r  r   r   )r   r  r|   r  r}   rT  8	  s   	z)ORMSelectCompileState._get_extra_criteriac           	      C  s
  | j D ]+}|jdd}|r.|jjdusd|jf| jv r.|| jvr.||jr(|jndf| j|< qt	| j
 }|D ]J\}}|| jv rBq8|jj}| jjrMg }n| |}|dur[||f7 }|  }|D ] }|rj||}|ryt|ddi}||d}|  j|f7  _qaq8dS )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  r5  TF)r  r  r   r  _single_table_criterionr   rV  r  r  rS  r  r  r   r   rT  rg  traverser1  _deep_annotater  )	r   
fromclauser  r2  r$  single_critr  rd  r  r|   r|   r}   r  H	  sB   







z0ORMSelectCompileState._adjust_for_extra_criteria)r   r   r   r   r   r   r   r~  r   )0rm   rn   ro   rE  r   r  r_  r(  rk  rl  rm  rn  r  r  r  r  r   r   r  r  r'  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r3  re  rg  r  rF  rQ  rR  rS  r)  r  r  rT  r  r|   r|   r|   r}   r~    sl     ' 





	[M0_cjB >


r~  Fquery_or_select_stmt#Union[Query, Select, FromStatement]rU   Optional[ORMSelectCompileState]r   r   r   List[ORMColumnDescription]c                 C  s8   |d u rt j| |d}|}dd dd |jD D }|S )N)r   c              
   S  sJ   g | ]!\}}|j |jt|d d|j|jdur|jst|ddnddqS )r  FNra  )r   typealiasedexprra  )_label_namer  r   r  r  rp  )r   r  insp_entr|   r|   r}   r  	  s    

z(_column_descriptions.<locals>.<listcomp>c                 S  s   g | ]}||j fqS r|   r  )r   _entr|   r|   r}   r  	  s    
)r~  r'  r   )r  rU   r   r	  dr|   r|   r}   r   	  s   r   query_or_augmented_selectUnion[Query[Any], Select[Any]]"Optional[_InternalEntityType[Any]]c                 C  sJ   | }|j r|j}|d ur|S |jr!d|jd jv r!|jd jd S t|S )Nr  r   )r  _last_joined_entityr  r  _entity_from_pre_ent_zero)r  r   r  r|   r|   r}   _legacy_filter_by_entity_zero	  s   r  c                 C  sT   | }|j sd S |j d }d|jv r|jd S t|tr|jS d|jv r(|jd S |S )Nr   r  bundle)rQ  r  r  r   ra  )r  r   r  r|   r|   r}   r  	  s   





r  r  Tuple[_SetupJoinsElement, ...]r  =Optional[Union[_InternalEntityType[Any], _JoinTargetElement]]c                 C  s.   | sd S | d \}}}}t |tjr|jS |S )N)r  r   rD  ra  )r  r  targetrJ  rK  rL  r|   r|   r}   r  	  s   r  c                   @  sr   e Zd ZU dZdZded< dZdZdZded< ded	< d
ed< ded< dddZ	dddZ
dd Zedd ZdS )rO  z: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  zOptional[_InternalEntityType]r  rU   rl   r   Nonec                 C  r   r   r   r   rU   r|   r|   r}   rW  	  r   z _QueryEntity.setup_compile_stater$  r*  c                 C  r   r   r   r   rU   r$  r|   r|   r}   ri  	  s   z._QueryEntity.setup_dml_returning_compile_statec                 C  r   r   r   )r   r
  r   r|   r|   r}   row_processor	  r   z_QueryEntity.row_processorc                 C  s   t |D ]f\}}|jr|jr| ||j|| q|j}|jra|jr9d|jv r.t|||| qt	
||j||| q|jddrHt|||| q|jrVt	
||j||| qt	
||g||| q|jrjt||| q|S )Nr  r  F)r\  _is_lambda_element_is_sequencerP  	_resolvedrp  r  r  r  _ColumnEntity_for_columns_select_iterabler   _BundleEntity_is_clause_list	is_bundle)r   rU   rq  rH  rC  rg  ra  r|   r|   r}   rP  	  sj   
z_QueryEntity.to_compile_stateN)rU   rl   r   r  rU   rl   r$  r*  r   r  )rm   rn   ro   r   r   r   _non_hashable_value_null_column_typeuse_id_for_hashrW  ri  r  r   rP  r|   r|   r|   r}   rO  	  s    
 

rO  c                   @  s   e Zd ZU dZdZded< ded< ded< ded	< d
ed< ded< dd ZdZdZdZ	e
dd Ze
dd Zdd Zdd Zdd Zd&d!d"Zd#d$ Zd%S )'r  z mapper/class/AliasedClass entity)
r  r  r  r  path_extra_entitiesr  r"  r   _polymorphic_discriminatorr>   r  zMapper[Any]r  r  r   r  r   r  strr  c                 C  s   | |  |r|jd u r| |_d|_d|_|jd }|j | }| _|j}|| _|j	 | _	}| jf| _
|jr;|j| _n|jj| _|j| _|j| _|j| _|j| _|j| _|jr`||| j d S d S )NTr  )r   r   r(  rk  r  _post_inspectr  ra  r  r  r  r  r   r  class_rm   r   r  r   with_polymorphic_mappersr"  polymorphic_onr   _should_select_with_poly_adapterr!  )r   rU   ra  rH  rC  r  r  r|   r|   r}   r   J
  s4   






z_MapperEntity.__init__Tc                 C  r  r   )r  r  r   r|   r|   r}   r  t
  r   z_MapperEntity.typec                 C  s   | j S r   r  r   r|   r|   r}   r]  x
  s   z'_MapperEntity.entity_zero_or_selectablec                 C  s   t | j|S r   )r   r  r   ra  r|   r|   r}   re  |
  s   z_MapperEntity.corresponds_toc                 C  sV   d }| j s|jr|j| jd }n| jj}|r&|jr"||j}|S |}|S |j}|S r   )r  r   r   r  r  r  r_  wrap)r   rU   r$  retr|   r|   r}   _get_entity_clauses
  s    z!_MapperEntity._get_entity_clausesc                 C  s   |j }| |}|jr|r||j}n|s|j}|j| u r&|jj}|j}nd  }}tj	| | j
||| j|||| jd	}|| j| jfS )N)only_load_propsra   polymorphic_discriminator)rU   r  rn  r  r   r   r   ra   r   _instance_processorr  r  r  r  r  )r   r
  r   rU   r$  r  ra   	_instancer|   r|   r}   r  
  s,   


z_MapperEntity.row_processorrU   rl   r$  r*  r   r  c                 C  s.   t j|| j| | j||j| j|jj| jd	 d S )Nrl  r  r  )	r   _setup_entity_queryr  r  r   r"  r   r   r  r  r|   r|   r}   ri  
  s   
z/_MapperEntity.setup_dml_returning_compile_statec                 C  s   |  |}| jj}|d usd| jf|jv r%| j}||jr|jnd f|j|< tj	|| j| | j
||j| j|jj| jd	 |j| j d S )Nr  r  )r  r  r  r   r  r  r  rV  r   r  r  r   r"  r   r   r  rT  r   r   )r   rU   r$  single_table_critr  r|   r|   r}   rW  
  s,   

z!_MapperEntity.setup_compile_stateNr  )rm   rn   ro   r   r   r   r   r  r  r  r)  r  r]  re  r  r  ri  rW  r|   r|   r|   r}   r  3
  s,   
 %


r  c                   @  s   e Zd ZU dZdZded< ded< ded< d	ed
< ded< ded< 		d&ddZedd Zedd Z	dd Z
edd Zdd Zd'd"d#Zd$d% ZdS )(r  r|   )r  r  r  r  r   r  r   r   r   r  z	Type[Any]r  r  r  r   r  r  TNc              	   C  s   d|_ |jd }|r|j|  n||  t|tjtjfr$|	 }n|}| | _
| _t|| _|j| _g | _|rj|jD ],}d|jv rNt||||| d q=t|tr]t||||| d q=tj||g|d || d q=| j
j| _d S )NTr  parent_bundle)rk  r  r   r   r  r   rD  r   rA  r  r  r  r  r   r  exprsr  r   _ORMColumnEntityr  single_entityr  )r   rU   r  rH  rC  setup_entitiesr  r  r|   r|   r}   r   
  sR   	






	z_BundleEntity.__init__c                 C  s   | j }|d ur
|jS d S r   )r  r  )r   r  r|   r|   r}   r  -  s   z_BundleEntity.mapperc                 C  &   | j D ]}|j}|d ur|  S qd S r   )r   r  r   r  r  r|   r|   r}   r  5     
z_BundleEntity.entity_zeroc                 C     dS r  r|   r  r|   r|   r}   re  >  s   z_BundleEntity.corresponds_toc                 C  r  r   )r   r]  r  r|   r|   r}   r]  C  r  z'_BundleEntity.entity_zero_or_selectablec                 C  s   | j D ]}|| qd S r   )r   rW  )r   rU   r  r|   r|   r}   rW  L  s   
z!_BundleEntity.setup_compile_staterU   rl   r$  r*  r   r  c                 C  
   |  |S r   rW  r  r|   r|   r}   ri  P     
z/_BundleEntity.setup_dml_returning_compile_statec                   s@   t  fdd| jD  \}}}| j j||}|| j| jfS )Nc                   s   g | ]}|  qS r|   )r  r  r
  r   r|   r}   r  Y  s    z/_BundleEntity.row_processor.<locals>.<listcomp>)r   r   r  create_row_processorrV   r  r  )r   r
  r   procslabelsextraprocr|   r  r}   r  W  s
   
z_BundleEntity.row_processorr  r  )rm   rn   ro   r  r   r   r   r)  r  r  re  r]  rW  ri  r  r|   r|   r|   r}   r  
  s,   
 	
;



r  c                   @  sL   e Zd ZdZe	dddZedd Zedd Zed	d
 Z	dd Z
dS )r  )_fetch_column_row_processorraw_column_indextranslate_raw_columnNc           
   
   C  s   |D ]=}|j }d|v r|d }	nt|d}	|	r5d|j v r)t||||	|||d qt||||	|||d qt||||||d qd S )Nr  rg   r  )r  r1  extract_first_column_annotation_IdentityTokenEntityr  _RawColumnEntity)
r   rU   r+  rH  r  rC  r  columnr   _entityr|   r|   r}   r  i  sH   




z_ColumnEntity._for_columnsc                 C  r  r   )r  r  r   r|   r|   r}   r    r   z_ColumnEntity.typec                 C  s   | j jj S r   )r  r  hashabler   r|   r|   r}   r    r  z!_ColumnEntity._non_hashable_valuec                 C  s
   | j jjS r   )r  r  _isnullr   r|   r|   r}   r    s   
z_ColumnEntity._null_column_typec           	      C  s   |j }| jd ur | j\}}}| jr||jj| j f7 }|||fS | jd ur)| j}n| j}|jr5|jj	| }|j
r<| }|jrE|jj	| }||}|| j| jf}|| _| jrh| j|jj| j f }|| j|fS |S r   )rU   r  r  rV   rQ  r  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   )rm   rn   ro   r   r   r  r)  r  r  r  r  r|   r|   r|   r}   r  a  s    1


r  c                   @  sB   e Zd ZdZdZdZdZ	dddZdd ZdddZ	dd Z
dS )r  NF)r  r  r  r]  r  c                 C  s   || _ || _|d u| _|jr| jddi7  _|r|jr d | _n|r'|j| _n||| _|r6|j	
|  n|
|  || _| jjrH| jjd nd | _| j | jf| _d  | _| _d S )Nr   Tr   )r  r  r  r   r   _is_text_clauser  
_proxy_keyr   r   r   r  r  r]  r  r  r  )r   rU   r  rH  r  rC  r  r|   r|   r}   r     s$   	



z_RawColumnEntity.__init__c                 C  r  r  r|   r  r|   r|   r}   re    rf  z_RawColumnEntity.corresponds_torU   rl   r$  r*  r   r  c                 C  r  r   r  r  r|   r|   r}   ri    r  z2_RawColumnEntity.setup_dml_returning_compile_statec                 C  s\   |  }|r|| jd}|d u rd S n| j}|jr| }|j| |j| || _d S r  )	rg  r  r  r  r   r   r   r   r  )r   rU   rd  r  r|   r|   r}   rW    s   
z$_RawColumnEntity.setup_compile_stater   r  )rm   rn   ro   r  r  r  r   r   re  ri  rW  r|   r|   r|   r}   r    s    
$
r  c                   @  s>   e Zd ZdZdZdZ	dddZdd ZdddZdd Z	dS )r  zColumn/expression based entity.F)r  r  r  r  r]  r  r  Nc                 C  s  |j }|}	|dd }
|d|	}|
rt|j|
| _d| _n|| _|d u| _|| _|r?|r6|
r1|
n|j| _n|j	||
d| _nd | _|	j
 |	 | _ | _}|	j | _}|r\|j|  n||  d|_|| _d  | _| _| j| jf| _|jr|||j d S d S )N	proxy_keyproxy_ownerF)r   T)r  r   r   ra  r  r  r  r  r  r   r  r  r]  r  r   r   rk  r  r  r  r  r  r!  r   )r   rU   r  rH  r  r  rC  r  r   r  orm_keyr  r  r  r|   r|   r}   r   7  s@   



z_ORMColumnEntity.__init__c                 C  s*   t |r	|| ju S t | j o|| jS r   )r   r  ro  r  r|   r|   r}   re  z  s   

z_ORMColumnEntity.corresponds_torU   rl   r$  r*  r   r  c                 C  s<   | j | _|| j d}|d ur|j| |j| d S d S r  )r  r  r   r   r   r   )r   rU   r$  r  r|   r|   r}   ri    s   z2_ORMColumnEntity.setup_dml_returning_compile_statec                 C  s   |  }|r|| jd}|d u r|jsJ | j| _d S n| j}| j}| jj}|d us2d| jf|jv r?||jr9|j	nd f|j
|< |jrI|jsI| }t| jj|jjr[|j|j |j| |j| || _d S )NFr  )rg  r  r   r  r  r  r  r   r  r  rV  r  _expression_labelr  rS  r  intersectionr   rT  r   r   r   r   )r   rU   rd  r  r  r  r|   r|   r}   rW    s6   


z$_ORMColumnEntity.setup_compile_stater   r  )
rm   rn   ro   r   r  r   r   re  ri  rW  r|   r|   r|   r}   r  (  s    
C
	r  c                   @  s    e Zd ZdZdd Zdd ZdS )r  Fc                 C  r   r   r|   r  r|   r|   r}   rW    rf  z(_IdentityTokenEntity.setup_compile_statec                   s    fdd}|| j | jfS )Nc                   s    j jS r   )rY   ru   )rowr
  r|   r}   r    s   z2_IdentityTokenEntity.row_processor.<locals>.getter)r  r  )r   r
  r   r  r|   r  r}   r    s   z"_IdentityTokenEntity.row_processorN)rm   rn   ro   r  rW  r  r|   r|   r|   r}   r    s    r  r  )r  r  rU   r  r   r   r   r  )r  r  r   r  r   )r  r  r  r  r   r  )
__future__r   r  typingr   r   r   r   r   r   r	   r
   r   r   r   r    r   r   r   baser   r   r   path_registryr   r#   r   r   r   r   r   r   r   r   rG  r    r!   r"   r$   r%   r&   r1  r'   sql._typingr(   r)   r*   r+   sql.baser,   r-   r.   r/   r0   r1   sql.dmlr2   sql.elementsr3   r4   sql.selectabler5   r6   r7   r8   r9   r:   r;   r<   sql.visitorsr=   _typingr>   r?   r@   r  rA   rV   rB   r\   rC   rD   enginerE   engine.interfacesrF   rG   sql.compilerrH   rI   rJ   rK   rL   rM   rN   rO   sql.type_apirP   rQ   rootr   r  r   r  rS   r  r   r   rl   r*  
plugin_forr=  r>  r  r~  r   r  r  r  rO  r  r  r  r  r  r  r|   r|   r|   r}   <module>   s  sl-  + -x
           &

_ - yN 