
    h'                    n   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r0d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$      Z. G d% d&ee.         Z/ G d' d(e      Z0ejb                  je                  d)*       G d+ d,e             Z3 G d- d.ee.         Z4 G d/ d0e4e.   ee.         Z5d3d1Z6y2)4zDynamic 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                  *    e Zd Z	 d	 	 	 	 	 	 	 	 	 ddZy)DynamicCollectionHistoryNc                j   |r_t        ||      j                  d      }t        j                  |      | _        |j
                  | _        |j                  | _        d| _        y t        j                         | _        t        j                         | _        t        j                         | _        d| _        y )NFT)AppenderQuery	autoflushr   OrderedIdentitySetunchanged_itemsadded_itemsdeleted_items_reconcile_collection)selfattrstatepassiveapply_tocolls         ?D:\EasyAligner\venv\Lib\site-packages\sqlalchemy/orm/dynamic.py__init__z!DynamicCollectionHistory.__init__>   s      u-77>D#'#:#:4#@D '33D!)!7!7D)-D&!%!8!8!:D#668D#'#:#:#<D ).D&    N)
r1   DynamicAttributeImplr2   InstanceState[_T]r3   r   r4   z&Optional[DynamicCollectionHistory[_T]]returnNone)__name__
__module____qualname__r7    r8   r6   r'   r'   =   s=     <@/"/ !/ 	/
 9/ 
/r8   r'   c                  P    e Zd ZU dZee   Zded<   	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)r:   TzType[AppenderMixin[Any]]query_classNc                    t        j                  j                  | ||d |fi | || _        |rt	        |      | _        |st        | _        y t        |j                         v r|| _        y t        |      | _        y r9   )r   AttributeImplr7   target_mappertupleorder_byr)   rC   AppenderMixinmromixin_user_query)r0   class_keydispatchrF   rH   rC   kws           r6   r7   zDynamicAttributeImpl.__init__W   sr     	  ))&#tX	
13	
 +!(ODM,Dkoo//*D/<Dr8   r9   )rL   z#Union[Type[Any], AliasedClass[Any]]rM   strrN   z"_Dispatch[QueryableAttribute[Any]]rF   z
Mapper[_T]rH   r   rC   z!Optional[Type[AppenderMixin[_T]]]rO   r   r<   r=   )	r>   r?   r@   _supports_dynamic_iterationr'   r   collection_history_cls__annotations__r7   rA   r8   r6   r:   r:   R   sq    "&5c:)) :>=3= = 5	=
 "= *= 7= = 
