o
    i&                     @  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 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/ Z0G d(d) d)eZ1ej2j3d*d+G d,d- d-e Z4G d.d/ d/ee/ Z5G d0d1 d1e5e/ ee/ Z6d9d6d7Z7d8S ):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)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                   @  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   OrderedIdentitySetunchanged_itemsadded_itemsdeleted_items_reconcile_collection)selfr'   r)   r+   r,   coll r9   V/var/www/html/arapca_proje/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__r;   r9   r9   r9   r:   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 r<   )r   AttributeImplr;   rG   tuplerI   r0   r@   AppenderMixinmromixin_user_query)r7   rA   rC   rE   rG   rI   r@   rK   r9   r9   r:   r;   X   s   




zDynamicAttributeImpl.__init__r<   )rA   rB   rC   rD   rE   rF   rG   rH   rI   r   r@   rJ   rK   r   r.   r/   )	r=   r>   r?   _supports_dynamic_iterationr&   r   collection_history_cls__annotations__r;   r9   r9   r9   r:   r(   S   s   
 	r(   dynamic)lazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r=   r>   r?   r(   
impl_classr9   r9   r9   r:   rV   p   s    rV   c                      s   e Zd ZU dZdZded< ded< d9 fddZed:ddZej	d;ddZd<ddZ
er3d=ddZed>ddZed?d"dZd@d%dZdAd&d'Z	dBdCd*d+ZdDd.d/ZdEd1d2ZdDd3d4ZdEd5d6ZdEd7d8Z  ZS )FrN   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 r<   )r   r;   rG   super)r7   r'   r)   	__class__r9   r:   r;      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 r<   )r   instancer1   flushorm_utilhas_identityr7   sessr9   r9   r:   session   s   
zAppenderMixin.sessionrc   r   c                 C  s
   || _ d S r<   )rb   )r7   rc   r9   r9   r:   rc      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)rc   r   instance_stater]   detachedr   warnr_   	state_strr   IteratorResultSimpleResultMetaDatar'   rA   r=   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr4   scalars	_generate_iter)r7   rb   r)   r9   r9   r:   rq      s.   
	zAppenderMixin._iterIterator[_T]c                 C     d S r<   r9   )r7   r9   r9   r:   __iter__   s    zAppenderMixin.__iter__indexintr$   c                 C  rs   r<   r9   r7   ru   r9   r9   r:   __getitem__      zAppenderMixin.__getitem__sliceList[_T]c                 C  rs   r<   r9   rw   r9   r9   r:   rx      ry   Union[int, slice]Union[_T, List[_T]]c                 C  s<   | j }|d u r| jt| jtj|S | 	|
|S r<   )rc   r'   rm   r   rf   r]   r   rn   indexedrp   rx   )r7   ru   rb   r9   r9   r:   rx      s   
c                 C  s:   | j }|d u rt| jt| jtjj	S | 
| S r<   )rc   lenr'   rm   r   rf   r]   r   rn   r4   rp   countra   r9   r9   r:   r      s   
zAppenderMixin.countrb   	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)rc   )r]   r   orm_excDetachedInstanceErrorr_   instance_strr'   rC   r@   rG   query_where_criteria	_from_objrX   )r7   rb   r]   r   r9   r9   r:   rp      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_add_all_implr7   r   r9   r9   r:   add_all   s   zAppenderMixin.add_allitemc                 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   r7   r   r9   r9   r:   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   r9   r9   r:   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   r9   r9   r:   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)_remove_implr   r9   r9   r:   remove  r   zAppenderMixin.remove)r'   r(   r)   r*   r.   r/   )r.   r\   )rc   r   r.   r/   )r.   rd   )r.   rr   )ru   rv   r.   r$   )ru   rz   r.   r{   )ru   r|   r.   r}   )r.   rv   r<   )rb   r\   r.   r   )r   r   r.   r/   )r   r$   r.   r/   )r=   r>   r?   __doc__r@   rS   r;   propertyrc   setterrq   r   rt   r   rx   r   rp   r   r   r   r   r   __classcell__r9   r9   rZ   r:   rN   u   s2   
 
	








		rN   c                   @  s   e Zd ZdZdS )r0   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   r9   r9   r9   r:   r0   %  s    r0   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.Appenderr@   )r=   typerN   )r   namer9   r9   r:   rP   0  s   
rP   N)r   r   r.   r   )8r   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   r    r   r   r   r   r   r_   baser   r   r   rc   r   	writeonlyr   r   r   r   enginer   r   mapperr   r   r   r)   r    r!   eventr"   sql.elementsr#   r$   r&   r(   RelationshipPropertystrategy_forrV   rN   r0   rP   r9   r9   r9   r:   <module>   sX    1