db

The db module provides the core database functionality for OpenLP

class openlp.core.lib.db.BaseModel[source]

Bases: object

BaseModel provides a base object with a set of generic functions

classmethod populate(**kwargs)[source]

Creates an instance of a class and populates it, returning the instance

class openlp.core.lib.db.Manager(plugin_name, init_schema, db_file_name=None, upgrade_mod=None, session=None)[source]

Bases: object

Provide generic object persistence management

delete_all_objects(object_class, filter_clause=None)[source]

Delete all object records. This method should only be used for simple tables and not ones with relationships. The relationships are not deleted from the database and this will lead to database corruptions.

Parameters:
  • object_class – The type of object to delete
  • filter_clause – The filter governing selection of objects to return. Defaults to None.
delete_object(object_class, key)[source]

Delete an object from the database

Parameters:
  • object_class – The type of object to delete
  • key – The unique reference or primary key for the instance to be deleted
finalise()[source]

VACUUM the database on exit.

get_all_objects(object_class, filter_clause=None, order_by_ref=None)[source]

Returns all the objects from the database

Parameters:
  • object_class – The type of objects to return
  • filter_clause – The filter governing selection of objects to return. Defaults to None.
  • order_by_ref – Any parameters to order the returned objects by. Defaults to None.
get_object(object_class, key=None)[source]

Return the details of an object

Parameters:
  • object_class – The type of object to return
  • key – The unique reference or primary key for the instance to return
get_object_count(object_class, filter_clause=None)[source]

Returns a count of the number of objects in the database.

Parameters:
  • object_class – The type of objects to return.
  • filter_clause – The filter governing selection of objects to return. Defaults to None.
get_object_filtered(object_class, filter_clause)[source]

Returns an object matching specified criteria

Parameters:
  • object_class – The type of object to return
  • filter_clause – The criteria to select the object by
save_object(object_instance, commit=True)[source]

Save an object to the database

Parameters:
  • object_instance – The object to save
  • commit – Commit the session with this object
save_objects(object_list, commit=True)[source]

Save a list of objects to the database

Parameters:
  • object_list – The list of objects to save
  • commit – Commit the session with this object
openlp.core.lib.db.delete_database(plugin_name, db_file_name=None)[source]

Remove a database file from the system.

Parameters:
  • plugin_name – The name of the plugin to remove the database for
  • db_file_name – The database file name. Defaults to None resulting in the plugin_name being used.
openlp.core.lib.db.get_db_path(plugin_name, db_file_name=None)[source]

Create a path to a database from the plugin name and database name

Parameters:
  • plugin_name – Name of plugin
  • db_file_name – File name of database
Returns:

The path to the database as type str

openlp.core.lib.db.get_upgrade_op(session)[source]

Create a migration context and an operations object for performing upgrades.

Parameters:session – The SQLAlchemy session object.
openlp.core.lib.db.handle_db_error(plugin_name, db_file_name)[source]

Log and report to the user that a database cannot be loaded

Parameters:
  • plugin_name – Name of plugin
  • db_file_name – File name of database
Returns:

None

openlp.core.lib.db.init_db(url, auto_flush=True, auto_commit=False, base=None)[source]

Initialise and return the session and metadata for a database

Parameters:
  • url – The database to initialise connection with
  • auto_flush – Sets the flushing behaviour of the session
  • auto_commit – Sets the commit behaviour of the session
  • base – If using declarative, the base class to bind with
openlp.core.lib.db.init_url(plugin_name, db_file_name=None)[source]

Return the database URL.

Parameters:
  • plugin_name – The name of the plugin for the database creation.
  • db_file_name – The database file name. Defaults to None resulting in the plugin_name being used.
openlp.core.lib.db.upgrade_db(url, upgrade)[source]

Upgrade a database.

Parameters:
  • url – The url of the database to upgrade.
  • upgrade – The python module that contains the upgrade instructions.