o
    Šåg!W  ã                   @  sü  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! dd lm"Z" dd!l#m$Z$ dd"l#m%Z% dd#l#m&Z& dd$l#m'Z' dd%l(m)Z) dd&l(m*Z* dd'l(m+Z+ dd(l,m-Z- e�r1dd)lm.Z. dd*l/m0Z0 dd+lm1Z1 dd,lm2Z2 dd-l3m4Z4 dd.l3m5Z5 dd/l6m7Z7 dd0lm8Z8 dd1l9m:Z: dd2l"m;Z; dd3l<m=Z= dd4l>m?Z? dd5l>m@Z@ ed6ed7�ZAG d8d9„ d9eeA ƒZBG d:d;„ d;ejCejDƒZEe!jFejGjHd<d=�G d>d?„ d?ejIe!jJƒƒƒZKG d@dA„ dAƒZLG dBdC„ dCeeA ƒZMG dDdE„ dEeMeA ƒZNdFS )GzèWrite-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                   @  s„   e Zd ZU dZded< ded< ded< ded< 	d(d)dd„Zed*dd„ƒZed*dd„ƒZd+dd„Z	d,d d!„Z
d-d$d%„Zd-d&d'„ZdS ).ÚWriteOnlyHistoryzDOverrides AttributeHistory to receive append/remove events directly.zutil.OrderedIdentitySetÚunchanged_itemsÚadded_itemsÚdeleted_itemsÚboolÚ_reconcile_collectionNÚattrÚWriteOnlyAttributeImplÚstateúInstanceState[_T]Úpassiver   Úapply_toúOptional[WriteOnlyHistory[_T]]ÚreturnÚNonec                 C  sr   |r%|t j@ rt d|› d|› d�¡‚|j| _|j| _|j| _|j| _d S t 	¡ | _t 	¡ | _t 	¡ | _d| _d S )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)
r   ÚSQL_OKr   ÚInvalidRequestErrorr9   r:   r;   r=   r    ÚOrderedIdentitySet)Úselfr>   r@   rB   rC   © rK   úT/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/orm/writeonly.pyÚ__init__U   s   
ýÿ



zWriteOnlyHistory.__init__úList[_T]c                 C  s   t | j | j¡ƒS ©N)Úlistr:   Úunionr9   ©rJ   rK   rK   rL   Úadded_plus_unchangedp   s   z%WriteOnlyHistory.added_plus_unchangedc                 C  s   t | j | j¡ | j¡ƒS rO   )rP   r:   rQ   r9   r;   rR   rK   rK   rL   Ú	all_itemst   s
   ÿÿzWriteOnlyHistory.all_itemsúattributes.Historyc                 C  s`   | j r| j | j¡}| j | j¡}| j |¡}n| j| j| j}}}t t|ƒt|ƒt|ƒ¡S rO   )	r=   r:   Ú
differencer9   r;   Úintersectionr   ÚHistoryrP   )rJ   ÚaddedÚdeletedÚ	unchangedrK   rK   rL   Ú
as_history|   s   
ýzWriteOnlyHistory.as_historyÚindexúUnion[int, slice]úUnion[List[_T], _T]c                 C  s   t | jƒ| S rO   )rP   r:   )rJ   r]   rK   rK   rL   Úindexed‰   s   zWriteOnlyHistory.indexedÚvaluer6   c                 C  s   | j  |¡ d S rO   )r:   Úadd©rJ   ra   rK   rK   rL   Ú	add_addedŒ   s   zWriteOnlyHistory.add_addedc                 C  s*   || j v r| j  |¡ d S | j |¡ d S rO   )r:   Úremover;   rb   rc   rK   rK   rL   Úadd_removed�   s   
zWriteOnlyHistory.add_removedrO   )
r>   r?   r@   rA   rB   r   rC   rD   rE   rF   )rE   rN   )rE   rU   )r]   r^   rE   r_   )ra   r6   rE   rF   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rM   ÚpropertyrS   rT   r\   r`   rd   rf   rK   rK   rK   rL   r8   M   s    
 û


