U kf!W@sdZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!dd lm"Z"dd!l#m$Z$dd"l#m%Z%dd#l#m&Z&dd$l#m'Z'dd%l(m)Z)dd&l(m*Z*dd'l(m+Z+dd(l,m-Z-erbdd)lm.Z.dd*l/m0Z0dd+lm1Z1dd,lm2Z2dd-l3m4Z4dd.l3m5Z5dd/l6m7Z7dd0lm8Z8dd1l9m:Z:dd2l"m;Z;dd3lm?Z?dd5l>m@Z@ed6ed7ZAGd8d9d9eeAZBGd:d;d;ejCejDZEe!jFejGjHdd?d?ejIe!jJZKGd@dAdAZLGdBdCdCeeAZMGdDdEdEeMeAZNdFS)GzWrite-only collection API. This is an alternate mapped attribute style that only supports single-item collection mutation operations. To read the collection, a select() object must be executed each time. .. versionadded:: 2.0 ) annotations)Any) Collection)Dict)Generic)Iterable)Iterator)List)NoReturn)Optional)overload)Tuple)Type) TYPE_CHECKING)TypeVar)Union) bindparam) attributes) interfaces) relationships) strategies) NEVER_SET) object_mapper) PassiveFlag)RelationshipDirection)exc)inspect)log)util)delete)insert)select)update)Delete)Insert)Update)Literal)QueryableAttribute) _InstanceDict)AttributeEventToken)LoaderCallableStatus)_AdaptedCollectionProtocol)CollectionAdapter)Mapper)_RelationshipOrderByArg) InstanceState) AliasedClass) _Dispatch) FromClause)Select_T)boundc@seZdZUdZded<ded<ded<ded<d%d d d d d dddZeddddZeddddZddddZ dddddZ dd d d!d"Z dd d d#d$Z dS)&WriteOnlyHistoryzDOverrides AttributeHistory to receive append/remove events directly.zutil.OrderedIdentitySetunchanged_items added_items deleted_itemsbool_reconcile_collectionNWriteOnlyAttributeImplInstanceState[_T]rzOptional[WriteOnlyHistory[_T]]None)attrstatepassiveapply_toreturncCsp|rH|tj@r&td|d|d|j|_|j|_|j|_|j|_n$t |_t |_t |_d|_dS)Nz Attribute z can't load the existing state from the database for this operation; full iteration is not permitted. If this is a delete operation, configure passive_deletes=True on the z- relationship in order to resolve this error.F) rSQL_OKrInvalidRequestErrorr9r:r;r=r ZOrderedIdentitySet)selfrArBrCrDrIH/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/writeonly.py__init__Us     zWriteOnlyHistory.__init__zList[_T]rEcCst|j|jSN)listr:unionr9rHrIrIrJadded_plus_unchangedpsz%WriteOnlyHistory.added_plus_unchangedcCst|j|j|jSrM)rNr:rOr9r;rPrIrIrJ all_itemsts zWriteOnlyHistory.all_itemsattributes.HistorycCs`|jr0|j|j}|j|j}|j|}n|j|j|j}}}tt|t|t|SrM) r=r: differencer9r; intersectionrZHistoryrN)rHaddedZdeletedZ unchangedrIrIrJ as_history|s zWriteOnlyHistory.as_historyzUnion[int, slice]zUnion[List[_T], _T])indexrEcCst|j|SrM)rNr:)rHrXrIrIrJindexedszWriteOnlyHistory.indexedr6)valuerEcCs|j|dSrM)r:addrHrZrIrIrJ add_addedszWriteOnlyHistory.add_addedcCs(||jkr|j|n |j|dSrM)r:remover;r[r\rIrIrJ add_removeds zWriteOnlyHistory.add_removed)N) __name__ __module__ __qualname____doc____annotations__rKpropertyrQrRrWrYr]r_rIrIrIrJr8Ms  r8c seZdZUdZded<dZded<dZded<dZded<dZded<dZ ded <d Z d ed <e Z d ed<ded<ddddd ddfdd Z ejfdddddddZedZddd!d"d#d$d%d&Zed[ddd'dd#d$d(d&Zed\ddd)dd*d$d+d&Zd,ejfddd)dd*d$d-d&Zejd.d/d0d1Zejd.d/d2d3Zd]dddd4d5d6d7d8d9Zd^dddd4d5d6d7d:d;Zdddd?Zd,ejd,ddfdddd4ddddd6d@ dAdBZdddCdDdEdFZddddCdGdHdIZejfddddJddKdLZejfddddMddNdOZ dddTr< uses_objectsFdefault_accepts_scalar_loadersupports_population_supports_dynamic_iteration collectiondynamicrIr0order_byzType[WriteOnlyHistory[Any]]collection_history_clszType[WriteOnlyCollection[Any]] query_classz#Union[Type[Any], AliasedClass[Any]]strz"_Dispatch[QueryableAttribute[Any]]z Mapper[_T]r)class_keydispatch target_mapperrlkwc s4tj||d|f|||_t|_|r0t||_dSrM)superrKrsWriteOnlyCollectionrntuplerl)rHrprqrrrsrlrt __class__rIrJrKs zWriteOnlyAttributeImpl.__init__zInstanceState[Any]r*rz8Union[util.OrderedIdentitySet, WriteOnlyCollection[Any]])rBdict_rCrEcCs*|tj@s||tjjS|||SdSrM)rrF_get_collection_historyPASSIVE_NO_INITIALIZEr:rn)rHrBrzrCrIrIrJgets  zWriteOnlyAttributeImpl.get.z Literal[None]z Literal[PassiveFlag.PASSIVE_OFF]r.)rBrz user_datarCrEcCsdSrMrIrHrBrzr~rCrIrIrJget_collectionsz%WriteOnlyAttributeImpl.get_collectionr-cCsdSrMrIrrIrIrJrsz$Optional[_AdaptedCollectionProtocol]zIUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]cCsdSrMrIrrIrIrJrs NcCs4|tj@s|||j}n|||}|j}t|SrM)rrFr{r:rQDynamicCollectionAdapter)rHrBrzr~rCdatahistoryrIrIrJrs  zattributes.AttributeEventTokenrLcCst|tjSrM)rr+Z OP_APPENDrPrIrIrJ _append_tokensz$WriteOnlyAttributeImpl._append_tokencCst|tjSrM)rr+Z OP_REMOVErPrIrIrJ _remove_tokensz$WriteOnlyAttributeImpl._remove_tokenzOptional[AttributeEventToken]zOptional[WriteOnlyHistory[Any]]r@)rBrzrZ initiatorcollection_historyrEcCsd|dkr|||}|||jjD]}||||p8|j}q&|jr`|dk r`|t||ddSNT) _modified_eventr]rrappendr trackparent sethasparentrinstance_staterHrBrzrZrrfnrIrIrJfire_append_events   z(WriteOnlyAttributeImpl.fire_append_eventcCsd|dkr|||}|||jr@|dk r@|t||d|jjD]}||||pZ|jqHdS)NF) rr_rrrrrrr^rrrIrIrJfire_remove_event s   z(WriteOnlyAttributeImpl.fire_remove_eventzWriteOnlyHistory[Any])rBrzrEcCsH|j|jkr$|||tj|j|j<|||td||j<|j|jSr)rqcommitted_statermrPASSIVE_NO_FETCHrr)rHrBrzrIrIrJr s   z&WriteOnlyAttributeImpl._modified_event) rBrzrZrrC check_oldpop_adaptrEc Cs|r|j|jkrdS|r$|dkr$dS|} t| } |jrd|jsNtd|dt|j|||d} | ||} |js~| j } n | | j } | | } t|  | }| | }| D] }||kr|j|||d| dq|D]}|j|||d| dqdS)Nz Collection "zV" does not support implicit iteration; collection replacement operations can't be usedrC)r)Z parent_tokenrN has_identityrirrGr Z IdentitySetr}rr:rOrUrTrr)rHrBrzrZrrCrrriterable new_valuesZold_collectionr constants additionsZremovalsmemberrIrIrJset/sP      zWriteOnlyAttributeImpl.setr )argskwargsrEcOs tdSrMNotImplementedError)rHrrrIrIrJr!lszWriteOnlyAttributeImpl.delete)rBrzrZrEcCs tddS)Nz7Dynamic attributes don't support collection population.r)rHrBrzrZrIrIrJset_committed_valueosz*WriteOnlyAttributeImpl.set_committed_valuerScCs|||}|SrM)r{rWrHrBrzrCcrIrIrJ get_historyvs z"WriteOnlyAttributeImpl.get_historyz$List[Tuple[InstanceState[Any], Any]]cCs|||}dd|jDS)NcSsg|]}t||fqSrI)rr).0xrIrIrJ sz:WriteOnlyAttributeImpl.get_all_pending..)r{rRrrIrIrJget_all_pendings z&WriteOnlyAttributeImpl.get_all_pending)rBrCrEcCsT|j|jkr|j|j}n|||tj}|jrL|tj@rL|j||||dS|SdS)N)rD)rqrrmrrrZINIT_OK)rHrBrCrrIrIrJr{s z.WriteOnlyAttributeImpl._get_collection_history)rBrzrZrrCrEcCs||k r|||||dSrM)rrHrBrzrZrrCrIrIrJrszWriteOnlyAttributeImpl.appendcCs||k r|||||dSrM)rrrIrIrJr^szWriteOnlyAttributeImpl.removecCs|j|||||ddS)Nr)r^rrIrIrJrszWriteOnlyAttributeImpl.pop)..)..)..)N)N)&r`rarbrfrdrgrhrirjrkrlr8rmrKrZ PASSIVE_OFFr}r rr Zmemoized_propertyrrrrrrr!rrrr|rr{rr^r __classcell__rIrIrxrJr>sn            =   r>Z write_only)Zlazyc@s eZdZeZdddddZdS)WriteOnlyLoaderz Mapper[Any]r@)mapperrEc Cs\d|_|jr |jjtjtjfkr0td|jt j |j|d|j |jj |jj |jjddS)NTztOn relationship %s, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.)Z useobject impl_classrsrlrn)Zis_class_levelZuselistZparent_property directionr ONETOMANYZ MANYTOMANYrrGrZ_register_attributerrrlrn)rHrrIrIrJinit_class_attributes& z$WriteOnlyLoader.init_class_attributeN)r`rarbr>rrrIrIrIrJrsrc@s`eZdZUdZded<ddddZddd d Zd dd d ZddddZddddZ dS)rz9simplified CollectionAdapter for internal API consistencyzCollection[Any]rrcCs ||_dSrMr)rHrrIrIrJrKsz!DynamicCollectionAdapter.__init__z Iterator[Any]rLcCs t|jSrM)iterrrPrIrIrJ__iter__sz!DynamicCollectionAdapter.__iter__r@cCsdSrMrIrPrIrIrJ _reset_emptysz%DynamicCollectionAdapter._reset_emptyintcCs t|jSrM)lenrrPrIrIrJ__len__sz DynamicCollectionAdapter.__len__r<cCsdSrrIrPrIrIrJ__bool__sz!DynamicCollectionAdapter.__bool__N) r`rarbrcrdrKrrrrrIrIrIrJrs rc@sZeZdZUdZesdZded<ded<ddd d d Zd d dddZdd dddZ dS)AbstractCollectionWriterzsVirtual collection which includes append/remove methods that synchronize into the attribute event system. rIr6instancezTuple[FromClause, ...] _from_objr>r?)rArBcCs|}tr|st||_||_t|}|j|jj}|jdk rT|j |jf|_ nd|_ |j |ddf|_ |jjr|jj|_nd|_dS)NrIF)Zalias_secondary)objrAssertionErrorrrAr_propsrqZ secondaryrZ__clause_element__rZ _with_parent_where_criteriarl_order_by_clauses)rHrArBrrproprIrIrJrKs   z!AbstractCollectionWriter.__init__ Iterable[_T]r@iteratorrEcCs0|D]&}|jt|jt|j|dqdSrM)rArrrr instance_dict)rHritemrIrIrJ _add_all_impls  z&AbstractCollectionWriter._add_all_implrrEcCs&|jt|jt|j|ddSrM)rAr^rrrrrHrrIrIrJ _remove_impl"s   z%AbstractCollectionWriter._remove_implN) r`rarbrcr __slots__rdrKrrrIrIrIrJrs ! rc@seZdZdZdZddddZdddd Zd dd d Zd dddZddddZ dddddZ dddddZ dddddZ dS)rvaWrite-only collection which can synchronize changes into the attribute event system. The :class:`.WriteOnlyCollection` is used in a mapping by using the ``"write_only"`` lazy loading strategy with :func:`_orm.relationship`. For background on this configuration, see :ref:`write_only_relationship`. .. versionadded:: 2.0 .. seealso:: :ref:`write_only_relationship` )rrArrrr rLcCs tddS)NzWriteOnly collections don't support iteration in-place; to query for collection items, use the select() method to produce a SQL statement and execute it with session.scalars().) TypeErrorrPrIrIrJrDszWriteOnlyCollection.__iter__zSelect[Tuple[_T]]cCs<t|jjj|j}|jr&|j|j}|jr8|j|j}|S)zProduce a :class:`_sql.Select` construct that represents the rows within this instance-local :class:`_orm.WriteOnlyCollection`. ) r#rArswhererr select_fromrrl)rHstmtrIrIrJr#Ks   zWriteOnlyCollection.selectr&cCst|j}|j}|j|jj}|jtjk r4t di}|j D],\}}| |||j |}td|d||j<q>t|jjjf|S)aNFor one-to-many collections, produce a :class:`_dml.Insert` which will insert new rows in terms of this this instance-local :class:`_orm.WriteOnlyCollection`. This construct is only supported for a :class:`_orm.Relationship` that does **not** include the :paramref:`_orm.relationship.secondary` parameter. For relationships that refer to a many-to-many table, use ordinary bulk insert techniques to produce new objects, then use :meth:`_orm.AbstractCollectionWriter.add_all` to associate them with the collection. zWrite only bulk INSERT only supported for one-to-many collections; for many-to-many, use a separate bulk INSERT along with add_all().N)Z callable_)rrrrrArqrrrrrGZsynchronize_pairsZ_get_attr_w_warn_on_nonedictrr"rsvalues)rHrBrrrzlrrrIrIrJr"Ws"  zWriteOnlyCollection.insertr'cCst|jjj|jS)zProduce a :class:`_dml.Update` which will refer to rows in terms of this instance-local :class:`_orm.WriteOnlyCollection`. )r$rArsrrrPrIrIrJr$szWriteOnlyCollection.updater%cCst|jjj|jS)zProduce a :class:`_dml.Delete` which will refer to rows in terms of this instance-local :class:`_orm.WriteOnlyCollection`. )r!rArsrrrPrIrIrJr!szWriteOnlyCollection.deleterr@rcCs||dS)zAdd an iterable of items to this :class:`_orm.WriteOnlyCollection`. The given items will be persisted to the database in terms of the parent instance's collection on the next flush. Nr)rHrrIrIrJadd_allszWriteOnlyCollection.add_allr6rcCs||gdS)zAdd an item to this :class:`_orm.WriteOnlyCollection`. The given item will be persisted to the database in terms of the parent instance's collection on the next flush. NrrrIrIrJr[szWriteOnlyCollection.addcCs||dS)zRemove an item from this :class:`_orm.WriteOnlyCollection`. The given item will be removed from the parent instance's collection on the next flush. N)rrrIrIrJr^szWriteOnlyCollection.removeN) r`rarbrcrrr#r"r$r!rr[r^rIrIrIrJrv+s (  rvN)Orc __future__rtypingrrrrrrr r r r r rrrrZsqlalchemy.sqlrrrrrbaserrrrrrrr sqlr!r"r#r$Zsql.dmlr%r&r'Z util.typingr(r)Z_typingr*r+r, collectionsr-r.rr/r0rBr1r2eventr3Zsql.selectabler4r5r6r8ZHasCollectionAdapterZ AttributeImplr>Z class_loggerZRelationshipPropertyZ strategy_forZAbstractRelationshipLoaderZ IdentifiedrrrrvrIrIrIrJs                                                   I ' ?