=r8   r:   dynamic)lazyc                      e Zd ZeZy)
DynaLoaderN)r>   r?   r@   r:   
impl_classrA   r8   r6   rW   rW   o   s    %Jr8   rW   c                       e Zd ZU dZdZded<   ded<   	 	 	 	 	 	 d fdZedd       Zej                  dd	       Zdd
Z
erddZddZddZ	 d	 	 	 ddZddZddZddZddZddZ xZS )rI   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    NzOptional[Type[Query[_T]]]rC   zTuple[ColumnElement[Any], ...]_order_by_clausesc                h    t        j                  | |j                  d        t        |   ||       y r9   )r   r7   rF   super)r0   r1   r2   	__class__s      r6   r7   zAppenderMixin.__init__~   s1     		

 	u%r8   c                    t        | j                        }|*|j                  r| j                  |v r|j                          t	        j
                  | j                        sy |S r9   )r   instancer*   flushorm_utilhas_identityr0   sesss     r6   sessionzAppenderMixin.session   sJ    dmm,4==D3HJJL$$T]]3Kr8   c                    || _         y r9   )rd   )r0   re   s     r6   re   zAppenderMixin.session   s	    	r8   c           
     j   | j                   }|t        j                  | j                        }|j                  r+t        j                  dt        j                  |      z         t        j                  t        j                  | j                  j                  j                  g      t        | j                  j!                  t        j                  | j                        t"        j$                        j&                        d      j)                         S | j+                  |      j-                         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)re   r   instance_stater_   detachedr   warnra   	state_strr   IteratorResultSimpleResultMetaDatar1   rL   r>   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr-   scalars	_generate_iter)r0   rd   r2   s      r6   rt   zAppenderMixin._iter   s    ||<--dmm<E~~		C  ))%02 ((++TYY-=-=-F-F,GHII55"11$--@#99 "k	 *.	 gi	 >>$'--//r8   c                     y r9   rA   )r0   s    r6   __iter__zAppenderMixin.__iter__   s    Cr8   c                
   | j                   }|V| j                  j                  t        j                  | j
                        t        j                        j                  |      S | j                  |      j                  |      S r9   )re   r1   rp   r   ri   r_   r   rq   indexedrs   __getitem__)r0   indexrd   s      r6   ry   zAppenderMixin.__getitem__   si    ||<9944))$--811 gen
 >>$'33E::r8   c                   | j                   }|Zt        | j                  j                  t	        j
                  | j                        t        j                        j                        S | j                  |      j                         S r9   )re   lenr1   rp   r   ri   r_   r   rq   r-   rs   countrc   s     r6   r}   zAppenderMixin.count   sk    ||<		11--dmm<55 +	  >>$'--//r8   c                   | j                   }|Pt        |      }|Ct        j                  dt	        j
                  |      d| j                  j                  d      | j                  r(| j                  | j                  j                  |      }n%|j                  | j                  j                        }| j                  |_        | j                  |_        | j                  |_        |S )NzParent instance zh is not bound to a Session, and no contextual session is established; lazy load operation of attribute 'z' cannot proceed)re   )r_   r   orm_excDetachedInstanceErrorra   instance_strr1   rM   rC   rF   query_where_criteria	_from_objrZ   )r0   rd   r_   r   s       r6   rs   zAppenderMixin._generate   s     ==<!(+D|33  ,,X6		G  $$TYY%<%<d$KEJJtyy667E $ 4 4.."&"8"8r8   c                &    | j                  |       y)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r0   iterators     r6   add_allzAppenderMixin.add_all   s     	8$r8   c                (    | j                  |g       y)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   r0   items     r6   addzAppenderMixin.add   s     	D6"r8   c                &    | j                  |       y)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   s     r6   extendzAppenderMixin.extend  s     	8$r8   c                (    | j                  |g       y)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   s     r6   appendzAppenderMixin.append  s     	D6"r8   c                &    | j                  |       y)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   s     r6   removezAppenderMixin.remove  s     	$r8   )r1   r:   r2   r;   r<   r=   )r<   Optional[Session])re   r   r<   r=   )r<   z1Union[result.ScalarResult[_T], result.Result[_T]])r<   zIterator[_T])rz   r   r<   zUnion[_T, List[_T]])r<   intr9   )rd   r   r<   z	Query[_T])r   zIterable[_T]r<   r=   )r   r$   r<   r=   )r>   r?   r@   __doc__rC   rS   r7   propertyre   setterrt   r   rv   ry   r}   rs   r   r   r   r   r   __classcell__)r]   s   @r6   rI   rI   t   s     .2K*155&(&1B&	&   ^^ 02 /;
0 #' 
:%#%# r8   rI   c                      e Zd ZdZy)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   rA   r8   r6   r)   r)     s    r8   r)   c                J    d| j                   z   }t        |t        | fd| i      S )zAReturn a new class with AppenderQuery functionality layered over.AppenderrC   )r>   typerI   )clsnames     r6   rK   rK   )  s)    $D}c*]C,@AAr8   N)r   r   r<   ztype[AppenderMixin[Any]])7r   
__future__r   typingr   r   r   r   r   r	   r
   r   r   r    r   r   r   r   r   ra   baser   r   r   re   r   	writeonlyr   r   r   r   enginer   r   mapperr   r   r   r2   r    r!   eventr"   sql.elementsr#   r$   r'   r:   RelationshipPropertystrategy_forrW   rI   r)   rK   rA   r8   r6   <module>r      s   	 #                  # / - ' &   $6 $"!,T//3 /*=1 =: ##00i0@& & A&g ,R0 g TM"%uRy Br8   