Databases

class sheraf.databases.Database(uri=None, storage=None, nestable=False, db_args=None)[source]

Bases: object

A ZODB ZODB.DB wrapper with a ZODB.interfaces.IStorage factory.

The storage factory will either create a ClientStorage, a FileStorage, a DemoStorage, a Relstorage PostgreSQLAdapter or use a user defined storage depending on the argument passed at the initialization of the object.

A Storage object is created and pass it to the ZODB.DB constructor

Several connections can be used at the same time. The connections are identified by their name.

Parameters:
  • database_name – The name of the connection.

  • storage – If set, this user defined storage will be used.

  • uri (An URI that will be parsed by zodburi.resolve_uri().) – A zodburi to the database.

  • db_args – Arguments to pass to the ZODB.DB.

  • nestable – If False, will raise a ConnectionAlreadyOpened if a connection has already been opened.

classmethod all()[source]
Returns:

A list containing all the existing Database in a tuple (name, Database).

close()[source]

Closes the database.

connection(commit=False, cache_minimize=False, reuse=False, _trackeback_shift=0)[source]

A context manager opening a connection on this database.

Parameters:
  • commit (boolean) – Whether to commit the transaction when leaving the context manager.

  • cache_minimize (boolean) – Whether to call ZODB.Connection.Connection.cache_minimize() when leaving the context manager.

  • reuse (boolean) – If a connection is already opened, reuse it.

>>> database = sheraf.Database()
>>> with database.connection() as connection:
...    sheraf.Database.current_connection() is connection
True
connection_close(connection=None)[source]

Closes a connection opened on the database.

Parameters:

connection – The connection to close, if None the last connection opened on the database is closed.

connection_open()[source]

Opens a connection. Returns a connection to this database.

If nestable is set and a connection has already been opened, raises a ConnectionAlreadyOpened exception. If nestable is False and a connection has already been opened, it returns a new connection with a new transaction_manager.

Returns:

A Connection object.

classmethod get(database_name=None)[source]
Parameters:

database_name – The name of the queried database.

Returns:

The database object if it exists. A KeyError is raised elsewise.

classmethod get_or_create(**kwargs)[source]
Returns:

The database object if it exists. If the database does not exist, it is created with the kwargs arguments.

reset(storage=None, uri=None)[source]

Close and reopen a database connection.

sheraf.databases.connection(database_name=None, commit=False, cache_minimize=False, reuse=False)[source]

Shortcut for sheraf.databases.Database.connection()

Parameters: