[Discussion] How do we deal with Tabula taxonomy items?
In the exposure-initializer we need to insert not only the GEM Taxonomy but also the Tabula. How do we deal with those? The function insert_taxonomy() requires a Taxonomy object, and up to now we only inserted GEM Taxonomy items there. Should we parse the Tabula Taxonomy too with taxonomylib? Or should we insert the Tabula taxonomy as a dictionary?
See snippet:
def insert_taxonomy(
self, category: int, taxonomy: Taxonomy, attributes: dict[str, Any]
) -> int:
"""
...
"""
taxonomy_id = self.get_taxonomy_id(taxonomy)
if taxonomy_id is None: # This taxonomy does not exist in the `taxonomies` table.
sql_statement = f"""
INSERT INTO {self.taxonomies_table} (category, taxonomy, attributes)
VALUES (
{category}, '{json.dumps(taxonomy.get_dict())}','{json.dumps(attributes)}'
)
RETURNING id
"""
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0] # Get ID of the new taxonomy.
else: # This taxonomy already exists in the table, so that its ID can be returned.
return taxonomy_id
We can add a function insert_taxonomy_from_dict, which would solve the problem for now. However, that means that Tabula would for now not be included in taxonomylib.
def insert_taxonomy_from_dict(
self, category: int, taxonomy_dict: dict[str, Any], attributes: dict[str, Any]
) -> int:
"""
...
"""
taxonomy_id = self.get_taxonomy_id_from_dict(taxonomy_dict)
if taxonomy_id is None: # This taxonomy does not exist in the `taxonomies` table.
sql_statement = f"""
INSERT INTO {self.taxonomies_table} (category, taxonomy, attributes)
VALUES (
{category}, '{json.dumps(taxonomy_dict)}','{json.dumps(attributes)}'
)
RETURNING id
"""
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0] # Get ID of the new taxonomy.
else: # This taxonomy already exists in the table, so that its ID can be returned.
return taxonomy_id
def get_taxonomy_id_from_dict(self, taxonomy_dict: dict, category):
where_statements = []
for key, value in taxonomy_dict.items():
where_statements.append(f"taxonomy->>'{key}' = '{value}'")
sql_statement = f"""
SELECT taxonomy, id
FROM {self.taxonomies_table}
WHERE category = {category} AND {" AND ".join(where_statements)}
"""
self.cursor.execute(sql_statement)
Edited by Laurens Jozef Nicolaas Oostwegel