我正在运行一个使用sqlalchemy访问数据库层的应用程序。现在我需要获取所有的插入,更新,删除操作,我在两个地方写了db操作,看起来改变这些代码将是非常危险的。反正我是否可以更改sqlalchemy的代码并捕获所有“插入,更新,删除”操作?
我试图找到执行,但定义太多了:
lib/python2.6/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/engine/base.py
463: def execute(self, *multiparams, **params):
500: def execute(self, object, *multiparams, **params):
819: def execute(self, object, *multiparams, **params):
1200: def execute(self, statement, *multiparams, **params):
1280: def execute(self, object, *multiparams, **params):
1808: def execute(self):
1855: def execute_string(self, stmt, params=None):
答案 0 :(得分:1)
请参阅before_execute
以及Connection Events中记录的其他事件。文档中的示例逐字:
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)