def _set_domain_export_dependencies(domain_names, domain_metadatas, database_url): """Returns data structures that are useful when determining if a set is eligible for export.""" domain_dependencies = {name: [] for name in domain_names} try: database_map = DatabaseMapping(database_url) except SpineDBAPIError: return dict(), dict() try: set_dependencies = dict() for domain_name, domain_metadata in zip(domain_names, domain_metadatas): if domain_metadata.is_additional: continue object_class_id = (database_map.query( database_map.object_class_sq).filter( database_map.object_class_sq.c.name == domain_name).first().id) relationships = database_map.wide_relationship_class_list( object_class_id=object_class_id).all() depending_relationships = domain_dependencies[domain_name] for relationship in relationships: depending_relationships.append(relationship.name) depending_domains = set_dependencies.setdefault( relationship.name, dict()) depending_domains[domain_name] = domain_metadata.is_exportable( ) except SpineDBAPIError: return dict(), dict() finally: database_map.connection.close() return domain_dependencies, set_dependencies
def fetch_database_items(self): """Reads filter information from database.""" resource_filters = dict() id_to_name_cache = dict() def update_filters(label, filter_type, db_row): filters_by_type = self._resource_filters.get(label) is_on = False if filters_by_type is not None: ids = filters_by_type.get(filter_type) if ids is not None: currently_on = ids.get(db_row.id) if currently_on is not None: is_on = currently_on resource_filters.setdefault(label, dict()).setdefault( filter_type, dict())[db_row.id] = is_on id_to_name_cache.setdefault(filter_type, dict())[db_row.id] = db_row.name for resource in self._resources: url = resource.url if not url: continue try: db_map = DatabaseMapping(url) except (SpineDBAPIError, SpineDBVersionError): continue try: for scenario_row in db_map.query(db_map.scenario_sq): update_filters(resource.label, SCENARIO_FILTER_TYPE, scenario_row) for tool_row in db_map.query(db_map.tool_sq): update_filters(resource.label, TOOL_FILTER_TYPE, tool_row) finally: db_map.connection.close() self._resource_filters = resource_filters self._id_to_name_cache = id_to_name_cache
import sys from spinedb_api import DatabaseMapping, from_database url = sys.argv[1] db_map = DatabaseMapping(url) parameter_value = from_database( db_map.query(db_map.parameter_value_sq).first().value) with open("out.dat", "w") as out_file: out_file.write(f"{parameter_value}") db_map.connection.close()