o
    ?ÊÆhF&  ã                   @  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rÄ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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 )9zâDynamic 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   ÚOrderedIdentitySetÚunchanged_itemsÚadded_itemsÚdeleted_itemsÚ_reconcile_collection)Úselfr&   r(   r*   r+   Úcoll© r8   úc/var/www/html/HyperOracle/crypto_trader/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:   r8   r8   r8   r9   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:   rF   ÚtuplerH   r/   r?   ÚAppenderMixinÚmroÚmixin_user_query)r6   r@   rB   rD   rF   rH   r?   rJ   r8   r8   r9   r:   W   s   

ÿÿ


zDynamicAttributeImpl.__init__r;   )r@   rA   rB   rC   rD   rE   rF   rG   rH   r   r?   rI   rJ   r   r-   r.   )	r<   r=   r>   Ú_supports_dynamic_iterationr%   r   Úcollection_history_clsÚ__annotations__r:   r8   r8   r8   r9   r'   R   s   
 	ùr'   Údynamic)Úlazyc                   @  s   e Zd ZeZdS )Ú
DynaLoaderN)r<   r=   r>   r'   Ú
impl_classr8   r8   r8   r9   rU   o   s    rU   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 )@rM   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:   rF   Úsuper)r6   r&   r(   ©Ú	__class__r8   r9   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   Úinstancer0   ÚflushÚorm_utilÚhas_identity©r6   Úsessr8   r8   r9   Úsessionˆ   s   
zAppenderMixin.sessionrb   r   c                 C  s
   || _ d S r;   )ra   )r6   rb   r8   r8   r9   rb   ’   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 )Nz•Instance %s is detached, dynamic relationship cannot return a correct result.   This warning will become a DetachedInstanceError in a future release.T)Ú_source_supports_scalars)rb   r   Úinstance_stater\   Údetachedr   Úwarnr^   Ú	state_strr   ÚIteratorResultÚSimpleResultMetaDatar&   r@   r<   Ú_get_collection_historyr   ÚPASSIVE_NO_INITIALIZEr3   ÚscalarsÚ	_generateÚ_iter)r6   ra   r(   r8   r8   r9   ro   –   s*   ýÿ
þúù	zAppenderMixin._iterúIterator[_T]c                 C  s   d S r;   r8   )r6   r8   r8   r9   Ú__iter__¯   s   zAppenderMixin.__iter__Úindexr   úUnion[_T, List[_T]]c                 C  s<   | j }|d u r| j t | j¡tj¡ |¡S |  	|¡ 
|¡S r;   )rb   r&   rk   r   re   r\   r   rl   Úindexedrn   Ú__getitem__)r6   rr   ra   r8   r8   r9   ru   ²   s   
þýzAppenderMixin.__getitem__Úintc                 C  s:   | j }|d u rt| j t | j¡tj¡j	ƒS |  
|¡ ¡ S r;   )rb   Úlenr&   rk   r   re   r\   r   rl   r3   rn   Úcountr`   r8   r8   r9   rx   ¼   s   
þüzAppenderMixin.countra   ú	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 )NzŒParent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed)rb   )r\   r   Úorm_excÚDetachedInstanceErrorr^   Úinstance_strr&   rB   r?   rF   ÚqueryÚ_where_criteriaÚ	_from_objrW   )r6   ra   r\   r}   r8   r8   r9   rn   È   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©r6   r€   r8   r8   r9   Ú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ƒ   ©r6   r‡   r8   r8   r9   Úaddó   s   zAppenderMixin.addc                 C  r‚   )zÊ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.

        Nrƒ   r…   r8   r8   r9   Úextend  ó   zAppenderMixin.extendc                 C  rˆ   )z¿Append 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‰   r8   r8   r9   Úappend
  s   zAppenderMixin.appendc                 C  r‚   )z¨Remove 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‰   r8   r8   r9   Úremove  rŒ   zAppenderMixin.remove)r&   r'   r(   r)   r-   r.   )r-   r[   )rb   r   r-   r.   )r-   rc   )r-   rp   )rr   r   r-   rs   )r-   rv   r;   )ra   r[   r-   ry   )r€   r�   r-   r.   )r‡   r#   r-   r.   )r<   r=   r>   Ú__doc__r?   rR   r:   Úpropertyrb   Úsetterro   r
   rq   ru   rx   rn   r†   rŠ   r‹   r�   r�   Ú__classcell__r8   r8   rY   r9   rM   t   s*   
 
	




þ



		rM   c                   @  s   e Zd ZdZdS )r/   zÞA 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�   r8   r8   r8   r9   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.ÚAppenderr?   )r<   ÚtyperM   )r”   Únamer8   r8   r9   rO   (  s   
rO   N)r”   r   r-   r•   )7r�   Ú
__future__r   Útypingr   r   r   r   r   r   r	   r
   r   r   Ú r   r   rz   r   r   r^   Úbaser   r}   r   rb   r   Ú	writeonlyr   r   r   r   Úenginer   r   Úmapperr   r   r   r(   r   r    Úeventr!   Úsql.elementsr"   r#   r%   r'   ÚRelationshipPropertyÚstrategy_forrU   rM   r/   rO   r8   r8   r8   r9   Ú<module>   sV    *