o
    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. Z5d8d5d6Z6d7S )9zDynamic 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ZdS )DynamicCollectionHistoryNattrDynamicAttributeImplstateInstanceState[_T]passiver   apply_to&Optional[DynamicCollectionHistory[_T]]returnNonec                 C  sb   |rt ||d}t|| _|j| _|j| _d| _d S t | _t | _t | _d| _d S )NFT)AppenderQuery	autoflushr   OrderedIdentitySetunchanged_itemsadded_itemsdeleted_items_reconcile_collection)selfr&   r(   r*   r+   coll r8   X/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/orm/dynamic.py__init__>   s   




z!DynamicCollectionHistory.__init__N)
r&   r'   r(   r)   r*   r   r+   r,   r-   r.   )__name__
__module____qualname__r:   r8   r8   r8   r9   r%   =   s    r%   c                   @  s0   e Zd ZU dZee Zded< 	ddddZdS )r'   TzType[AppenderMixin[Any]]query_classNclass_#Union[Type[Any], AliasedClass[Any]]keystrdispatch"_Dispatch[QueryableAttribute[Any]]target_mapper
Mapper[_T]order_byr   !Optional[Type[AppenderMixin[_T]]]kwr   r-   r.   c                 K  sb   t jj| ||d |fi | || _|rt|| _|st| _d S t|	 v r*|| _d S t
|| _d S r;   )r   AttributeImplr:   rF   tuplerH   r/   r?   AppenderMixinmromixin_user_query)r6   r@   rB   rD   rF   rH   r?   rJ   r8   r8   r9   r:   W   s   




zDynamicAttributeImpl.__init__r;   )r@   rA   rB   rC   rD   rE   rF   rG   rH   r   r?   rI   rJ   r   r-   r.   )	r<   r=   r>   _supports_dynamic_iterationr%   r   collection_history_cls__annotations__r:   r8   r8   r8   r9   r'   R   s   
 	r'   dynamic)lazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r<   r=   r>   r'   
impl_classr8   r8   r8   r9   rU   o   s    rU   c                      s   e Zd ZU dZdZded< ded< d5 fddZed6ddZej	d7ddZd8ddZ
er3d9ddZd:ddZd;d!d"Z	d<d=d%d&Zd>d)d*Zd?d-d.Zd>d/d0Zd?d1d2Zd?d3d4Z  ZS )@rM   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 r;   )r   r:   rF   super)r6   r&   r(   	__class__r8   r9   r:   ~   s   zAppenderMixin.__init__Optional[Session]c                 C  s>   t | j}|d ur|jr| j|v r|  t| jsd S |S r;   )r   instancer0   flushorm_utilhas_identityr6   sessr8   r8   r9   session   s   
zAppenderMixin.sessionrb   r   c                 C  s
   || _ d S r;   )ra   )r6   rb   r8   r8   r9   rb      s   
1Union[result.ScalarResult[_T], result.Result[_T]]c                 C  s~   | j }|d u r8t| j}|jrtdt|  t	j
t	| jjjgt| jt| jtjjdd S | | 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)rb   r   instance_stater\   detachedr   warnr^   	state_strr   IteratorResultSimpleResultMetaDatar&   r@   r<   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr3   scalars	_generate_iter)r6   ra   r(   r8   r8   r9   rp      s.   
	zAppenderMixin._iterIterator[_T]c                 C  s   d S r;   r8   )r6   r8   r8   r9   __iter__   s    zAppenderMixin.__iter__indexr   Union[_T, List[_T]]c                 C  s<   | j }|d u r| jt| jtj|S | 	|
|S r;   )rb   r&   rl   r   re   r\   r   rm   indexedro   __getitem__)r6   rs   ra   r8   r8   r9   rv      s   
zAppenderMixin.__getitem__intc                 C  s:   | j }|d u rt| jt| jtjj	S | 
| S r;   )rb   lenr&   rl   r   re   r\   r   rm   r3   ro   countr`   r8   r8   r9   ry      s   
zAppenderMixin.countra   	Query[_T]c                 C  s~   | j }|d u rt|}|d u rtdt|| jjf | jr*| 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)rb   )r\   r   orm_excDetachedInstanceErrorr^   instance_strr&   rB   r?   rF   query_where_criteria	_from_objrW   )r6   ra   r\   r~   r8   r8   r9   ro      s    zAppenderMixin._generateiteratorIterable[_T]c                 C     |  | 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_add_all_implr6   r   r8   r8   r9   add_all   s   zAppenderMixin.add_allitemr#   c                 C     |  |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

        Nr   r6   r   r8   r8   r9   add   s   zAppenderMixin.addc                 C  r   )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.

        Nr   r   r8   r8   r9   extend     zAppenderMixin.extendc                 C  r   )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.

        Nr   r   r8   r8   r9   append  s   zAppenderMixin.appendc                 C  r   )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)_remove_implr   r8   r8   r9   remove  r   zAppenderMixin.remove)r&   r'   r(   r)   r-   r.   )r-   r[   )rb   r   r-   r.   )r-   rc   )r-   rq   )rs   r   r-   rt   )r-   rw   r;   )ra   r[   r-   rz   )r   r   r-   r.   )r   r#   r-   r.   )r<   r=   r>   __doc__r?   rR   r:   propertyrb   setterrp   r
   rr   rv   ry   ro   r   r   r   r   r   __classcell__r8   r8   rY   r9   rM   t   s*   
 
	








		rM   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)r<   r=   r>   r   r8   r8   r8   r9   r/     s    r/   clsr   r-   type[AppenderMixin[Any]]c                 C  s   d| j  }t|t| fd| iS )zAReturn a new class with AppenderQuery functionality layered over.Appenderr?   )r<   typerM   )r   namer8   r8   r9   rO   )  s   
rO   N)r   r   r-   r   )7r   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r    r   r   r{   r   r   r^   baser   r~   r   rb   r   	writeonlyr   r   r   r   enginer   r   mapperr   r   r   r(   r   r    eventr!   sql.elementsr"   r#   r%   r'   RelationshipPropertystrategy_forrU   rM   r/   rO   r8   r8   r8   r9   <module>   sV    +