Signals
Signals allow you to listen and react to events emitted by sqlorm. The blinker lib is used to implement signals.
Engine
The following signals exist on the Engine class. The sender is always the engine instance.
connected: receiveconn(connection instance)pool_checkout: connection checked out of the pool, receiveconnpool_checkin: connection returned to the pool, receiveconndisconnected: receiveconn(connection instance)
Example:
from sqlorm import Engine
@Engine.connected.connect
def on_connected(engine, conn, from_pool):
# do something
Session
The following signals exist on the Session class. The sender is always the session instance.
before_commitafter_commitbefore_rollbackafter_rollback
Use session.conn to retreive the connection object (may be None if no connection have been established, use session.connect() in this case).
Example:
from sqlorm import Session
@Session.before_commit.connect
def on_before_commit(session):
# do something
Transaction
The following signals exist on the Transaction class. The sender is always the transaction instance.
before_execute: receivestmt,paramsandmany(to distinguish between execute and executemany)- when called from execute: returning a cursor will stop sqlorm execution and return the cursor directly
- when called from executemany: returning False will stop sqlorm execution
- in both case, returning a tuple
(stmt, params)will override stmt and params
after_execute: receivecursor,stmt,paramsandmanyhandle_error: receivecursor,stmt,params,manyandexc:- when handling for execute: return a cursor to prevent raising the exception
- when handling for executemany: return True to prevent raising
Model
The following signals exist on the Model class. The sender is always the model class.
before_query: receivestmtandparamsargs. can override them by returning a tuple(stmt, params)
The following signals receive obj kwargs containing the model instance.
Returning False from any listener will cancel the operation. Returning a value will override the statement.
before_refreshbefore_insertbefore_updatebefore_deletebefore_save(receive ais_newkwargs, can only cancel operations)
The following signals are sent only if an operation is performed. They receive the obj kwargs containing the model instance.
after_refreshafter_insertafter_update: receive alsoupdateda boolean indicating if an update stmt was actually performedafter_saveafter_delete
Examples:
from sqlorm import Model
@Model.before_query.connect
def on_before_query(model_class, stmt, params):
# do something
@Model.before_query.connect_via(MyModel)
def on_my_model_before_query(model_class, stmt, params):
# do something only on before_query of MyModel
@Model.before_insert.connect
def on_before_insert(model_class, obj, stmt):
return False # cancel the insert
@Model.before_insert.connect
def on_before_insert(model_class, obj, stmt):
return SQL.insert(model_class.table, {"col": "value"}) # return custom statement