r8   c                      s²  e Zd ZU dZded< dZded< dZded< dZded< dZded< dZ	ded	< d
Z
ded< eZded< ded< da‡ fdd„Zejfdbd%d&„Ze	'	'dcddd,d-„ƒZe	'	'dcded/d-„ƒZe	'	'dcdfd2d-„ƒZd3ejfdfd4d-„Zejdgd6d7„ƒZejdgd8d9„ƒZ	3dhdid@dA„Z	3dhdidBdC„ZdjdEdF„Zd3ejd3ddfdkdJdK„ZdldOdP„ZdmdQdR„ZejfdndTdU„ZejfdodWdX„Z dpdYdZ„Z!ejfdqd[d\„Z"ejfdqd]d^„Z#ejfdqd_d`„Z$‡  Z%S )rr?   Tr<   Úuses_objectsFÚdefault_accepts_scalar_loaderÚsupports_populationÚ_supports_dynamic_iterationÚ
collectionÚdynamicrK   r0   Úorder_byzType[WriteOnlyHistory[Any]]Úcollection_history_clszType[WriteOnlyCollection[Any]]Úquery_classÚclass_ú#Union[Type[Any], AliasedClass[Any]]ÚkeyÚstrÚdispatchú"_Dispatch[QueryableAttribute[Any]]Útarget_mapperú
Mapper[_T]Úkwr   c                   s<   t ƒ j||d |fi |¤Ž || _t| _|rt|ƒ| _d S d S rO   )ÚsuperrM   r|   ÚWriteOnlyCollectionru   Útuplers   )rJ   rv   rx   rz   r|   rs   r~   ©Ú	__class__rK   rL   rM   ¤   s   	ÿzWriteOnlyAttributeImpl.__init__r@   úInstanceState[Any]Údict_r*   rB   r   rE   ú8Union[util.OrderedIdentitySet, WriteOnlyCollection[Any]]c                 C  s&   |t j@ s|  |t j¡jS |  | |¡S rO   )r   rG   Ú_get_collection_historyÚPASSIVE_NO_INITIALIZEr:   ru   )rJ   r@   r…   rB   rK   rK   rL   Úget³   s   
ÿþzWriteOnlyAttributeImpl.get.Ú	user_dataúLiteral[None]ú Literal[PassiveFlag.PASSIVE_OFF]r.   c                 C  ó   d S rO   rK   ©rJ   r@   r…   rŠ   rB   rK   rK   rL   Úget_collectionÀ   ó   z%WriteOnlyAttributeImpl.get_collectionr-   c                 C  r�   rO   rK   rŽ   rK   rK   rL   r�   É   r�   ú$Optional[_AdaptedCollectionProtocol]úIUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  r�   rO   rK   rŽ   rK   rK   rL   r�   Ò   s   	Nc                 C  s:   |t j@ s|  ||¡j}t|ƒS |  ||¡}|j}t|ƒS rO   )r   rG   r‡   r:   rS   ÚDynamicCollectionAdapter)rJ   r@   r…   rŠ   rB   ÚdataÚhistoryrK   rK   rL   r�   Ý   s   

þúattributes.AttributeEventTokenc                 C  ó   t  | t j¡S rO   )r   r+   Ú	OP_APPENDrR   rK   rK   rL   Ú_append_tokenî   ó   z$WriteOnlyAttributeImpl._append_tokenc                 C  r—   rO   )r   r+   Ú	OP_REMOVErR   rK   rK   rL   Ú_remove_tokenô   rš   z$WriteOnlyAttributeImpl._remove_tokenra   Ú	initiatorúOptional[AttributeEventToken]Úcollection_historyúOptional[WriteOnlyHistory[Any]]rF   c                 C  sl   |d u r
|   ||¡}| |¡ | jjD ]}||||p| jƒ}q| jr2|d ur4|  t |¡|d¡ d S d S d S ©NT)	Ú_modified_eventrd   rz   Úappendr™   ÚtrackparentÚsethasparentr   Úinstance_state©rJ   r@   r…   ra   r�   rŸ   ÚfnrK   rK   rL   Úfire_append_eventú   s   
ÿz(WriteOnlyAttributeImpl.fire_append_eventc                 C  sd   |d u r
|   ||¡}| |¡ | jr |d ur |  t |¡|d¡ | jjD ]}||||p-| jƒ q$d S )NF)	r¢   rf   r¤   r¥   r   r¦   rz   re   rœ   r§   rK   rK   rL   Úfire_remove_event  s   
ÿz(WriteOnlyAttributeImpl.fire_remove_eventúWriteOnlyHistory[Any]c                 C  sH   | j |jvr|  | |tj¡|j| j < | || t¡ d|| j < |j| j  S r¡   )rx   Úcommitted_statert   r   ÚPASSIVE_NO_FETCHr¢   r   )rJ   r@   r…   rK   rK   rL   r¢      s   ÿ
z&WriteOnlyAttributeImpl._modified_eventÚ	check_oldÚpopÚ_adaptc	                 C  sö   |r
|j | j u r
d S |r|d u rd S |}	t|	ƒ}
|jr2| js't d| › d�¡‚t | j|||d�¡}|  	||¡}|js?|j
}n| |j
¡}| |
¡}t |
¡ |¡}| |¡}|
D ]}||v ri| j|||d |d� qY|D ]}| j|||d |d� qld S )NzCollection "zV" does not support implicit iteration; collection replacement operations can't be used©rB   )rŸ   )Úparent_tokenrP   Úhas_identityrp   r   rH   r    ÚIdentitySetr‰   r¢   r:   rQ   rW   rV   r©   rª   )rJ   r@   r…   ra   r�   rB   r®   r¯   r°   ÚiterableÚ
new_valuesÚold_collectionrŸ   Ú	constantsÚ	additionsÚremovalsÚmemberrK   rK   rL   Úset/  sT   
ÿÿÿ

