Databases

Databases.

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

Bases: object

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

The storage factory will either create a ZEO.ClientStorage.ClientStorage, a ZODB.FileStorage.FileStorage.FileStorage, a ZODB.DemoStorage.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()
Returns

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

close()

Closes the database.

connection(commit=False, cache_minimize=False, _trackeback_shift=0)

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.

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

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()

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)
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)
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)

Close and reopen a database connection.