o
    =a.:                     @   s   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je
ddddgg dg ddG dd deZG dd dZG dd deeZdd Zdd Zee_dS )   )engine)result)ReversibleProxy)StartableContext   )util)object_session)Session)state)greenlet_spawnz:class:`_orm.Session`z:class:`_asyncio.AsyncSession`r   identity_key)__contains____iter__addadd_allexpire
expire_allexpungeexpunge_allget_bindis_modifiedin_transactionin_nested_transaction)dirtydeletednewidentity_map	is_active	autoflushno_autoflushinfo)classmethodsmethods
attributesc                   @   s   e Zd ZdZdZdZd2ddZ	d2ddZdd	 Zde	j
dfd
dZde	j
dfddZ				d3ddZde	j
dfddZdd Zd4ddZd5ddZdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zed*d+ Zd,d- Zd.d/ Zd0d1 ZdS )6AsyncSessionzKAsyncio version of :class:`_orm.Session`.


    .. versionadded:: 1.4

    )bindsbindsync_session_proxied_slots_dispatchNc                 K   s^   d|d< |r|| _ t|}|r|| _dd | D }| td||d| | _| _d S )NTfuturec                 S   s   i | ]
\}}|t |qS  )r   _get_sync_engine_or_connection).0keybr+   r+   T/var/www/html/gps/gps/lib/python3.10/site-packages/sqlalchemy/ext/asyncio/session.py
<dictcomp>J   s    
z)AsyncSession.__init__.<locals>.<dictcomp>)r&   r%   r+   )	r&   r   r,   r%   items_assign_proxiedr	   r'   r(   )selfr&   r%   kwr+   r+   r0   __init__B   s   
zAsyncSession.__init__c                    s   t | jj|||dI dH S )aT  Expire and refresh the attributes on the given instance.

        A query will be issued to the database and all attributes will be
        refreshed with their current database value.

        This is the async version of the :meth:`_orm.Session.refresh` method.
        See that method for a complete description of all options.

        )attribute_nameswith_for_updateN)r   r'   refresh)r4   instancer7   r8   r+   r+   r0   r9   S   s   zAsyncSession.refreshc                    s"   t || jg|R i |I dH S )a  Invoke the given sync callable passing sync self as the first
        argument.

        This method maintains the asyncio event loop all the way through
        to the database connection by running the given callable in a
        specially instrumented greenlet.

        E.g.::

            with AsyncSession(async_engine) as session:
                await session.run_sync(some_business_method)

        .. note::

            The provided callable is invoked inline within the asyncio event
            loop, and will block on traditional IO calls.  IO within this
            callable should only call into SQLAlchemy's asyncio database
            APIs which will be properly adapted to the greenlet context.

        .. seealso::

            :ref:`session_run_sync`
        N)r   r'   )r4   fnargr5   r+   r+   r0   run_syncg   s    zAsyncSession.run_syncc                    s4   | ddi}t| jj|f|||d|I dH S )zQExecute a statement and return a buffered
        :class:`_engine.Result` object.prebuffer_rowsTparamsexecution_optionsbind_argumentsN)unionr   r'   execute)r4   	statementr@   rA   rB   r5   r+   r+   r0   rD      s   zAsyncSession.executec                    s*   | j |f|||d|I dH }| S )z/Execute a statement and return a scalar result.r?   N)rD   scalarr4   rE   r@   rA   rB   r5   r   r+   r+   r0   rF      s   
zAsyncSession.scalarFc              	      s"   t | jj||||||dI dH S )zjReturn an instance based on the given primary key identifier,
        or ``None`` if not found.


        )optionspopulate_existingr8   identity_tokenN)r   r'   get)r4   entityidentrH   rI   r8   rJ   r+   r+   r0   rK      s   zAsyncSession.getc                    s>   | ddi}t| jj|f|||d|I dH }t|S )zXExecute a statement and return a streaming
        :class:`_asyncio.AsyncResult` object.stream_resultsTr?   N)rC   r   r'   rD   _resultAsyncResultrG   r+   r+   r0   stream   s   
zAsyncSession.streamc                    s   t | jj|I dH S )zMark an instance as deleted.

        The database delete operation occurs upon ``flush()``.

        As this operation may need to cascade along unloaded relationships,
        it is awaitable to allow for those queries to take place.


        N)r   r'   delete)r4   r:   r+   r+   r0   rR      s   
zAsyncSession.deleteTc                    s   t | jj||dI dH S )z~Copy the state of a given instance into a corresponding instance
        within this :class:`_asyncio.AsyncSession`.

        )loadN)r   r'   merge)r4   r:   rS   r+   r+   r0   rT      s   
