a
    •˜ëj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�rbd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dddœdd„Zeddœdd„ƒZeddœdd„ƒZddœdd„Z	dddœdd„Z
ddd œd!d"„Zdd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ÚWriteOnlyAttributeImplúInstanceState[_T]r   zOptional[WriteOnlyHistory[_T]]ÚNone)ÚattrÚstateÚpassiveÚapply_toÚreturnc                 C  sp   |rH|t j@ r&t d|› d|› d�¡‚|j| _|j| _|j| _|j| _n$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    ZOrderedIdentitySet)ÚselfrA   rB   rC   rD   © rI   úW/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/orm/writeonly.pyÚ__init__U   s    
ýÿ



zWriteOnlyHistory.__init__zList[_T]©rE   c                 C  s   t | j | j¡ƒS ©N)Úlistr:   Úunionr9   ©rH   rI   rI   rJ   Úadded_plus_unchangedp   s    z%WriteOnlyHistory.added_plus_unchangedc                 C  s   t | j | j¡ | j¡ƒS rM   )rN   r:   rO   r9   r;   rP   rI   rI   rJ   Ú	all_itemst   s
    ÿÿzWriteOnlyHistory.all_itemsúattributes.Historyc                 C  s`   | j r0| j | j¡}| j | j¡}| j |¡}n| j| j| j  }}}t t|ƒt|ƒt|ƒ¡S rM   )	r=   r:   Ú
differencer9   r;   Úintersectionr   ZHistoryrN   )rH   ÚaddedZdeletedZ	unchangedrI   rI   rJ   Ú
as_history|   s    ý
zWriteOnlyHistory.as_historyzUnion[int, slice]zUnion[List[_T], _T])ÚindexrE   c                 C  s   t | jƒ| S rM   )rN   r:   )rH   rX   rI   rI   rJ   Úindexed‰   s    zWriteOnlyHistory.indexedr6   )ÚvaluerE   c                 C  s   | j  |¡ d S rM   )r:   Úadd©rH   rZ   rI   rI   rJ   Ú	add_addedŒ   s    zWriteOnlyHistory.add_addedc                 C  s(   || j v r| j  |¡ n| j |¡ d S rM   )r:   Úremover;   r[   r\   rI   rI   rJ   Úadd_removed�   s    
zWriteOnlyHistory.add_removed)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rK   ÚpropertyrQ   rR   rW   rY   r]   r_   rI   rI   rI   rJ   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< dddddddœ‡ fdd„Zejfdddddœdd„ZedZddd!d"d#d$œd%d&„ƒZed[ddd'dd#d$œd(d&„ƒZed\ddd)dd*d$œd+d&„ƒZd,ejfddd)dd*d$œd-d&„Zejd.d/œd0d1„ƒZejd.d/œd2d3„ƒZd]dddd4d5d6d7œd8d9„Zd^dddd4d5d6d7œd:d;„Zddd<d=œd>d?„Zd,ejd,ddfdddd4ddddd6d@œ	dAdB„ZdddCdDœdEdF„ZddddCdGœdHdI„ZejfddddJdœdKdL„ZejfddddMdœdNdO„Z ddd<dPœdQdR„Z!ejfdddd4dd6dSœdTdU„Z"ejfdddd4dd6dSœdVdW„Z#ejfdddd4dd6dSœdXdY„Z$‡  Z%S )_r>   Tr<   Úuses_objectsFÚdefault_accepts_scalar_loaderÚsupports_populationÚ_supports_dynamic_iterationÚ
collectionÚdynamicrI   r0   Úorder_byzType[WriteOnlyHistory[Any]]Úcollection_history_clszType[WriteOnlyCollection[Any]]Úquery_classz#Union[Type[Any], AliasedClass[Any]]Ústrz"_Dispatch[QueryableAttribute[Any]]z
Mapper[_T]r   )Úclass_ÚkeyÚdispatchÚtarget_mapperrl   Úkwc                   s8   t ƒ j||d |fi |¤Ž || _t| _|r4t|ƒ| _d S rM   )ÚsuperrK   rs   ÚWriteOnlyCollectionrn   Útuplerl   )rH   rp   rq   rr   rs   rl   rt   ©Ú	__class__rI   rJ   rK   ¤   s
    	zWriteOnlyAttributeImpl.__init__zInstanceState[Any]r*   r   z8Union[util.OrderedIdentitySet, WriteOnlyCollection[Any]])rB   Údict_rC   rE   c                 C  s*   |t j@ s|  |t j¡jS |  | |¡S d S rM   )r   rF   Ú_get_collection_historyÚPASSIVE_NO_INITIALIZEr:   rn   )rH   rB   rz   rC   rI   rI   rJ   Úget³   s
    
ÿzWriteOnlyAttributeImpl.get.zLiteral[None]z Literal[PassiveFlag.PASSIVE_OFF]r.   )rB   rz   Ú	user_datarC   rE   c                 C  s   d S rM   rI   ©rH   rB   rz   r~   rC   rI   rI   rJ   Úget_collectionÀ   s    z%WriteOnlyAttributeImpl.get_collectionr-   c                 C  s   d S rM   rI   r   rI   rI   rJ   r€   É   s    z$Optional[_AdaptedCollectionProtocol]zIUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  s   d S rM   rI   r   rI   rI   rJ   r€   Ò   s    	Nc                 C  s4   |t j@ s|  ||¡j}n|  ||¡}|j}t|ƒS rM   )r   rF   r{   r:   rQ   ÚDynamicCollectionAdapter)rH   rB   rz   r~   rC   ÚdataÚhistoryrI   rI   rJ   r€   Ý   s
    

zattributes.AttributeEventTokenrL   c                 C  s   t  | t j¡S rM   )r   r+   Z	OP_APPENDrP   rI   rI   rJ   Ú_append_tokenî   s    z$WriteOnlyAttributeImpl._append_tokenc                 C  s   t  | t j¡S rM   )r   r+   Z	OP_REMOVErP   rI   rI   rJ   Ú_remove_tokenò   s    z$WriteOnlyAttributeImpl._remove_tokenzOptional[AttributeEventToken]zOptional[WriteOnlyHistory[Any]]r@   )rB   rz   rZ   Ú	initiatorÚcollection_historyrE   c                 C  sd   |d u r|   ||¡}| |¡ | jjD ]}||||p8| jƒ}q&| jr`|d ur`|  t |¡|d¡ d S ©NT)	Ú_modified_eventr]   rr   Úappendr„   ÚtrackparentÚsethasparentr   Úinstance_state©rH   rB   rz   rZ   r†   r‡   ÚfnrI   rI   rJ   Úfire_append_eventö   s    
z(WriteOnlyAttributeImpl.fire_append_eventc                 C  sd   |d u r|   ||¡}| |¡ | jr@|d ur@|  t |¡|d¡ | jjD ]}||||pZ| jƒ qHd S )NF)	r‰   r_   r‹   rŒ   r   r�   rr   r^   r…   rŽ   rI   rI   rJ   Úfire_remove_event	  s    
z(WriteOnlyAttributeImpl.fire_remove_eventzWriteOnlyHistory[Any])rB   rz   rE   c                 C  sH   | j |jvr$|  | |tj¡|j| j < | || t¡ d|| j < |j| j  S rˆ   )rq   Úcommitted_staterm   r   ÚPASSIVE_NO_FETCHr‰   r   )rH   rB   rz   rI   rI   rJ   r‰     s    ÿ
z&WriteOnlyAttributeImpl._modified_event)	rB   rz   rZ   r†   rC   Ú	check_oldÚpopÚ_adaptrE   c	                 C  sö   |r|j | j u rd S |r$|d u r$d S |}	t|	ƒ}
|jrd| jsNt d| › d�¡‚t | j|||d�¡}|  	||¡}|js~|j
}n| |j
¡}| |
¡}t |
¡ |¡}| |¡}|
D ] }||v r²| j|||d |d� q²|D ]}| j|||d |d� qØd S )NzCollection "zV" does not support implicit iteration; collection replacement operations can't be used©rC   )r‡   )Zparent_tokenrN   Úhas_identityri   r   rG   r    ZIdentitySetr}   r‰   r:   rO   rU   rT   r�   r‘   )rH   rB   rz   rZ   r†   rC   r”   r•   r–   ÚiterableÚ
new_valuesZold_collectionr‡   Ú	constantsZ	additionsZremovalsÚmemberrI   rI   rJ   Úset+  sP    
ÿÿÿ

ûûzWriteOnlyAttributeImpl.setr
   )ÚargsÚkwargsrE   c                 O  s
   t ƒ ‚d S rM   ©ÚNotImplementedError)rH   rž   rŸ   rI   rI   rJ   r!   h  s    zWriteOnlyAttributeImpl.delete)rB   rz   rZ   rE   c                 C  s   t dƒ‚d S )Nz7Dynamic attributes don't support collection population.r    )rH   rB   rz   rZ   rI   rI   rJ   Úset_committed_valuek  s    ÿz*WriteOnlyAttributeImpl.set_committed_valuerS   c                 C  s   |   ||¡}| ¡ S rM   )r{   rW   ©rH   rB   rz   rC   ÚcrI   rI   rJ   Úget_historyr  s    z"WriteOnlyAttributeImpl.get_historyz$List[Tuple[InstanceState[Any], Any]]c                 C  s   |   ||¡}dd„ |jD ƒS )Nc                 S  s   g | ]}t  |¡|f‘qS rI   )r   r�   )Ú.0ÚxrI   rI   rJ   Ú
<listcomp>‚  ó    z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)r{   rR   r£   rI   rI   rJ   Úget_all_pending{  s    z&WriteOnlyAttributeImpl.get_all_pending)rB   rC   rE   c                 C  sT   | j |jv r|j| j  }n|  | |tj¡}|jrL|tj@ rL| j| |||d�S |S d S )N)rD   )rq   r’   rm   r   r“   r˜   ZINIT_OK)rH   rB   rC   r¤   rI   rI   rJ   r{   „  s    ÿÿz.WriteOnlyAttributeImpl._get_collection_history)rB   rz   rZ   r†   rC   rE   c                 C  s   || ur|   ||||¡ d S rM   )r�   ©rH   rB   rz   rZ   r†   rC   rI   rI   rJ   rŠ   –  s    zWriteOnlyAttributeImpl.appendc                 C  s   || ur|   ||||¡ d S rM   )r‘   r«   rI   rI   rJ   r^   ¡  s    zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d� d S )Nr—   )r^   r«   rI   rI   rJ   r•   ¬  s    zWriteOnlyAttributeImpl.pop)..)..)..)N)N)&r`   ra   rb   rf   rd   rg   rh   ri   rj   rk   rl   r8   rm   rK   r   ZPASSIVE_OFFr}   r   r€   r    Zmemoized_propertyr„   r…   r�   r‘   r‰   r�   r!   r¢   r“   r¥   r|   rª   r{   rŠ   r^   r•   Ú__classcell__rI   rI   rx   rJ   r>   –   sn   
ü  û  û  ûû	 ú ú÷ =üü	úúúr>   Z
write_only)Zlazyc                   @  s    e Zd ZeZdddœdd„ZdS )ÚWriteOnlyLoaderzMapper[Any]r@   )ÚmapperrE   c              	   C  s\   d| _ | jr | jjtjtjfvr0t 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.)Z	useobjectÚ
impl_classrs   rl   rn   )Zis_class_levelZuselistZparent_propertyÚ	directionr   Ú	ONETOMANYZ
MANYTOMANYr   rG   r   Z_register_attributer¯   r®   rl   rn   )rH   r®   rI   rI   rJ   Úinit_class_attribute¼  s&    þþÿùz$WriteOnlyLoader.init_class_attributeN)r`   ra   rb   r>   r¯   r²   rI   rI   rI   rJ   r­   ·  s   r­   c                   @  s`   e Zd ZU dZded< ddœdd„Zddœd	d
„Zddœdd„Zddœdd„Zddœdd„Z	dS )r�   z9simplified CollectionAdapter for internal API consistencyzCollection[Any]r‚   ©r‚   c                 C  s
   || _ d S rM   r³   )rH   r‚   rI   rI   rJ   rK   Ø  s    z!DynamicCollectionAdapter.__init__zIterator[Any]rL   c                 C  s
   t | jƒS rM   )Úiterr‚   rP   rI   rI   rJ   Ú__iter__Û  s    z!DynamicCollectionAdapter.__iter__r@   c                 C  s   d S rM   rI   rP   rI   rI   rJ   Ú_reset_emptyÞ  s    z%DynamicCollectionAdapter._reset_emptyÚintc                 C  s
   t | jƒS rM   )Úlenr‚   rP   rI   rI   rJ   Ú__len__á  s    z DynamicCollectionAdapter.__len__r<   c                 C  s   dS rˆ   rI   rP   rI   rI   rJ   Ú__bool__ä  s    z!DynamicCollectionAdapter.__bool__N)
