File: //usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/__pycache__/events.cpython-310.pyc
o
���g� � @ s� d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d
l m
Z
d dl mZ d dlm
Z
d d
lmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlmZ ejr�d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z ddl!m"Z" ddl#m$Z$ ddl%m&Z& G d d!� d!ej'e
�Z(G d"d#� d#ej'e �Z)dS )$� )�annotationsN)�Any)�Dict)�Optional)�Tuple)�Type)�Union� )�
Connection)�Engine)�ConnectionEventsTarget)�DBAPIConnection)�DBAPICursor)�Dialect� )�event)�exc)�Literal)�_CoreMultiExecuteParams)�_CoreSingleExecuteParams)�_DBAPIAnyExecuteParams)�_DBAPIMultiExecuteParams)�_DBAPISingleExecuteParams)�_ExecuteOptions)�ExceptionContext)�ExecutionContext)�Result)�ConnectionPoolEntry)�
Executable)�
BindParameterc s: e Zd ZdZdZeZeda� fd d
��Zedd�dbdd��Z e
�dg d�dd� �dcd%d&��Ze
�dg d'�d(d� �ddd+d,��Z
ded6d7�Zdfd8d9�Ze
jd:dd;gd<d� d=�dgd>d?��ZdhdBdC�ZdidFdG�ZdjdHdI�ZdgdJdK�ZdgdLdM�ZdgdNdO�ZdkdQdR�ZdldSdT�ZdldUdV�ZdmdXdY�ZdmdZd[�Zdnd]d^�Zdnd_d`�Z� ZS )o�ConnectionEventsa Available events for
:class:`_engine.Connection` and :class:`_engine.Engine`.
The methods here define the name of an event as well as the names of
members that are passed to listener functions.
An event listener can be associated with any
:class:`_engine.Connection` or :class:`_engine.Engine`
class or instance, such as an :class:`_engine.Engine`, e.g.::
from sqlalchemy import event, create_engine
def before_cursor_execute(
conn, cursor, statement, parameters, context, executemany
):
log.info("Received statement: %s", statement)
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test")
event.listen(engine, "before_cursor_execute", before_cursor_execute)
or with a specific :class:`_engine.Connection`::
with engine.begin() as conn:
@event.listens_for(conn, "before_cursor_execute")
def before_cursor_execute(
conn, cursor, statement, parameters, context, executemany
):
log.info("Received statement: %s", statement)
When the methods are called with a `statement` parameter, such as in
:meth:`.after_cursor_execute` or :meth:`.before_cursor_execute`,
the statement is the exact SQL string that was prepared for transmission
to the DBAPI ``cursor`` in the connection's :class:`.Dialect`.
The :meth:`.before_execute` and :meth:`.before_cursor_execute`
events can also be established with the ``retval=True`` flag, which
allows modification of the statement and parameters to be sent
to the database. The :meth:`.before_cursor_execute` event is
particularly useful here to add ad-hoc string transformations, such
as comments, to all executions::
from sqlalchemy.engine import Engine
from sqlalchemy import event
@event.listens_for(Engine, "before_cursor_execute", retval=True)
def comment_sql_calls(
conn, cursor, statement, parameters, context, executemany
):
statement = statement + " -- some comment"
return statement, parameters
.. note:: :class:`_events.ConnectionEvents` can be established on any
combination of :class:`_engine.Engine`, :class:`_engine.Connection`,
as well
as instances of each of those classes. Events across all
four scopes will fire off for a given instance of
:class:`_engine.Connection`. However, for performance reasons, the
:class:`_engine.Connection` object determines at instantiation time
whether or not its parent :class:`_engine.Engine` has event listeners
established. Event listeners added to the :class:`_engine.Engine`
class or to an instance of :class:`_engine.Engine`
*after* the instantiation
of a dependent :class:`_engine.Connection` instance will usually
*not* be available on that :class:`_engine.Connection` instance.
The newly
added listeners will instead take effect for
:class:`_engine.Connection`
instances created subsequent to those event listeners being
established on the parent :class:`_engine.Engine` class or instance.
:param retval=False: Applies to the :meth:`.before_execute` and
:meth:`.before_cursor_execute` events only. When True, the
user-defined event function must have a return value, which
is a tuple of parameters that replace the given statement
and parameters. See those methods for a description of
specific return arguments.
�
SomeEngine�target�;Union[ConnectionEventsTarget, Type[ConnectionEventsTarget]]�
identifier�str�return�EOptional[Union[ConnectionEventsTarget, Type[ConnectionEventsTarget]]]c s, t � �||�}|d u rt|d�r|�� |S )N�_no_async_engine_events)�super�_accept_with�hasattrr( )�clsr"