a
    ~gX&                     @  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! erd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. Z/G d'd( d(eZ0ej1j2d)d*G d+d, d,eZ3G d-d. d.ee. Z4G d/d0 d0e4e. ee. Z5d1d2d3d4d5Z6d6S )7zDynamic 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)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   OrderedIdentitySetZunchanged_itemsadded_itemsdeleted_itemsZ_reconcile_collection)selfr)   r*   r+   r,   coll r5   [/var/www/html/emsaiapi.evdpl.com/venv/lib/python3.9/site-packages/sqlalchemy/orm/dynamic.py__init__>   s    


z!DynamicCollectionHistory.__init__)N)__name__
__module____qualname__r7   r5   r5   r5   r6   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_byr;   kwr-   c                 K  s^   t jj| ||d |fi | || _|r0t|| _|s<t| _nt|	 v rP|| _n
t
|| _d S N)r   AttributeImplr7   r@   tuplerA   r.   r;   AppenderMixinmromixin_user_query)r3   r=   r>   r?   r@   rA   r;   rB   r5   r5   r6   r7   W   s    


zDynamicAttributeImpl.__init__)N)	r8   r9   r:   Z_supports_dynamic_iterationr%   r   Zcollection_history_cls__annotations__r7   r5   r5   r5   r6   r&   R   s
   
	 r&   dynamic)lazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r8   r9   r:   r&   
impl_classr5   r5   r5   r6   rL   o   s   rL   c                      s  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rddddZdddddZddd d!Zd4dd"d#d$d%Zd&d	d'd(d)Zd*d	d+d,d-Zd&d	d'd.d/Zd*d	d+d0d1Zd*d	d+d2d3Z  ZS )5rF   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    NzOptional[Type[Query[_T]]]r;   zTuple[ColumnElement[Any], ...]_order_by_clausesr&   r'   r(   )r)   r*   r-   c                   s"   t | |jd  t || d S rC   )r   r7   r@   super)r3   r)   r*   	__class__r5   r6   r7   ~   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 rC   )r   instancer/   flushorm_utilhas_identityr3   sessr5   r5   r6   session   s    
zAppenderMixin.sessionr   )rX   r-   c                 C  s
   || _ d S rC   )rW   )r3   rX   r5   r5   r6   rX      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 )NzInstance %s is detached, dynamic relationship cannot return a correct result.   This warning will become a DetachedInstanceError in a future release.T)_source_supports_scalars)rX   r   instance_staterR   detachedr   warnrT   	state_strr   IteratorResultSimpleResultMetaDatar)   r=   r8   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr1   scalars	_generate_iter)r3   rW   r*   r5   r5   r6   re      s(    

zAppenderMixin._iterzIterator[_T]c                 C  s   d S rC   r5   )r3   r5   r5   r6   __iter__       zAppenderMixin.__iter__r   zUnion[_T, List[_T]])indexr-   c                 C  s@   | j }|d u r,| jt| jtj|S | 	|
|S d S rC   )rX   r)   ra   r   rZ   rR   r   rb   Zindexedrd   __getitem__)r3   rh   rW   r5   r5   r6   ri      s    
zAppenderMixin.__getitem__intc                 C  s>   | j }|d u r,t| jt| jtjj	S | 
| S d S rC   )rX   lenr)   ra   r   rZ   rR   r   rb   r1   rd   countrV   r5   r5   r6   rl      s    
zAppenderMixin.countz	Query[_T])rW   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 )NzParent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed)rX   )rR   r   orm_excDetachedInstanceErrorrT   instance_strr)   r>   r;   r@   query_where_criteria	_from_objrN   )r3   rW   rR   rp   r5   r5   r6   rd      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r3   rs   r5   r5   r6   add_all   s    zAppenderMixin.add_allr#   )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

        Nrt   r3   rw   r5   r5   r6   add   s    zAppenderMixin.addc                 C  s   |  | dS )zAdd 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.

        Nrt   ru   r5   r5   r6   extend  s    zAppenderMixin.extendc                 C  s   |  |g dS )zAppend 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.

        Nrt   rx   r5   r5   r6   append  s    zAppenderMixin.appendc                 C  s   |  | dS )zRemove 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_implrx   r5   r5   r6   remove  s    zAppenderMixin.remove)N)r8   r9   r:   __doc__r;   rI   r7   propertyrX   setterre   r
   rf   ri   rl   rd   rv   ry   rz   r{   r|   __classcell__r5   r5   rP   r6   rF   t   s(   

	
 		rF   c                   @  s   e Zd ZdZdS )r.   zA 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)r8   r9   r:   r}   r5   r5   r5   r6   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Appenderr;   )r8   typerF   )r   namer5   r5   r6   rH   )  s    
rH   N)7r}   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r    r   r   rm   r   r   rT   baser   rp   r   rX   r   	writeonlyr   r   r   r   enginer   r   mapperr   r   r   r*   r   r    eventr!   Zsql.elementsr"   r#   r%   r&   RelationshipPropertystrategy_forrL   rF   r.   rH   r5   r5   r5   r6   <module>	   sT    +