û€ûÿzWriteOnlyAttributeImpl.setÚargsÚkwargsr
   c                 O  s   t ƒ ‚rO   ©ÚNotImplementedError)rJ   r½   r¾   rK   rK   rL   r!   l  s   zWriteOnlyAttributeImpl.deletec                 C  ó   t dƒ‚)Nz7Dynamic attributes don't support collection population.r¿   )rJ   r@   r…   ra   rK   rK   rL   Úset_committed_valueo  s   ÿz*WriteOnlyAttributeImpl.set_committed_valuerU   c                 C  s   |   ||¡}| ¡ S rO   )r‡   r\   ©rJ   r@   r…   rB   ÚcrK   rK   rL   Úget_historyv  s   z"WriteOnlyAttributeImpl.get_historyú$List[Tuple[InstanceState[Any], Any]]c                 C  s   |   ||¡}dd„ |jD ƒS )Nc                 S  s   g | ]	}t  |¡|f‘qS rK   )r   r¦   )Ú.0ÚxrK   rK   rL   Ú
<listcomp>†  s    z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)r‡   rT   rÃ   rK   rK   rL   Úget_all_pending  s   z&WriteOnlyAttributeImpl.get_all_pendingc                 C  sP   | j |jv r|j| j  }n|  | |tj¡}|jr&|tj@ r&| j| |||d�S |S )N)rC   )rx   r¬   rt   r   r­   r³   ÚINIT_OK)rJ   r@   rB   rÄ   rK   rK   rL   r‡   ˆ  s   ÿÿz.WriteOnlyAttributeImpl._get_collection_historyc                 C  ó    || ur|   ||||¡ d S d S rO   )r©   ©rJ   r@   r…   ra   r�   rB   rK   rK   rL   r£   š  ó   ÿzWriteOnlyAttributeImpl.appendc                 C  rÌ   rO   )rª   rÍ   rK   rK   rL   re   ¥  rÎ   zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d� d S )Nr±   )re   rÍ   rK   rK   rL   r¯   °  s   zWriteOnlyAttributeImpl.pop)rv   rw   rx   ry   rz   r{   r|   r}   rs   r0   r~   r   )r@   r„   r…   r*   rB   r   rE   r†   )..)
r@   r„   r…   r*   rŠ   r‹   rB   rŒ   rE   r.   )
r@   r„   r…   r*   rŠ   r-   rB   r   rE   r.   )
r@   r„   r…   r*   rŠ   r‘   rB   r   rE   r’   )rE   r–   rO   )r@   r„   r…   r*   ra   r   r�   rž   rŸ   r    rE   rF   )r@   r„   r…   r*   rE   r«   )r@   r„   r…   r*   ra   r   r�   rž   rB   r   r®   r   r¯   r<   r°   r<   rE   rF   )r½   r   r¾   r   rE   r
   )r@   r„   r…   r*   ra   r   rE   r
   )r@   r„   r…   r*   rB   r   rE   rU   )r@   r„   r…   r*   rB   r   rE   rÆ   )r@   r„   rB   r   rE   r«   )r@   r„   r…   r*   ra   r   r�   rž   rB   r   rE   rF   )&rg   rh   ri   rm   rk   rn   ro   rp   rq   rr   rs   r8   rt   rM   r   ÚPASSIVE_OFFr‰   r   r�   r    Úmemoized_propertyr™   rœ   r©   rª   r¢   r¼   r!   rÂ   r­   rÅ   rˆ   rÊ   r‡   r£   re   r¯   Ú__classcell__rK   rK   r‚   rL   r?   –   sp   
 üûûûûúú
