
    YՁi!                       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rddlmZ  ede      Z G d de      Z G d de      Zej>                  jA                  d       G d de             Z! G d  d!ee         Z" G d" d#e"e   ee         Z#d$ Z$y%)&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                      e Zd Z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         R/home/ccuser/rateright-main-site/.venv/Lib/site-packages/sqlalchemy/orm/dynamic.py__init__z!DynamicCollectionHistory.__init__2   s     u-77>D#'#:#:4#@D '33D!)!7!7D)-D&!%!8!8!:D#668D#'#:#:#<D ).D&    N)__name__
__module____qualname__r*    r+   r)   r   r   1   s    /r+   r   c                      e Zd ZdZeZ	 ddZy)DynamicAttributeImplTNc                    t        j                  j                  | ||||fi | || _        |rt	        |      | _        |st        | _        y t        |j                         v r|| _        y t        |      | _        y r,   )r
   AttributeImplr*   target_mappertupleorder_byr   query_classAppenderMixinmromixin_user_query)	r#   class_keytypecallabledispatchr5   r7   r8   kws	            r)   r*   zDynamicAttributeImpl.__init__D   sr     	  ))&#|X	
9;	
 +!(ODM,Dkoo//*D/<Dr+   r,   )r-   r.   r/   _supports_dynamic_iterationr   collection_history_clsr*   r0   r+   r)   r2   r2   @   s    "&5 =r+   r2   dynamic)lazyc                      e Zd ZeZy)
DynaLoaderN)r-   r.   r/   r2   
impl_classr0   r+   r)   rF   rF   ]   s    %Jr+   rF   c                       e Zd ZdZdZ fdZedd       Zej                  dd       Zd Z	e
rddZddZdd	Zdd
ZddZddZddZddZddZ xZS )r9   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    Nc                h    t        j                  | |j                  d        t        |   ||       y r,   )r   r*   r5   super)r#   r$   r%   	__class__s      r)   r*   zAppenderMixin.__init__k   s(    tT//6u%r+   c                    t        | j                        }|6| j                  r*|j                  r| j                  |v r|j                          t	        j
                  | j                        sy |S r,   )r   instancer   flushorm_utilhas_identityr#   sesss     r)   sessionzAppenderMixin.sessiono   sS    dmm,%JJL$$T]]3Kr+   c                    || _         y r,   )rR   )r#   rS   s     r)   rS   zAppenderMixin.session~   s	    	r+   c                V   | j                   }|t        j                  | j                        }|j                  r+t        j                  dt        j                  |      z         t        j                  t        j                  | j                  j                  j                  g      | 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)rS   r
   instance_staterM   detachedr   warnrO   	state_strr   IteratorResultSimpleResultMetaDatar$   r<   r-   _get_collection_historyPASSIVE_NO_INITIALIZEr    scalars	_generate_iter)r#   rR   r%   s      r)   ra   zAppenderMixin._iter   s    ||<--dmm<E~~		C  ))%02 ((++TYY-=-=-F-F,GH		11--dmm<44 +)- gi >>$'--//r+   c                     y r,   r0   )r#   s    r)   __iter__zAppenderMixin.__iter__   s    r+   c                
   | j                   }|V| j                  j                  t        j                  | j
                        t        j                        j                  |      S | j                  |      j                  |      S r,   )
rS   r$   r]   r
   rW   rM   r^   indexedr`   __getitem__)r#   indexrR   s      r)   rf   zAppenderMixin.__getitem__   si    ||<9944))$--800 gen
 >>$'33E::r+   c                   | j                   }|Zt        | j                  j                  t	        j
                  | j                        t        j                        j                        S | j                  |      j                         S r,   )rS   lenr$   r]   r
   rW   rM   r^   r    r`   countrQ   s     r)   rj   zAppenderMixin.count   sk    ||<		11--dmm<44 +	  >>$'--//r+   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)rS   )rM   r   orm_excDetachedInstanceErrorrO   instance_strr$   r=   r8   r5   query_where_criteria	_from_obj_order_by_clauses)r#   rR   rM   ro   s       r)   r`   zAppenderMixin._generate   s     ==<!(+D|33  ,,X6		G  $$TYY%<%<d$KEJJtyy667E $ 4 4.."&"8"8r+   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r#   iterators     r)   add_allzAppenderMixin.add_all   s     	8$r+   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

        Nrt   r#   items     r)   addzAppenderMixin.add   s     	D6"r+   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.

        Nrt   rv   s     r)   extendzAppenderMixin.extend   s     	8$r+   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.

        Nrt   rz   s     r)   appendzAppenderMixin.append   s     	D6"r+   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_implrz   s     r)   removezAppenderMixin.remove   s     	$r+   )returnr   )rS   r   r   None)r   zIterator[_T])rg   r   r   r   )r   intr,   )rw   zIterable[_T]r   r   )r{   r   r   r   )r-   r.   r/   __doc__r8   r*   propertyrS   setterra   r   rc   rf   rj   r`   rx   r|   r~   r   r   __classcell__)rK   s   @r)   r9   r9   b   sw     K&   ^^ 0. 	;
04%#%# r+   r9   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   r0   r+   r)   r   r     s    r+   r   c                J    d| j                   z   }t        |t        | fd| i      S )zAReturn a new class with AppenderQuery functionality layered over.Appenderr8   )r-   typer9   )clsnames     r)   r;   r;     s)    $D}c*]C,@AAr+   N)%r   
__future__r   typingr   r   r   r   r    r
   r   rl   r   r   rO   ro   r   rS   r   	writeonlyr   r   r   r   enginer   r   r   r   r2   RelationshipPropertystrategy_forrF   r9   r   r;   r0   r+   r)   <module>r      s   	 #            # / - ' &    T// /=1 =: ##00i0@& & A&a ,R0 a HM"%uRy Br+   