spinetoolbox.spine_db_manager

The SpineDBManager class

authors:
  1. Vennström (VTT) and M. Marin (KTH)
date:

2.10.2019

Module Contents

spinetoolbox.spine_db_manager.do_create_new_spine_database(url, for_spine_model)[source]

Creates a new spine database at the given url.

class spinetoolbox.spine_db_manager.SpineDBManager(logger, project)[source]

Bases: PySide2.QtCore.QObject

Class to manage DBs within a project.

TODO: Expand description, how it works, the cache, the signals, etc.

Initializes the instance.

Parameters:
  • logger (LoggingInterface) – a general, non-database-specific logger
  • project (SpineToolboxProject) –
session_refreshed[source]
session_committed[source]
session_rolled_back[source]
object_classes_added[source]
objects_added[source]
relationship_classes_added[source]
relationships_added[source]
parameter_definitions_added[source]
_parameter_definitions_added[source]
parameter_values_added[source]
_parameter_values_added[source]
parameter_value_lists_added[source]
parameter_tags_added[source]
object_classes_removed[source]
objects_removed[source]
relationship_classes_removed[source]
relationships_removed[source]
parameter_definitions_removed[source]
parameter_values_removed[source]
parameter_value_lists_removed[source]
parameter_tags_removed[source]
object_classes_updated[source]
objects_updated[source]
relationship_classes_updated[source]
relationships_updated[source]
parameter_definitions_updated[source]
_parameter_definitions_updated[source]
parameter_values_updated[source]
_parameter_values_updated[source]
parameter_value_lists_updated[source]
parameter_tags_updated[source]
parameter_definition_tags_set[source]
items_removed_from_cache[source]
_GROUP_SEP = ǀ[source]
db_maps[source]
create_new_spine_database(self, url, for_spine_model=False)[source]
close_session(self, url)[source]

Pops any db map on the given url and closes its connection.

Parameters:url (str) –
close_all_sessions(self)[source]

Closes connections to all database mappings.

get_db_map(self, url, upgrade=False, codename=None)[source]

Returns a DiffDatabaseMapping instance from url if possible, None otherwise. If needed, asks the user to upgrade to the latest db version.

Parameters:
  • url (str, URL) –
  • upgrade (bool, optional) –
  • codename (str, NoneType, optional) –
Returns:

DiffDatabaseMapping, NoneType

do_get_db_map(self, url, upgrade, codename)[source]

Returns a memorized DiffDatabaseMapping instance from url. Called by get_db_map.

Parameters:
  • url (str, URL) –
  • upgrade (bool, optional) –
  • codename (str, NoneType, optional) –
Returns:

DiffDatabaseMapping

get_db_map_for_listener(self, listener, url, upgrade=False, codename=None)[source]
remove_db_map_listener(self, db_map, listener)[source]
set_logger_for_db_map(self, logger, db_map)[source]
unset_logger_for_db_map(self, db_map)[source]
refresh_session(self, *db_maps)[source]
commit_session(self, *db_maps)[source]
static _get_commit_msg(db_map)[source]
rollback_session(self, *db_maps)[source]
_commit_db_map_session(self, db_map)[source]
_rollback_db_map_session(self, db_map)[source]
ok_to_close(self, db_map)[source]

Prompts the user to commit or rollback changes to given database map.

Returns:True if successfully committed or rolled back, False otherwise
Return type:bool
connect_signals(self)[source]

Connects signals.

error_msg(self, db_map_error_log)[source]
cache_items(self, item_type, db_map_data)[source]

Caches data for a given type. It works for both insert and update operations.

Parameters:
  • item_type (str) –
  • db_map_data (dict) – lists of dictionary items keyed by DiffDatabaseMapping
cache_parameter_definition_tags(self, db_map_data)[source]

Caches parameter definition tags in the parameter definition dictionary.

Parameters:db_map_data (dict) – lists of parameter definition items keyed by DiffDatabaseMapping
uncache_items(self, item_type, db_map_data)[source]

Removes data from cache.

Parameters:
  • item_type (str) –
  • db_map_data (dict) – lists of dictionary items keyed by DiffDatabaseMapping
update_icons(self, db_map_data)[source]

Runs when object classes are added or updated. Setups icons for those classes. :param item_type: :type item_type: str :param db_map_data: lists of dictionary items keyed by DiffDatabaseMapping :type db_map_data: dict

entity_class_icon(self, db_map, entity_type, entity_class_id)[source]

