
    3)i&                    z   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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/         Z0 G d( d)e      Z1ejd                  jg                  d*+       G d, d-e              Z4 G d. d/ee/         Z5 G d0 d1e5e/   ee/         Z6d4d2Z7y3)5zDynamic 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                  *    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         Z/var/www/html/spbu.com/backend/venv/lib/python3.12/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)
r2   DynamicAttributeImplr3   InstanceState[_T]r4   r   r5   z&Optional[DynamicCollectionHistory[_T]]returnNone)__name__
__module____qualname__r8    r9   r7   r(   r(   >   s=     <@/"/ !/ 	/
 9/ 
/r9   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 r:   )r   AttributeImplr8   target_mappertupleorder_byr*   rD   AppenderMixinmromixin_user_query)r1   class_keydispatchrG   rI   rD   kws           r7   r8   zDynamicAttributeImpl.__init__X   sr     	  ))&#tX	
13	
 +!(ODM,Dkoo//*D/<Dr9   r:   )rM   z#Union[Type[Any], AliasedClass[Any]]rN   strrO   z"_Dispatch[QueryableAttribute[Any]]rG   z
Mapper[_T]rI   r   rD   z!Optional[Type[AppenderMixin[_T]]]rP   r   r=   r>   )	r?   r@   rA   _supports_dynamic_iterationr(   r   collection_history_cls__annotations__r8   rB   r9   r7   r;   r;   S   sq    "&5c:)) :>=3= = 5	=
 "= *= 7= = 
=r9   r;   dynamic)lazyc                      e Zd ZeZy)
DynaLoaderN)r?   r@   rA   r;   
impl_classrB   r9   r7   rX   rX   p   s    %Jr9   rX   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edd       Ze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 )#rJ   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    NzOptional[Type[Query[_T]]]rD   zTuple[ColumnElement[Any], ...]_order_by_clausesc                h    t        j                  | |j                  d        t        |   ||       y r:   )r   r8   rG   super)r1   r2   r3   	__class__s      r7   r8   zAppenderMixin.__init__   s1     		

 	u%r9   c                    t        | j                        }|*|j                  r| j                  |v r|j                          t	        j
                  | j                        sy |S r:   )r   instancer+   flushorm_utilhas_identityr1   sesss     r7   sessionzAppenderMixin.session   sJ    dmm,4==D3HJJL$$T]]3Kr9   c                    || _         y r:   )re   )r1   rf   s     r7   rf   zAppenderMixin.session   s	    	r9   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)rf   r   instance_stater`   detachedr   warnrb   	state_strr   IteratorResultSimpleResultMetaDatar2   rM   r?   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr.   scalars	_generate_iter)r1   re   r3   s      r7   ru   zAppenderMixin._iter   s    ||<--dmm<E~~		C  ))%02 ((++TYY-=-=-F-F,GHII55"11$--@#99 "k	 *.	 gi	 >>$'--//r9   c                     y r:   rB   )r1   s    r7   __iter__zAppenderMixin.__iter__   s    Cr9   c                     y r:   rB   r1   indexs     r7   __getitem__zAppenderMixin.__getitem__   s    -0r9   c                     y r:   rB   ry   s     r7   r{   zAppenderMixin.__getitem__   s    58r9   c                
   | j                   }|V| j                  j                  t        j                  | j
                        t        j                        j                  |      S | j                  |      j                  |      S r:   )rf   r2   rq   r   rj   r`   r   rr   indexedrt   r{   )r1   rz   re   s      r7   r{   zAppenderMixin.__getitem__   si    ||<9944))$--811 gen
 >>$'33E::r9   c                   | j                   }|Zt        | j                  j                  t	        j
                  | j                        t        j                        j                        S | j                  |      j                         S r:   )rf   lenr2   rq   r   rj   r`   r   rr   r.   rt   countrd   s     r7   r   zAppenderMixin.count   sk    ||<		11--dmm<55 +	  >>$'--//r9   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)rf   )r`   r   orm_excDetachedInstanceErrorrb   instance_strr2   rN   rD   rG   query_where_criteria	_from_objr[   )r1   re   r`   r   s       r7   rt   zAppenderMixin._generate   s     ==<!(+D|33  ,,X6		G  $$TYY%<%<d$KEJJtyy667E $ 4 4.."&"8"8r9   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r1   iterators     r7   add_allzAppenderMixin.add_all   s     	8$r9   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   r1   items     r7   addzAppenderMixin.add   s     	D6"r9   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     r7   extendzAppenderMixin.extend	  s     	8$r9   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     r7   appendzAppenderMixin.append  s     	D6"r9   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     r7   removezAppenderMixin.remove  s     	$r9   )r2   r;   r3   r<   r=   r>   )r=   Optional[Session])rf   r    r=   r>   )r=   z1Union[result.ScalarResult[_T], result.Result[_T]])r=   zIterator[_T])rz   intr=   r%   )rz   slicer=   zList[_T])rz   zUnion[int, slice]r=   zUnion[_T, List[_T]])r=   r   r:   )re   r   r=   z	Query[_T])r   zIterable[_T]r=   r>   )r   r%   r=   r>   )r?   r@   rA   __doc__rD   rT   r8   propertyrf   setterru   r   rw   r	   r{   r   rt   r   r   r   r   r   __classcell__)r^   s   @r7   rJ   rJ   u   s     .2K*155&(&1B&	&   ^^ 02 /0 08 8;
0 #' 
:%#%# r9   rJ   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@   rA   r   rB   r9   r7   r*   r*   %  s    r9   r*   c                J    d| j                   z   }t        |t        | fd| i      S )zAReturn a new class with AppenderQuery functionality layered over.AppenderrD   )r?   typerJ   )clsnames     r7   rL   rL   0  s)    $D}c*]C,@AAr9   N)r   r   r=   ztype[AppenderMixin[Any]])8r   
__future__r   typingr   r   r   r   r   r	   r
   r   r   r   r    r   r   r   r   r   rb   baser   r   r   rf   r   	writeonlyr   r   r   r   enginer   r   mapperr   r   r    r3   r!   r"   eventr#   sql.elementsr$   r%   r(   r;   RelationshipPropertystrategy_forrX   rJ   r*   rL   rB   r9   r7   <module>r      s  	 #                   # / - ' &   $6 $"!,T//3 /*=1 =: ##00i0@& & A&m ,R0 m `M"%uRy Br9   