o
    èã¤g]3  ã                   @  sÄ   d dl mZ d dl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jrUddlmZ ddlmZ G dd„ deje	 ƒZdS )é    )ÚannotationsN)ÚAny)ÚOptional)ÚType)ÚUnioné   )ÚConnectionPoolEntry)ÚPool)ÚPoolProxiedConnection)ÚPoolResetStateé   )Úevent)Úutil)ÚEngine)ÚDBAPIConnectionc                   @  s¸   e Zd ZdZdZeZe d¡e	d6d
d„ƒƒZ
e	d7dd„ƒZd8dd„Zd8dd„Zd9dd„Zd:d d!„Ze d"ddgd#d$„ ¡d;d'd(„ƒZd<d+d,„Zd<d-d.„Zd8d/d0„Zd8d1d2„Zd=d3d4„Zd5S )>Ú
PoolEventsaœ  Available events for :class:`_pool.Pool`.

    The methods here define the name of an event as well
    as the names of members that are passed to listener
    functions.

    e.g.::

        from sqlalchemy import event


        def my_on_checkout(dbapi_conn, connection_rec, connection_proxy):
            "handle an on checkout event"


        event.listen(Pool, "checkout", my_on_checkout)

    In addition to accepting the :class:`_pool.Pool` class and
    :class:`_pool.Pool` instances, :class:`_events.PoolEvents` also accepts
    :class:`_engine.Engine` objects and the :class:`_engine.Engine` class as
    targets, which will be resolved to the ``.pool`` attribute of the
    given engine or the :class:`_pool.Pool` class::

        engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test")

        # will associate with engine.pool
        event.listen(engine, "checkout", my_on_checkout)

    ÚSomeEngineOrPoolzsqlalchemy.engineÚtargetú-Union[Pool, Type[Pool], Engine, Type[Engine]]Ú
identifierÚstrÚreturnú!Optional[Union[Pool, Type[Pool]]]c                 C  sr   t jstjjj}t|tƒrt||ƒrt	S t|t	ƒsJ ‚|S t||ƒr%|j
S t|t	ƒr,|S t|dƒr7| ¡  d S d S )NÚ_no_async_engine_events)ÚtypingÚTYPE_CHECKINGr   Ú	preloadedÚenginer   Ú
isinstanceÚtypeÚ
issubclassr	   ÚpoolÚhasattrr   )Úclsr   r   r   © r$   úX/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/pool/events.pyÚ_accept_with=   s   





zPoolEvents._accept_withÚ	event_keyúevent._EventKey[Pool]Úkwr   ÚNonec                 K  s(   |j }| d|j¡ |jdi |¤Ž d S )NÚasyncior$   )Údispatch_targetÚ
setdefaultÚ_is_asyncioÚbase_listen)r#   r'   r)   r   r$   r$   r%   Ú_listenV   s   zPoolEvents._listenÚdbapi_connectionr   Úconnection_recordr   c                 C  ó   dS )a  Called at the moment a particular DBAPI connection is first
        created for a given :class:`_pool.Pool`.

        This event allows one to capture the point directly after which
        the DBAPI module-level ``.connect()`` method has been used in order
        to produce a new DBAPI connection.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        Nr$   ©Úselfr1   r2   r$   r$   r%   Úconnectb   ó    zPoolEvents.connectc                 C  r3   )a)  Called exactly once for the first time a DBAPI connection is
        checked out from a particular :class:`_pool.Pool`.

        The rationale for :meth:`_events.PoolEvents.first_connect`
        is to determine
        information about a particular series of database connections based
        on the settings used for all connections.  Since a particular
        :class:`_pool.Pool`
        refers to a single "creator" function (which in terms
        of a :class:`_engine.Engine`
        refers to the URL and connection options used),
        it is typically valid to make observations about a single connection
        that can be safely assumed to be valid about all subsequent
        connections, such as the database version, the server and client
        encoding settings, collation settings, and many others.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        Nr$   r4   r$   r$   r%   Úfirst_connectv   r7   zPoolEvents.first_connectÚconnection_proxyr
   c                 C  r3   )a”  Called when a connection is retrieved from the Pool.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        :param connection_proxy: the :class:`.PoolProxiedConnection` object
          which will proxy the public interface of the DBAPI connection for the
          lifespan of the checkout.

        If you raise a :class:`~sqlalchemy.exc.DisconnectionError`, the current
        connection will be disposed and a fresh connection retrieved.
        Processing of all checkout listeners will abort and restart
        using the new connection.

        .. seealso:: :meth:`_events.ConnectionEvents.engine_connect`
           - a similar event
           which occurs upon creation of a new :class:`_engine.Connection`.

        Nr$   )r5   r1   r2   r9   r$   r$   r%   Úcheckout“   r7   zPoolEvents.checkoutúOptional[DBAPIConnection]c                 C  r3   )aö  Called when a connection returns to the pool.

        Note that the connection may be closed, and may be None if the
        connection has been invalidated.  ``checkin`` will not be called
        for detached connections.  (They do not return to the pool.)

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        Nr$   r4   r$   r$   r%   Úcheckin°   r7   zPoolEvents.checkinz2.0c                 C  s   | |fS )Nr$   )r1   r2   Úreset_stater$   r$   r%   Ú<lambda>Æ   s   þzPoolEvents.<lambda>r=   r   c                 C  r3   )aŸ  Called before the "reset" action occurs for a pooled connection.

        This event represents
        when the ``rollback()`` method is called on the DBAPI connection
        before it is returned to the pool or discarded.
        A custom "reset" strategy may be implemented using this event hook,
        which may also be combined with disabling the default "reset"
        behavior using the :paramref:`_pool.Pool.reset_on_return` parameter.

        The primary difference between the :meth:`_events.PoolEvents.reset` and
        :meth:`_events.PoolEvents.checkin` events are that
        :meth:`_events.PoolEvents.reset` is called not just for pooled
        connections that are being returned to the pool, but also for
        connections that were detached using the
        :meth:`_engine.Connection.detach` method as well as asyncio connections
        that are being discarded due to garbage collection taking place on
        connections before the connection was checked in.

        Note that the event **is not** invoked for connections that were
        invalidated using :meth:`_engine.Connection.invalidate`.    These
        events may be intercepted using the :meth:`.PoolEvents.soft_invalidate`
        and :meth:`.PoolEvents.invalidate` event hooks, and all "connection
        close" events may be intercepted using :meth:`.PoolEvents.close`.

        The :meth:`_events.PoolEvents.reset` event is usually followed by the
        :meth:`_events.PoolEvents.checkin` event, except in those
        cases where the connection is discarded immediately after reset.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        :param reset_state: :class:`.PoolResetState` instance which provides
         information about the circumstances under which the connection
         is being reset.

         .. versionadded:: 2.0

        .. seealso::

            :ref:`pool_reset_on_return`

            :meth:`_events.ConnectionEvents.rollback`

            :meth:`_events.ConnectionEvents.commit`

        Nr$   )r5   r1   r2   r=   r$   r$   r%   ÚresetÃ   r7   zPoolEvents.resetÚ	exceptionúOptional[BaseException]c                 C  r3   )a!  Called when a DBAPI connection is to be "invalidated".

        This event is called any time the
        :meth:`.ConnectionPoolEntry.invalidate` method is invoked, either from
        API usage or via "auto-invalidation", without the ``soft`` flag.

        The event occurs before a final attempt to call ``.close()`` on the
        connection occurs.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        :param exception: the exception object corresponding to the reason
         for this invalidation, if any.  May be ``None``.

        .. seealso::

            :ref:`pool_connection_invalidation`

        Nr$   ©r5   r1   r2   r@   r$   r$   r%   Ú