Returns an appropriate icon for a given entity class.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • entity_type (str) – either ‘object class’ or ‘relationship class’
  • entity_class_id (int) –
Returns:

QIcon

get_item(self, db_map, item_type, id_)[source]

Returns the item of the given type in the given db map that has the given id, or an empty dict if not found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
  • id (int) –
Returns:

dict

get_item_by_field(self, db_map, item_type, field, value)[source]

Returns the first item of the given type in the given db map that has the given value for the given field Returns an empty dictionary if none found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
  • field (str) –
  • value
Returns:

dict

get_items_by_field(self, db_map, item_type, field, value)[source]

Returns all items of the given type in the given db map that have the given value for the given field. Returns an empty list if none found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
  • field (str) –
  • value
Returns:

list

get_items(self, db_map, item_type)[source]

Returns all the items of the given type in the given db map, or an empty list if none found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
Returns:

list

_get_items_from_db(self, db_map, item_type)[source]

Returns all items of the given type in the given db map. Called by the above methods whenever they don’t find what they’re looking for in cache.

get_field(self, db_map, item_type, id_, field)[source]
get_value(self, db_map, item_type, id_, field, role=Qt.DisplayRole)[source]

Returns the value or default value of a parameter.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) – either “parameter definition” or “parameter value”
  • id (int) –
  • field (str) – either “value” or “default_value”
  • role (int, optional) –
parse_value(self, value)[source]
static _display_data(parsed_value)[source]

Returns the value’s database representation formatted for Qt.DisplayRole.

static _tool_tip_data(parsed_value)[source]

Returns the value’s database representation formatted for Qt.ToolTipRole.

get_object_classes(self, db_map, cache=True)[source]

Returns object classes from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_objects(self, db_map, class_id=None, cache=True)[source]

Returns objects from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • class_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_relationship_classes(self, db_map, ids=None, object_class_id=None, cache=True)[source]

Returns relationship classes from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set, optional) –
  • object_class_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_relationships(self, db_map, ids=None, class_id=None, object_id=None, cache=True)[source]

Returns relationships from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set, optional) –
  • class_id (int, optional) –
  • object_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_object_parameter_definitions(self, db_map, ids=None, object_class_id=None, cache=True)[source]

Returns object parameter definitions from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set, optional) –
  • object_class_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_relationship_parameter_definitions(self, db_map, ids=None, relationship_class_id=None, cache=True)[source]

Returns relationship parameter definitions from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set, optional) –
  • relationship_class_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_object_parameter_values(self, db_map, ids=None, object_class_id=None, cache=True)[source]

Returns object parameter values from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set) –
  • object_class_id (int) –
Returns:

dictionary items

Return type:

list

get_relationship_parameter_values(self, db_map, ids=None, relationship_class_id=None, cache=True)[source]

Returns relationship parameter values from database.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set) –
  • relationship_class_id (int) –
Returns:

dictionary items

Return type:

list

get_parameter_definitions(self, db_map, ids=None, entity_class_id=None, cache=True)[source]

Returns both object and relationship parameter definitions.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set, optional) –
  • entity_class_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_parameter_values(self, db_map, ids=None, entity_class_id=None, cache=True)[source]

Returns both object and relationship parameter values.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • ids (set, optional) –
  • entity_class_id (int, optional) –
Returns:

dictionary items

Return type:

list

get_parameter_value_lists(self, db_map, cache=True)[source]

Returns parameter value lists from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_parameter_tags(self, db_map, cache=True)[source]

Get parameter tags from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
add_or_update_items(self, db_map_data, method_name, signal_name)[source]

Adds or updates items in db.

Parameters:
  • db_map_data (dict) – lists of items to add or update keyed by DiffDatabaseMapping
  • method_name (str) – attribute of DiffDatabaseMapping to call for performing the operation
  • signal_name (str) – signal attribute of SpineDBManager to emit if successful
add_object_classes(self, db_map_data)[source]

Adds object classes to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_objects(self, db_map_data)[source]

Adds objects to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_relationship_classes(self, db_map_data)[source]

Adds relationship classes to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_relationships(self, db_map_data)[source]

Adds relationships to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_definitions(self, db_map_data)[source]

Adds parameter definitions to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_values(self, db_map_data)[source]

Adds parameter values to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_checked_parameter_values(self, db_map_data)[source]

Adds parameter values in db without checking integrity.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_value_lists(self, db_map_data)[source]

Adds parameter value lists to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_tags(self, db_map_data)[source]

Adds parameter tags to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
update_object_classes(self, db_map_data)[source]

