U kfK @sdZddlmZddlZddlZddlmZddlmZddlmZddlm Z dd l m Z dd l m Z dd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd l&m'Z'dd!l(m)Z)dd"l*m+Z+dd#lm,Z,dd$lm-Z-d%d&l m.Z.d%d l m Z/d%d'l m0Z0d%d(l m1Z1d%d)l m2Z2d%dl mZd%dl2mZ3d%d*l2m4Z4d%d+l5m6Z6d%d,l5m7Z7e r6dd-lm8Z8d%d.l9m:Z:dbd0d1Z;eje1j?ejd?d?e$ZBGd@dAdAZCGdBdCdCe$ZDe1j?ej8j=d5d=GdDdEdEe$ZEe1j?ej8j=dFdGej8j=ddGGdHdIdIeDZFe1j?ej8j=d5dGej8j=dJdGej8j=dKdGej8j=dLdGej8j=dMdGGdNdOdOeDejGe1jHZIGdPdQdQZJGdRdSdSeDZKej8j=dTdGGdUdVdVeKZLe1j?ej8j=dWdGGdXdYdYeKZMe1j?ej8j=dZdGej8j=d/dGGd[d\d\eDZNe1j?ej8j=d]dGGd^d_d_eKejGZOd`daZPdS)czWsqlalchemy.orm.interfaces.LoaderStrategy implementations, and related MapperOptions.) annotationsN)Any)Dict)Tuple) TYPE_CHECKING) attributes)exc) interfaces)loading) path_registry) properties)query) relationships) unitofwork)util)_DEFER_FOR_STATE)_RAISE_FOR_STATE)_SET_DEFERRED_EXPIRED) ATTR_WAS_SET)LoaderCallableStatus) PASSIVE_OFF) PassiveFlag)_column_descriptions)ORMCompileState)ORMSelectCompileState) QueryContext)LoaderStrategy)StrategizedProperty)_state_session) InstanceState)Load) _none_set) AliasedClass)event)inspect)log)sql)visitors)LABEL_STYLE_TABLENAME_PLUS_COL)Select)RelationshipProperty) ColumnElementFc  s"g} |o |j|r"|jr"| t|j|jjkrV|jj|j\| fdd|rf| tj|r|j r|j r| fdd|j D]} || j |jkr| j|jstj| j|jf||||o|jp|jtjk||||| p|j |jd | } | D]} | | |q qdS)Ncstj||jfSN)orm_utilZ_validator_eventskeydescprop)fnoptsI/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/strategies.pyXs z%_register_attribute..cst|Sr.)rZbackref_listenersr1)backrefuselistr6r7r8fs) Z parent_tokenr:compare_function useobjectZ trackparent typecallable callable_active_history impl_classZsend_modified_eventsdoc)r:Z single_parentappendsingle_parent_validatorr0parentZ validatorsrZtrack_cascade_eventsZback_populatesZ_effective_sync_backrefZself_and_descendants_propsget class_managerZ_attr_has_implrZregister_attribute_implclass_ directionr Z ONETOMANYZviewonlyrA)r3mapperr<r;r=r>Zproxy_propertyr?r@kwZ listen_hooksmr2hookr6)r9r4r5r:r7_register_attributeBsZ            rN) instrumentdeferredcs6eZdZdZdZfddZd ddZdd ZZS) UninstrumentedColumnLoaderzRepresent a non-instrumented MapperProperty. The polymorphic_on argument of mapper() often results in this, if the argument is against the with_polymorphic selectable. columnscst|||jj|_dSr.)super__init__parent_propertyrSselfrD strategy_key __class__r6r7rUsz#UninstrumentedColumnLoader.__init__Nc Ks*|jD]}|r|j|}|||qdSr.)rS_append_dedupe_col_collection) rX compile_state query_entitypathloadoptadaptercolumn_collectionkwargscr6r6r7 setup_querys  z&UninstrumentedColumnLoader.setup_queryc CsdSr.r6 rXcontextr^r_r`rJresultra populatorsr6r6r7create_row_processors z/UninstrumentedColumnLoader.create_row_processor)N) __name__ __module__ __qualname____doc__ __slots__rUrerj __classcell__r6r6rZr7rQs  rQTcs>eZdZdZdZfddZd ddZdd Zd d ZZ S) ColumnLoaderz8Provide loading behavior for a :class:`.ColumnProperty`.)rS is_compositecs*t|||jj|_t|jd|_dS)Ncomposite_class)rTrUrVrShasattrrrrWrZr6r7rUs zColumnLoader.__init__Fc Ksv|jD]<} |r6|r,|| } | dkr6dSn |j| } || |q|jd} |rh|j| } | dkrhdS| ||j<dSNr)rSZadapt_check_presentr\rV) rXr]r^r_r`rarbmemoized_populatorsZcheck_for_adaptrcrdfetchr6r6r7res     zColumnLoader.setup_querycCs`d|_|jdj}|jjpD|jdjpD|jdk oD|j|jd|jk}t |j|d|j |ddS)NTrF)r<r;r?) is_class_levelrStyperVr? primary_keyZversion_id_col_columntopropertyrFrNcompare_values)rXrJZcoltyper?r6r6r7init_class_attributes"    z!ColumnLoader.init_class_attributec CsZ|jD]:} |r|j| } || d} | r|d|j| fqVq|d|jdfdSNFZquickexpireT)rS_getterrBr0) rXrgr^r_r`rJrhraricolgetterr6r6r7rjs   z!ColumnLoader.create_row_processor)F) rkrlrmrnrorUrer}rjrpr6r6rZr7rqs  !rq)Zquery_expressioncs4eZdZfddZddZddZddZZS) ExpressionColumnLoadercs<t||tdtfdd|jjD|_dS)Nc3s|]}| VqdSr.)compare.0rdnullr6r7 %sz2ExpressionColumnLoader.__init__..) rTrUr(rlabelanyrVrS_have_default_expressionrWrZrr7rUs  zExpressionColumnLoader.__init__c Ksd} |r|jr|j} n|jr$|jj} | dkr0dS| D]} |rF|j| } || |q4| d} |rv|j| } | dkrvdS| ||j<dSru)_extra_criteriarrVrSr\) rXr]r^r_r`rarbrvrcrSrdrwr6r6r7re)s"    z"ExpressionColumnLoader.setup_queryc Csh|rd|jrd|j} | D]:} |r&|j| } || d} | r|d|j| fqdq|d|jdfdSr~)rrSrrBr0) rXrgr^r_r`rJrhrarirSrrr6r6r7rjNs   z+ExpressionColumnLoader.create_row_processorcCs(d|_t|j|d|jdjjdddS)NTFr)r<r;Zaccepts_scalar_loader)rxrNrVrSryr|rXrJr6r6r7r}hs z+ExpressionColumnLoader.init_class_attribute)rkrlrmrUrerjr}rpr6r6rZr7rs %r)rPrO)rPrO raiseload)Z do_nothingcsNeZdZdZdZfddZddZddZdd d Zd d Z ddZ Z S)DeferredColumnLoaderzAProvide loading behavior for a deferred :class:`.ColumnProperty`.)rSgrouprcsJt||t|jdr"td|jdd|_|jj|_|jj |_ dS)Nrsz8Deferred loading for composite types not implemented yetrF) rTrUrtrVNotImplementedError strategy_optsrFrrSrrWrZr6r7rUs  zDeferredColumnLoader.__init__c Cs|jrB|jjjrB|j|jjjkrB|jd||||||||nH|jsv|j rX|jj } n|jj } |d |j| fn|d |jdfdS)N)rPFrOTnewrF) refresh_stater_compile_options_only_load_propsr0rV _get_strategyrjrxrZ_raise_column_loaderZ_deferred_column_loaderrB) rXrgr^r_r`rJrhrariZset_deferred_for_local_stater6r6r7rjs4 z)DeferredColumnLoader.create_row_processorcCs,d|_t|j|d|jdjj|jdddS)NTFr)r<r;r>Zload_on_unexpire)rxrNrVrSryr|_load_for_staterr6r6r7r}s z)DeferredColumnLoader.init_class_attributeNc Ks|jjr|jjsT|r(t|j|jjsT|rF|j rF|j d|j dsT|rx|j |krx|j dj|||||||f| n.|jrt||j<n|jst||j<n t||j<dS)Nzundefer_group_%sFr)compile_optionsZ_render_for_subqueryrVZ_renders_in_subqueriessetrS intersectionrDZ_should_undefer_in_wildcardr local_optsrFr0rrerxrrrr) rXr]r^r_r`rarbrvZonly_load_propsrKr6r6r7resR       z DeferredColumnLoader.setup_querycsjs tjS|tj@stjSjj}jr@fdd|j D}njg}fdd|D}t }|dkrt dt jfjr|dtjt|ttjS)Ncs4g|],}t|trt|jtr|jjkr|jqSr6) isinstancerstrategyrrr0)rprXr6r7 s    z8DeferredColumnLoader._load_for_state..csg|]}|jkr|qSr6)Z unmodified)rk)stater6r7rs zfParent instance %s is not bound to a Session; deferred load operation of attribute '%s' cannot proceedraise)r0r ATTR_EMPTYrSQL_OKPASSIVE_NO_RESULTmanagerrJrZiterate_propertiesrorm_excDetachedInstanceErrorr/ state_strr_invoke_raise_loadr Zload_scalar_attributesrrr)rXrpassive localparentZtoloadrsessionr6)rXrr7rs6  z$DeferredColumnLoader._load_for_statecCstd|fdS)Nz+'%s' is not available due to raiseload=Truesa_excInvalidRequestErrorrXrrlazyr6r6r7r#sz'DeferredColumnLoader._invoke_raise_load)N) rkrlrmrnrorUrjr}rerrrpr6r6rZr7rts 3 3(rc@s0eZdZdZd dddddZejfdd Zd S) LoadDeferredColumnsz7serializable loader object used by DeferredColumnLoaderFstrboolr0rcCs||_||_dSr.r)rXr0rr6r6r7rU,szLoadDeferredColumns.__init__cCs>|j}|jj}|j|}|jr$d}nd}||}|||S)N)rPTr)rT)rr)r0rrJrErrr)rXrrr0rr3rYrr6r6r7__call__0s  zLoadDeferredColumns.__call__N)F)rkrlrmrnrUrrrr6r6r6r7r)srcs,eZdZdZdZfddZddZZS)AbstractRelationshipLoaderz0LoaderStratgies which deal with related objects.)rJtargetr:entitycs:t|||jj|_|jj|_|jj|_|jj|_dSr.)rTrUrVrJrrr:rWrZr6r7rUFs    z#AbstractRelationshipLoader.__init__c Cs |jd||||||||S)N))r immediate)rVrrjrfr6r6r7#_immediateload_create_row_processorMs z>AbstractRelationshipLoader._immediateload_create_row_processor)rkrlrmrnrorUrrpr6r6rZr7rAs rc@seZdZdZdS)DoNothingLoaderzRelationship loader that makes no change to the object's state. Compared to NoLoader, this loader does not initialize the collection/attribute to empty/none; the usual default LazyLoader will take effect. N)rkrlrmrnr6r6r6r7rfsrZnoload)rc@s$eZdZdZdZddZddZdS)NoLoaderzQProvide loading behavior for a :class:`.Relationship` with "lazy=None". r6cCs d|_t|j|d|jjddS)NT)r<r=)rxrNrVcollection_classrr6r6r7r}}szNoLoader.init_class_attributec s$fdd} |dj| fdS)Ncs&jrt||jn d|j<dSr.)r:rinit_state_collectionr0rdict_rowrr6r7invoke_no_loadsz5NoLoader.create_row_processor..invoke_no_loadr)rBr0) rXrgr^r_r`rJrhrarirr6rr7rjs zNoLoader.create_row_processorN)rkrlrmrnror}rjr6r6r6r7rrs rselectr raise_on_sqlZ baked_selectcseZdZUdZdZded<ded<ded<ded<d ed <d d d fd d ZddZddZddZ ddZ dddde j fddZ ddZe dddZd d!ZZS)" LazyLoaderztProvide loading behavior for a :class:`.Relationship` with "lazy=True", that is loads when first accessed. ) _lazywhere_rev_lazywhereZ_lazyload_reverse_option _order_byuse_getis_aliased_class _bind_to_col_equated_columns_rev_bind_to_col_rev_equated_columns_simple_lazy_clause _raise_always _raise_on_sqlzColumnElement[bool]rzDict[str, ColumnElement[Any]]rrrzRelationshipProperty[Any]rVzTuple[Any, ...])rDrYcs8t|||jddk|_|jddk|_t|jj|_|jj }| \|_ |_ |_ |j dd\|_|_|_|jjrddt|jjD|_nd|_|jd||j |j o|j o|jjd j|j dd |jjd |_|jr4t|j D]4}||jjkr|jj|D]}|j ||j |<q q|jd |dS) NrrrT)Zreverse_directioncSsg|]}t|ddiqS) _orm_adaptT)sql_util_deep_annotaterelemr6r6r7rsz'LazyLoader.__init__..z%s lazy loading clause %srFZ use_proxiesZ compare_keys equivalentsz4%s will use Session.get() to optimize instance loads)rTrUrrrr&rrrV_join_conditionZcreate_lazy_clauserrrrrrorder_byrto_listrloggerinfor: _get_clauserrJ_equivalent_columnsrlist)rXrDrYZjoin_conditionrrdrZr6r7rUsP      zLazyLoader.__init__c Csvd|_|jj}|jjr d}d}n4|jjtjk s4|jsL|rBd}d}qTd}d}nd}}t|j|d|j |jj ||ddS)NTF)r<r>r=r?_deferred_history) rxrV_legacy_inactive_history_styler?rIr MANYTOONErrNrr)rXrJrr?rr6r6r7r}s2 zLazyLoader.init_class_attributecsdt|jddi}||j}gdd}t|id|ifdd}t|id|i}|fS)NrTcSs d|_dS)NF)unique bindparamr6r6r7visit_bindparam'szFLazyLoader._memoized_attr__simple_lazy_clause..visit_bindparamrcsF|jkr$|j|jdfn|jdkrB|jd|jfdSr.)Z_identifying_keyrBr0callablevaluerZ bind_to_colparamsr6r7r,s  )rrrrr)traverseZcloned_traverse)rXZ lazywhere criterionrr6rr7"_memoized_attr__simple_lazy_clauses  z-LazyLoader._memoized_attr__simple_lazy_clausec Cs|j\}}|dkr(t|dd|DS|jj}|}t|}|tj @rV|tj N}i}|D]J\} } } | dk r|r|tj @r| ||| |} n| ||| |} | || <q^||fS)NcSsg|]\}}}|qSr6r6)rr0identrr6r6r7rCsz4LazyLoader._generate_lazy_clause..) rrZadapt_criterion_to_nullrVrDobjr instance_dictrZINIT_OKLOAD_AGAINST_COMMITTED#_get_committed_state_attr_by_column_get_state_attr_by_column) rXrrrZ param_keysrJorrr0rrr6r6r7_generate_lazy_clause>s:      z LazyLoader._generate_lazy_clausecCstd||fdS)Nz&'%s' is not available due to lazy='%s'rrr6r6r7r^s zLazyLoader._invoke_raise_loadNr6c Cs|js |jjs|jr|js tjS|j }d} |jo>| p>|j } |t j @sN| r\|t j @sb|rbtj S|jr|t j@s| r|t j@r|||dt|} | s|t j@rtj Stdt||jf| rz|| ||} tj | krtj Stj| krtjSt| rdS|j|jkr(|t j@s(tjS| j|j| ||d} | dk r\| tj krVdS| Sn|t j @rt|t j@sztj S|!| || |||||| S)NrzbParent instance %s is not bound to a Session; lazy load operation of attribute '%s' cannot proceed)rZlazy_loaded_from)"r0rVZload_on_pendingZ _load_pendingZ session_idrrrrrrrZNON_PERSISTENT_OKrrNO_RAISEZRELATED_OBJECT_OKrrrrr/r_get_ident_for_use_getZ NEVER_SETr" issupersetdictDEFERRED_HISTORY_LOADrZ_identity_lookuprZPASSIVE_CLASS_MISMATCH_emit_lazyload) rXrrr`extra_criteria extra_optionsalternate_effective_pathexecution_optionspendingprimary_key_identityrrinstancer6r6r7rcs          zLazyLoader._load_for_statecsDjj}tj@r|jn|jjfddjjDS)Ncs"g|]}j|dqS)rrrpkrZget_attrrrXrr6r7rsz5LazyLoader._get_ident_for_use_get..)rrJrrrrrrz)rXrrrinstance_mapperr6rr7rs z!LazyLoader._get_ident_for_use_getzsqlalchemy.orm.strategy_optionsc stjjj} tj| g| jtt j d} t j } | d|d7} j jdk r\| jj j} |j } | sr|tj@rtddi| _j}|js|r|jr|dkr|jj }n |j }|j}|r|jrd}|tj|f7}|| _n$|dkr|jjj }n |j }|r | j|7_| jd|i7_|rnjrZ|t j!@sZ"||dt#j$|| || | dSj%r~j%| _&fdd }| j'|j ff7_'(||\}}| rtj)*| d | i} nd | i} j|j+kr|t j,@st-j.S| rt/tj0|1r,dSnt/tj2|1r,dSjrN|t j!@sN"||d|f| _3|j4| || d }|567}j8r|St9|}|r|d krt:d j |dSdSdS)N) _raw_columns_propagate_attrs _label_stylerF)Z_invoke_all_eagers_lazy_loaded_fromZ autoflush _current_pathr) load_optionsrcsRjjD]D}|jtjkr|jrt|jtsj |j j |j ||qdSr.)rVZ_reverse_propertyrIr rZ_use_getrrrr!Z_construct_for_existing_pathrrrDZlazyloadZprocess_compile_state)compile_contextrevrXstrategy_optionsr6r7_lazyload_reverse7s    z4LazyLoader._emit_lazyload.._lazyload_reverseZ_sa_orm_load_options)rrzKMultiple rows returned with uselist=False for lazily-loaded attribute '%s' r);rZ preloadedZorm_strategy_optionsr__clause_element__r+_create_raw_selectrr*rdefault_compile_optionsrZdefault_load_optionsrVZ secondary select_fromrJr0rZ NO_AUTOFLUSH immutabledict_execution_optionsrrrZ load_pathr/LoaderCriteriaOption _with_options_path_registryrrrrrr Zload_on_pk_identityr_order_by_clausesZ_with_context_optionsr EMPTY_DICT merge_withrrrrZhas_intersectionr"valuesZ _never_setZ_where_criteriaexecuterZscalarsallr:lenwarn)rXrrr rr`rrrrZ clauseelementstmtrr reffective_pathr5rZ lazy_clauserrhlr6rr7rs          zLazyLoader._emit_lazyloadc s|j|jjr@|jjjr@|j|jjjkr@|||||||||S|jrP|r|jrt |j t |||jrp| |nd} |d|j| fn,|js|jrfdd} |d|j| fdS)Nrcs||dSr.)_resetrr0r6r7reset_for_lazy_callables z@LazyLoader.create_row_processor..reset_for_lazy_callable)r0rZ_is_user_refreshrrrrrxrr Z"_instance_level_callable_processorrGLoadLazyAttribute_generate_extra_criteriarBpopulate_existingZalways_refresh) rXrgr^r_r`rJrhrariZset_lazy_callabler3r6r2r7rjsF       zLazyLoader.create_row_processor)rkrlrmrnro__annotations__rUr}rrrrr'rrZpreload_modulerrjrpr6r6rZr7rs. <"    p rc@s.eZdZdZddZddZejfddZdS) r4asemi-serializable loader object used by LazyLoader Historically, this object would be carried along with instances that needed to run lazyloaders, so it had to be serializable to support cached instances. this is no longer a general requirement, and the case where this object is used is exactly the case where we can't really serialize easily, which is when extra criteria in the loader option is present. We can't reliably serialize that as it refers to mapped entities and AliasedClass objects that are local to the current process, which would need to be matched up on deserialize e.g. the sqlalchemy.ext.serializer approach. cCs||_|j|_||_||_dSr.r0rYr`r)rXr0Zinitiating_strategyr`rr6r6r7rUszLoadLazyAttribute.__init__cCs(|jdk rtd|j|j|jddS)NzvCan't reliably serialize a lazyload() option that contains additional criteria; please use eager loading for this caser6r8)rrr-r0rYr`rr6r6r7 __getstate__s zLoadLazyAttribute.__getstate__cCs:|j}|jj}|j|}|j|j}|j|||j|jdS)N)r`r) r0rrJrEZ _strategiesrYrr`r)rXrrr0rr3rr6r6r7rs  zLoadLazyAttribute.__call__N) rkrlrmrnrUr9rrrr6r6r6r7r4sr4c@seZdZdZdZdddZdS) PostLoaderz;A relationship loader that emits a second SELECT statement.r6Nc CsN|jjptjj|}|}td|i}|rF|j dd}|dk} nd}d} |dk r|j j srt d|j d|j dt||}| s|dkr|d||fS| s|dt||d i}tj|||j r|d||fS||j } ||j } | |jd sB|r(| jd |krB|d||fSn| |jrB|d||fS|d ||fS) NZsa_top_level_orm_contextrecursion_depthFz'recursion_depth option on relationship z0 not valid for non-self-referential relationshipZ_recursion_depth_rrloaderr$T)r] current_pathr/ PathRegistryrootZ_get_top_level_contextrr!rrFrVZ_is_self_referentialrrridunionr PostLoadZ path_existscontainsrlengthcontains_mapperrJ) rXrgr_r` join_depthr/Ztop_level_contextrr;Zunlimited_recursion path_w_propZeffective_path_w_propr6r6r7_setup_for_recursionsp        zPostLoader._setup_for_recursion)N)rkrlrmrnrorIr6r6r6r7r:sr:rcs8eZdZdZfddZddZddZdd ZZS) ImmediateLoaderrGcst|||jj|_dSr.rTrUrVrGrWrZr6r7rULszImmediateLoader.__init__cCs|jd|dSN)rrrVrr}rr6r6r7r}Ps z$ImmediateLoader.init_class_attributec  Csl|jjjsdS|||||j\} } } } | s:tjtjB} n tj tjB} t j || |j |j|j|| | | dSr.)r]r_enable_eagerloadsrIrGrZPASSIVE_NO_FETCH_RELATEDrrrr rCcallable_for_pathrDrV_load_for_path)rXrgr^r_r`rJrhrarir/ run_loaderrr;flagsr6r6r7rjUs,  z$ImmediateLoader.create_row_processorc Cs|r.t|jj} ||f| _|} | f} n d} |} d} |j} |jd} |D]P\}}|j }|sj| |krP| j ||| | |d}|t t j fkrP|| |||qPdS)Nr6rN)rrr)r!r_rZ_recurserg_truncate_recursiver0rVrrrrrrget_implset_committed_value)rXrgr_states load_onlyr`rTr;rZnew_optrrr0 lazyloaderr overwriterrr6r6r7rRs>      zImmediateLoader._load_for_path) rkrlrmrorUr}rjrRrpr6r6rZr7rJHs  *rJsubquerycseZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ GdddZ ddZ ddZddZddZZS)SubqueryLoaderrKcst|||jj|_dSr.rLrWrZr6r7rUszSubqueryLoader.__init__cCs|jd|dSrMrOrr6r6r7r}s z#SubqueryLoader.init_class_attributec s|}|j}t|d}|j|rB|j|dkrB|j|j}n||d}|r|j|jj|t |t }|rt j || d7n||j } fdd| D} | |fS)Nrrcs&g|]}tjdjj|jqSr)getattrr_rr{r0rleftmost_mapperZ new_subq_pathr6r7rs   z0SubqueryLoader._get_leftmost..)r_r/Z_class_to_mapperrDisarV _entities entity_zeror%r,r r?coerce local_columns) rXorig_query_entity_index subq_pathZcurrent_compile_stateis_rootZgiven_subq_pathZ subq_mapperZ leftmost_prop additionalZ leftmost_cols leftmost_attrr6r`r7 _get_leftmosts4    zSubqueryLoader._get_leftmostcCs.|d}tjtj}|j|j|}|js^d|_|j j |fddt ||dD| dd|D| } | |_|j} | dkrd|_n6| dkrdd| DD]} t| | jsd|_qq|jsd |_|jdkr |jr t| |j} | r || | |t} tj|| dd }|S) NFcSs h|]}|ddk r|dqS)rNr6)rZentr6r6r7 s z?SubqueryLoader._generate_from_original_query..)r]cSsg|]}tjtjj|qSr6)r(Z coercionsexpectZrolesZColumnsClauseRole)rrr6r6r7r%sz@SubqueryLoader._generate_from_original_query..TcSsh|] }|jqSr6)tablerr6r6r7rm4sr6use_mapper_path)Z_cloneZ correlaterQuery__new____dict__updateZ _from_objZ_enable_assertionsr Znon_generativerZ_adapt_col_listZ_get_current_adapterrdistinct_target_keyZ _distinctrrrzZ_has_row_limiting_clauser&rZ expand_column_list_from_order_byZ _set_entitiesZset_label_styler*r\r/r#)rXorig_compile_state orig_queryrarkleftmost_relationshipZ orig_entityqq2Z target_colsrvtZto_addZembed_q left_aliasr6r6r7_generate_from_original_querys^  z,SubqueryLoader._generate_from_original_queryc sg}t|}t|D]N\}\}}|dkrR||ddj}||rL|n|} n|} || |jfqt|dkrz|n.t|dd} | j r| j nt j | j ddj j} fdd| D} || fS) Nrrr$r<Trpcs g|]}tjj|jqSr6)r_rDr{r0r parent_aliasrXr6r7rysz2SubqueryLoader._prep_for_joins..)rpairs enumeraterJrbrBr0r,r&rrr/r#rVrf) rXr}rhto_joinrirJr3Z prev_mapperZ to_appendr local_cols local_attrr6rr7_prep_for_joinsPs,   zSubqueryLoader._prep_for_joinsc Cs0t|}|dkr,t||dd|g}n|dkrft||dd|t||dd|g}n|dkrdd|ddD}g}|r|d} t| d| d} |r| |dd} n | |} || qt||dd|djg|t||dd|g}|D]} || }q|S)Nrrr$r<cSs:g|]2}t|djs$t|dn|dj|dfqS)rr)r&rr/r#r)ritemr6r6r7rs   z/SubqueryLoader._apply_joins..)r,r_of_typepoprBrDjoin) rXrzrr}reffective_entityZltjmiddleinnerrattrr6r6r7 _apply_joinssD       zSubqueryLoader._apply_joinsc CsB|j}|r*|jr*|t|j||f7}||}|j|}|Sr.)r$rr/r#rr5Z_with_current_pathoptions) rXrgrzrhrewritten_pathrxrr` new_optionsr6r6r7_setup_optionss   zSubqueryLoader._setup_optionscs&jjr"fdd}||j}|S)Ncs |jttjj7_dSr.eager_order_bytuplerrrVrrrr6r7_setup_outermost_orderbys zISubqueryLoader._setup_outermost_orderby.._setup_outermost_orderby)rVr_add_context_option)rXrzrr6rr7rs z'SubqueryLoader._setup_outermost_orderbyc@s4eZdZdZdZddZddZddZd d Zd S) zSubqueryLoader._SubqCollectionszGiven a :class:`_query.Query` used to emit the "subquery load", provide a load interface that executes the query at the first moment a value is needed. rrrrsubq_datacCs4|j|_|j|_|j|_|jp i|_||_d|_dSr.r)rXrgrr6r6r7rUs  z(SubqueryLoader._SubqCollections.__init__cCs |jdkr||j||Sr.)r_loadrF)rXr0defaultr6r6r7rFs z#SubqueryLoader._SubqCollections.getcCstt|_|j}|jdks t||j}|jj r<| }t| |j }t |ddD]"\}}|j|dd|Dq\dS)NcSs |ddS)Nrr6xr6r6r7r8z7SubqueryLoader._SubqCollections._load..css|]}|dVqdS)rNr6rZvvr6r6r7rsz8SubqueryLoader._SubqCollections._load..) collections defaultdictrrrrAssertionErrorZ with_sessionrZ_populate_existingr6r itertoolsgroupbyextend)rXrzrowsrvr6r6r7rs  z%SubqueryLoader._SubqCollections._loadcCs|jdkr|dSr.)rr)rXrrrr6r6r7r=s z&SubqueryLoader._SubqCollections.loaderN) rkrlrmrnrorUrFrr=r6r6r6r7_SubqCollectionss   rc Cs|j}|jjr|jjrdS|j|}d|_||j}||j dd} | dk rT| } n|j } |j j dt jjt jjf\} } | t jjk} | |} | |} |j j dtf|j }t|d}|jr|jjdkrtd||f|j}|j|dd}||| || \}}}} |||||||}t | }|j j |jdtf|d| | fi|_ |d}| || \}}}|j!|}|"||||| }|#||| | || |}|$|}|S) NTpath_with_polymorphic)Zsubquery_pathsNrxormzsubqueryloader for "%s" must invoke lambda callable at %r in order to produce a new query, decreasing the efficiency of caching for this statement. Consider using selectinload() for more effective full-lambda cachingF)legacy)%r]rrPZ_for_refresh_statercindexZloaders_require_bufferingrVrFrrrr"r/r?r@r]rZ_get_plugin_class_for_pluginZ_is_lambda_elementrrrr-Z _resolvedZ_create_entities_collectionrlr~rrr(rZ_set_enable_single_critrZ add_columnsrrr)rXrgr^r_rr`rar]rgwith_poly_entityrrhrrirxZcompile_state_clsrwrarkryr}rzrrrr6r6r7_setup_query_from_rowprocs            z(SubqueryLoader._setup_query_from_rowprocc  sj|jr||||||||S|||||j\} } } } | s@dSt|jtsPdS|jj|j j j spt d|t|dkrt|j|jsdSnt|d|jsdS|||||d|} | dkrdS| jdkst||j}|jj} ||jd} | dkr ||| } ||jd| r8fdd| D} |jrT|||| | |n|||| | |dS)NJ'%s' does not support object population - eager loading cannot be applied.rr<rcsg|]}j|qSr6rRrrar6r7rsz7SubqueryLoader.create_row_processor..)rrrIrGrr]rrDrGr0implsupports_populationrrr,r/ _entity_isardrrrrVrfrFrrrr:_create_collection_loader_create_scalar_loader)rXrgr^r_r`rJrhrari_rSrrrr6rr7rjs         z#SubqueryLoader.create_row_processorcsp||fddfdd}|djf|dj|f|jrl|djjfdS)Ncs*|d}|j|||dS)Nr6)rFrVr0rW)rrr collectionrrX tuple_getterr6r7load_collection_from_subqs  zKSubqueryLoader._create_collection_loader..load_collection_from_subqcsj|kr|||dSr.r2r)rrXr6r7&load_collection_from_subq_existing_rows zXSubqueryLoader._create_collection_loader..load_collection_from_subq_existing_rowrexistingeagerZ _tuple_getterrBr0invoke_all_eagersr=)rXrgrhrrrirr6)rrrXrr7rs z(SubqueryLoader._create_collection_loadercsp||fddfdd}|djf|dj|f|jrl|djjfdS)NcsL|d}t|dkr*td|d}|j|||dS)Nr.rLMultiple rows returned with uselist=False for eagerly-loaded attribute '%s' r)rFr,rr-rVr0rW)rrrrZscalarrr6r7load_scalar_from_subqs zCSubqueryLoader._create_scalar_loader..load_scalar_from_subqcsj|kr|||dSr.r2r)rrXr6r7"load_scalar_from_subq_existing_row!s zPSubqueryLoader._create_scalar_loader..load_scalar_from_subq_existing_rowrrrr)rXrgrhrrrirr6)rrrXrr7rs  z$SubqueryLoader._create_scalar_loader)rkrlrmrorUr}rlr~rrrrrrrjrrrpr6r6rZr7r]s 1d/6"4 Yr]ZjoinedcseZdZdZdZfddZddZdd d Zd d Zd dZ ddZ ddZ dddZ ddZ ddZddZddZZS) JoinedLoaderz[Provide loading behavior for a :class:`.Relationship` using joined eager loading. rGcst|||jj|_dSr.rLrWrZr6r7rU8szJoinedLoader.__init__cCs|jd|dSrMrOrr6r6r7r}<s z!JoinedLoader.init_class_attributeNFc  KsR|jjs dS|jrd|_||j}d} |r:||||jnd} | dk r`|||||| \} }} n`||jds|j r|j d|j krdSn| |j rdS| ||||||||\} }} }d|_||jdd}|dk rt|j} nd} ||j}tj||j ||| | | |j |d tj|j}|rN|dk r.)rrPr:multi_row_eager_loadersrV_init_user_defined_eager_procr$_setup_query_on_user_defined_adapterrDrGrErFrJ_generate_row_adapterZeager_adding_joinsrFr&Zwith_polymorphic_mappersrr Z_setup_entity_queryrZNONE_SETrsecondary_columnsrr)rXr]r^r_r`rarbrrrcruser_defined_adapterclausesadd_to_collectionrZ has_nonesr6r6r7reAs     zJoinedLoader.setup_queryc Csd|jkrdS|jj}||jdd}|dk r2|S|jd}|dd\}}|dk rt|trj|j|}t j t j j |j ||j jdd}nF||jdr||jd} t j t j j| |j jd}n|j|j d}||d||S)NZeager_from_aliasF user_defined_eager_row_processor) selectablerZlimit_on_entityr)r)rr_rDrFrrrraliasr/ ORMAdapter_TraceAdaptRoleZJOINEDLOAD_USER_DEFINED_ALIASrJrrDZ JOINEDLOAD_PATH_WITH_POLYMORPHICZ_polymorphic_adaptersr) rXr`r]Ztarget_attributesr_rarZ root_mapperr3rr6r6r7rsZ    z*JoinedLoader._init_user_defined_eager_proccCsV||}|r.|r.||}||jd|n|rF|}||jd||j}|||fS)Nr)Z_get_entity_clauseswraprrprimary_columns)rXrgrr_rarrr6r6r7rs"  z1JoinedLoader._setup_query_on_user_defined_adapterc  Cs||jdd} | r| } nDt|j} | jr2| j} nd} tj|j| dk rR| j ddndddd} t| } | j d|ftj tj j | |jjdddd}|jst|dk r|jd|jjn|jj}|sd}|j|j||||||||r|jnd f |j}||jd |||||fS) NrT)flat)rrrqZjoinedloader_ormadapterF)rZadapt_requiredZallow_label_resolveZanonymize_labels innerjoinr6eager_row_processor)rFrr&rrrr/r#rJZ_anonymous_fromclauseZ_memorrZJOINEDLOAD_MEMOIZED_ADAPTERrrrrVrZcreate_eager_joinsrB_create_eager_joinrrr)rXr]rr_r`rarbrrrZto_adaptZinspZalt_selectableZ to_adapt_insprrrr6r6r7r sh     z"JoinedLoader._generate_row_adapterc  s\|dkr|j} n|} |jo|j} d} ||jkr~| s~|jr~t|j|j} t| dkr^t d| r~|j| d}| d|} }| dkr||j} }|j | |}|rt |ddr|j | jr| nj}t |jjj}n t tjj|jddjj}nj}|jst| p8| p8|dkp8|jj}| }|jd jfd }|rr|tfd d |D7}|rtj||j || p|jjp|ot|tj j|j|d }n!|||||}||j| <|j|_"|s*t#jj$D]4}| j%j&'|r|r|j(|}|)||j*qjj+rX|j,t|j-.t/0jj+7_,dS)NrzCan't identify which query entity in which to joined eager load from. Please use an exact match when specifying the join path.rrFTrpZunnestedadditional_entity_criteriar6c3s |]}|jr|jVqdSr.)Zpropagate_to_loadersZ_resolve_where_criteriarJ)rZaerr6r7r sz2JoinedLoader._create_eager_join..isouter _left_memo _right_memor)1rJrZ_should_nest_selectableZ eager_joinsZ from_clausesrZ#find_left_clause_that_matches_givenrr,rr setdefaultr_ aliased_inspZ_entity_for_mapperrbrDrr0rVr/r#rrrdZrepresents_outer_joinZglobal_attributesrFr_ORMJoinrr(ZJoin_splice_nested_inner_joinZstop_onZ _find_columnsZ primaryjoinZpersist_selectablerdZcontains_columnrSr\rrr_target_adapterZcopy_and_processrr)rXr]r^r_rarrrrrrZshould_nest_selectableZquery_entity_keyZindexesZclauseZdefault_towrapZtowrapZefmonclauseZattach_on_outsideZextra_join_criteriar eagerjoinrr6rr7rY s            zJoinedLoader._create_eager_joinc Cs|dkrt|tjstnt|tjjr>|||j||||St|tjs|d |r|r| |sjdSt j |t|d |rdStj||j|d|||dj|dSdS|j||j||||jdk r|jdjndd}|dkr0d} |j||j||||j|jd}|dkr4|dk s*tddSnd} | rt|jsV|jsV||} ntj|j||j|j|jd } ntj||j|j|j|jd } |j| _| S) NFrr<r)splicing)detected_existing_pathz9assertion failed attempting to produce joined eager loadsT)rr)rr)rr/rrr(rZ FromGroupingrelementrbrFr r?rer,rrJrightrleftrrZ_splice_into_centerrr) rXr_Zjoin_objrrrrrZ target_joinZ right_splicerr6r6r7r s       z&JoinedLoader._splice_nested_inner_joinc Cs|j}|r||||jnd}|dk rP|}|jrB|rB||j}qj|jrj|j}n||jd}|dkrjdS|j||r||SdSdS)NFr)r]rrZcompound_eager_adapterrrFrJZ_result_has_identity_key) rXrgrhrar_r`r]r decoratorr6r6r7_create_eager_adapteri s4 z"JoinedLoader._create_eager_adapterc Cs|jjjsdS|jj|jjjs.t d||j r:d|_ ||j } | |||| |} | dk r|j} t||j||| |j| } |j s||| | |q||| | |n |j d||||||||dS)NrTFrN)r]rrPrDrGr0rrrrr:Zloaders_require_uniquingrVrr Z_instance_processorrJrrrrrj) rXrgr^r_r`rJrhrariZour_pathZ eager_adapterr0 _instancer6r6r7rj s`   z!JoinedLoader.create_row_processorcsrfdd}fdd}fdd}|d|j|f|d|j|fjrn|d |j|fdS) NcsFt||}t|d}|j|f<|}|dk rB||dSNZappend_without_eventrrrZUniqueAppenderrB)rrrr result_listinstrrgr0r6r7#load_collection_from_joined_new_row szSJoinedLoader._create_collection_loader..load_collection_from_joined_new_rowcsd|fjkrj|f}n(t||}t|d}|j|f<|}|dk r`||dSrr)rrrrrrrr6r7(load_collection_from_joined_existing_row szXJoinedLoader._create_collection_loader..load_collection_from_joined_existing_rowcs |dSr.r6rrr6r7 load_collection_from_joined_exec szPJoinedLoader._create_collection_loader..load_collection_from_joined_execrrrrBr0r)rXrgr0rrirrrr6rr7r s  z&JoinedLoader._create_collection_loadercspfdd}fdd}fdd}|dj|f|dj|f|jrl|d j|fdS) Ncs||<dSr.r6r)rr0r6r7load_scalar_from_joined_new_row szKJoinedLoader._create_scalar_loader..load_scalar_from_joined_new_rowcs8|}|kr,||k r4tdn||<dS)Nr)rr-)rrrrrr0rXr6r7$load_scalar_from_joined_existing_row s  zPJoinedLoader._create_scalar_loader..load_scalar_from_joined_existing_rowcs |dSr.r6rrr6r7load_scalar_from_joined_exec szHJoinedLoader._create_scalar_loader..load_scalar_from_joined_execrrrr)rXrgr0rrirrrr6rr7r s z"JoinedLoader._create_scalar_loader)NNF)FN)rkrlrmrnrorUr}rerrrrrrrjrrrpr6r6rZr7r-s(   w9O' q$>/rZselectinc s|eZdZdZedddddddgZd Zfd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZZS)SelectInLoader)rG omit_join _parent_alias _query_info_fallback_query_infoZ queryinfoload_only_childload_with_joinin_exprpk_colszero_idxchild_lookup_colsicst|||jj|_|jjtjk}|jjdk r>|jj|_n<|jd}|rX|j |_n"|j j dj |j dd|j jd|_|jr|r||_||_q||_n ||_dS)NrNrTFr)rTrUrVrGrIr rrrrrDrrrr_init_for_omit_join_m2or_init_for_joinr_init_for_omit_join)rXrDrYZis_m2orZrZr6r7rU9 s,        zSelectInLoader.__init__cstjjjfddtDfddjjD}}t|dkrbt j |}d}n |d}d} dd|||dS) Nc3s0|](}jj|dD]}||fVqqdS)r6N)rDrrF)rrequivpk_to_fkrXr6r7r[ sz5SelectInLoader._init_for_omit_join..csg|]}|kr|qSr6r6rr)rr6r7ra sz6SelectInLoader._init_for_omit_join..rFrT) rrVrZlocal_remote_pairsrurrDrzr,r(tuple_ query_info)rXr Zfk_colsrr r6rr7rW s    z"SelectInLoader._init_for_omit_joincsb|jj}t|dkr$tj|}d}n |d}d}|jdfdd|D}|dd||||S)NrFrTrNcsg|]}j|qSr6r rrZr6r7rw sz:SelectInLoader._init_for_omit_join_m2o..)rJrzr,r(rrVrr)rXr rr Z lookup_colsr6rr7r m s    z&SelectInLoader._init_for_omit_join_m2ocsjt|jj|_t|jfdd|jjD}t|dkrJtj|}d}n |d}d}| dd|||dS)Ncsg|]}|qSr6_adapt_elementrZpa_inspr6r7r sz1SelectInLoader._init_for_join..rFrT) r#rDrHrr&rzr,r(rr)rXr rr r6rr7r } s    zSelectInLoader._init_for_joincCs|jd|dSrMrOrr6r6r7r} s z#SelectInLoader.init_class_attributec  Cs|jr|||||||||S|j||||jd\} } } } | sBdS|jjjsPdS|jj|j j j spt d|t|dkrt|j|jsdSnt|d|jsdS| } ||j}||jdd}|dk rt|}n|j}tj|| |j|j|j||| | dS)NrKrrr<r)rrrIrGr]rrPrDrGr0rrrrr,r/rrdrVrFrr&rr rCrQrR)rXrgr^r_r`rJrhrarir/rSrr;Z selectin_pathrHrrr6r6r7rj st     z#SelectInLoader.create_row_processorc s|rj|krdSj} | jrtt} g} j|D]l\} jtfdd| j D} t j | krtj } qd| kr| |  | fq6| | fq6| jsdd|D} | j}| j}| jsjrfdd|D}|}tjd|}|}}tj||gttjddd }| js<|}n$|jtjjj }|!|"t#$d }j%j&}|j}|j'kr|j(}n&|j(}j'j(}fd dt)||D}|r|j*r|t+|,f7}|dk r|-}|j.|}|/d |i}j0r&|j1d d}jj2r| jsfjj2}jrZfdd|D}|j2|}nfdd}|3|j}| jr4| | | ||n5| | ||dS)Nc3s"|]}j|tjdVqdS)r N)rrZPASSIVE_NO_FETCH)rZlk)rJr state_dictr6r7r sz0SelectInLoader._load_for_path..cSs$g|]\}}|jd||j|fqS)r)r0r)rrr[r6r6r7r sz1SelectInLoader._load_for_path..csg|]}|qSr6rrrr6r7r% srr)Zcompile_state_pluginZplugin_subject)rrrr primary_keyscsg|]\}}||qSr6)Z'_adapt_cached_option_to_uncached_option)rZorig_optZ uncached_opt)rgr6r7rw s rT)r6csg|]}|qSr6rrrr6r7r scs |jttjj7_dSr.rrrr6r7r s z?SelectInLoader._load_for_path.._setup_outermost_orderby)r)6r0rrrrrrDrrr rrrrBr rrrrr/ZBundlerr+rr*rrr rrr_rVrfilterZin_r(rr]Zselect_statementrr$ziprr#r5rUrZ_update_compile_optionsr6rrr_load_via_child_load_via_parent)rXrgr_rXrYrr`r;rr our_states none_statesr[Z related_identr rZ bundle_entZ bundle_sqlZ entity_sqlrzrxr/rZcached_optionsZuncached_optionsrrr6)rgrrJrXrrr7rR s                     zSelectInLoader._load_for_pathcs|j}t|}|r|d|j} ||jd}dd|jj|dfdd| Di|dD} | D]T} | | d} || D]:\} }}|s|j|krq|| |j | ||s| n| gq|qdq|D]0\} }}|s|j|krq| |j | |dqdS)NrcSsi|]\}}||qSr6r6)rrrr6r6r7 sz2SelectInLoader._load_via_child..rcsg|]}jr|dn|qSr^r )rr0rr6r7r sz2SelectInLoader._load_via_child..rr) r:sorted _chunksizerr*rrFr0rVrW)rXr r!rrzrgrr:Zour_keyschunkdatar0Z related_objrrr[r6r$r7r s<     zSelectInLoader._load_via_childcs$|j}|rdnd}|r |d|j}||jd}fdd|D} tt} t|jj|d| i|d ddD] \} } | |  d d | Dqv|D]\} }}}|s|j |krq| | |}|s|rt |d krtd |||j |||dq||j |||qqdS) Nr6rcs&g|]\}}}}jr|dn|qSr^r#)rr0rrr[r$r6r7r s z3SelectInLoader._load_via_parent..rr%cSs|dSrur6rr6r6r7r8 rz1SelectInLoader._load_via_parent..css|]}|dVqdS)rNr6rr6r6r7r sz2SelectInLoader._load_via_parent..rr)r:r'rrrrrrr*rrr0rFr,rr-rVrW)rXr rrzrgrr:Z _empty_resultr(rr)rrr0rrr[rr6r$r7r sN         zSelectInLoader._load_via_parent)rkrlrmror namedtuplerr'rUrr r r}rjrRrrrpr6r6rZr7r s,  PT7rcsTfddfdd}fdd}tj|d|dddd tj|d |dddd dS) NcsT|dk rP|jjkrP|t|}|rP||k rPtjdt||jfdd|S)NzsInstance %s is already associated with an instance of %s via its %s attribute, and is only allowed a single parent.Zbbf1)code) r0 hasparentrZinstance_staterrr/Z instance_strrH)rroldvalue initiatorr,)r3r6r7 _do_check s z*single_parent_validator.._do_checkcs||d|Sr.r6)rrr.r/r6r7rB* sz'single_parent_validator..appendcs||||Sr.r6)rrr-r.r0r6r7set_- sz%single_parent_validator..set_rBT)rawretvalr?r)r%listen)r2r3rBr1r6)r/r3r7rC s  rC)NNNNFN)Qrn __future__rrrtypingrrrrrr rr r r r rrrrr/baserrrrrrrrgrrrrrrrrrr rr!r"r#r%rr&r'r(rr)Zsql.selectabler*r+r,Z sql.elementsr-rNZColumnPropertyZ strategy_forrQZ class_loggerrqrrrrrrZ MemoizedSlotsZ Identifiedrr4r:rJr]rrrCr6r6r6r7 s                                              Q+Z W 0%    &      +4L d   u }