本文整理汇总了Python中sqlalchemy.event方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.event方法的具体用法?Python sqlalchemy.event怎么用?Python sqlalchemy.event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.event方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register_table
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import event [as 别名]
def register_table(table):
"""
Register a database table. This will populate the information provided in
DATABASE_TABLES dictionary. This also forwards signals to the appropriate
listeners within the :py:mod:`server.signal` module.
:param cls table: The table to register.
"""
metatable = table.metatable()
database_tables[metatable.name] = metatable
sqlalchemy.event.listen(table, 'before_delete', forward_signal_delete)
sqlalchemy.event.listen(table, 'before_insert', forward_signal_insert)
sqlalchemy.event.listen(table, 'before_update', forward_signal_update)
return table
示例2: _install_session_variables_setter
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import event [as 别名]
def _install_session_variables_setter(self):
session_variables = collections.OrderedDict(self.constant_session_variables)
session_variables.update(
sorted(self.config[self.config_section_session_variables].iteritems()))
setter_sql = 'SET ' + ' , '.join(
'SESSION {} = {}'.format(name, value)
for name, value in session_variables.iteritems())
@sqlalchemy.event.listens_for(self.engine, 'connect')
def set_session_variable(dbapi_connection, connection_record):
"""
Execute "SET SESSION <var1> = <val1>, ..." to set the
variables specified by the `constant_session_variables`
attribute and in the appropriate configuration section.
To be called automatically whenever a new low-level
connection to the database is established.
WARNING: for simplicity, the variable names and values are
inserted "as is", *without* any escaping -- we assume we
can treat config options (and, even more so, Python-level
object attributes, of course) as a *trusted* source of
data.
"""
cursor = dbapi_connection.cursor()
try:
cursor.execute(setter_sql)
cursor.execute('COMMIT')
finally:
cursor.close()
# TODO after SQLAlchemy upgrade to 1.2+:
# * add `pool_pre_ping = true :: bool` to config
# * remove this method and the copyright note concerning this method...
示例3: _install_reconnector
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import event [as 别名]
def _install_reconnector(self):
# copied from:
# http://docs.sqlalchemy.org/en/rel_0_9/core/pooling.html#disconnect-handling-pessimistic
# and slightly adjusted
@sqlalchemy.event.listens_for(sqlalchemy.pool.Pool, "checkout")
def ping_connection(dbapi_connection, connection_record, connection_proxy):
cursor = dbapi_connection.cursor()
try:
cursor.execute("SELECT 1")
except Exception:
# dispose the whole pool instead of invalidating one at a time
connection_proxy._pool.dispose()
# pool will try connecting again up to three times before giving up
raise sqlalchemy.exc.DisconnectionError()
cursor.close()
示例4: add_cors_headers_response_callback
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import event [as 别名]
def add_cors_headers_response_callback(event):
def cors_headers(request, response):
response.headers.update({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST,GET,DELETE,PUT,OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Max-Age': '1728000',
})
event.request.add_response_callback(cors_headers)