U kf=@shddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm 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:mZ>dd6l=m?Z?dd7l=m@Z@dd8l=mAZAdd9l=mBZBdd:l=mCZCdd;l=mDZDddlHmIZIdd?lHmJZJdd@lmKZKddAlLmMZMddBlNmOZOddClPmQZQddDlPmRZRddElSmTZTddFlUmVZVddGl,mWZWddHlXmYZYddIl8mZZZddJl:m[Z[ddKl=m\Z\ddLl=m]Z]ddMl=m^Z^ddNl=m_Z_ddOl=m`Z`ddPlambZbedQedRZcejdZeefZgeCjhZhGdSdTdTZiefdUdViZjGdWdXdXe4ZkGdYdZdZekZlGd[d\d\ekZmGd]d^d^Zne&jj4od_d`GdadbdbemZpGdcdddde;e6eEe-Zqe&jj4od_deGdfdgdgele>Zre&jj4od_dhGdidjdjemeDZsddldmdndodpdqdrZtdsdtdudvdwZudsdtdudxdyZvddzdtd{d|d}d~ZwGdddZxGdddexZyGdddexZzGdddexZ{Gddde{Z|Gddde{Z}Gddde}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 @sdeZdZUdZded<ded<ded<Gdd d eZdd d d dddddddZddddZd S) QueryContext)top_level_context compile_statequeryparams load_optionsbind_argumentsexecution_optionssession autoflushpopulate_existinginvoke_all_eagers version_check refresh_statecreate_eager_joinspropagated_loader_optionsrrunidZpartialspost_load_pathsidentity_token yield_perloaders_require_bufferingloaders_require_uniquingintrczDict[PathRegistry, PostLoad]rdORMCompileStaterUc@s<eZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ dZdS)z!QueryContext.default_load_optionsFTN)__name__ __module__ __qualname__Z_only_return_tuples_populate_existing_version_check_invoke_all_eagers _autoflush_identity_token _yield_per_refresh_stateZ_lazy_loaded_fromZ_legacy_uniquing_sa_top_level_orm_contextZ_is_user_refreshrvrvF/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/context.pydefault_load_optionssrxNr.&Union[Select[Any], FromStatement[Any]]rFrDzQUnion[Type[QueryContext.default_load_options], QueryContext.default_load_options]z$Optional[OrmExecuteOptionsParameter]zOptional[_BindArguments])rU statementrWr[rXrZrYc s|_|p t_|pt_|_|_|_d_d_|_ |j _ |j j }|j } tfddt|| D_t|j_|j_|j_|j_|j_|j_|j_|j_ dS)NFc3s$|]\}}|jr||VqdSN)Zpropagate_to_loadersZ'_adapt_cached_option_to_uncached_option).0optZ uncached_optselfrvrw sz(QueryContext.__init__..)!rX _EMPTY_DICTrZrYrUrVr[rgrhrWrurTselect_statement _with_optionstupleziprbdictrrqr\rnr]rpr^ror_rtr`rsrfrrre) rrUrzrWr[rXrZrYZcached_optionsZuncached_optionsrvr~rw__init__s.      zQueryContext.__init__returncCs |jp|Sr{)rTr~rvrvrw_get_top_level_contextsz#QueryContext._get_top_level_context)NN) rkrlrm __slots____annotations__r1rxrrrvrvrvrwrSfs 6rSZ _result_disable_adapt_to_contextTcsbeZdZdZddZedddddfdd Zed d Zed d ddZeddZ Z S)AbstractORMCompileStateFc Csi|_|dkr$i|_}|s tdS|j|_}|r`d|d<|r\|jD]}|jrF||qFdS|ddrpdS|jd}z |d}Wnt k rYn$X|jD]}|j r|jr||qd|d<dS)NTZ toplevel_ormFr selectable) rglobal_attributesAssertionErrorZ_global_attributesrZ_is_criteria_optionprocess_compile_stategetstackKeyError_is_compile_state) rrzcompilertoplevelprocess_criteria_for_toplevelgar}Zstack_0Z toplevel_stmtrvrvrw_init_global_attributess0          z/AbstractORMCompileState._init_global_attributesUnion[Select, FromStatement]Optional[SQLCompiler]rrzrkwrc stj||f|S)aCreate 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(). )supercreate_for_statementclsrzrr __class__rvrwrsz,AbstractORMCompileState.create_for_statementcCs tdSr{NotImplementedError)rr[rzrWrZrY is_pre_eventrvrvrworm_pre_session_execs z,AbstractORMCompileState.orm_pre_session_execrErcCs(|j||p i|d}|||||||S)N)rZ)executeorm_setup_cursor_result)rr[rzrWrZrYconnresultrvrvrworm_execute_statements z-AbstractORMCompileState.orm_execute_statementcCs tdSr{rrr[rzrWrZrYrrvrvrwr1s z/AbstractORMCompileState.orm_setup_cursor_result) rkrlrmis_dml_returningr classmethodrrrr __classcell__rvrvrrwrs' rc@s(eZdZdZeddZeddZdS)AutoflushOnlyORMCompileStatez>ORM compile state that is a passthrough, except for autoflush.cCs4tjddh||j\}}|s,|jr,|||fS)N_sa_orm_load_optionsr\)rSrxfrom_execution_options_execution_optionsrq)rr[rzrWrZrYrrXrvrvrwrAs z1AutoflushOnlyORMCompileState.orm_pre_session_execcCs|Sr{rvrrvrvrwr^s z4AutoflushOnlyORMCompileState.orm_setup_cursor_resultN)rkrlrm__doc__rrrrvrvrvrwr>s  rc@s,eZdZUGdddeZded<ded<ded<ded<d ed <d ed <d ed<ded<ded<ded<ded<ded<ded<ded<eZded<dZdd Z e re d!d"d#dd$d%d&Z d'd(Z e d)ddd*d+d,Ze d-d.Ze d/d0Ze d1d2Zed3d4Zd5d6Zd7d8Ze d9d:Zd;S).name)N)r;_column_naming_convention)rrrrrvrvrwrs z)ORMCompileState._column_naming_conventioncCst|Sr{)_column_descriptionsrrzrvrvrwget_column_descriptionssz'ORMCompileState.get_column_descriptionsc Cstjddddddh||j\}}d|krD|d}|jj|j|}|sNt}n |t}|j rn|d|j i}t |j ddrt |j j dkr|d d dk r|dd d }||d <z|jd} Wn tk rdstdYnX| r| j|d<|s|jr|||fS)Nrr]r\rfreZsa_top_level_orm_contextr compiled_cacheTz#excess depth for ORM loader options)rZ_cache_disable_reasonclauseplugin_subjectFz0statement had 'orm' plugin but no plugin_subjectmapper)rSrxrrrVZ merge_withrZ_orm_load_exec_optionsunionrsr_compile_optionslenrr_propagate_attrsrrrrq) rr[rzrWrZrYrrXctxrrvrvrwrsb     z$ORMCompileState.orm_pre_session_execc CsH|j}|jj}|dtj} |jjr(|St||||| ||} t || S)Nr) contextcompiledrUrrSrxrrrZ instances) rr[rzrWrZrYrZexecution_contextrUrXZ querycontextrvrvrwr)s$  z'ORMCompileState.orm_setup_cursor_resultcCsdd|jDS)zreturn all _MapperEntity objects in the lead entities collection. Does **not** include entities that have been replaced by with_entities(), with_only_columns() cSsg|]}t|tr|qSrv isinstance _MapperEntityr|entrvrvrw Us z9ORMCompileState._lead_mapper_entities..)rr~rvrvrw_lead_mapper_entitiesMsz%ORMCompileState._lead_mapper_entitiesc CsD|js@|jj|jkr@|jD] }||ttj||j |dqdS)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). ) equivalentsrN) is_aliased_classrpersist_selectableriterate_to_root"_mapper_loads_polymorphically_withrrZWITH_POLYMORPHIC_ADAPTER_equivalent_columns)rext_informprvrvrw _create_with_polymorphic_adapterYs z0ORMCompileState._create_with_polymorphic_adaptercCs:|jp |gD](}||j|<|D]}||j|j<q"q dSr{)_with_polymorphic_mappersrr local_table)rradapterm2mrvrvrwrvs  z2ORMCompileState._mapper_loads_polymorphically_withcCs tddS)Nz0this method only works for ORMSelectCompileStater)rrVrrvrvrw_create_entities_collection}sz+ORMCompileState._create_entities_collectionN)rkrlrmr-rrrr_has_mapper_entitiesrr rrrrrrrpropertyrrrrrvrvrvrwrjksH %   Y # rjc@s,eZdZdZdZddZddZddZd 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. )rcolumns __weakref__cCs:|dk r|j|jk r||_n||_t|j|_|_dSr{)rrr#ZWeakPopulateDictadapt_check_presentr)r target_mapperZimmediate_dml_mapperrvrvrwrszDMLReturningColFilter.__init__cCs.t|D]}||}|dk r |Sq dSr{)sql_utilZ _find_columnsr )rr as_filterccc2rvrvrw__call__s   zDMLReturningColFilter.__call__cCs.|j}|j|d}|dkr dS|jj|Sr{)rZ_columntopropertyrrcZcorresponding_column)rrrproprvrvrwr s z)DMLReturningColFilter.adapt_check_presentN)rkrlrmrrrrr rvrvrvrwrs  rZormorm_from_statementc@szeZdZUdZdZded<ded<dZded<dZdZdZ dZ e Z e Z ed d d dd d dZddZddZddZdS)ORMFromStatementCompileStateNF FromStatementstatement_containerz)Union[SelectBase, TextClause, UpdateBase]requested_statementzOptional[_DMLTableElement] dml_tablerrr)rrrrc Kst|tst|dk r&|jr&td||}d|_|jj |_ ||_ |_ |j |_}|jrl|j|_d|_g|_i|_|j|_|j rt|tjr|js|js|jtkr|t|_n||_||js|js|jnt|j |_t j!||j"|jdd|jj#|_$|j%||ddd|j&r8|j&D]}|j'r|(|q|j)rZ|j)D]\}}||qFg|_*g|_+t,|_-g|_.g|_/d|_0t|jtj1ri|_2|jD]} | 3|qd|_4|_5d|_6|j*D]} |j7| d|j8dqnt9t:j;|j|jrrrrsa_exc CompileError__new__rrrrrrelementrr)tablerrrrrr%rOZ _is_textual _label_styler7Zset_label_styler8rzrr _QueryEntityto_compile_state _raw_columnsrrrrrr_with_context_optionsrrsetrra_fallback_from_clausesorder_byr4extra_criteria_entitiessetup_compile_stateZ_ordered_columnsZ_textual_ordered_columnsZ_loose_column_name_matchingprocessZ_add_to_result_maprrZADAPT_FROM_STATEMENT_adapt_on_names_from_obj_alias) rrrrrrzr}fnrentityrrvrvrwrs            z1ORMFromStatementCompileState.create_for_statementcCs|Sr{rvrcolscurrent_adapterrvrvrw_adapt_col_listBsz,ORMFromStatementCompileState._adapt_col_listcCsdSr{rvr~rvrvrw_get_current_adapterEsz1ORMFromStatementCompileState._get_current_adaptercCsV|jjdd}t||}|jjr:t|jdkr:t d|jD]}| ||q@dS)zused by BulkORMInsert (and Update / Delete?) to set up a handler for RETURNING to return ORM objects and expressions rNrsCan't generate ORM query that includes multiple expressions at the same time as '*'; query for '*' alone if present) rzrrrrrrrrr!setup_dml_returning_compile_state)rZ dml_mapperr rr.rvrvrwr5Hs  z>ORMFromStatementCompileState.setup_dml_returning_compile_state)rkrlrmr,rrr_has_orm_entitiesmulti_row_eager_loaderseager_adding_joinscompound_eager_adapterrr( eager_joinsrrr2r3r5rvrvrvrwrs"  rcseZdZUdZdZejZejZ dZ de d<de d<de j fde jfgejZed e jfgZd Zdd ddd d dZddZeddZddZfddZeddZeddZeddZeddZZS) ra*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`. rNz(Union[ExecutableReturnsRows, TextClause]rrr+r#rTz%Iterable[_ColumnsClauseArgument[Any]])entitiesrr+csffddt|D_|_|j_|j_|j_|j_|j_t |rV|j nd_ |_ dS)Ncs g|]}tjtj|ddqS)T)apply_propagate_attrsZ post_inspect)r$expectr&ZColumnsClauseRolerr~rvrwrsz*FromStatement.__init__..) r#Zto_listr#rr)Z is_selectZ is_deleteZ is_insertZ is_updater+r r+)rr;rr+rvr~rwr}s  zFromStatement.__init__cKs2|j||f|}|j }|r"||_|j|jf|S)zprovide a fixed _compiler_dispatch method. This is roughly similar to using the sqlalchemy.ext.compiler ``@compiles`` extension. )_compile_state_factoryrrUr*rz)rrrrUrrvrvrw_compiler_dispatchs 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 )rORMSelectCompileStater;Zget_plugin_classr)rmethrvrvrwcolumn_descriptionss z!FromStatement.column_descriptionscCs|Sr{rvr~rvrvrw_ensure_disambiguated_namessz)FromStatement._ensure_disambiguated_namesc+s6tjdd|jDEdHtjf|EdHdS)Ncss|] }|jVqdSr{) _from_objectsr|rrvrvrwrsz-FromStatement.get_children..) itertoolschain from_iterabler#r get_children)rrrrvrwrIs zFromStatement.get_childrencCs|jjSr{)r_all_selected_columnsr~rvrvrwrJsz#FromStatement._all_selected_columnscCst|jr|jjSdSr{)r)r_return_defaultsr~rvrvrwrKszFromStatement._return_defaultscCst|jr|jjSdSr{)r)r _returningr~rvrvrwrLszFromStatement._returningcCst|jr|jjSdSr{)r*r_inliner~rvrvrwrMszFromStatement._inline)T) rkrlrmrZ__visit_name__rrrrr>_for_update_argrr=Zdp_clauseelement_listZdp_clauseelementr/Z_executable_traverse_internalsZ_traverse_internalsrrZis_from_statementrr?rrBrCrIrJrKrLrMrrvrvrrwr\s>       rZcompound_selectc@s eZdZdS)CompoundSelectCompileStateN)rkrlrmrvrvrvrwrOsrOselectc@sZeZdZdZeZdZdZdZdZ dZ dZ dZ dZ dZdZeddddddd Zd d Zd d ZeddZeddZeddZeddZeddZddZddZddZddZd d!ZdDd"d#Zd$d%Zd&d'Z d(d)Z!d*d+Z"d,d-Z#d.d/Z$d0d1Z%d2d3Z&d4d5Z'd6d7Z(d8d9Z)d:d;Z*e+dd?Z-d@dAZ.dBdCZ/dS)Er@rvNFrrrrc  s4|||dk r|j }nd}|}|j|j|_|jrR|_t j_n|_|jj _ |jj _ g_d_i_|j_|sjddd7_j rƈjjtkrƈj st_qt_n jj_|jrfdd|jD_|jj _tj|jjdd|jj_d_ j!|||dd |r|j"sL|jr|jD]8}|j"D]*}|j#r\|$d d j|Dq\qRjj"D]}|j#r|%q|j&r|j&D]\}} |qg_'g_(t)_*i_+i_,g_-g_./d d |j0D_12t3j4j5|f|S)z@compiler hook, we arrive here from compiler.visit_select() only.NTF)rrc s"i|]}|tj|jgddqS)Fr)r!r"r#)r|memoized_entitiesr~rvrw 5sz>ORMSelectCompileState.create_for_statement..rrv)rrcSsg|]}t|tr|qSrvrrrvrvrwrfs z>ORMSelectCompileState.create_for_statement..css|] }|jVqdSr{)r)r|inforvrvrwrsz=ORMSelectCompileState.create_for_statement..)6rrrZ safe_mergerrZ_clonerr# immutabledictr for_statementrrrrrrr LABEL_STYLE_LEGACY_ORMr8rr6_memoized_select_entities_memoized_entitiesrrr!r"r#rreager_order_byrrrZ'process_compile_state_replaced_entitiesrr$rrr%rr:r(rar&_normalize_froms _from_obj from_clauses_setup_for_generater;rrz) rrzrrrrrQr}r-rrvr~rwrs              z*ORMSelectCompileState.create_for_statementcCstdtd|j|jD]}t|tr||ddkr|tdt|dtd|j|td|j|jqt|tr|ddkrtd t|dtd|j|qdS) Nz5 --------------------------------------------------- zcurrent path: rloaderz Loader: rz Zpath_with_polymorphicz With Polymorphic: )printrrrrrZcoerce__dict__)rrrvrvrw_dump_option_structs z)ORMSelectCompileState._dump_option_structcs|j}d|_d|_|jjr"||jD]}|jr(||j|j |q(|jr^||j|j | |j r|j |_ rt fdd|j D|_ r|jdkr||jn|j|_|jrt fdd|jD|_r|jdkr|t|jn |jpd|_|jr(|jdj}||j|_|jrB||j|_nd|_|j|_|jrvt tdd|jD|_n8|j dk rt tdd|j D|_!n|j"sd |_|j#|_#|jj$rt%|j d krt&'d |j D]}|(|q|j)D]$}|d}||f|d dq|jj*r0|+|j,sZ|jj-rPd sPt.d t&/d|jsnt0|j1|_|jd krd|_|j2r|j3r|j4r|5|_n |6|_|j7r|8}|dk r|j9d|i|_dS)Nrvc3s|]}|dVqdSTNrvr|critr1rvrwrsz.NFc3s |]}r|dn|VqdSrbrvrcrervrwrsrcss$|]}|dk rt|ndVqdSr{r Zsurface_selectablesr|srvrvrwrscss$|]}|dk rt|ndVqdSr{rgrhrvrvrwrsr{rr4Fz,no columns were included in _only_load_propsz4Query contains no columns with which to SELECT from.Z deepentity):rrz_join_entitiesrr_set_select_from_aliasrW _setup_joins_joinrXrr3_where_criteriar_order_by_clausesr2r'_having_criteria_group_by_clausesr#Zflatten_iteratorgroup_byrYr\Z_target_adaptercopy_and_processZ _distinct_on distinct_onZ _distinctdistinctZ _correlate correlateZ_correlate_exceptcorrelate_exceptZ_auto_correlaterNrrrrr)rar_adjust_for_extra_criteriarrrInvalidRequestErrorlistr&r7r8_should_nest_selectable_compound_eager_statement_simple_statementrU _mapper_zeroZ _annotate)rrVrQrr.ZrecZstrategyezerorvrerwr]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. NTr) rrrrrr rr!r"r#)rrVrrrvrvrwr.s z1ORMSelectCompileState._create_entities_collectioncCs|j}t|dSr{)rl_determine_last_joined_entity)rrz setup_joinsrvrvrwdetermine_last_joined_entityHsz2ORMSelectCompileState.determine_last_joined_entityccsj|jD]^}|jrTd|jkrT|jd}|jsB|jsBt|gEdHqdt|jEdHqt|gEdHqdS)NZentity_namespace)r# is_selectable _annotationsZ is_mapperrr,Z_all_column_expressions)rrzrZensrvrvrwall_selected_columnsNs   z*ORMSelectCompileState.all_selected_columnscCs|tjdd|jDS)Ncss.|]&}d|jkr|jn|jdgVqdS) parententityN)rrD__clause_element__rErvrvrwr`s zAORMSelectCompileState.get_columns_clause_froms..)rZrFrGrHr#rrvrvrwget_columns_clause_froms]s  z.ORMSelectCompileState.get_columns_clause_fromscCs>tjtj||d}t|j|}|jj|j|j |j |j d|S)N)r<)rr$rr) r$r=r&ZReturnsRowsRolerr#r`updaterr$rr)rrzfrom_statementstmtrvrvrwrns z$ORMSelectCompileState.from_statementcCsT|j}|jjstt|jdks$t||jd}|rP|jddi7_||_dS) used only for legacy Query casesrrrFN)rrrrrr[_get_select_from_alias_from_objr,)rrVrrvrvrwrks z,ORMSelectCompileState._set_select_from_aliascCs||}d|jkr|jd}t|dr<|js4tdqx|jSn.cSsg|]}t|tr|qSrvrrrvrvrwrs )rXvaluesrrrr)rrrQrrvrvrwrs  z!ORMSelectCompileState._all_equivscsHjr&ddjD}tj|}ng}d}jjfdd|Djjjjjfj j j j j j jdj}|}}ttj||d_tj|gj}j|_j dk rވj jdkrވj |_ |}jD]}t|||j}q|j |||r0|jj |fj!||jj |fj"|S)NcSs$g|]}t|tjjr|jn|qSrv)rr"elementsZ_label_referencer)r|elemrvrvrwrs zCORMSelectCompileState._compound_eager_statement..csg|]}|jkr|qSrv)r)r|rr~rvrwrs  for_updatehintsstatement_hintsrvrwr)#r'r Z expand_column_list_from_order_byr_select_statementr\rnrprrNr_hints_statement_hintsrvrw _select_argsaliasrrrZCOMPOUND_EAGER_STATEMENTr9r rPrr Zofr:rZ splice_joinsZstop_on select_fromnon_generativersrY)rZunwrapped_order_byZorder_by_col_exprinnerrrzZ from_clauseZ eager_joinrvr~rwr|s|    z/ORMSelectCompileState._compound_eager_statementcCsz|j|j|jt|jt|j|j|j|j |j f|j |j j |j j|j|jd|j}|jrv|j j|f|j|S)Nr)rrrrr\r:rrnrprr'rNrrrrvrwrrYr)rrzrvrvrwr}8s&  z'ORMSelectCompileState._simple_statementcCstj|||d}|r||_|r$||_|r:|jt|7_|rR|jj|f|n|rb|j||rx|jt|7_| |_ | |_ ||_ ||_ ||_ ||_|r||_|r||_||_|r||_| r| |_| r|jj|f| | dk r|jj|f| |S)N)r#r[r )r9Z_create_raw_selectrnrprorrurrq _limit_clause_offset_clause _fetch_clause_fetch_clause_options_independent_ctes_independent_ctes_opts _prefixes _suffixesrNrrrvrw)rZ raw_columnsrZwhere_criteriaZhaving_criteriarr'rrrrvrw limit_clause offset_clause fetch_clausefetch_clause_optionsrurtprefixessuffixesrrindependent_ctesindependent_ctes_optsrzrvrvrwrLsR z'ORMSelectCompileState._select_statementcCsxd|jkr0|jd}|j|d}|r0||St|tjrB|}nt|drT|j}ndS|j|d}|rt||SdS)Nrr) rrrZ adapt_clauserr%Z FromClauserr)rrsearchrrvrvrw_adapt_polymorphic_elements     z0ORMSelectCompileState._adapt_polymorphic_elementcsrfdd|DS|SdS)Ncsg|]}|dqS)Trv)r|orervrwrsz9ORMSelectCompileState._adapt_col_list..rvr/rvrerwr2sz%ORMSelectCompileState._adapt_col_listcsJg|jrd|jjf|jr2d|jfs:dSfdd}|S)NTFcsfdd}t|i|S)NcsFd|jkpd|jk}D](\}}|s(|r||}|dk r|SqdS)N _orm_adaptr)r)rZ is_orm_adaptZ always_adaptreadaptersrvrwreplaces  zRORMSelectCompileState._get_current_adapter.._adapt_clause..replace)r'Zreplacement_traverse)rr rrrvrw _adapt_clauses zAORMSelectCompileState._get_current_adapter.._adapt_clause)r,rrrr)rrrvrrwr3s   z*ORMSelectCompileState._get_current_adapterc Cs|D]\}}}}|d}|d}t|}|dk r:t|}t|tjrb|dk rXtd|}d}nd|jkrv|jd}|dkr|jst|dst dd} t|tjrt |dd} |dkr| r| }nF|j }z |j }Wn4t k r} zt d|| W5d} ~ XYnX|j} |j } t|tjs.| }| || jf|jkrDq|dk r|| k r|jdd| k rtd |jd||fn|dk rd} |} nd} } ||| ||| ||qdS) NisouterfullzUNo 'on clause' argument may be passed when joining to a relationship path as a targetrrz9Expected mapped entity or selectable/table as join targetZ_of_typez0Join target %s does not refer to a mapped entityzMexplicit from clause %s does not match left side of relationship attribute %s)r!rrPropComparatorrryrrrrrrr.AttributeErrorZ _parententityrQueryableAttributer_already_joined_edgesr_join_left_to_right) rargsentities_collectionrightonclausefrom_flagsrrZof_typeerrleftrrvrvrwrms          zORMSelectCompileState._joinc Cs|dkr,|dkst|||||\}}} n|||\}} ||krVtd||f|||||\} }}| js~|| } nd} |dk r|j|} |jd|t | ||||| dg|j|dd|_nH| dk rt || t st|| j } n|} |jt | | |||| dg|_dS)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 entityrv)rrZ_extra_criteriar) r"_join_determine_implicit_left_side_join_place_explicit_left_siderry _join_check_and_adapt_right_sider_get_extra_criteriar\rrrr) rrrrrrZ outerjoinrreplace_from_obj_indexuse_entity_indexr_infoZextra_criteriaZ left_clauservrvrwr@s        z)ORMSelectCompileState._join_left_to_rightcCsXt|}d}}|jrrt|j|j|}t|dkrH|d}|j|}n(t|dkr`tdntd|fn|rDi} t|D]R\} } | j } | dkrqt| } | |krqt | t r| | f| | j<qd| f| | j<qt | }t||j|}t|dkr| ||d\}}n*t|dkr2tdntd|fn td|||fS)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. 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.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\r Zfind_left_clause_to_join_fromrrrry enumerateentity_zero_or_selectablerrrzkeys)rrrrrrrrindexesZ potentialZ entity_indexrr.Zent_infoZ all_clausesrvrvrwrsj       z8ORMSelectCompileState._join_determine_implicit_left_sidec Csd}}t|}|jrHt|j|j}t|dkr, flat=True) construct explicitly, see the linked example.Zxaj2r)rr)*r!rwith_polymorphicrrr%ZJoinr\rr Zselectables_overlaprry common_parentrrrjis_clause_elementZ _is_lateralr3Zis_derived_from descriptionrOr$r=r&ZFromClauseRolerr#Zwarn_deprecatedwarnrrrZ DEPRECATED_JOIN_ADAPT_RIGHT_SIDErrZ#_has_aliased_polymorphic_fromclauseZ#WITH_POLYMORPHIC_ADAPTER_RIGHT_JOINZ ClauseElementrr)rrrrrrrZoverlapZ right_mapperrZright_selectableZright_is_aliasedZ need_adapterr1Zaliased_entityrrvrvrwrOs                 z6ORMSelectCompileState._join_check_and_adapt_right_sidec CsF|jj|jj|j|j|jj|jj|jp(d|jj|jj |jj |jj d S)N) rrrurtrrrrrrrr) rrrrurtrrrrrrrrr~rvrvrwr sz"ORMSelectCompileState._select_argscCsF|j}|ddk pD|ddk pD|ddpD|ddpD|ddS)NrrruFrtrvrr)rr)rkwargsrvrvrwr{ s    z-ORMSelectCompileState._should_nest_selectablecs<djfjkr4tfddjdjfDSdSdS)Nadditional_entity_criteriac3s2|]*}|js|jkr|r|VqdSr{)Zinclude_aliasesr.Z_should_includeZ_resolve_where_criteria)r|Zaerrrvrwr0 s   z.rv)rrr)rrrvrrwr+ s z)ORMSelectCompileState._get_extra_criteriac Cs |jD]V}|jdd}|r|jjdk s8d|jf|jkr||jkr||jrP|jndf|j|<qt |j }|D]\}}||j krqp|jj}|j j rg}n ||}|dk r||f7}|}|D]@}|r||}|rt|ddi}||d}|j|f7_qqpdS)aApply 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. rNrrTF)r\rrr_single_table_criterionrr(rrr%rrjrrrr3traverser Z_deep_annotatern) rZ fromclauserrrZ single_critrr1rdrvrvrwrx; sB          z0ORMSelectCompileState._adjust_for_extra_criteria)N)0rkrlrmrrrXr,rr6r7r8r9rvrwrnrprrrar]rrrrrrkrr~rrrrr|r}rrr2r3rmrrrrrrr{rrxrvrvrvrwr@sj'        [M0_cjB>  r@Fz#Union[Query, Select, FromStatement]zOptional[ORMSelectCompileState]rzList[ORMColumnDescription])query_or_select_stmtrUrrcCs8|dkrtj||d}|}dddd|jDD}|S)N)rc SsJg|]B\}}|j|jt|dd|j|jdk r>|js>t|ddnddqS)rFNr.)rtypealiasedexprr.) _label_namerrrrr)r|rZinsp_entrvrvrwr s  z(_column_descriptions..cSsg|]}||jfqSrvr)r|Z_entrvrvrwr s)r@rr)rrUrrdrvrvrwru s rzUnion[Query[Any], Select[Any]]z"Optional[_InternalEntityType[Any]])query_or_augmented_selectrcCsJ|}|jr|j}|dk r|S|jrBd|jdjkrB|jdjdSt|S)Nrr)rl_last_joined_entityr[r_entity_from_pre_ent_zero)rrrrvrvrw_legacy_filter_by_entity_zero srcCsX|}|jsdS|jd}d|jkr,|jdSt|tr<|jSd|jkrP|jdS|SdS)Nrrbundle)r#rrrr.)rrrrvrvrwr s      rzTuple[_SetupJoinsElement, ...]z=Optional[Union[_InternalEntityType[Any], _JoinTargetElement]])rrrcCs2|sdS|d\}}}}t|tjr*|jS|SdS)N)rrrr.)rrtargetrrrrvrvrwr src@seZdZUdZdZded<dZdZdZded<ded <d ed <d ed <dddddZ ddddddZ ddZ e ddZ dS)r!z:represent an entity column returned within a Query result.rvrsupports_single_entityFz Optional[str]rz!Union[Type[Any], TypeEngine[Any]]rz.Union[_InternalEntityType, ColumnElement[Any]]rzOptional[_InternalEntityType]rrjNone)rUrcCs tdSr{rrrUrvrvrwr) sz _QueryEntity.setup_compile_staterrUrrcCs tdSr{rrrUrrvrvrwr5 sz._QueryEntity.setup_dml_returning_compile_statecCs tdSr{r)rrrrvrvrw row_processor sz_QueryEntity.row_processorcCst|D]\}}|jr8|jr2|||j||qn|j}|jr|jrtd|jkr^t||||qt ||j |||q|j ddrt ||||q|jrt ||j |||qt ||g|||q|jrt |||q|S)NrrF)rZ_is_lambda_elementZ _is_sequencer"Z _resolvedrrrr _ColumnEntity _for_columnsZ_select_iterabler _BundleEntityZ_is_clause_listZ is_bundle)rrUr;rrrr.rvrvrwr" sh z_QueryEntity.to_compile_stateN)rkrlrmrrr_non_hashable_value_null_column_typeuse_id_for_hashr)r5rrr"rvrvrvrwr! s r!c@seZdZUdZdZded<ded<ded<ded <d ed <d ed <ddZdZdZdZ e ddZ e ddZ ddZ ddZddZdddddd Zd!d"Zd#S)$rz mapper/class/AliasedClass entity) rrrrpath_extra_entitiesrrr_polymorphic_discriminatorr>rz Mapper[Any]rrrrrrstrrcCs|||r*|jdkr||_d|_d|_|jd}|j|}|_|j}||_|j |_ }|jf|_ |j rv|j |_ n |jj|_ |j |_ |j|_|j|_|j|_|j|_|jr|||jdS)NTr)rrrr6r _post_inspectrr.rrrrrrclass_rkrrrZwith_polymorphic_mappersrZpolymorphic_onr _should_select_with_poly_adapterr)rrUr.rrrrrvrvrwr= s4        z_MapperEntity.__init__TcCs|jjSr{)rrr~rvrvrwrg sz_MapperEntity.typecCs|jSr{rr~rvrvrwrk sz'_MapperEntity.entity_zero_or_selectablecCs t|j|Sr{)rrrr.rvrvrwro sz_MapperEntity.corresponds_tocCsRd}|js"|jr*|j|jd}n|jj}|rH|jrB||j}qN|}n|j}|Sr{)rrrrrrr,wrap)rrUrretrvrvrw_get_entity_clausesr sz!_MapperEntity._get_entity_clausesc Cs|j}||}|jr(|r(||j}n |s2|j}|j|krL|jj}|j}nd}}tj ||j |||j ||||j d }||j |jfS)N)only_load_propsr`polymorphic_discriminator)rUr r9r rrrr`rZ_instance_processorrrrrr)rrrrUrrr`Z _instancervrvrwr s,    z_MapperEntity.row_processorrjrrrc Cs.tj||j||j||j|j|jj|jd dS)Nrrr) r_setup_entity_queryrrrrrrrrrvrvrwr5 sz/_MapperEntity.setup_dml_returning_compile_statec Cs||}|jj}|dk s*d|jf|jkrJ|j}||jr>|jndf|j|<tj ||j||j ||j |j |j j|jd |j|jdS)Nrr)r rrrrrrr(rrrrrrrrr&rr)rrUrsingle_table_critrrvrvrwr) s0   z!_MapperEntity.setup_compile_stateN)rkrlrmrrrrrrrrrrrr rr5r)rvrvrvrwr& s*  %  rc@seZdZUdZdZded<ded<ded<d ed <d ed <ded <d$ddZeddZeddZ ddZ eddZ ddZ ddddd d!Z d"d#ZdS)%rrv)rrrrrrrrrrz Type[Any]rrrrrrTNc Csd|_|jd}|r"|j|n ||t|tjtjfrH| }n|}||_ |_ t ||_ |j |_g|_|r|jD]X}d|jkrt|||||dqzt|trt|||||dqztj||g|d||dqz|j j|_dS)NTr parent_bundle)r6rrrrrrrrrrrrrrZexprsrr_ORMColumnEntityrZ single_entityr)rrUrrrZsetup_entitiesrrrvrvrwr sT           z_BundleEntity.__init__cCs|j}|dk r|jSdSdSr{)rr)rrrvrvrwr sz_BundleEntity.mappercCs&|jD]}|j}|dk r|SqdSr{)rrrrrrvrvrwr( s   z_BundleEntity.entity_zerocCsdSrfrvr rvrvrwr1 sz_BundleEntity.corresponds_tocCs&|jD]}|j}|dk r|SqdSr{)rrrrvrvrwr6 s   z'_BundleEntity.entity_zero_or_selectablecCs|jD]}||qdSr{)rr))rrUrrvrvrwr)? s z!_BundleEntity.setup_compile_staterjrrrcCs ||Sr{r)rrvrvrwr5C sz/_BundleEntity.setup_dml_returning_compile_statecs@tfdd|jD\}}}|jj||}||j|jfS)Ncsg|]}|qSrv)rrrrrvrwrL sz/_BundleEntity.row_processor..)rrrZcreate_row_processorrVrr)rrrZprocslabelsextraprocrvrrwrJ s  z_BundleEntity.row_processor)TN)rkrlrmrrrrrrrrrr)r5rrvrvrvrwr s*   ;   rc@sJeZdZdZed ddZeddZeddZed d Z d d Z dS)r) _fetch_column_row_processorraw_column_indextranslate_raw_columnNc Cs|D]z}|j}d|kr |d} n t|d} | rjd|jkrRt|||| |||dq~t|||| |||dqt||||||dqdS)Nrrer)rr Zextract_first_column_annotation_IdentityTokenEntityr_RawColumnEntity) rrUrrrrrcolumnr_entityrvrvrwr\ sH     z_ColumnEntity._for_columnscCs|jjSr{)r"rr~rvrvrwr sz_ColumnEntity.typecCs |jjj Sr{)r"rZhashabler~rvrvrwr sz!_ColumnEntity._non_hashable_valuecCs |jjjSr{)r"rZ_isnullr~rvrvrwr sz_ColumnEntity._null_column_typec Cs|j}|jdk r@|j\}}}|jr6||jj|jf7}|||fS|jdk rR|j}n&|j}|jrj|jj |}|j rx| }|j r|j j |}| |}||j|jf}||_|jr|j|jj|jf}||j|fS|SdSr{)rUrrrVr#rrr"r,rr _deannotater9_getterrr) rrrrUgetterZ label_nameZextra_entitiesr"r rvrvrwr s4          z_ColumnEntity.row_processor)N) rkrlrmrrrrrrrrrvrvrvrwrT s 1   rc@sHeZdZdZdZdZdZdddZddZdd d d d d Z ddZ dS)r!NF)rr"rrrcCs||_||_|dk |_|jr.|jddi7_|r8|jr@d|_n|rN|j|_n |||_|rl|j |n | |||_ |j j r|j j dnd|_ |j|j f|_d|_|_dS)NrTr)rrrrrZ_is_text_clauser _proxy_keyrrrr"rDrrrr)rrUr"rrrrrvrvrwr s$      z_RawColumnEntity.__init__cCsdSrfrvr rvrvrwr sz_RawColumnEntity.corresponds_torjrrrcCs ||Sr{rrrvrvrwr5 sz2_RawColumnEntity.setup_dml_returning_compile_statecCs\|}|r&||jd}|dkr,dSn|j}|jr:|}|j||j|||_dSrf) r3r"rr$rrrrr)rrUr1r"rvrvrwr) s   z$_RawColumnEntity.setup_compile_state)N) rkrlrmrrrrrrr5r)rvrvrvrwr! s $r!c@sDeZdZdZdZdZdddZddZd d d d d dZddZ dS)rzColumn/expression based entity.F)rrr"rrrrNcCs|j}|} |dd} |d| } | r|js*t|j|_dSn|j}|j}|jj}|dk sdd|jf|jkr~||j rr|j ndf|j |<|j r|j s|}t|jj|jjr|j|j|j||j|||_dS)NFr)r3r"rrrrrrrrrr(rZ_expression_labelr$r%rD intersectionrr&rrrr)rrUr1r"rrrvrvrwr) s8       z$_ORMColumnEntity.setup_compile_state)N) rkrlrmrrrrrr5r)rvrvrvrwr s C  rc@s eZdZdZddZddZdS)r FcCsdSr{rvrrvrvrwr) sz(_IdentityTokenEntity.setup_compile_statecsfdd}||j|jfS)NcsjjSr{)rXrr)rowrrvrwr& sz2_IdentityTokenEntity.row_processor..getter)rr)rrrr&rvr,rwr s z"_IdentityTokenEntity.row_processorN)rkrlrmrr)rrvrvrvrwr  sr )NF)N) __future__rrFtypingrrrrrrr r r r r rrrrbaserrrZ path_registryrr#rrrrrrrrrr r!r"r$r%r&r r'Z sql._typingr(r)r*r+Zsql.baser,r-r.r/r0r1Zsql.dmlr2Z sql.elementsr3r4Zsql.selectabler5r6r7r8r9r:r;r<Z sql.visitorsr=Z_typingr>r?r@rrArVrBr[rCrDZenginerEZengine.interfacesrFrGZ sql.compilerrHrIrJrKrLrMrNrOZ sql.type_apirPrQrootrrTrrVrSrrrrjrZ plugin_forrrrOr@rrrrr!rrrr!rr rvrvrvrw s                                                                               gl-+-x &_-yN