a
    •˜ëjÝ&  ã                   @  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" e�r–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d$ed%�Z/G d&d'„ d'ee/ ƒZ0G d(d)„ d)eƒZ1ej2j3d*d+�G d,d-„ d-e ƒƒZ4G d.d/„ d/ee/ ƒZ5G d0d1„ d1e5e/ ee/ ƒZ6d2d3d4œd5d6„Z7d7S )8zâDynamic collection API.

Dynamic collections act like Query() objects for read operations and support
basic add/delete mutation.

.. legacy:: the "dynamic" loader is a legacy feature, superseded by the
 "write_only" loader.


é    )Úannotations)ÚAny)ÚIterable)ÚIterator)ÚList)ÚOptional)Úoverload)ÚTuple)ÚType)ÚTYPE_CHECKING)ÚTypeVar)ÚUnioné   )Ú
attributes)Úexc)Úrelationships)Úutil)ÚPassiveFlag)ÚQuery)Úobject_session)ÚAbstractCollectionWriter)ÚWriteOnlyAttributeImpl)ÚWriteOnlyHistory)ÚWriteOnlyLoaderé   )Úresult)ÚQueryableAttribute)ÚMapper)Ú_RelationshipOrderByArg)ÚSession)ÚInstanceState)ÚAliasedClass)Ú	_Dispatch)ÚColumnElementÚ_T)Úboundc                   @  s$   e Zd Zd
ddddddœdd	„ZdS )ÚDynamicCollectionHistoryNÚDynamicAttributeImplúInstanceState[_T]r   z&Optional[DynamicCollectionHistory[_T]]ÚNone)ÚattrÚstateÚpassiveÚapply_toÚreturnc                 C  s`   |r8t ||ƒ d¡}t |¡| _|j| _|j| _d| _n$t ¡ | _t ¡ | _t ¡ | _d| _d S )NFT)ÚAppenderQueryÚ	autoflushr   ZOrderedIdentitySetZunchanged_itemsÚadded_itemsZdeleted_itemsZ_reconcile_collection)Úselfr*   r+   r,   r-   Zcoll© r3   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/orm/dynamic.pyÚ__init__>   s    


z!DynamicCollectionHistory.__init__)N)Ú__name__Ú
__module__Ú__qualname__r5   r3   r3   r3   r4   r&   =   s    ûr&   c                
   @  s@   e Zd ZU dZee Zded< dddddd	d
dddœdd„ZdS )r'   TzType[AppenderMixin[Any]]Úquery_classNz#Union[Type[Any], AliasedClass[Any]]Ústrz"_Dispatch[QueryableAttribute[Any]]z
Mapper[_T]r   z!Optional[Type[AppenderMixin[_T]]]r   r)   )Úclass_ÚkeyÚdispatchÚtarget_mapperÚorder_byr9   Úkwr.   c                 K  s^   t jj| ||d |fi |¤Ž || _|r0t|ƒ| _|s<t| _nt| 	¡ v rP|| _n
t
|ƒ| _d S ©N)r   ZAttributeImplr5   r>   Útupler?   r/   r9   ÚAppenderMixinÚmroÚmixin_user_query)r2   r;   r<   r=   r>   r?   r9   r@   r3   r3   r4   r5   W   s    

ÿÿ
zDynamicAttributeImpl.__init__)N)	r6   r7   r8   Z_supports_dynamic_iterationr&   r   Zcollection_history_clsÚ__annotations__r5   r3   r3   r3   r4   r'   R   s
   
	 ùr'   Zdynamic)Zlazyc                   @  s   e Zd ZeZdS )Ú
DynaLoaderN)r6   r7   r8   r'   Z
impl_classr3   r3   r3   r4   rG   o   s   rG   c                      s0  e Zd ZU dZdZded< ded< ddd	d
œ‡ fdd„Zeddœdd„ƒZej	dd	dœdd„ƒZddœdd„Z
er€ddœdd„Zedddœdd„ƒZedd dœd!d„ƒZd"d#dœd$d„Zddœd%d&„Zd8dd'd(œd)d*„Zd+d	d,œd-d.„Zdd	d/œd0d1„Zd+d	d,œd2d3„Zdd	d/œd4d5„Zdd	d/œd6d7„Z‡  ZS )9rC   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    NzOptional[Type[Query[_T]]]r9   zTuple[ColumnElement[Any], ...]Ú_order_by_clausesr'   r(   r)   )r*   r+   r.   c                   s"   t  | |jd ¡ tƒ  ||¡ d S rA   )r   r5   r>   Úsuper)r2   r*   r+   ©Ú	__class__r3   r4   r5   ~   s    ýzAppenderMixin.__init__zOptional[Session])r.   c                 C  sB   t | jƒ}|d ur*|jr*| j|v r*| ¡  t | j¡s:d S |S d S rA   )r   Úinstancer0   ÚflushÚorm_utilZhas_identity©r2   Úsessr3   r3   r4   Úsessionˆ   s    
zAppenderMixin.sessionr   )rQ   r.   c                 C  s
   || _ d S rA   )rP   )r2   rQ   r3   r3   r4   rQ   ’   s    z1Union[result.ScalarResult[_T], result.Result[_T]]c                 C  s‚   | j }|d u rpt | j¡}|jr4t dt |¡ ¡ t	j
t	 | jjjg¡t| j t | j¡tj¡jƒdd� ¡ S |  |¡ ¡ S d S )Nz•Instance %s is detached, dynamic relationship cannot return a correct result.   This warning will become a DetachedInstanceError in a future release.T)Z_source_supports_scalars)rQ   r   Úinstance_staterL   Zdetachedr   ÚwarnrN   Z	state_strr   ZIteratorResultZSimpleResultMetaDatar*   r;   r6   ÚiterÚ_get_collection_historyr   ÚPASSIVE_NO_INITIALIZEr1   ZscalarsÚ	_generateÚ_iter)r2   rP   r+   r3   r3   r4   rX   –   s(    ýÿ
þÿø
zAppenderMixin._iterzIterator[_T]c                 C  s   d S rA   r3   )r2   r3   r3   r4   Ú__iter__±   ó    zAppenderMixin.__iter__Úintr$   )Úindexr.   c                 C  s   d S rA   r3   ©r2   r\   r3   r3   r4   Ú__getitem__³   s    zAppenderMixin.__getitem__ÚslicezList[_T]c                 C  s   d S rA   r3   r]   r3   r3   r4   r^   ¶   s    zUnion[int, slice]zUnion[_T, List[_T]]c                 C  s@   | j }|d u r,| j t | j¡tj¡ |¡S |  	|¡ 
|¡S d S rA   )rQ   r*   rU   r   rR   rL   r   rV   ZindexedrW   r^   )r2   r\   rP   r3   r3   r4   r^   ¹   s    
þýc                 C  s>   | j }|d u r,t| j t | j¡tj¡j	ƒS |  
|¡ ¡ S d S rA   )rQ   Úlenr*   rU   r   rR   rL   r   rV   r1   rW   ÚcountrO   r3   r3   r4   ra   Ã   s    
þÿzAppenderMixin.countz	Query[_T])rP   r.   c                 C  s~   | j }|d u r:t|ƒ}|d u r:t dt |¡| jjf ¡‚| jrT| j| jj	|d�}n| 
| jj	¡}| j|_| j|_| j|_|S )NzŒParent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed)rQ   )rL   r   Úorm_excZDetachedInstanceErrorrN   Zinstance_strr*   r<   r9   r>   ÚqueryZ_where_criteriaZ	_from_objrH   )r2   rP   rL   rc   r3   r3   r4   rW   Ï   s     ýÿzAppenderMixin._generatezIterable[_T])Úiteratorr.   c                 C  s   |   |¡ dS )a~  Add an iterable of items to this :class:`_orm.AppenderQuery`.

        The given items will be persisted to the database in terms of
        the parent instance's collection on the next flush.

        This method is provided to assist in delivering forwards-compatibility
        with the :class:`_orm.WriteOnlyCollection` collection class.

        .. versionadded:: 2.0

        N©Z_add_all_impl©r2   rd   r3   r3   r4   Úadd_allì   s    zAppenderMixin.add_all)Úitemr.   c                 C  s   |   |g¡ dS )ap  Add an item to this :class:`_orm.AppenderQuery`.

        The given item will be persisted to the database in terms of
        the parent instance's collection on the next flush.

        This method is provided to assist in delivering forwards-compatibility
        with the :class:`_orm.WriteOnlyCollection` collection class.

        .. versionadded:: 2.0

        Nre   ©r2   rh   r3   r3   r4   Úaddú   s    zAppenderMixin.addc                 C  s   |   |¡ dS )zÊAdd an iterable of items to this :class:`_orm.AppenderQuery`.

        The given items will be persisted to the database in terms of
        the parent instance's collection on the next flush.

        Nre   rf   r3   r3   r4   Úextend  s    zAppenderMixin.extendc                 C  s   |   |g¡ dS )z¿Append an item to this :class:`_orm.AppenderQuery`.

        The given item will be persisted to the database in terms of
        the parent instance's collection on the next flush.

        Nre   ri   r3   r3   r4   Úappend  s    zAppenderMixin.appendc                 C  s   |   |¡ dS )z¨Remove an item from this :class:`_orm.AppenderQuery`.

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

        N)Z_remove_implri   r3   r3   r4   Úremove  s    zAppenderMixin.remove)N)r6   r7   r8   Ú__doc__r9   rF   r5   ÚpropertyrQ   ÚsetterrX   r   rY   r   r^   ra   rW   rg   rj   rk   rl   rm   Ú__classcell__r3   r3   rJ   r4   rC   t   s0   

	
 þ		rC   c                   @  s   e Zd ZdZdS )r/   zÞA dynamic query that supports basic collection storage operations.

    Methods on :class:`.AppenderQuery` include all methods of
    :class:`_orm.Query`, plus additional methods used for collection
    persistence.


    N)r6   r7   r8   rn   r3   r3   r3   r4   r/   $  s   r/   r   ztype[AppenderMixin[Any]])Úclsr.   c                 C  s   d| j  }t|t| fd| iƒS )zAReturn a new class with AppenderQuery functionality layered over.ZAppenderr9   )r6   ÚtyperC   )rr   Únamer3   r3   r4   rE   /  s    
rE   N)8rn   Ú
__future__r   Útypingr   r   r   r   r   r   r	   r
   r   r   r   Ú r   r   rb   r   r   rN   Úbaser   rc   r   rQ   r   Z	writeonlyr   r   r   r   Zenginer   r   Zmapperr   r   r   r+   r    r!   Úeventr"   Zsql.elementsr#   r$   r&   r'   ZRelationshipPropertyZstrategy_forrG   rC   r/   rE   r3   r3   r3   r4   Ú<module>	   sV    1