÷
=
üü
	úúúr?   Ú
write_only)Úlazyc                   @  s   e Zd ZeZddd„ZdS )	ÚWriteOnlyLoaderÚmapperúMapper[Any]rE   rF   c              	   C  s\   d| _ | jr| jjtjtjfvrt d| j ¡‚t	j
| j|d| j| jj| jj| jjd� d S )NTztOn relationship %s, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.)Ú	useobjectÚ
impl_classr|   rs   ru   )Úis_class_levelÚuselistÚparent_propertyÚ	directionr   Ú	ONETOMANYÚ
MANYTOMANYr   rH   r   Ú_register_attributerØ   rÕ   rs   ru   )rJ   rÕ   rK   rK   rL   Úinit_class_attributeÀ  s&   þþÿ
ùz$WriteOnlyLoader.init_class_attributeN)rÕ   rÖ   rE   rF   )rg   rh   ri   r?   rØ   rà   rK   rK   rK   rL   rÔ   »  s    rÔ   c                   @  sL   e Zd ZU dZded< ddd„Zddd	„Zddd„Zddd„Zddd„Z	dS )r“   z9simplified CollectionAdapter for internal API consistencyúCollection[Any]r”   c                 C  s
   || _ d S rO   )r”   )rJ   r”   rK   rK   rL   rM   Ü  ó   
z!DynamicCollectionAdapter.__init__rE   úIterator[Any]c                 C  ó
   t | jƒS rO   )Úiterr”   rR   rK   rK   rL   Ú__iter__ß  râ   z!DynamicCollectionAdapter.__iter__rF   c                 C  r�   rO   rK   rR   rK   rK   rL   Ú_reset_emptyâ  ó   z%DynamicCollectionAdapter._reset_emptyÚintc                 C  rä   rO   )Úlenr”   rR   rK   rK   rL   Ú__len__å  râ   z DynamicCollectionAdapter.__len__r<   c                 C  s   dS r¡   rK   rR   rK   rK   rL   Ú__bool__è  rè   z!DynamicCollectionAdapter.__bool__N)r”   rá   )rE   rã   )rE   rF   )rE   ré   )rE   r<   )
rg   rh   ri   rj   rk   rM   ræ   rç   rë   rì   rK   rK   rK   rL   r“   ×  s   
 



r“   c                   @  sH   e Zd ZU dZesdZded< ded< ddd„Zddd„Zddd„Z	dS )ÚAbstractCollectionWriterzsVirtual collection which includes append/remove methods that synchronize
    into the attribute event system.

    rK   r6   ÚinstancezTuple[FromClause, ...]Ú	_from_objr>   r?   r@   rA   c                 C  sŒ   |  ¡ }tr
|s
J ‚|| _|| _t|ƒ}|j| jj }|jd ur*|j 	¡ |jf| _
nd| _
|j|dd�f| _| jjrA| jj| _d S d| _d S )NrK   F)Úalias_secondary)Úobjr   rî   r>   r   Ú_propsrx   Ú	secondaryrÕ   Ú__clause_element__rï   Ú_with_parentÚ_where_criteriars   Ú_order_by_clauses)rJ   r>   r@   rî   rÕ   ÚproprK   rK   rL   rM   ø  s   

ÿ
z!AbstractCollectionWriter.__init__ÚiteratorúIterable[_T]rE   rF   c                 C  s0   |D ]}| j  t | j¡t | j¡|d ¡ qd S rO   )r>   r£   r   r¦   rî   Úinstance_dict)rJ   rù   ÚitemrK   rK   rL   Ú_add_all_impl  s   

