U kf@sdZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZ ddlmZddl!m"Z"ddl#m$Z$ddl#m%Z%dd l#m&Z&dd!l'm(Z(ddl'mZ)dd"l*m+Z+dd#l*m,Z,dd$l*m-Z-dd%lm.Z.e r>dd&l/m0Z0dd'lm1Z1dd(l2m3Z3dd)l4m5Z5dd*l6m7Z7dd+l8m9Z9dd,l:m;Z;dd-lm?Z?dd/l#m@Z@dd0l'mAZAe d1ed2ZBe d3eCd2ZDeEZFeeGee eGeffZHd4d5d6d7d8d9ZIeJd:dzdd?d@eJd:d{dAdBdCdDdEdFdGZMdHdIdJdKdLdMdNdOZNdPdPdPdPdQej.ej.dQdQdR dHdSdTdUdVdWdXdCdYdZdCdCd[ d\d]ZOdPdPdPdPdPdQej.ej.dQdQd^ dHdSd_dUdVdWdXd`dCdYdZdCdCda dbdcZPd|dddeZQd}dfdgZRdhdiZSd~djdkZTdldmZUdndoZVdpdqZWdrdsZXdtduZYGdvdwdwZZdxdyZ[dPS)zprivate module containing functions used to convert database rows into object instances and associated state. the functions here are called primarily by Query, Mapper, as well as some of the attribute loading strategies. ) annotations)Any)Dict)Iterable)List)Mapping)Optional)Sequence)Tuple) TYPE_CHECKING)TypeVar)Union) attributes)exc) path_registry)_DEFER_FOR_STATE)_RAISE_FOR_STATE)_SET_DEFERRED_EXPIRED) PassiveFlag) FromStatement)ORMCompileState) QueryContext) _none_set) state_str)util) result_tuple)ChunkedIteratorResult) FrozenResult)SimpleResultMetaData)select) ForUpdateArg)LABEL_STYLE_TABLENAME_PLUS_COL) SelectState) EMPTY_DICT)_IdentityKeyType)LoaderCallableStatus) ORMOption)Mapper)Query)Session) InstanceState) CursorResult)_ExecuteOptions)Result)Select_T)bound_OzCursorResult[Any]rz Result[Any])cursorcontextreturnc st_jrdjj_n di_j}|j}jj oVt|j dkoV|j dj zFt t fddjj D\}}j rjsjrtdWn0tk rtW5QRXYnXdd ddd d d fd djj D}t|||d}fdd}jddrLt |dfdd}t||jjd} | jt|d| _jjrdd} d| f| _ j r| j | S)aReturn a :class:`.Result` given an ORM query context. :param cursor: a :class:`.CursorResult`, generated by a statement which came from :class:`.ORMCompileState` :param context: a :class:`.QueryContext` object :return: a :class:`.Result` object representing ORM results .. versionchanged:: 1.4 The instances() function now uses :class:`.Result` objects and has an all new interface. FTrrcsg|]}|qS)Z row_processor).0 query_entity)r5r4r7F/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/loading.py qszinstances..zCan't use yield_per with eager loaders that require uniquing or row buffering, e.g. joinedload() against collections or subqueryload(). Consider the selectinload() strategy for better flexibility in loading objects.cSstddS)Nz@Can't use the ORM yield_per feature in conjunction with unique()sa_excInvalidRequestError)entryr7r7r: _no_uniqueszinstances.._no_uniquelegacy uncertaincs6|sfdd}|SstSdfdd}|SdS)Ncs@rz t|WSYnXtddr0dndddS)Nz=Can't apply uniqueness to row tuple containing value of type z; z the values returned appear to bezthis datatype producesz non-hashable values)hashr=r>obj)datatyperCr7r:gos z,instances.._not_hashable..goFcs(s z t|WSYnXdt|S)NT)rDidrE)_use_idr7r:rHs )rI)rGrBrCrHr7)rJrGrCr: _not_hashables z instances.._not_hashablecsNg|]F}jrn6|js<|js$|jr<|jjjj|jdn |jrFtndqS)rAN) yield_perZuse_id_for_hashZ_non_hashable_valueZ_null_column_typecolumntype load_optionsZ_legacy_uniquingrIr8Zent)r@rKr5r7r:r;s  )Z_unique_filtersc3s|}i_|r |}|s(qn}rHdfdd|D}nfdd|D}r|rptj}jrtj}j|D]\}}||qqp|rĈjj||V|sqqdS)Nrcsg|] }|qSr7r7)r8rowprocr7r:r;sz-instances..chunks..cs"g|]tfddDqS)csg|] }|qSr7r7)r8rSrQr7r:r;sz8instances..chunks...)tuple)r8)processrTr:r;s) partialsZ fetchmanyZ _raw_all_rowslistpost_load_pathsitemsclearinvokeupdate)sizerLfetchrowsZtop_level_post_loadsZ post_loadspath post_load)r5r4 is_top_levelrV single_entityrRr:chunkss:       zinstances..chunksZprebuffer_rowsNcstSN)iter)r^) _prebufferedr7r:res)Zsource_supports_scalarsrawZdynamic_yield_per)filteredZis_single_entitycSstddS)Nz~The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collectionsr<rEr7r7r:require_uniquesz!instances..require_unique)! _new_runidrunidZtop_level_contextrY compile_stateZ_has_mapper_entitiesrOZ_only_return_tupleslen _entitiesZsupports_single_entityrXziprLZloaders_require_bufferingZloaders_require_uniquingr=r> ExceptionrZ safe_reraisecloser execution_optionsgetrr5Z_is_server_side _attributesuniondictZmulti_row_eager_loadersZ_unique_filter_state) r4r5rnrjlabelsextraZunique_filtersZ row_metadatareresultrkr7)r@rKrhr5r4rcrVrdr: instancesNs|       +2       r|zsqlalchemy.orm.contextTc stjj|r|jj|dd}|j}zd|_fddt|jD}dd|jD}t |dd|jD}g} | D]T} |D]<} | | dk r|j t | | t | | |iid| | <q| || q||| WS||_XdS) azMerge a :class:`_engine.FrozenResult` back into a :class:`_orm.Session`, returning a new :class:`_engine.Result` object with :term:`persistent` objects. See the section :ref:`do_orm_execute_re_executing` for an example. .. seealso:: :ref:`do_orm_execute_re_executing` :meth:`_engine.Result.freeze` :class:`_engine.FrozenResult` FrBcs g|]\}}t|jr|qSr7 isinstance _MapperEntityr8ie querycontextr7r:r;Is z'merge_frozen_result..cSsg|] }|jqSr7Z _label_namerPr7r7r:r;NscSsg|] }|jqSr7Z_extra_entitiesrPr7r7r:r;QsNloadZ _recursiveZ_resolve_conflict_map)r preloaded orm_context _autoflushORMSelectCompileState_create_entities_collection autoflush enumeraterprZ rewrite_rows_mergerinstance_state instance_dictappend with_new_rows) session statement frozen_resultrctxrmapped_entitieskeys keyed_tupler{newrowrr7rr:merge_frozen_result+s@      rz:func:`_orm.merge_result`zyThe function as well as the method on :class:`_orm.Query` is superseded by the :func:`_orm.merge_frozen_result` function.) alternativez Query[Any]z>Union[FrozenResult, Iterable[Sequence[Any]], Iterable[object]]boolz"Union[FrozenResult, Iterable[Any]])queryiteratorrr6c svtjj|jrt|tr4|}t|j}nd}j j |dd}j }zd_ | ojt |j dk}|rt|j djrfdd|D}nt|}nfd dt|j D}g}d d|j D} t| d d|j D} |D]\} t| } |D]<} | | dk rjt| | t| | iid | | <q|| | q|rZ||WSt|WSW5|_ XdS) zMerge a result into the given :class:`.Query` object's Session. See :meth:`_orm.Query.merge_result` for top-level documentation on this function. NTr}Frrc s,g|]$}jt|t|iidqS)r)rrrr)r8instance)rrr7r:r;sz merge_result..cs g|]\}}t|jr|qSr7r~rrr7r:r;s cSsg|] }|jqSr7rrPr7r7r:r;scSsg|] }|jqSr7rrPr7r7r:r;sr)rrrrrrrrgdatarrrrorprrXrrrrrrrr)rrrrrrrdr{rrrrQrrr7)rrrr: merge_resultgs^         rr+z Mapper[_O]z_IdentityKeyType[_O]rz)Union[LoaderCallableStatus, Optional[_O]])rmapperkeypassiver6cCs|j|}|dk rt|}|jr6|j|s6tjS|jr|tj @sLtj S|tj @sZ|Sz| ||Wn$t jk r||gYdSX|SdSdS)zqLook up the given key in the given session's identity map, check the object for expired state if found. N) identity_maprurrinheritsrisaZPASSIVE_CLASS_MISMATCHZexpiredZSQL_OKZPASSIVE_NO_RESULTZRELATED_OBJECT_OKZ _load_expiredorm_excObjectDeletedErrorZ_remove_newly_deleted)rrrrrstater7r7r:get_from_identitys"     rNF) rO refresh_statewith_for_updateonly_load_props no_autoflushbind_argumentsrtrequire_pk_colsis_user_refreshzUnion[Select, FromStatement]zOptional[_IdentityKeyType]zOptional[Sequence[ORMOption]]zOptional[InstanceState[Any]]zOptional[ForUpdateArg]zOptional[Iterable[str]]zMapping[str, Any]r.) rrrrOrrrrrrtrrc CsD|dk r|d} |d} nd} } t||| ||||| ||| | | d S)z.Load the given identity key from the database.Nrr rOrrridentity_tokenrrrtrr)load_on_pk_identity)rrrrOrrrrrrtrridentrr7r7r: load_on_idents& rrzOptional[Tuple[Any, ...]]z Optional[Any]) rrprimary_key_identityrOrrrrrrrtrrc  sL|} | }|jrt|dkr$tj}|jtjkr8tj }n|j}|dk r| j d}|j \}d|krfddt |j |D}t||}t|t|krtdt|ddif|_fdd t ||j D}nd}|dk rd}||_n| jdk rd}| j|_nd }| r|rs$td jj}fd d||D}|rdtd |djdjj}|D]"}||jrp||}qqprjr|dj j!i7}|j"j}t#|||||| d\}}||_d|_$|r|dd i7}tj%&| d|i} |j'||| | d()}z |*WSt+j,k rFYdSXdS)z6Load the given primary key identity from the database.NZplugin_subjectcs"h|]\}}|dkr|jqSrfr)r8colvalue _get_paramsr7r: 8sz&load_on_pk_identity..zofully NULL primary key identity cannot load any object. This condition may raise an error in a future release.Z _orm_adaptTcsi|]\}}|j|qSr7r)r8Zid_val primary_keyrr7r: Ksz'load_on_pk_identity..Fz9refresh_state is required when require_pk_cols is presentcs h|]}j|jr|qSr7)attrshistory has_changes)r8r)rr7r:rdsz7Please flush pending primary key changes on attributes z for mapper z! before proceeding with a refresh _current_path) version_checkrrrrrZ_sa_orm_load_options)paramsrtr)-Z_cloneZ_is_lambda_elementAssertionErrorrZdefault_load_options_compile_optionsr$Zdefault_select_compile_optionsrdefault_compile_optionsZ_propagate_attrs _get_clauserqrsql_utilZadapt_criterion_to_nullrorwarnZ_deep_annotateZ_where_criteriaZ_for_update_argr= ArgumentErrorrZ_primary_key_propkeys differencer>_propsZ_is_relationshiprwrO load_pathparentoptions_set_get_optionsZ _order_byr%Z merge_withexecuteuniquescalarsonerZ NoResultFound)rrrrOrrrrrrrtrrrqcompile_optionsrrZnonesrrZrefresh_state_prokeysrmppZnew_compile_optionsr{r7)rrr:rs            "         rc Cs|i}i} |r|| d<|r || d<|r4|| d<d|d<|rDt||d<|rP|| d<|r\|| d<| rh|| 7}|rt||7}||fS) NZ_version_checkZ_populate_existingZ_refresh_stateTZ_for_refresh_stateZ_only_load_propsZ_identity_tokenZ_is_user_refresh) frozenset) Z compile_optZload_optpopulate_existingrrrrrrrOr7r7r:rs(  rc Ks|r||} n|j} i} ||jd| |oBt|dkoB|dj} | D]6} |r\| j|kr\qH| j||||f||| | d| qH|dk r||jk r|r|j |}n|}| |dS)Nmemoized_setupsr)rcolumn_collectionZmemoized_populatorscheck_for_adapt) Z_iterate_polymorphic_propertiesZ_polymorphic_propertiessetrrois_aliased_classrsetuppolymorphic_oncolumnsr)rnrr9raadapterrZwith_polymorphicrpolymorphic_discriminatorkwZpoly_propertiesquick_populatorsrrpdr7r7r:_setup_entity_querys@    rcCstdt|fdS)NamLoading context for %s has changed within a load/refresh handler, suggesting a row refresh operation took place. If this event handler is expected to be emitting row refresh operations within an existing load or refresh operation, set restore_load_context=True when establishing the listener to ensure the context remains unchanged when the event handler completes.)rrr)rr7r7r:_warn_for_runid_changeds rc s jj} d f} || j| d} | dkrˆ j} dk rT|  fdd D} |jdt}g}ggggggd}dkr j}rfdd|D}||nd||d } | D]}||kr||}|t kr|d  |j |j fn|t kr|d  |j d fn|tkr6|d  |j |jfnnd}r`j|}|dk r`||d }|sr||d }|r|d  |j |fn||| ||q| |q|| j| | | d}dd|D| dD]}||| |qjjjjr jj|n|jjjp8 j t jjjt jjj tjjj! rtjjj! tj"tj#jj$j%j&j'rЈ j(}|dk r̈rj|}||nds| dk rd|jf}|jkrj|j)dkrj|j*d}nd} +|| }|r|| k rʈ dksHt,|j-r~g}|}|r|| k r| ||j.}qXn|g}|D]@}t/0|rqt1||| |}t/2|j3|||qt/4 rj dkr 5n d| d j6rt7j8nt7j fdd} j9r| ssfdd}t:|| |||| }|S)z]Produce a mapper level row processor callable which processes rows into mapped instances.gettersNc3s|]}j|VqdSrf)r)r8krr7r: Usz&_instance_processor..r)newquickdeferredexpireexistingeagercsg|]}j|qSr7)r)r8c)rr7r:r;lsz'_instance_processor..)cached_populatorstodoprimary_key_getterrrFrrcSsi|]\}}|t|qSr7)rX)r8rrr7r7r:rsz'_instance_processor..rloader))Zselectinload_polymorphicTentitiesrc sr,}|}|}|jk}d}d}n|f}|}|dk r|}|}|jk}| }d}rr|st |||nT|drdSd}d}d} j}|}|}||_|_|_ || }|krd}|s|r |rs|s|_ |_ t |||||| |r|j} |rrn|j j||j| krnt| rˆj||j| krt|n,r|j j| |jkrt||s|jrr r|| n ||r|dn|j} |jk}|r0| s0 drt||||| } |rr~|j} |j j|| |j| kr~t|||| rjr|d|S)NTFrr)rFrmru_validate_version_id class_managerZ new_instancerr session_idZ_add_unpresentrOr_populate_fullmanagerdispatchrrrrefreshmodifiedZ_commitZ _commit_all add_stateunloadedrW_populate_partialZinvoke_all_eagers) rQrrdict_isnewZ currentloadloaded_instance identitykeyZeffective_populate_existingZexisting_runidr to_load)r5identity_classrrris_not_primary_keyload_evtrloaded_as_persistentrrpersistent_evtr populatorsrbrpropagated_loader_options refresh_evtrefresh_identity_keyrrmrsession_identity_maprversion_id_getterr7r: _instance+s                     z&_instance_processor.._instancecs&|f}|ds|SdSdS)Nrr7)rQr)rrrrr7r: ensure_no_pks z)_instance_processor..ensure_no_pk);Z_identity_classrnrurZ _prop_set intersectionr%rZ _tuple_getterrrrZ_deferred_column_loaderrrZ_raise_column_loaderr_getterZcreate_row_processorrrZr current_pathrarrrZalways_refreshrrrrrrrrZhash_keyrmrrversion_id_colZstrategyZ local_optsZ_should_selectin_loadrZ is_mapperrPostLoad path_exists_load_subclass_via_incallable_for_pathr for_context_identity_key_from_stateallow_partial_pksr issupersetpolymorphic_map_decorate_polymorphic_switch) r9rr5r{rarrrr_polymorphic_fromrnZ getter_keyrpropsrrrZpk_colsproprgetterZ adapted_colr"roption_entitiesZselectin_load_viaZ _load_supersZ_endmost_mapperZ_selectinload_entityZ callable_rrr7)rr5rrrrrrrrrrrrrrbrrrrrrmrrrrr:_instance_processor+s\                       <!  r2csj|j|j}|jdk }tjjdk|js0|rD||\n j\fdd}|S)Nrc ssfdd|D}|sdS|j}|jr@|}|}n}}|f|j|f}j|} |jj| _| jd|ji7_|jr| j dd} |j | t fdd|Dd dS)Ncs$g|]\}}|jr||fqSr7)rZ+_would_selectin_load_only_from_given_mapper)r8svrr7r:r;s z:_load_subclass_via_in..do_load..rT)rcs,g|]$\}}r|jddn|jdqS)rrr)r8rZ load_attrs)zero_idxr7r:r;#s)Z primary_keys)rrZ _prepend_pathZ _with_optionsrrnrrrrtrrrxrrall) r5rastates load_onlyZeffective_entityZ orig_queryZenable_opt_lclZdisable_opt_lclrq2Z disable_optZ enable_optrr1rr5r7r:do_loads6       z&_load_subclass_via_in..do_load)rrroZ base_mapperrrZ_subclass_load_via_inZ_subclass_load_via_in_mapper)r5raentityZpolymorphic_fromr1Zpolymorphic_from_mapperZnot_against_basemostr;r7r:r:r%s    .r%c Cs|r|j|_|dD]\} } | ||| <q|r^|dD]$\} } || d| r6|j| q6n"|dD]\} } | rf|j| qf|dD]\} } | |||qnt||jkr||_|dD]\} } | |kr| ||| <q|dD]\} } | |||qn|dD]\} } | |||qdS)Nrrrr)rmpopexpired_attributesaddr) r5rQrr rrrrrrr0 set_callable populatorr7r7r:r-s.   rc Cs|s`|r.|dD]\}} ||kr| |||<q|j|} |dD]\}} || kr@| |||q@n|} | |j|<|dD]\}} || krv| |||<qv|dD],\}} || kr||d| r|j|q|dD]\}} || kr| |||q|dD]\}} ||kr| |||q| S)Nrrrrr)rWr=r>r?) r5rQrr rrr rrr0rrAr@r7r7r:r fs4   r c CsD||||j||kr@tdt|||||j||fdS)NzWInstance '%s' has version id '%s' which does not match database-loaded version id '%s'.)Z_get_state_attr_by_columnr"rZStaleDataErrorr)rrr rQr0r7r7r:rs&rc sr|dk r|njdkr Sr.j fdd} t|  fdd} | S)Nc sdzj|}Wn tk r.td|Yn2X|kr.configure_subclass_mappercs|}|dk rf|}|r$||S|dkr\|}|rVtd|j|fqddSq|Sn$|}|rtd|fndSdS)NFzRow with identity key %s can't be loaded into an object; the polymorphic discriminator column '%s' refers to %s, which is not a sub-mapper of the requested %szjRow with identity key %s can't be loaded into an object; the polymorphic discriminator column '%s' is NULL)r=r>r+)rQrCrr)rr0 instance_fnrpolymorphic_instancesrr7r:polymorphic_instances6  z:_decorate_polymorphic_switch..polymorphic_instance)rrrZ PopulateDictr ) rEr5r9rr{rarrrrDrGr7) rr5rr0rErrarFrr9r{r:r,s    &r,c@sPeZdZdZdZddZddZddZed d Z ed d Z ed dZ dS)r#z4Track loaders and states for "post load" operations.)loadersr7 load_keyscCsi|_t|_d|_dSrf)rHr OrderedDictr7rI)selfr7r7r:__init__s zPostLoad.__init__cCs||j|<dSrf)r7)rKr overwriter7r7r:r szPostLoad.add_statec st|js dStj|}|jD]D\}}}}}fdd|jD}|r |||||jf||q |jdS)Ncs&g|]\}}|jjr||fqSr7)rrr)r8rrMlimit_to_mapperr7r:r;sz#PostLoad.invoke..) r7rZ PathRegistryZcoercerHvaluesrZrIr[) rKr5raZeffective_contexttokenrargrr7r7rNr:r\s4  zPostLoad.invokecCs$|j|j}|dk r |r ||_|Srf)rYrurarI)clsr5rarplr7r7r:r's zPostLoad.for_contextcCs|j|jko||j|jjkSrf)rarYrH)rKr5rarr7r7r:r$$s zPostLoad.path_existsc OsF|j|jkr|j|j}nt}|j|j<||||||f|j|<dSrf)rarYr#rH) rSr5rarOrQZloader_callablerRrrTr7r7r:r&+s zPostLoad.callable_for_pathN) __name__ __module__ __qualname____doc__ __slots__rLr r\ classmethodr'r$r&r7r7r7r:r#s  r#c s8|j}|stdt|t|tj@}|r>|j }j r|j s| ||}|dk r|t |}t||d|||dSt|j}|r|j} n<fddjD} |j| rtdt||} t| rވjrt| rtdt|dSt|tt| |||d} |r4| dkr4t|dS) z4initiate a column-based attribute refresh operation.zQInstance %s is not bound to a Session; attribute refresh operation cannot proceedN)rrrcsg|]}j|jqSr7)Z_columntopropertyr)r8rrr7r:r;psz*load_scalar_attributes..z_Instance %s cannot be refreshed - it's not persistent and does not contain a full primary key.zwInstance %s to be refreshed doesn't contain a full primary key - can't be refreshed (and shouldn't be expired, either).)rrr)rrZDetachedInstanceErrorrrrZ NO_AUTOFLUSHrrrrZconcreteZ_optimized_get_statementrrrrr>r=r>r(rissubsetr)r*rZ warn_limitedr!Zset_label_styler#r) rrZattribute_namesrrrrstmtZhas_keyZ identity_keyZpk_attrsr{r7rr:load_scalar_attributes=sp         r])T)T)NNNNNN)NNN)NNNN)\rX __future__rtypingrrrrrrr r r r r rrrrbaserrrrr5rrrrrrr=ZenginerZ engine.resultrrr sqlr!rZsql.selectabler"r#r$r%Z_typingr&r'Z interfacesr(rr)rr*rr+rr,Z engine.cursorr-Zengine.interfacesr.r/r0r1objectr3counterrlstrZ_PopulatorDictr|Zpreload_modulerZbecame_legacy_20rrrrrrrr2r%rr rr,r#r]r7r7r7r: s                                                 ^ ;T+(,*< ) 7 @E9%YH