r`   ra   rb   rc   rd   rK   rµ   r¶   r¹   rº   rI   rI   rI   rJ   r�   Ó  s   
r�   c                   @  sZ   e Zd ZU dZesdZded< ded< ddd	œd
d„Zdddœdd„Zdddœdd„Z	dS )ÚAbstractCollectionWriterzsVirtual collection which includes append/remove methods that synchronize
    into the attribute event system.

    rI   r6   ÚinstancezTuple[FromClause, ...]Ú	_from_objr>   r?   )rA   rB   c                 C  sŠ   |  ¡ }tr|sJ ‚|| _|| _t|ƒ}|j| jj }|jd urT|j 	¡ |jf| _
nd| _
|j|dd�f| _| jjr€| jj| _nd| _d S )NrI   F)Zalias_secondary)Úobjr   r¼   rA   r   Ú_propsrq   Z	secondaryr®   Z__clause_element__r½   Z_with_parentÚ_where_criteriarl   Ú_order_by_clauses)rH   rA   rB   r¼   r®   ÚproprI   rI   rJ   rK   ô  s    

ÿz!AbstractCollectionWriter.__init__úIterable[_T]r@   ©ÚiteratorrE   c                 C  s0   |D ]&}| j  t | j¡t | j¡|d ¡ qd S rM   )rA   rŠ   r   r�   r¼   Úinstance_dict)rH   rÅ   ÚitemrI   rI   rJ   Ú_add_all_impl  s    

üz&AbstractCollectionWriter._add_all_impl©rÇ   rE   c                 C  s&   | j  t | j¡t | j¡|d ¡ d S rM   )rA   r^   r   r�   r¼   rÆ   ©rH   rÇ   rI   rI   rJ   Ú_remove_impl  s    

üz%AbstractCollectionWriter._remove_implN)
r`   ra   rb   rc   r   Ú	__slots__rd   rK   rÈ   rË   rI   rI   rI   rJ   r»   è  s   
!	r»   c                   @  sŠ   e Zd ZdZdZddœdd„Zddœdd	„Zd
dœdd„Zddœdd„Zddœdd„Z	dddœdd„Z
dddœdd„Zdddœdd„ZdS )rv   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¼   rA   rÀ   r½   rÁ   r
   rL   c                 C  s   t dƒ‚d S )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().)Ú	TypeErrorrP   rI   rI   rJ   rµ   @  s    ÿzWriteOnlyCollection.__iter__zSelect[Tuple[_T]]c                 C  s<   t | jjƒj| jŽ }| jr&|j| jŽ }| jr8|j| jŽ }|S )z‘Produce a :class:`_sql.Select` construct that represents the
        rows within this instance-local :class:`_orm.WriteOnlyCollection`.

        )	r#   rA   rs   ÚwhererÀ   r½   Úselect_fromrÁ   rl   )rH   ÚstmtrI   rI   rJ   r#   G  s    zWriteOnlyCollection.selectr&   c                 C  s„   t | jƒ}|j}|j| jj }|jtjur4t	 
