a
    c	g!                     @  sp  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 erddlmZ ededZG dd deZG dd deZejj ddG dd deZ!G d d! d!ee Z"G d"d# d#e"e ee Z#d$d% Z$d&S )'zDynamic 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)TYPE_CHECKING)TypeVar   )
attributes)exc)relationships)util)Query)object_session)AbstractCollectionWriter)WriteOnlyAttributeImpl)WriteOnlyHistory)WriteOnlyLoader   )result)Session_T)boundc                   @  s   e Zd ZdddZdS )DynamicCollectionHistoryNc                 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)selfattrstateZpassiveZapply_toZcoll r   Y/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/sqlalchemy/orm/dynamic.py__init__2   s    


z!DynamicCollectionHistory.__init__)N)__name__
__module____qualname__r!   r   r   r   r    r   1   s   r   c                   @  s   e Zd ZdZeZdddZdS )DynamicAttributeImplTNc           	      K  s^   t jj| ||||fi | || _|r0t|| _|s<t| _nt|	 v rP|| _n
t
|| _d S N)r	   ZAttributeImplr!   target_mappertupleorder_byr   query_classAppenderMixinmromixin_user_query)	r   class_keyZtypecallabledispatchr'   r)   r*   kwr   r   r    r!   D   s    

zDynamicAttributeImpl.__init__)N)r"   r#   r$   Z_supports_dynamic_iterationr   Zcollection_history_clsr!   r   r   r   r    r%   @   s   
 r%   Zdynamic)Zlazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r"   r#   r$   r%   Z
impl_classr   r   r   r    r2   ]   s   r2   c                      s   e Zd ZdZdZ fddZeddddZejdd	d
ddZdd Z	e
r^ddddZdddddZddddZ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d$d%Zdd	dd&d'Z  ZS ))r+   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    Nc                   s"   t | |jd  t || d S r&   )r   r!   r'   super)r   r   r   	__class__r   r    r!   k   s    zAppenderMixin.__init__r   )returnc                 C  sH   t | j}|d ur0| jr0|jr0| j|v r0|  t| js@d S |S d S r&   )r   instancer   flushorm_utilZhas_identityr   sessr   r   r    sessiono   s    
zAppenderMixin.sessionNone)r<   r6   c                 C  s
   || _ d S r&   )r;   )r   r<   r   r   r    r<   ~   s    c                 C  s~   | j }|d u rlt| j}|jr4tdt|  t	j
t	| jjjg| 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)Z_source_supports_scalars)r<   r	   instance_stater7   Zdetachedr   warnr9   Z	state_strr   ZIteratorResultZSimpleResultMetaDatar   r.   r"   _get_collection_historyPASSIVE_NO_INITIALIZEr   Zscalars	_generate_iter)r   r;   r   r   r   r    rC      s$    

	zAppenderMixin._iterzIterator[_T]c                 C  s   d S r&   r   )r   r   r   r    __iter__   s    zAppenderMixin.__iter__r   r   )indexr6   c                 C  s@   | j }|d u r,| jt| jtj|S | |	|S d S r&   )
r<   r   r@   r	   r>   r7   rA   ZindexedrB   __getitem__)r   rE   r;   r   r   r    rF      s    
zAppenderMixin.__getitem__intc                 C  s>   | j }|d u r,t| jt| jtjjS | 	|
 S d S r&   )r<   lenr   r@   r	   r>   r7   rA   r   rB   countr:   r   r   r    rI      s    
zAppenderMixin.countc                 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)r<   )r7   r   orm_excZDetachedInstanceErrorr9   Zinstance_strr   r/   r*   r'   queryZ_where_criteriaZ	_from_objZ_order_by_clauses)r   r;   r7   rK   r   r   r    rB      s     zAppenderMixin._generatezIterable[_T])iteratorr6   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r   rL   r   r   r    add_all   s    zAppenderMixin.add_all)itemr6   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

        NrM   r   rP   r   r   r    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.

        NrM   rN   r   r   r    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.

        NrM   rQ   r   r   r    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_implrQ   r   r   r    remove   s    zAppenderMixin.remove)N)r"   r#   r$   __doc__r*   r!   propertyr<   setterrC   r   rD   rF   rI   rB   rO   rR   rS   rT   rU   __classcell__r   r   r4   r    r+   b   s$   

		r+   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$   rV   r   r   r   r    r     s   r   c                 C  s   d| j  }t|t| fd| iS )zAReturn a new class with AppenderQuery functionality layered over.ZAppenderr*   )r"   typer+   )clsnamer   r   r    r-     s    
r-   N)%rV   
__future__r   typingr   r   r   r   r    r	   r
   rJ   r   r   r9   rK   r   r<   r   Z	writeonlyr   r   r   r   Zenginer   r   r   r   r%   ZRelationshipPropertyZstrategy_forr2   r+   r   r-   r   r   r   r    <module>
   s:    %