zAsyncSession.mergec                    s   t | jj|dI dH  dS )zuFlush all the object changes to the database.

        .. seealso::

            :meth:`_orm.Session.flush`

        )objectsN)r   r'   flush)r4   rU   r+   r+   r0   rV      s   zAsyncSession.flushc                 C       | j  }|durt|S dS )zReturn the current root transaction in progress, if any.

        :return: an :class:`_asyncio.AsyncSessionTransaction` object, or
         ``None``.

        .. versionadded:: 1.4.18

        N)r'   get_transactionAsyncSessionTransaction_retrieve_proxy_for_targetr4   transr+   r+   r0   rX      s   
	
zAsyncSession.get_transactionc                 C   rW   )zReturn the current nested transaction in progress, if any.

        :return: an :class:`_asyncio.AsyncSessionTransaction` object, or
         ``None``.

        .. versionadded:: 1.4.18

        N)r'   get_nested_transactionrY   rZ   r[   r+   r+   r0   r]     s   


z#AsyncSession.get_nested_transactionc                    s    t | jjI dH }tj|S )zReturn a :class:`_asyncio.AsyncConnection` object corresponding to
        this :class:`.Session` object's transactional state.

        N)r   r'   
connectionr   AsyncConnectionrZ   )r4   sync_connectionr+   r+   r0   r^     s
   zAsyncSession.connectionc                 K      t | S )a  Return an :class:`_asyncio.AsyncSessionTransaction` object.

        The underlying :class:`_orm.Session` will perform the
        "begin" action when the :class:`_asyncio.AsyncSessionTransaction`
        object is entered::

            async with async_session.begin():
                # .. ORM transaction is begun

        Note that database IO will not normally occur when the session-level
        transaction is begun, as database transactions begin on an
        on-demand basis.  However, the begin block is async to accommodate
        for a :meth:`_orm.SessionEvents.after_transaction_create`
        event hook that may perform IO.

        For a general description of ORM begin, see
        :meth:`_orm.Session.begin`.

        rY   r4   r5   r+   r+   r0   begin#  s   zAsyncSession.beginc                 K   s   t | ddS )a:  Return an :class:`_asyncio.AsyncSessionTransaction` object
        which will begin a "nested" transaction, e.g. SAVEPOINT.

        Behavior is the same as that of :meth:`_asyncio.AsyncSession.begin`.

        For a general description of ORM begin nested, see
        :meth:`_orm.Session.begin_nested`.

        T)nestedrb   rc   r+   r+   r0   begin_nested:  s   zAsyncSession.begin_nestedc                       t | jjI dH S )z-Rollback the current transaction in progress.N)r   r'   rollbackr4   r+   r+   r0   rh   G     zAsyncSession.rollbackc                    rg   )z+Commit the current transaction in progress.N)r   r'   commitri   r+   r+   r0   rk   K  rj   zAsyncSession.commitc                    rg   )aP  Close out the transactional resources and ORM objects used by this
        :class:`_asyncio.AsyncSession`.

        This expunges all ORM objects associated with this
        :class:`_asyncio.AsyncSession`, ends any transaction in progress and
        :term:`releases` any :class:`_asyncio.AsyncConnection` objects which
        this :class:`_asyncio.AsyncSession` itself has checked out from
        associated :class:`_asyncio.AsyncEngine` objects. The operation then
        leaves the :class:`_asyncio.AsyncSession` in a state which it may be
        used again.

        .. tip::

            The :meth:`_asyncio.AsyncSession.close` method **does not prevent
            the Session from being used again**. The
            :class:`_asyncio.AsyncSession` itself does not actually have a
            distinct "closed" state; it merely means the
            :class:`_asyncio.AsyncSession` will release all database
            connections and ORM objects.


        .. seealso::

            :ref:`session_closing` - detail on the semantics of
            :meth:`_asyncio.AsyncSession.close`

        N)r   r'   closeri   r+   r+   r0   rl   O  s   zAsyncSession.closec                    rg   )z2Close all :class:`_asyncio.AsyncSession` sessions.N)r   r'   	close_allri   r+   r+   r0   rm   m  s   zAsyncSession.close_allc                    s   | S Nr+   ri   r+   r+   r0   
