o
    !Zh:&                     @  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   ZOrderedIdentitySetZunchanged_itemsadded_itemsZdeleted_itemsZ_reconcile_collection)selfr&   r(   r*   r+   Zcoll r3   M/var/www/html/lang_env/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__r5   r3   r3   r3   r4   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 r6   )r   ZAttributeImplr5   rA   tuplerC   r/   r:   AppenderMixinmromixin_user_query)r2   r;   r=   r?   rA   rC   r:   rE   r3   r3   r4   r5   W   s   




zDynamicAttributeImpl.__init__r6   )r;   r<   r=   r>   r?   r@   rA   rB   rC   r   r:   rD   rE   r   r-   r.   )	r7   r8   r9   Z_supports_dynamic_iterationr%   r   Zcollection_history_cls__annotations__r5   r3   r3   r3   r4   r'   R   s   
 	r'   Zdynamic)Zlazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r7   r8   r9   r'   Z
impl_classr3   r3   r3   r4   rK   o   s    rK   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 )@rG   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 r6   )r   r5   rA   super)r2   r&   r(   	__class__r3   r4   r5   ~   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 r6   )r   instancer0   flushorm_utilZhas_identityr2   sessr3   r3   r4   session   s   
zAppenderMixin.sessionrV   r   c                 C  s
   || _ d S r6   )rU   )r2   rV   r3   r3   r4   rV      s   
1Union[result.ScalarResult[_T], result.Result[_T]]c                 C  sz   | j }|d u r6t| j}|jrtdt|  t	j
t	| jjjg| 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)Z_source_supports_scalars)rV   r   instance_staterQ   Zdetachedr   warnrS   Z	state_strr   ZIteratorResultZSimpleResultMetaDatar&   r;   r7   _get_collection_historyr   PASSIVE_NO_INITIALIZEr1   Zscalars	_generate_iter)r2   rU   r(   r3   r3   r4   r]      s*   
	zAppenderMixin._iterIterator[_T]c                 C  s   d S r6   r3   )r2   r3   r3   r4   __iter__   s    zAppenderMixin.__iter__indexr   Union[_T, List[_T]]c                 C  s<   | j }|d u r| jt| jtj|S | 	|
|S r6   )rV   r&   rZ   r   rX   rQ   r   r[   Zindexedr\   __getitem__)r2   r`   rU   r3   r3   r4   rb      s   
zAppenderMixin.__getitem__intc                 C  s:   | j }|d u rt| jt| jtjj	S | 
| S r6   )rV   lenr&   rZ   r   rX   rQ   r   r[   r1   r\   countrT   r3   r3   r4   re      s   
zAppenderMixin.countrU   	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)rV   )rQ   r   orm_excZDetachedInstanceErrorrS   Zinstance_strr&   r=   r:   rA   queryZ_where_criteriaZ	_from_objrL   )r2   rU   rQ   rh   r3   r3   r4   r\      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Z_add_all_implr2   ri   r3   r3   r4   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

        Nrl   r2   ro   r3   r3   r4   add   s   zAppenderMixin.addc                 C  rk   )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.

        Nrl   rm   r3   r3   r4   extend      zAppenderMixin.extendc                 C  rp   )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.

        Nrl   rq   r3   r3   r4   append	  s   zAppenderMixin.appendc                 C  rk   )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   r3   r3   r4   remove  rt   zAppenderMixin.remove)r&   r'   r(   r)   r-   r.   )r-   rP   )rV   r   r-   r.   )r-   rW   )r-   r^   )r`   r   r-   ra   )r-   rc   r6   )rU   rP   r-   rf   )ri   rj   r-   r.   )ro   r#   r-   r.   )r7   r8   r9   __doc__r:   rJ   r5   propertyrV   setterr]   r
   r_   rb   re   r\   rn   rr   rs   ru   rv   __classcell__r3   r3   rN   r4   rG   t   s*   
 
	








		rG   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)r7   r8   r9   rw   r3   r3   r3   r4   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.ZAppenderr:   )r7   typerG   )r{   namer3   r3   r4   rI   '  s   
rI   N)r{   r   r-   r|   )7rw   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r    r   r   rg   r   r   rS   baser   rh   r   rV   r   Z	writeonlyr   r   r   r   Zenginer   r   Zmapperr   r   r   r(   r   r    eventr!   Zsql.elementsr"   r#   r%   r'   ZRelationshipPropertyZstrategy_forrK   rG   r/   rI   r3   r3   r3   r4   <module>   sV    )