invalidate  r7   zPoolEvents.invalidatec                 C  r3   )aN  Called when a DBAPI connection is to be "soft invalidated".

        This event is called any time the
        :meth:`.ConnectionPoolEntry.invalidate`
        method is invoked with the ``soft`` flag.

        Soft invalidation refers to when the connection record that tracks
        this connection will force a reconnect after the current connection
        is checked in.   It does not actively close the dbapi_connection
        at the point at which it is called.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        :param exception: the exception object corresponding to the reason
         for this invalidation, if any.  May be ``None``.

        Nr$   rB   r$   r$   r%   Úsoft_invalidate!  r7   zPoolEvents.soft_invalidatec                 C  r3   )aÍ  Called when a DBAPI connection is closed.

        The event is emitted before the close occurs.

        The close of a connection can fail; typically this is because
        the connection is already closed.  If the close operation fails,
        the connection is discarded.

        The :meth:`.close` event corresponds to a connection that's still
        associated with the pool. To intercept close events for detached
        connections use :meth:`.close_detached`.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        Nr$   r4   r$   r$   r%   Úclose=  r7   zPoolEvents.closec                 C  r3   )a¶  Called when a DBAPI connection is "detached" from a pool.

        This event is emitted after the detach occurs.  The connection
        is no longer associated with the given connection record.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        :param connection_record: the :class:`.ConnectionPoolEntry` managing
         the DBAPI connection.

        Nr$   r4   r$   r$   r%   ÚdetachV  r7   zPoolEvents.detachc                 C  r3   )a¤  Called when a detached DBAPI connection is closed.

        The event is emitted before the close occurs.

        The close of a connection can fail; typically this is because
        the connection is already closed.  If the close operation fails,
        the connection is discarded.

        :param dbapi_connection: a DBAPI connection.
         The :attr:`.ConnectionPoolEntry.dbapi_connection` attribute.

        Nr$   )r5   r1   r$   r$   r%   Úclose_detachedh  r7   zPoolEvents.close_detachedN)r   r   r   r   r   r   )r'   r(   r)   r   r   r*   )r1   r   r2   r   r   r*   )r1   r   r2   r   r9   r
   r   r*   )r1   r;   r2   r   r   r*   )r1   r   r2   r   r=   r   r   r*   )r1   r   r2   r   r@   rA   r   r*   )r1   r   r   r*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_target_class_docr	   Ú_dispatch_targetr   Úpreload_moduleÚclassmethodr&   r0   r6   r8   r:   r<   r   Ú_legacy_signaturer?   rC   rD   rE   rF   rG   r$   r$   r$   r%   r      s0    



ý
8


r   )Ú
__future__r   r   r   r   r   r   Úbaser   r	   r
   r   Ú r   r   r   r   r   Úengine.interfacesr   ÚEventsr   r$   r$   r$   r%   Ú<module>   s    