__aenter__r  s   zAsyncSession.__aenter__c                    s   |   I d H  d S rn   )rl   r4   type_value	tracebackr+   r+   r0   	__aexit__u  s   zAsyncSession.__aexit__c                 C   ra   rn   )_AsyncSessionContextManagerri   r+   r+   r0   _maker_context_managerx  s   z#AsyncSession._maker_context_manager)NN)NFNN)Trn   )__name__
__module____qualname____doc__	__slots__dispatchr6   r9   r=   r   
EMPTY_DICTrD   rF   rK   rQ   rR   rT   rV   rX   r]   r^   rd   rf   rh   rk   rl   classmethodrm   ro   rt   rv   r+   r+   r+   r0   r$      sR    







	

r$   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ru   c                 C   s
   || _ d S rn   )async_session)r4   r   r+   r+   r0   r6   ~  s   
z$_AsyncSessionContextManager.__init__c                    s$   | j  | _| j I d H  | j S rn   )r   rd   r\   ro   ri   r+   r+   r0   ro     s   z&_AsyncSessionContextManager.__aenter__c                    s2   | j |||I d H  | j|||I d H  d S rn   )r\   rt   r   rp   r+   r+   r0   rt     s   z%_AsyncSessionContextManager.__aexit__N)rw   rx   ry   r6   ro   rt   r+   r+   r+   r0   ru   }  s    ru   c                   @   sT   e Zd ZdZdZdddZedd Zdd	 Zd
d Z	dd Z
dddZdd ZdS )rY   a  A wrapper for the ORM :class:`_orm.SessionTransaction` object.

    This object is provided so that a transaction-holding object
    for the :meth:`_asyncio.AsyncSession.begin` may be returned.

    The object supports both explicit calls to
    :meth:`_asyncio.AsyncSessionTransaction.commit` and
    :meth:`_asyncio.AsyncSessionTransaction.rollback`, as well as use as an
    async context manager.


    .. versionadded:: 1.4

    )sessionsync_transactionre   Fc                 C   s   || _ || _d | _d S rn   )r   re   r   )r4   r   re   r+   r+   r0   r6     s   
z AsyncSessionTransaction.__init__c                 C   s   |   d uo
|   jS rn   )_sync_transactionr   ri   r+   r+   r0   r     s   z!AsyncSessionTransaction.is_activec                 C   s   | j s|   | j S rn   )r   _raise_for_not_startedri   r+   r+   r0   r     s   z)AsyncSessionTransaction._sync_transactionc                       t |  jI dH  dS )z2Roll back this :class:`_asyncio.AsyncTransaction`.N)r   r   rh   ri   r+   r+   r0   rh     s   z AsyncSessionTransaction.rollbackc                    r   )z/Commit this :class:`_asyncio.AsyncTransaction`.N)r   r   rk   ri   r+   r+   r0   rk     s   zAsyncSessionTransaction.commitc                    s@   |  t| jr| jjjn| jjjI d H | _|r| j  | S rn   )	r3   r   re   r   r'   rf   rd   r   	__enter__)r4   is_ctxmanagerr+   r+   r0   start  s   
zAsyncSessionTransaction.startc                    s    t |  j|||I d H  d S rn   )r   r   __exit__rp   r+   r+   r0   rt     s   z!AsyncSessionTransaction.__aexit__N)F)rw   rx   ry   rz   r{   r6   propertyr   r   rh   rk   r   rt   r+   r+   r+   r0   rY     s    


rY   c                 C   s   t | }|durt|S dS )a  Return the :class:`_asyncio.AsyncSession` to which the given instance
    belongs.

    This function makes use of the sync-API function
    :class:`_orm.object_session` to retrieve the :class:`_orm.Session` which
    refers to the given instance, and from there links it to the original
    :class:`_asyncio.AsyncSession`.

    If the :class:`_asyncio.AsyncSession` has been garbage collected, the
    return value is ``None``.

    This functionality is also available from the
    :attr:`_orm.InstanceState.async_session` accessor.

    :param instance: an ORM mapped instance
    :return: an :class:`_asyncio.AsyncSession` object, or ``None``.

    .. versionadded:: 1.4.18

    N)r   r   )r:   r   r+   r+   r0   async_object_session  s   r   c                 C   s   t j| ddS )a  Return the :class:`_asyncio.AsyncSession` which is proxying the given
    :class:`_orm.Session` object, if any.

    :param session: a :class:`_orm.Session` instance.
    :return: a :class:`_asyncio.AsyncSession` instance, or ``None``.

    .. versionadded:: 1.4.18

    F)
regenerate)r$   rZ   )r   r+   r+   r0   r     s   
r   N) r   r   rO   baser   r   r   ormr   r	   r
   _instance_stateutil.concurrencyr   create_proxy_methodsr$   ru   rY   r   r   _async_providerr+   r+   r+   r0   <module>   s2     O>
