spinedb_api.db_mapping_base
Module Contents
Classes
Mapped item status. |
|
An in-memory mapping of a DB, mapping item types (table names), to numeric ids, to items. |
|
A dictionary that represents a db item. |
- class spinedb_api.db_mapping_base.DatabaseMappingBase[source]
An in-memory mapping of a DB, mapping item types (table names), to numeric ids, to items.
This class is not meant to be used directly. Instead, you should subclass it to fit your particular DB schema.
When subclassing, you need to implement
item_types()
,item_factory()
,_make_sq()
, and_query_commit_count()
.- reset(*item_types)[source]
Resets the mapping for given item types as if nothing was fetched from the DB or modified in the mapping. Any modifications in the mapping that aren’t committed to the DB are lost after this.
- reset_purging()[source]
Resets purging status for all item types.
Fetching items of an item type that has been purged will automatically mark those items removed. Resetting the purge status lets fetched items to be added unmodified.
- do_fetch_more(item_type, offset=0, limit=None, **kwargs)[source]
Fetches items from the DB and adds them to the mapping.
- do_fetch_all(item_type, commit_count=None)[source]
Fetches all items of given type, but only once for each commit_count. In other words, the second time this method is called with the same commit_count, it does nothing. If not specified, commit_count defaults to the result of self._get_commit_count().
- class spinedb_api.db_mapping_base.MappedItemBase(db_map, item_type, **kwargs)[source]
Bases:
dict
A dictionary that represents a db item.
- Parameters:
db_map (DatabaseMappingBase) – the DB where this item belongs.
- property key[source]
Returns a tuple (item_type, id) for convenience, or None if this item doesn’t yet have an id. TODO: When does the latter happen?
- Returns:
tuple(str,int) or None
- fields[source]
A dictionary mapping fields to a another dict mapping “type” to a Python type, “value” to a description of the value for the key, and “optional” to a bool.
- handle_refetch()[source]
Called when an equivalent item is fetched from the DB.
If this item is compromised, then mark it as committed.
If this item is committed, then assume the one from the DB is newer and reset the state. Otherwise assume this is newer and do nothing.
- handle_refresh()[source]
Called when the mapping is refreshed.
If this item is committed, then set it as compromised.
- classmethod ref_types()[source]
Returns a set of item types that this class refers.
- Returns:
set(str)
- merge(other)[source]
Merges this item with another and returns the merged item together with any errors. Used for updating items.
- first_invalid_key()[source]
Goes through the
_references
class attribute and returns the key of the first reference that cannot be resolved.- Returns:
unresolved reference’s key if any.
- Return type:
str or None
- unique_key_values(skip_keys=())[source]
Yields tuples of unique keys and their values.
- Parameters:
skip_keys – Don’t yield these keys
- Yields:
tuple(tuple,tuple) – the first element is the unique key, the second is the values.
- resolve_internal_fields(skip_keys=())[source]
Goes through the
_internal_fields
class attribute and updates this item by resolving those references. Returns any error.
- polish()[source]
Polishes this item once all it’s references have been resolved. Returns any error.
The base implementation sets defaults but subclasses can do more work if needed.
- Returns:
error description if any.
- Return type:
str or None
- check_mutability()[source]
Called before adding, updating, or removing this item. Returns any errors that prevent that.
- Returns:
error description if any.
- Return type:
str or None
- is_valid()[source]
Checks if this item has all its references. Removes the item from the in-memory mapping if not valid by calling
cascade_remove
.- Returns:
bool
- validate()[source]
Resolves all references and checks if the item is valid. The item is valid if it’s not removed, has all of its references, and none of them is removed.
- add_referrer(referrer)[source]
Adds a strong referrer to this item. Strong referrers are removed, updated and restored in cascade with this item.
- Parameters:
referrer (MappedItemBase)
- remove_referrer(referrer)[source]
Removes a strong referrer.
- Parameters:
referrer (MappedItemBase)
- add_weak_referrer(referrer)[source]
Adds a weak referrer to this item. Weak referrers’ update callbacks are called whenever this item changes.
- Parameters:
referrer (MappedItemBase)
- cascade_restore(source=None)[source]
Restores this item (if removed) and all its referrers in cascade. Also, updates items’ status and calls their restore callbacks.
- cascade_remove(source=None)[source]
Removes this item and all its referrers in cascade. Also, updates items’ status and calls their remove callbacks.
- cascade_update()[source]
Updates this item and all its referrers in cascade. Also, calls items’ update callbacks.
- update(other)[source]
Overridden to update the item status and also to invalidate references that become obsolete.