o
    - hX&                     @  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   OrderedIdentitySetZunchanged_itemsadded_itemsdeleted_itemsZ_reconcile_collection)selfr&   r(   r*   r+   coll r6   ^/var/www/html/figdemos/bartoux_crm/venv/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__r8   r6   r6   r6   r7   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 r9   )r   AttributeImplr8   rD   tuplerF   r/   r=   AppenderMixinmromixin_user_query)r4   r>   r@   rB   rD   rF   r=   rH   r6   r6   r7   r8   W   s   




zDynamicAttributeImpl.__init__r9   )r>   r?   r@   rA   rB   rC   rD   rE   rF   r   r=   rG   rH   r   r-   r.   )	r:   r;   r<   Z_supports_dynamic_iterationr%   r   Zcollection_history_cls__annotations__r8   r6   r6   r6   r7   r'   R   s   
 	r'   dynamic)lazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r:   r;   r<   r'   
impl_classr6   r6   r6   r7   rQ   o   s    rQ   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 )@rK   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 r9   )r   r8   rD   super)r4   r&   r(   	__class__r6   r7   r8   ~   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 r9   )r   instancer0   flushorm_utilhas_identityr4   sessr6   r6   r7   session   s   
zAppenderMixin.sessionr^   r   c                 C  s
   || _ d S r9   )r]   )r4   r^   r6   r6   r7   r^      s   
1Union[result.ScalarResult[_T], result.Result[_T]]c                 C  s~   | j }|d u r8t| j}|jrtdt|  t	j
t	| jjjgt| 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)_source_supports_scalars)r^   r   instance_staterX   detachedr   warnrZ   	state_strr   IteratorResultSimpleResultMetaDatar&   r>   r:   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr2   scalars	_generate_iter)r4   r]   r(   r6   r6   r7   rl      s.   
	zAppenderMixin._iterIterator[_T]c                 C  s   d S r9   r6   )r4   r6   r6   r7   __iter__   s    zAppenderMixin.__iter__indexr   Union[_T, List[_T]]c                 C  s<   | j }|d u r| jt| jtj|S | 	|
|S r9   )r^   r&   rh   r   ra   rX   r   ri   Zindexedrk   __getitem__)r4   ro   r]   r6   r6   r7   rq      s   
zAppenderMixin.__getitem__intc                 C  s:   | j }|d u rt| jt| jtjj	S | 
| S r9   )r^   lenr&   rh   r   ra   rX   r   ri   r2   rk   countr\   r6   r6   r7   rt      s   
zAppenderMixin.countr]   	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)r^   )rX   r   orm_excDetachedInstanceErrorrZ   instance_strr&   r@   r=   rD   query_where_criteria	_from_objrS   )r4   r]   rX   ry   r6   r6   r7   rk      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r4   r|   r6   r6   r7   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

        Nr   r4   r   r6   r6   r7   add   s   zAppenderMixin.addc                 C  r~   )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   r6   r6   r7   extend     zAppenderMixin.extendc                 C  r   )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   r6   r6   r7   append  s   zAppenderMixin.appendc                 C  r~   )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_implr   r6   r6   r7   remove  r   zAppenderMixin.remove)r&   r'   r(   r)   r-   r.   )r-   rW   )r^   r   r-   r.   )r-   r_   )r-   rm   )ro   r   r-   rp   )r-   rr   r9   )r]   rW   r-   ru   )r|   r}   r-   r.   )r   r#   r-   r.   )r:   r;   r<   __doc__r=   rN   r8   propertyr^   setterrl   r
   rn   rq   rt   rk   r   r   r   r   r   __classcell__r6   r6   rU   r7   rK   t   s*   
 
	








		rK   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<   r   r6   r6   r6   r7   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=   )r:   typerK   )r   namer6   r6   r7   rM   )  s   
rM   N)r   r   r-   r   )7r   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r    r   r   rv   r   r   rZ   baser   ry   r   r^   r   	writeonlyr   r   r   r   enginer   r   mapperr   r   r   r(   r   r    eventr!   Zsql.elementsr"   r#   r%   r'   RelationshipPropertystrategy_forrQ   rK   r/   rM   r6   r6   r6   r7   <module>   sV    +