U kfzr@sdZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddl"m#Z#ddl$m%Z%dd l$m&Z&d!d"lm'Z(d!d#lm)Z)d!d$lm*Z*d!d%lm$Z$d!d&l+m,Z,d!d'l+m-Z-d!d(l.m/Z/d!d)l0m1Z1d!d*l0m2Z2d!d+l3m4Z4d!d,l5m6Z6d!d-l5m7Z7d!d.l5m8Z8d!d/l5m9Z9d!d0l5m:Z:d!d1l5m;Z;d!d2l5mm?Z?dd5l>m@Z@dd6l>mAZAdd7l>mBZBdd8lmCZCdd9lDmEZEdd:lFmGZGdd;lHmIZIddlMmNZNd!d?lOmPZPd!d@lOmQZQd!dAlRmSZSd!dBl5mTZTd!dCl5mUZUedDedEZVedFedEZWedGdHdEZXdIdJdKdLdMgZYe*jZGdNdIdIeeVe!eVee*j[Z\GdOdPdPe\eVeeVZ]GdQdRdReeeVeeVZ^dSS)Tz|MapperProperty implementations. This is a private module which defines the behavior of individual ORM- mapped attributes. ) annotations)Any)cast)Dict)List)Optional)Sequence)Set)Tuple)Type) TYPE_CHECKING)TypeVar)Union) attributes)strategy_options)_DeclarativeMapped) class_mapper)CompositeProperty)ConcreteInheritedProperty)SynonymProperty)_AttributeOptions)_DEFAULT_ATTRIBUTE_OPTIONS)_IntrospectsAnnotations) _MapsColumns)MapperProperty)PropComparator)StrategizedProperty)RelationshipProperty)de_stringify_annotation)de_stringify_union_elements)exc) ForeignKey)log)util) coercions)roles)_NoArg)Column) SchemaConst) TypeEngine)de_optionalize_union_types) is_fwd_ref)is_optional_union) is_pep593) is_pep695)is_union)Self)typing_get_args)_IdentityKeyType) _InstanceDict)_ORMColumnExprArgument) _RegistryType)Mapped)_ClassScanMapperConfig)Mapper)Session)_InstallLoaderCallableProto) InstanceState) _InfoType) ColumnElement) NamedColumn) OperatorType)_AnnotationScanType)RODescriptorReference_T)bound_PT_NCzNamedColumn[Any]ColumnPropertyrrrrcseZdZUdZejZdZdZde d<de d<de d <d e d <d Z d d ddd ddd d ddd dddddddddddddd fddZ ddddddddddd dd Z e d!d"d#d$Ze d%d"d&d'Zdd"d(d)Zed*d+d,d"d-d.Zed*d+d,d"d/d0Zd1d"d2d3Ze d1d"d4d5Zd6dd7d8d9Zdd"fd:d; ZdZd?d@dAd@dAddBdCddD dEdFZGdGdHdHejeeZdd"dIdJZZ S)KrHzDescribes an object attribute that corresponds to a table column or other column expression. Public constructor is the :func:`_orm.column_property` function. TFzList[NamedColumn[Any]]columnsbool_is_polymorphic_discriminator Optional[str]_mapped_by_synonymzType[PropComparator[_T]]comparator_factory)rIgroupdeferred instrumentrNactive_historyexpire_on_flush_creation_orderrKrMZ_deferred_column_loaderZ_raise_column_loaderZ_renders_in_subqueries raiseloadN) attribute_optionsrOrPrUrNrRrSinfodoc _instrument_assume_readonly_dc_attributesz_ORMColumnExprArgument[_T]z_ORMColumnExprArgument[Any]zOptional[_AttributeOptions]z"Optional[Type[PropComparator[_T]]]zOptional[_InfoType]) columnadditional_columnsrVrOrPrUrNrRrSrWrXrYrZc stj|| d|f| }dd|D|_||_||_||_| |_|dk rN|n|jj|_ ||_ ||_ | dk rx|j | | dk r| |_n4t|jD]"}t|dd} | dk r| |_qqd|_t|d|jfd|jff|_|jr|jd7_dS)N)rVrZcSsg|]}ttj|qS)r&expectr'ZLabeledColumnExprRole.0cr]r]I/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/properties.py sz+ColumnProperty.__init__..rXrPrQ))rUT)super__init__rIrOrPrUrQ __class__ ComparatorrNrRrSrWupdaterXreversedgetattrr%set_creation_order strategy_key)selfr[rVrOrPrUrNrRrSrWrXrYrZr\rIcolrfr]rbresD    zColumnProperty.__init__r9r7 Type[Any]strOptional[Type[Mapped[Any]]]Optional[_AnnotationScanType]None decl_scanregistryclsoriginating_modulekeymapped_container annotationextracted_mapped_annotationis_dataclass_fieldreturnc Cs.|jd} | jdkr|| _| jdkr*|| _dSNr)rIrzname) rmrvrwrxryrzr{r|r}r~r[r]r]rbdeclarative_scans   zColumnProperty.declarative_scanOptional[MapperProperty[_T]]rcCs|SNr]rmr]r]rbmapper_property_to_assignsz(ColumnProperty.mapper_property_to_assignList[Tuple[Column[Any], int]]cCsdd|jDS)NcSs(g|] }t|tr|jdkr|dfqSr) isinstancer)tabler_r]r]rbrcs z4ColumnProperty.columns_to_assign..rIrr]r]rbcolumns_to_assignsz ColumnProperty.columns_to_assigncCs(d|jkr|jjSd|jkp&||jjkS)N)Zquery_expressionT)rPT)rlZstrategyZ_have_default_expressionparentZ_readonly_propsrr]r]rb%_memoized_attr__renders_in_subqueriess    z4ColumnProperty._memoized_attr__renders_in_subquerieszsqlalchemy.orm.statezsqlalchemy.orm.strategiesz _InstallLoaderCallableProto[Any]cCs.tjj}tjj}|j|jj||j |j Sr r% preloadedZ orm_stateZorm_strategiesr=Z"_instance_level_callable_processorrZ class_managerZLoadDeferredColumnsrzrmstateZ strategiesr]r]rb&_memoized_attr__deferred_column_loaders z5ColumnProperty._memoized_attr__deferred_column_loadercCs0tjj}tjj}|j|jj||j d|j S)NTrrr]r]rb#_memoized_attr__raise_column_loaders z2ColumnProperty._memoized_attr__raise_column_loaderzroles.ColumnsClauseRolecCs|jS)zsAllow the ColumnProperty to work in expression before it is turned into an instrumented attribute. ) expressionrr]r]rb__clause_element__sz!ColumnProperty.__clause_element__cCs |jdS)aReturn the primary column or expression for this ColumnProperty. E.g.:: class File(Base): # ... name = Column(String(64)) extension = Column(String(8)) filename = column_property(name + '.' + extension) path = column_property('C:/' + filename.expression) .. seealso:: :ref:`mapper_column_property_sql_expressions_composed` rrrr]r]rbrszColumnProperty.expressionz Mapper[Any])mapperrcCs0|js dStj|j|j|||||jddS)N)Z comparator parententityrX)rQrZregister_descriptorclass_rzrNrX)rmrr]r]rbinstrument_class%s zColumnProperty.instrument_classcsVtt|jdkrRt|jj|jrRt d|j|jd|jd|j fdS)NrzOn mapper %s, primary key column '%s' is being combined with distinct primary key column '%s' in attribute '%s'. Use explicit properties to give each column its own mapped attribute name.r) rddo_initlenrIsetrZ primary_key issupersetr%warnrzrror]rbr1s zColumnProperty.do_initzColumnProperty[_T]cCst|j|j|j|jdS)N)rPrOrR)rHrIrPrOrRrr]r]rbcopyAs zColumnProperty.copyr;zInstanceState[Any]r5zDict[Any, object]z#Dict[_IdentityKeyType[Any], object]) session source_state source_dict dest_state dest_dictload _recursive_resolve_conflict_maprc Cst|js dS|j|krL||j} |s.| ||j<qp||j} | ||| dn$|jrp|j|krp|j||jgdddS)NT)Z no_loader)rQrzZget_implrZ has_identityZ_expire_attributes) rmrrrrrrrrvalueimplr]r]rbmergeIs     zColumnProperty.mergec@seZdZUdZesdZded<ded<dddd d ZerHd d d dZd d ddZ dd ddZ dd ddZ dddddZ dddddddZ dddddd d!Zd"S)#zColumnProperty.ComparatoraProduce boolean, comparison, and other operators for :class:`.ColumnProperty` attributes. See the documentation for :class:`.PropComparator` for a brief overview. .. seealso:: :class:`.PropComparator` :class:`.ColumnOperators` :ref:`types_operators` :attr:`.TypeEngine.comparator_factory` )rrW expressionsz*RODescriptorReference[ColumnProperty[_PT]]propzSequence[NamedColumn[Any]]rrG)r[rcCsT|j}||||jjd}|}|jjr>|}|jj|}||d<||d|dS)aannotate and possibly adapt a column to be returned as the mapped-attribute exposed version of the column. The column in this context needs to act as much like the column in an ORM mapped context as possible, so includes annotations to give hints to various ORM functions as to the source entity of this column. It also adapts it to the mapper's with_polymorphic selectable if one is present. )Zentity_namespacerZ parentmapper proxy_keyZ adapt_columnZorm)Zcompile_state_pluginZplugin_subject)Z _parententityrrzZ _parentmapperZ_polymorphic_adaptertraverseZ _annotateZ_set_propagate_attrs)rmr[ZperrnZmapper_local_colr]r]rb_orm_annotate_columns  z.ColumnProperty.Comparator._orm_annotate_columnzNamedColumn[_PT]rcCsdSrr]rr]r]rbrz,ColumnProperty.Comparator.__clause_element__cCs4|jr||jjd|jjS||jjdSdSr)adapterrrIrzrrr]r]rb#_memoized_method___clause_element__sz=ColumnProperty.Comparator._memoized_method___clause_element__r>cCs2|}z|jWStk r,|jjYSXdS)z(The .info dictionary for this attribute.N)rrWAttributeErrorr)rmZcer]r]rb_memoized_attr_infos z-ColumnProperty.Comparator._memoized_attr_infocs6jrfddjjDSfddjjDSdS)zThe full sequence of columns referenced by this attribute, adjusted for any aliasing in progress. .. versionadded:: 1.3.17 csg|]}|jjqSr])rrrzr`rnrr]rbrcszHColumnProperty.Comparator._memoized_attr_expressions..csg|]}|qSr])rrrr]rbrcsN)rrrIrr]rrb_memoized_attr_expressionss  z4ColumnProperty.Comparator._memoized_attr_expressionsrqr)rzrcCst||S)zproxy attribute access down to the mapped column. this allows user-defined comparison methods to be accessed. )rjr)rmrzr]r]rb_fallback_getattrsz+ColumnProperty.Comparator._fallback_getattrrAColumnElement[Any]opotherkwargsrcOs||f||Srrrmrrrr]r]rboperatesz!ColumnProperty.Comparator.operatecKs|}|||||f|SrrZ _bind_paramrmrrrrnr]r]rbreverse_operatesz)ColumnProperty.Comparator.reverse_operateN)__name__ __module__ __qualname____doc__r __slots____annotations__rrrrrrrrr]r]r]rbrgcs  ( rgcCs.|jr |jst|St|jjjd|jS)N.)rrzobject__repr__rqrrrr]r]rb__str__s  zColumnProperty.__str__)!rrrrrZ _COLUMN_TOKENZstrategy_wildcard_key inherit_cacheZ_links_to_entityrrrerpropertyrrrr%preload_modulerrrrrrrrZ MemoizedSlotsrrFrgr __classcell__r]r]rorbrHasR .;     c@seZdZdZdZdS)MappedSQLExpressionaNDeclarative front-end for the :class:`.ColumnProperty` class. Public constructor is the :func:`_orm.column_property` function. .. versionchanged:: 2.0 Added :class:`_orm.MappedSQLExpression` as a Declarative compatible subclass for :class:`_orm.ColumnProperty`. .. seealso:: :class:`.MappedColumn` TN)rrrrrr]r]r]rbrs rc @s:eZdZUdZdZded<ded<ded<d ed <d ed <d ed<dddddZdddddZeddddZ eddddZ edddd Z d dd!d"Z d#ddd$d%d&d'Z d#ddd$d%d(d)Zddd*d+Zd,d-d.ddd/d0d0dd1d2 d3d4Zed5d-d.dddd6d1d7d8d9Zd.d-d6dd1d:d;d<Zd=S)> MappedColumna_Maps a single :class:`_schema.Column` on a class. :class:`_orm.MappedColumn` is a specialization of the :class:`_orm.ColumnProperty` class and is oriented towards declarative configuration. To construct :class:`_orm.MappedColumn` objects, use the :func:`_orm.mapped_column` constructor function. .. versionadded:: 2.0 ) r[rT _sort_order foreign_keys _has_nullable_has_insert_defaultrPdeferred_groupdeferred_raiseloadrR_attribute_options_has_dataclass_arguments_use_existing_columnzUnion[_NoArg, bool]rPrJrrLr Column[_T]r[zOptional[Set[ForeignKey]]rrrr)argkwcs"|dt|_|dd|_dk oJtkoJtfddtjD|_|dtj }|tj k |_ |j rx||d<nj tj k rj |d<|dd|_ |d d|_ |d tj |_|d d|_|d tj |_td t|||_|jj|_d|ko|ddtjfk|_t|dS)NrVZuse_existing_columnFc3s(|] \}}|dkr|tjk VqdS)dataclasses_defaultN)r(NO_ARG)r`iattrZ attr_optsr]rb 4sz(MappedColumn.__init__..insert_defaultdefaultrrrPrRZ sort_orderrnullable)poprrrany enumerate_fieldsrr(rrrrrrPrRrrr)r[rgetr*ZNULL_UNSPECIFIEDrr%rk)rmrrrr]rrbre*s<        zMappedColumn.__init__r2)rrcKs|j|j}|jjf||_|j|_|j|_|j|_|jj|_|j|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_t||Sr)rf__new__r[_copyrPrrrrRrrrrrrr%rk)rmrnewr]r]rbrQs  zMappedColumn._copyrqrcCs|jjSr)r[rrr]r]rbrbszMappedColumn.namercCsP|j}|tjkr t|jp|j}|s*|jrHt|j||j|j|j |jdSdSdS)N)rPrOrUrVrR) rPr(rrJrrrRrHr[r)rmZeffective_deferredr]r]rbrfs    z&MappedColumn.mapper_property_to_assignrcCs|j|jtjk r|jndfgSr)r[rr(rrr]r]rbrzs  zMappedColumn.columns_to_assigncCs|jSr)r[rr]r]rbrszMappedColumn.__clause_element__rArrcOs||f||Srrrr]r]rbrszMappedColumn.operatecKs|}|||||f|Srrrr]r]rbrszMappedColumn.reverse_operatecCstSr)rrr]r]rbfound_in_pep593_annotatedsz&MappedColumn.found_in_pep593_annotatedr9r7rprrrsrtruc Cs|j} |jr^|jr^|jr^|jr(tdt|jd} | jdk rD| jn|} | j j | | } |_| j dkrn|| _ | jdkr~|| _| j } |dkr| jr|jjs|||ndS|||||dS)Nz3Can't use use_existing_column with deferred mappersF)r[rZinheritsZsingleZ is_deferredsa_exc ArgumentErrorrrZ local_tablerarrztype_isnullrZ_raise_for_required_init_column_for_annotation)rmrvrwrxryrzr{r|r}r~r[Zsupercls_mapperZcolnamesqltyper]r]rbrs@     zMappedColumn.declarative_scanzsqlalchemy.orm.decl_baserB)rwrxryrz param_nameparam_annotationrcCs*tjj}|||j|||||dSr)r%rZ orm_decl_baseZ_undefer_column_namer[r)rmrwrxryrzrr decl_baser]r]rbdeclarative_scan_for_composites z+MappedColumn.declarative_scan_for_composite)rxrwargumentryrcCs|jj}t|tst|ddr:|dk s*tt|||dd}t|rZ|dk sNtt|||}t |}|j sp||j_ t |}d}|} t |r|j}t|rd} t|} | d} t | rt | } d}|j s||j_ | ddD]} t| tr| }qqnd} d} |dk r^|js"|jjdk r"d|j_|j|j|jj}|jtjk r\|jtjkr\|j|_|jdk r||jdkr||j|_|jdk r|jdkr|j|_|jr|jsd|_|jr|j|_|jdk r|jtjkr|j|_|jjdk s|jjdk r t dd|j!r^t"|j#j$D]<\}}|j#|tjk r |%d d }t d |d dq |j&r|jj'sd}| r| | g}n| g}|D] }|(|}|dk rqqt|t)st|trt*|t)rt+,d |jjd|dnt+,d|d|jjd|j-|dS)NT)Z check_generic)Zinclude_genericrrFz_Can't use the 'key' or 'name' arguments in Annotated with mapped_column(); this will be ignoredz2.0.22Z dataclasses_z Argument 'zp' is a dataclass argument and cannot be specified within a mapped_column() bundled inside of an Annotated objectzThe type provided inside the z4 attribute Mapped annotation is the SQLAlchemy type z . Expected a Python type insteadz6Could not locate SQLAlchemy Core type for Python type z inside the z attribute Mapped annotation).r[rrrqr-AssertionErrorrr1r r.rrr,r0 __value__r/r3rrr_mergerPr(rrrrrRrrzrr%Zwarn_deprecatedrrrrreplacerrZ _resolve_typer+ issubclassrrZ _set_type)rmrxrwrryrrZour_typeZ use_args_fromZour_original_typeZour_type_is_pep593Zpep_593_componentsZraw_pep_593_typeelemidxrZ new_sqltypeZchecksZ check_typer]r]rbrs                   z(MappedColumn._init_column_for_annotationN)rrrrrrrerrrrrrrrrrr%rrrr]r]r]rbrs0 '  2rN)_r __future__rtypingrrrrrrr r r r r rrrrbaserrZdescriptor_propsrrrZ interfacesrrrrrrrZ relationshipsrr%rr r"rr#r$sqlr&r'Zsql.baser(Z sql.schemar)r*Z sql.type_apir+Z util.typingr,r-r.r/r0r1r2r3Z_typingr4r5r6r7r8rr9rr:rr;rr<r=Z sql._typingr>Z sql.elementsr?r@Z sql.operatorsrArBrCrDrFrG__all__Z class_loggerZ IdentifiedrHrrr]r]r]rbs