d¡‚i }|jD ],\}}| |||j|¡}td|d�||j< q>t| jjƒjf 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)Z	callable_)r   r¼   r®   r¿   rA   rq   r°   r   r±   r   rG   Zsynchronize_pairsZ_get_attr_w_warn_on_noneÚdictr   r"   rs   Úvalues)rH   rB   r®   rÂ   rz   ÚlÚrr�   rI   rI   rJ   r"   S  s"    
ÿüzWriteOnlyCollection.insertr'   c                 C  s   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$   rA   rs   rÎ   rÀ   rP   rI   rI   rJ   r$   {  s    zWriteOnlyCollection.updater%   c                 C  s   t | jjƒj| jŽ S )zŒProduce a :class:`_dml.Delete` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r!   rA   rs   rÎ   rÀ   rP   rI   rI   rJ   r!   ‚  s    zWriteOnlyCollection.deleterÃ   r@   rÄ   c                 C  s   |   |¡ 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È   )rH   rÅ   rI   rI   rJ   Úadd_all‰  s    zWriteOnlyCollection.add_allr6   rÉ   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Ê   rI   rI   rJ   r[   ’  s    zWriteOnlyCollection.addc                 C  s   |   |¡ dS )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Ê   rI   rI   rJ   r^   ›  s    zWriteOnlyCollection.removeN)r`   ra   rb   rc   rÌ   rµ   r#   r"   r$   r!   rÖ   r[   r^   rI   rI   rI   rJ   rv   '  s   (		rv   N)Orc   Ú
__future__r   Útypingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Zsqlalchemy.sqlr   Ú r   r   r   r   Úbaser   r   r   r   r   r   r   r    Zsqlr!   r"   r#   r$   Zsql.dmlr%   r&   r'   Zutil.typingr(   r)   Ú_typingr*   r+   r,   Úcollectionsr-   r.   r®   r/   r0   rB   r1   r2   Úeventr3   Zsql.selectabler4   r5   r6   r8   ZHasCollectionAdapterZAttributeImplr>   Zclass_loggerZRelationshipPropertyZstrategy_forZAbstractRelationshipLoaderZ
Identifiedr­   r�   r»   rv   rI   rI   rI   rJ   Ú<module>   s€   I
ÿ  #?