Table Of Contents

Previous topic

trac.core – the Trac “kernel”

Next topic

trac.db.utils – Trac DB utilities

This Page

trac.db.api – Trac DB abstraction layer

Interfaces

class trac.db.api.IDatabaseConnector

Extension point interface for components that support the connection to relational databases.

See also trac.db.api.IDatabaseConnector extension point.

backup(dest)

Backup the database to a location defined by trac.backup_dir

get_connection(path, log=None, **kwargs)

Create a new connection to the database.

get_exceptions()

Return an object (typically a module) containing all the backend-specific exception types as attributes, named according to the Python Database API (http://www.python.org/dev/peps/pep-0249/).

get_supported_schemes()

Return the connection URL schemes supported by the connector, and their relative priorities as an iterable of (scheme, priority) tuples.

If priority is a negative number, this is indicative of an error condition with the connector. An error message should be attached to the error attribute of the connector.

init_db(path, schema=None, log=None, **kwargs)

Initialize the database.

to_sql(table)

Return the DDL statements necessary to create the specified table, including indices.

Classes

The following classes are not meant to be used directly, but rather via the Environment methods db_transaction and db_query.

class trac.db.api.QueryContextManager(env)

Bases: trac.db.api.DbContextManager

Database Context Manager for retrieving a read-only ConnectionWrapper.

class trac.db.api.TransactionContextManager(env)

Bases: trac.db.api.DbContextManager

Transactioned Database Context Manager for retrieving a ConnectionWrapper.

The outermost such context manager will perform a commit upon normal exit or a rollback after an exception.

The above are both subclasses of DbContextManager:

class trac.db.api.DbContextManager(env)

Database Context Manager

The outermost DbContextManager will close the connection.

execute(query, params=None)

Shortcut for directly executing a query.

executemany(query, params=None)

Shortcut for directly calling “executemany” on a query.

Components

class trac.db.api.DatabaseManager

Component used to manage the IDatabaseConnector implementations.

backup(dest=None)

Save a backup of the database.

Parameters:dest – base filename to write to.

Returns the file actually written.

backup_dir

Database backup location

connection_uri

Database connection [wiki:TracEnvironment#DatabaseConnectionStrings string] for this project

connectors

List of components that implement IDatabaseConnector

create_tables(schema)

Create the specified tables.

Parameters:schema – an iterable of table objects.
Since:version 1.0.2
debug_sql

Show the SQL queries in the Trac log, at DEBUG level. ‘’(Since 0.11.5)’‘

drop_tables(schema)

Drop the specified tables.

Parameters:schema – an iterable of Table objects or table names.
Since:version 1.0.2
get_connection(readonly=False)

Get a database connection from the pool.

If readonly is True, the returned connection will purposely lack the rollback and commit methods.

timeout

Timeout value for database connection, in seconds. Use ‘0’ to specify ‘’no timeout’‘. ‘’(Since 0.11)’‘

Functions

trac.db.api.get_column_names(cursor)

Retrieve column names from a cursor, if possible.

trac.db.api.with_transaction(env, db=None)

Function decorator to emulate a context manager for database transactions.

>>> def api_method(p1, p2):
>>>     result[0] = value1
>>>     @with_transaction(env)
>>>     def implementation(db):
>>>         # implementation
>>>         result[0] = value2
>>>     return result[0]

In this example, the implementation() function is called automatically right after its definition, with a database connection as an argument. If the function completes, a COMMIT is issued on the connection. If the function raises an exception, a ROLLBACK is issued and the exception is re-raised. Nested transactions are supported, and a COMMIT will only be issued when the outermost transaction block in a thread exits.

This mechanism is intended to replace the former practice of getting a database connection with env.get_db_cnx() and issuing an explicit commit or rollback, for mutating database accesses. Its automatic handling of commit, rollback and nesting makes it much more robust.

The optional db argument is intended for legacy code and should not be used in new code.

Deprecated:This decorator is in turn deprecated in favor of context managers now that python 2.4 support has been dropped. It will be removed in Trac 1.3.1. Use instead the new context managers, QueryContextManager and TransactionContextManager, which make for much simpler to write code:
>>> def api_method(p1, p2):
>>>     result = value1
>>>     with env.db_transaction as db:
>>>         # implementation
>>>         result = value2
>>>     return result