U
    !Jj&                     @  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/ Z6d2d3d4d5d6Z7d7S )8zDynamic 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ddddd	ZdS )DynamicCollectionHistoryNDynamicAttributeImplInstanceState[_T]r   z&Optional[DynamicCollectionHistory[_T]]None)attrstatepassiveapply_toreturnc                 C  s`   |r8t ||d}t|| _|j| _|j| _d| _n$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   w/var/www/api.dyntland.com/public_html/python_social_comments/venv/lib/python3.8/site-packages/sqlalchemy/orm/dynamic.py__init__>   s    


z!DynamicCollectionHistory.__init__)N)__name__
__module____qualname__r5   r3   r3   r3   r4   r&   =   s    r&   c                
   @  s@   e Zd ZU dZee Zded< dddddd	d
dddddZdS )r'   TzType[AppenderMixin[Any]]query_classNz#Union[Type[Any], AliasedClass[Any]]strz"_Dispatch[QueryableAttribute[Any]]z
Mapper[_T]r   z!Optional[Type[AppenderMixin[_T]]]r   r)   )class_keydispatchtarget_mapperorder_byr9   kwr.   c                 K  sZ   t jj| ||d |f| || _|r,t|| _|s8t| _nt|	 krL|| _n
t
|| _d S N)r   ZAttributeImplr5   r>   tupler?   r/   r9   AppenderMixinmromixin_user_query)r2   r;   r<   r=   r>   r?   r9   r@   r3   r3   r4   r5   W   s"    
    
zDynamicAttributeImpl.__init__)N)	r6   r7   r8   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)r6   r7   r8   r'   Z
impl_classr3   r3   r3   r4   rG   o   s   rG   c                      s0  e Zd ZU dZdZded< ded< ddd	d
 fddZeddddZej	dd	dddZddddZ
erddddZedddddZedd dd!dZd"d#dd$dZddd%d&Zd8dd'd(d)d*Zd+d	d,d-d.Zdd	d/d0d1Zd+d	d,d2d3Zdd	d/d4d5Zdd	d/d6d7Z  ZS )9rC   zTA mixin that expects to be mixing in a Query class with
    AbstractAppender.


    NzOptional[Type[Query[_T]]]r9   zTuple[ColumnElement[Any], ...]_order_by_clausesr'   r(   r)   )r*   r+   r.   c                   s"   t | |jd  t || d S rA   )r   r5   r>   super)r2   r*   r+   	__class__r3   r4   r5   ~   s    zAppenderMixin.__init__zOptional[Session])r.   c                 C  sB   t | j}|d k	r*|jr*| j|kr*|  t| js:d S |S d S rA   )r   instancer0   flushorm_utilZhas_identityr2   sessr3   r3   r4   session   s    
zAppenderMixin.sessionr   )rQ   r.   c                 C  s
   || _ d S rA   )rP   )r2   rQ   r3   r3   r4   rQ      s    z1Union[result.ScalarResult[_T], result.Result[_T]]c                 C  s   | j }|d krpt| j}|jr4tdt|  t	j
t	| jjjgt| jt| jtjjdd S | | S d 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)rQ   r   instance_staterL   Zdetachedr   warnrN   Z	state_strr   ZIteratorResultZSimpleResultMetaDatar*   r;   r6   iter_get_collection_historyr   PASSIVE_NO_INITIALIZEr1   Zscalars	_generate_iter)r2   rP   r+   r3   r3   r4   rX      s(    

zAppenderMixin._iterzIterator[_T]c                 C  s   d S rA   r3   )r2   r3   r3   r4   __iter__       zAppenderMixin.__iter__intr$   )indexr.   c                 C  s   d S rA   r3   r2   r\   r3   r3   r4   __getitem__   s    zAppenderMixin.__getitem__slicezList[_T]c                 C  s   d S rA   r3   r]   r3   r3   r4   r^      s    zUnion[int, slice]zUnion[_T, List[_T]]c                 C  s@   | j }|d kr,| jt| jtj|S | 	|
|S d S rA   )rQ   r*   rU   r   rR   rL   r   rV   ZindexedrW   r^   )r2   r\   rP   r3   r3   r4   r^      s    
c                 C  s>   | j }|d kr,t| jt| jtjj	S | 
| S d S rA   )rQ   lenr*   rU   r   rR   rL   r   rV   r1   rW   countrO   r3   r3   r4   ra      s    
zAppenderMixin.countz	Query[_T])rP   r.   c                 C  s~   | j }|d kr:t|}|d kr:tdt|| jjf | jrT| 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)rQ   )rL   r   orm_excZDetachedInstanceErrorrN   Zinstance_strr*   r<   r9   r>   queryZ_where_criteriaZ	_from_objrH   )r2   rP   rL   rc   r3   r3   r4   rW      s     zAppenderMixin._generatezIterable[_T])iteratorr.   c                 C  s   |  | 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   rd   r3   r3   r4   add_all   s    zAppenderMixin.add_all)itemr.   c                 C  s   |  |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

        Nre   r2   rh   r3   r3   r4   add   s    zAppenderMixin.addc                 C  s   |  | dS )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.

        Nre   rf   r3   r3   r4   extend  s    zAppenderMixin.extendc                 C  s   |  |g dS )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.

        Nre   ri   r3   r3   r4   append  s    zAppenderMixin.appendc                 C  s   |  | dS )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_implri   r3   r3   r4   remove  s    zAppenderMixin.remove)N)r6   r7   r8   __doc__r9   rF   r5   propertyrQ   setterrX   r   rY   r   r^   ra   rW   rg   rj   rk   rl   rm   __classcell__r3   r3   rJ   r4   rC   t   s0   

	
 		rC   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)r6   r7   r8   rn   r3   r3   r3   r4   r/   $  s   r/   r   ztype[AppenderMixin[Any]])clsr.   c                 C  s   d| j  }t|t| fd| iS )zAReturn a new class with AppenderQuery functionality layered over.ZAppenderr9   )r6   typerC   )rr   namer3   r3   r4   rE   /  s    
rE   N)8rn   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   r    r   r   rb   r   r   rN   baser   rc   r   rQ   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_forrG   rC   r/   rE   r3   r3   r3   r4   <module>	   sV    1