üÿz&AbstractCollectionWriter._add_all_implrü   c                 C  s&   | j  t | j¡t | j¡|d ¡ d S rO   )r>   re   r   r¦   rî   rû   ©rJ   rü   rK   rK   rL   Ú_remove_impl"  s   

üz%AbstractCollectionWriter._remove_implN)r>   r?   r@   rA   ©rù   rú   rE   rF   ©rü   r6   rE   rF   )
rg   rh   ri   rj   r   Ú	__slots__rk   rM   rý   rÿ   rK   rK   rK   rL   rí   ì  s   
 

!	rí   c                   @  sd   e Zd ZdZdZddd„Zd dd	„Zd!dd„Zd"dd„Zd#dd„Z	d$dd„Z
d%dd„Zd%dd„ZdS )&r€   a�  Write-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`

    )rî   r>   rö   rï   r÷   rE   r
   c                 C  rÁ   )Nz°WriteOnly 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().)Ú	TypeErrorrR   rK   rK   rL   ræ   D  s   ÿzWriteOnlyCollection.__iter__úSelect[Tuple[_T]]c                 C  s<   t | jjƒj| jŽ }| jr|j| jŽ }| jr|j| jŽ }|S )z‘Produce a :class:`_sql.Select` construct that represents the
        rows within this instance-local :class:`_orm.WriteOnlyCollection`.

        )	r#   r>   r|   Úwhererö   rï   Úselect_fromr÷   rs   )rJ   ÚstmtrK   rK   rL   r#   K  s   zWriteOnlyCollection.selectr&   c                 C  s„   t | jƒ}|j}|j| jj }|jtjurt	 
d¡‚i }|jD ]\}}| |||j|¡}td|d�||j< qt| jjƒjdi |¤ŽS )aN  For 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.


        z…Write only bulk INSERT only supported for one-to-many collections; for many-to-many, use a separate bulk INSERT along with add_all().N)Ú	callable_rK   )r   rî   rÕ   rò   r>   rx   rÜ   r   rÝ   r   rH   Úsynchronize_pairsÚ_get_attr_w_warn_on_noneÚdictr   r"   r|   Úvalues)rJ   r@   rÕ   rø   r…   ÚlÚrr¨   rK   rK   rL   r"   W  s"   
ÿüzWriteOnlyCollection.insertr'   c                 C  ó   t | jjƒj| jŽ S )zŒProduce a :class:`_dml.Update` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r$   r>   r|   r  rö   rR   rK   rK   rL   r$     ó   zWriteOnlyCollection.updater%   c                 C  r  )zŒProduce a :class:`_dml.Delete` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r!   r>   r|   r  rö   rR   rK   rK   rL   r!   †  r  zWriteOnlyCollection.deleterù   rú   rF   c                 C  ó   |   |¡ dS )zÐAdd 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.

        N©rý   )rJ   rù   rK   rK   rL   Úadd_all�  ó   zWriteOnlyCollection.add_allrü   r6   c                 C  s   |   |g¡ dS )zÂAdd 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.

        Nr  rþ   rK   rK   rL   rb   –  s   zWriteOnlyCollection.addc                 C  r  )z®Remove an item from this :class:`_orm.WriteOnlyCollection`.

        The given item will be removed from the parent instance's collection on
        the next flush.

        N)rÿ   rþ   rK   rK   rL   re   Ÿ  r  zWriteOnlyCollection.removeN)rE   r
   )rE   r  )rE   r&   )rE   r'   )rE   r%   r   r  )rg   rh   ri   rj   r  ræ   r#   r"   r$   r!   r  rb   re   rK   rK   rK   rL   r€   +  s    



(


		r€   N)Orj   Ú
__future__r   Útypingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Úsqlalchemy.sqlr   Ú r   r   r   r   Úbaser   r   r   r   r   r   r   r    Úsqlr!   r"   r#   r$   Úsql.dmlr%   r&   r'   Úutil.typingr(   r)   Ú_typingr*   r+   r,   Úcollectionsr-   r.   rÕ   r/   r0   r@   r1   r2   Úeventr3   Úsql.selectabler4   r5   r6   r8   ÚHasCollectionAdapterÚAttributeImplr?   Úclass_loggerÚRelationshipPropertyÚstrategy_forÚAbstractRelationshipLoaderÚ
IdentifiedrÔ   r“   rí   r€   rK   rK   rK   rL   Ú<module>   s‚   
Iÿ  '?