本文整理汇总了Python中trac.db.api.DatabaseManager.get_connection方法的典型用法代码示例。如果您正苦于以下问题:Python DatabaseManager.get_connection方法的具体用法?Python DatabaseManager.get_connection怎么用?Python DatabaseManager.get_connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trac.db.api.DatabaseManager
的用法示例。
在下文中一共展示了DatabaseManager.get_connection方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: trac_db_transaction
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import get_connection [as 别名]
def trac_db_transaction(env):
"""
Context manager to handle database connection and cursor from trac environment's
read / write connection. This handles automatic commits, rollbacks and connection
closing.
Internally, this uses Trac's connection pool via the Trac DatabaseManager class.
This is because it would appear that to multiproject uses, Trac's with_transaction()
is not sufficient.
For examples of use, see db_transaction()
:param Environment env: The Trac Environment in use.
"""
conn = None
cursor = None
try:
dm = DatabaseManager(env)
conn = dm.get_connection()
except Exception:
env.log.exception("Failed to get database connection from trac database manager")
raise
try:
cursor = conn.cursor()
yield cursor
conn.commit()
except Exception:
env.log.error("Failed to perform transaction to database")
conn.rollback()
raise
finally:
cursor.close()
conn.close()
示例2: trac_db_query
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import get_connection [as 别名]
def trac_db_query(env):
"""
Context manager to handle database connection and cursor from trac environment's
read only connection. This does not attempt to roll back or commit, the connection
is meant only for accessing the data. For examples on use, see db_query().
Internally, this uses Trac's connection pool via the Trac DatabaseManager class
:param Environment env: The Trac environment
"""
conn = None
cursor = None
try:
dm = DatabaseManager(env)
conn = dm.get_connection()
except Exception:
env.log.exception("Failed to get database connection from trac database manager")
raise
try:
# NOTE: Trac's connection does not support alternative cursor types!
cursor = conn.cursor()
yield cursor
except Exception:
env.log.error("Failed to query database")
raise
finally:
cursor.close()
conn.close()
示例3: _utf8_size
# 需要导入模块: from trac.db.api import DatabaseManager [as 别名]
# 或者: from trac.db.api.DatabaseManager import get_connection [as 别名]
def _utf8_size(self, cnx):
if cnx is None:
connector, args = DatabaseManager(self.env).get_connector()
cnx = connector.get_connection(**args)
charset = cnx.charset
cnx.close()
else:
charset = cnx.charset
return 4 if charset == 'utf8mb4' else 3