Updates object classes in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_objects(self, db_map_data)[source]

Updates objects in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_relationship_classes(self, db_map_data)[source]

Updates relationship classes in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_relationships(self, db_map_data)[source]

Updates relationships in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_definitions(self, db_map_data)[source]

Updates parameter definitions in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_values(self, db_map_data)[source]

Updates parameter values in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_checked_parameter_values(self, db_map_data)[source]

Updates parameter values in db without checking integrity.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_value_lists(self, db_map_data)[source]

Updates parameter value lists in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_tags(self, db_map_data)[source]

Updates parameter tags in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
set_parameter_definition_tags(self, db_map_data)[source]

Sets parameter definition tags in db.

Parameters:db_map_data (dict) – lists of items to set keyed by DiffDatabaseMapping
remove_items(self, db_map_typed_data)[source]
do_remove_items(self, db_map_typed_data)[source]

Removes items from database.

Parameters:db_map_typed_data (dict) – lists of items to remove, keyed by item type (str), keyed by DiffDatabaseMapping
static _to_ids(db_map_data)[source]
cascade_remove_objects(self, db_map_data)[source]

Removes objects in cascade when removing object classes.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_relationship_classes(self, db_map_data)[source]

Removes relationship classes in cascade when removing object classes.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_relationships_by_class(self, db_map_data)[source]

Removes relationships in cascade when removing objects.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_relationships_by_object(self, db_map_data)[source]

Removes relationships in cascade when removing relationship classes.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_parameter_definitions(self, db_map_data)[source]

Removes parameter definitions in cascade when removing entity classes.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_parameter_values_by_entity_class(self, db_map_data)[source]

Removes parameter values in cascade when removing entity classes.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_parameter_values_by_entity(self, db_map_data)[source]

Removes parameter values in cascade when removing entity classes when removing entities.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_remove_parameter_values_by_definition(self, db_map_data)[source]

Removes parameter values in cascade when when removing parameter definitions.

Parameters:db_map_data (dict) – lists of removed items keyed by DiffDatabaseMapping
cascade_refresh_relationship_classes(self, db_map_data)[source]

Refreshes cached relationship classes when updating object classes.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_relationships_by_object(self, db_map_data)[source]

Refreshed cached relationships in cascade when updating objects.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_definitions(self, db_map_data)[source]

Refreshes cached parameter definitions in cascade when updating entity classes.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_definitions_by_value_list(self, db_map_data)[source]

Refreshes cached parameter definitions when updating parameter value lists.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_definitions_by_tag(self, db_map_data)[source]

Refreshes cached parameter definitions when updating parameter tags.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_entity_class(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating entity classes.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_entity(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating entities.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_definition(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating parameter definitions.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
find_cascading_relationship_classes(self, db_map_ids)[source]

Finds and returns cascading relationship classes for the given object class ids.

find_cascading_entities(self, db_map_ids, item_type)[source]

Finds and returns cascading entities for the given entity class ids.

find_cascading_relationships(self, db_map_ids)[source]

Finds and returns cascading relationships for the given object ids.

find_cascading_parameter_data(self, db_map_ids, item_type)[source]

Finds and returns cascading parameter definitions or values for the given entity class ids.

find_cascading_parameter_definitions_by_value_list(self, db_map_ids)[source]

Finds and returns cascading parameter definitions for the given parameter value list ids.

find_cascading_parameter_definitions_by_tag(self, db_map_ids)[source]

Finds and returns cascading parameter definitions for the given parameter tag ids.

find_cascading_parameter_values_by_entity(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given entity ids.

find_cascading_parameter_values_by_definition(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given parameter definition ids.

do_add_parameter_definitions(self, db_map_data)[source]

Adds parameter definitions in extended format given data in compact format.

Parameters:db_map_data (dict) – lists of parameter definition items keyed by DiffDatabaseMapping
do_add_parameter_values(self, db_map_data)[source]

Adds parameter values in extended format given data in compact format.

Parameters:db_map_data (dict) – lists of parameter value items keyed by DiffDatabaseMapping
do_update_parameter_definitions(self, db_map_data)[source]

Updates parameter definitions in extended format given data in compact format.

Parameters:db_map_data (dict) – lists of parameter definition items keyed by DiffDatabaseMapping
do_update_parameter_values(self, db_map_data)[source]

Updates parameter values in extended format given data in compact format.

Parameters:db_map_data (dict) – lists of parameter value items keyed by DiffDatabaseMapping