This section describes the event interfaces provided in SQLAlchemy Core. The event system in 0.7 is all new and supercedes the previous system of “extension”, “listener”, and “proxy” classes. For an introduction to the event listening API, see Events. ORM events are described in ORM Events.
Available events for 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"
events.listen(Pool, 'checkout', my_on_checkout)
In addition to accepting the Pool class and Pool instances, PoolEvents also accepts Engine objects and the Engine class as targets, which will be resolved to the .pool attribute of the given engine or the Pool class:
engine = create_engine("postgresql://scott:tiger@localhost/test")
# will associate with engine.pool
events.listen(engine, 'checkout', my_on_checkout)
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.)
Parameters: |
|
---|
Called when a connection is retrieved from the Pool.
Parameters: |
|
---|
If you raise an 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.
Called once for each new DB-API connection or Pool’s creator().
Parameters: |
|
---|
Called exactly once for the first DB-API connection.
Parameters: |
|
---|
Available events for Connection.
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, create_engine
def before_execute(conn, clauseelement, multiparams, params):
log.info("Received statement: %s" % clauseelement)
engine = create_engine('postgresql://scott:tiger@localhost/test')
event.listen(engine, "before_execute", before_execute)
Some events allow modifiers to the listen() function.
Parameters: |
|
---|
Intercept low-level cursor execute() events.
Intercept high level execute() events.
Intercept low-level cursor execute() events.
Intercept high level execute() events.
Intercept begin() events.
Intercept begin_twophase() events.
Intercept commit() events.
Intercept commit_twophase() events.
Intercept prepare_twophase() events.
Intercept release_savepoint() events.
Intercept rollback() events.
Intercept rollback_savepoint() events.
Intercept rollback_twophase() events.
Intercept savepoint() events.
Define event listeners for schema objects, that is, SchemaItem and SchemaEvent subclasses, including MetaData, Table, Column.
MetaData and Table support events specifically regarding when CREATE and DROP DDL is emitted to the database.
Attachment events are also provided to customize behavior whenever a child schema element is associated with a parent, such as, when a Column is associated with its Table, when a ForeignKeyConstraint is associated with a Table, etc.
Example using the after_create event:
from sqlalchemy import event
from sqlalchemy import Table, Column, Metadata, Integer
m = MetaData()
some_table = Table('some_table', m, Column('data', Integer))
def after_create(target, connection, **kw):
connection.execute("ALTER TABLE %s SET name=foo_%s" %
(target.name, target.name))
event.listen(some_table, "after_create", after_create)
DDL events integrate closely with the DDL class and the DDLElement hierarchy of DDL clause constructs, which are themselves appropriate as listener callables:
from sqlalchemy import DDL
event.listen(
some_table,
"after_create",
DDL("ALTER TABLE %(table)s SET name=foo_%(table)s")
)
The methods here define the name of an event as well as the names of members that are passed to listener functions.
See also:
Called after CREATE statments are emitted.
Parameters: |
|
---|
Called after DROP statments are emitted.
Parameters: |
|
---|
Called after a SchemaItem is associated with a parent SchemaItem.
Parameters: |
|
---|
event.listen() also accepts a modifier for this event:
Parameters: |
|
---|
Called before CREATE statments are emitted.
Parameters: |
|
---|
Called before DROP statments are emitted.
Parameters: |
|
---|
Called before a SchemaItem is associated with a parent SchemaItem.
Parameters: |
|
---|
event.listen() also accepts a modifier for this event:
Parameters: |
|
---|
Base class for elements that are the targets of DDLEvents events.
This includes SchemaItem as well as SchemaType.