o
    i!                     @  sp  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rvddlmZ ededZG dd deZG dd deZejj ddG dd deZ!G d d! d!ee Z"G d"d# d#e"e ee Z#d$d% Z$d&S )'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                   @  s   e Zd ZdddZdS )DynamicCollectionHistoryNc                 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)selfattrstatepassiveapply_tocoll r&   V/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/sqlalchemy/orm/dynamic.py__init__2   s   




z!DynamicCollectionHistory.__init__N)__name__
__module____qualname__r(   r&   r&   r&   r'   r   1   s    r   c                   @  s    e Zd ZdZeZ	dddZdS )DynamicAttributeImplTNc           	      K  sb   t jj| ||||fi | || _|rt|| _|st| _d S t|	 v r*|| _d S t
|| _d S r)   )r	   AttributeImplr(   target_mappertupleorder_byr   query_classAppenderMixinmromixin_user_query)	r    class_keytypecallabledispatchr/   r1   r2   kwr&   r&   r'   r(   D   s   



zDynamicAttributeImpl.__init__r)   )r*   r+   r,   _supports_dynamic_iterationr   collection_history_clsr(   r&   r&   r&   r'   r-   @   s
    
r-   dynamic)lazyc                   @  s   e Zd ZeZdS )
DynaLoaderN)r*   r+   r,   r-   
impl_classr&   r&   r&   r'   r?   ]   s    r?   c                      s   e Zd ZdZdZ fddZed(ddZejd)ddZdd Z	e
r(d*ddZd+ddZd,ddZd-ddZd.ddZd/d d!Zd.d"d#Zd/d$d%Zd/d&d'Z  ZS )0r3   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    Nc                   s"   t | |jd  t || d S r)   )r   r(   r/   super)r    r!   r"   	__class__r&   r'   r(   k   s   zAppenderMixin.__init__returnr   c                 C  sD   t | j}|d ur| jr|jr| j|v r|  t| js d S |S r)   )r   instancer   flushorm_utilhas_identityr    sessr&   r&   r'   sessiono   s   

zAppenderMixin.sessionrK   Nonec                 C  s
   || _ d S r)   )rJ   )r    rK   r&   r&   r'   rK   ~   s   
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)_source_supports_scalars)rK   r	   instance_staterE   detachedr   warnrG   	state_strr   IteratorResultSimpleResultMetaDatar!   r6   r*   _get_collection_historyPASSIVE_NO_INITIALIZEr   scalars	_generate_iter)r    rJ   r"   r&   r&   r'   rX      s*   
	zAppenderMixin._iterIterator[_T]c                 C  s   d S r)   r&   )r    r&   r&   r'   __iter__   s   zAppenderMixin.__iter__indexr   r   c                 C  s<   | j }|d u r| jt| jtj|S | |	|S r)   )
rK   r!   rT   r	   rN   rE   rU   indexedrW   __getitem__)r    r[   rJ   r&   r&   r'   r]      s   
zAppenderMixin.__getitem__intc                 C  s:   | j }|d u rt| jt| jtjjS | 	|
 S r)   )rK   lenr!   rT   r	   rN   rE   rU   r   rW   countrI   r&   r&   r'   r`      s   
zAppenderMixin.countc                 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)rK   )rE   r   orm_excDetachedInstanceErrorrG   instance_strr!   r7   r2   r/   query_where_criteria	_from_obj_order_by_clauses)r    rJ   rE   rd   r&   r&   r'   rW      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r    rh   r&   r&   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

        Nrk   r    ro   r&   r&   r'   add   s   zAppenderMixin.addc                 C  rj   )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.

        Nrk   rm   r&   r&   r'   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.

        Nrk   rq   r&   r&   r'   append   s   zAppenderMixin.appendc                 C  rj   )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_implrq   r&   r&   r'   remove   rt   zAppenderMixin.remove)rD   r   )rK   r   rD   rL   )rD   rY   )r[   r   rD   r   )rD   r^   r)   )rh   ri   rD   rL   )ro   r   rD   rL   )r*   r+   r,   __doc__r2   r(   propertyrK   setterrX   r   rZ   r]   r`   rW   rn   rr   rs   ru   rw   __classcell__r&   r&   rB   r'   r3   b   s&    








		r3   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,   rx   r&   r&   r&   r'   r     s    r   c                 C  s   d| j  }t|t| fd| iS )zAReturn a new class with AppenderQuery functionality layered over.Appenderr2   )r*   typer3   )clsnamer&   r&   r'   r5     s   
r5   N)%rx   
__future__r   typingr   r   r   r   r    r	   r
   ra   r   r   rG   rd   r   rK   r   	writeonlyr   r   r   r   enginer   r   r   r   r-   RelationshipPropertystrategy_forr?   r3   r   r5   r&   r&   r&   r'   <module>   s<   	 %