Skip to content

Commit

Permalink
Merge pull request #74 from opengisch/basketoids
Browse files Browse the repository at this point in the history
Basket OIDs
  • Loading branch information
signedav authored Nov 17, 2023
2 parents e43e917 + c59253f commit e452e29
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion modelbaker/dbconnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def get_topics_info(self):
"""
return {}

def create_basket(self, dataset_tid, topic):
def create_basket(self, dataset_tid, topic, tilitid_value=None):
"""
Returns the state and the errormessage
"""
Expand Down
14 changes: 11 additions & 3 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,13 @@ def get_topics_info(self):
"""
SELECT DISTINCT substr(CN.IliName, 0, instr(CN.IliName, '.')) as model,
substr(substr(CN.IliName, instr(CN.IliName, '.')+1),0, instr(substr(CN.IliName, instr(CN.IliName, '.')+1),'.')) as topic,
MA.attr_value as bid_domain,
{relevance}
FROM T_ILI2DB_CLASSNAME as CN
JOIN T_ILI2DB_TABLE_PROP as TP
ON CN.sqlname = TP.tablename
LEFT JOIN T_ILI2DB_META_ATTRS as MA
ON substr( CN.IliName, 0, instr(substr( CN.IliName, instr(CN.IliName, '.')+1), '.')+instr(CN.IliName, '.')) = MA.ilielement and MA.attr_name = 'ili2db.ili.bidDomain'
WHERE topic != '' and TP.setting != 'ENUM'
""".format(
# it's relevant, when it's not extended
Expand All @@ -851,7 +854,7 @@ def get_topics_info(self):
return contents
return {}

def create_basket(self, dataset_tid, topic):
def create_basket(self, dataset_tid, topic, tilitid_value=None):
if self._table_exists(GPKG_BASKET_TABLE):
cursor = self.conn.cursor()
cursor.execute(
Expand All @@ -877,18 +880,23 @@ def create_basket(self, dataset_tid, topic):
return False, self.tr(
'Could not create basket for topic "{}": {}'
).format(topic, fetch_and_increment_feedback)
if not tilitid_value:
# default value
tilitid_value = f"'{uuid.uuid4()}'"
elif not tilitid_value.isnumeric():
tilitid_value = f"'{tilitid_value}'"
cursor.execute(
"""
INSERT INTO {basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES ({next_id}, {dataset_tid}, '{topic}', '{uuid}', 'modelbaker')
VALUES ({next_id}, {dataset_tid}, '{topic}', {tilitid}, 'modelbaker')
""".format(
tid_name=self.tid,
tilitid_name=self.tilitid,
basket_table=GPKG_BASKET_TABLE,
next_id=next_id,
dataset_tid=dataset_tid,
topic=topic,
uuid=uuid.uuid4(),
tilitid=tilitid_value,
)
)
self.conn.commit()
Expand Down
17 changes: 13 additions & 4 deletions modelbaker/dbconnector/mssql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,11 +946,14 @@ def get_topics_info(self):
cur = self.conn.cursor()
cur.execute(
"""
SELECT DISTINCT PARSENAME(cn.iliname,1) as model,
PARSENAME(cn.iliname,2) as topic
SELECT DISTINCT PARSENAME(cn.iliname,3) as model,
PARSENAME(cn.iliname,2) as topic,
ma.attr_value as bid_domain
FROM {schema}.t_ili2db_classname as cn
JOIN {schema}.t_ili2db_table_prop as tp
ON cn.sqlname = tp.tablename
LEFT JOIN {schema}.t_ili2db_meta_attrs as ma
ON CONCAT(PARSENAME(cn.iliname,3),'.',PARSENAME(cn.iliname,2)) = ma.ilielement AND ma.attr_name = 'ili2db.ili.bidDomain'
WHERE PARSENAME(cn.iliname,3) != '' and tp.setting != 'ENUM'
""".format(
schema=self.schema
Expand All @@ -959,7 +962,7 @@ def get_topics_info(self):
result = self._get_dict_result(cur)
return result

def create_basket(self, dataset_tid, topic):
def create_basket(self, dataset_tid, topic, tilitid_value=None):
if self.schema and self._table_exists(BASKET_TABLE):
cur = self.conn.cursor()
cur.execute(
Expand All @@ -978,10 +981,15 @@ def create_basket(self, dataset_tid, topic):
topic
)
try:
if not tilitid_value:
# default value
tilitid_value = "NEWID()"
elif not tilitid_value.isnumeric():
tilitid_value = f"'{tilitid_value}'"
cur.execute(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', NEWID(), 'modelbaker')
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', {tilitid}, 'modelbaker')
""".format(
schema=self.schema,
sequence="t_ili2db_seq",
Expand All @@ -990,6 +998,7 @@ def create_basket(self, dataset_tid, topic):
basket_table=BASKET_TABLE,
dataset_tid=dataset_tid,
topic=topic,
tilitid=tilitid_value,
)
)
self.conn.commit()
Expand Down
13 changes: 11 additions & 2 deletions modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,10 +974,13 @@ def get_topics_info(self):
"""
SELECT DISTINCT (string_to_array(cn.iliname, '.'))[1] as model,
(string_to_array(cn.iliname, '.'))[2] as topic,
ma.attr_value as bid_domain,
{relevance}
FROM {schema}.t_ili2db_classname as cn
JOIN {schema}.t_ili2db_table_prop as tp
ON cn.sqlname = tp.tablename
LEFT JOIN {schema}.t_ili2db_meta_attrs as ma
ON CONCAT((string_to_array(cn.iliname, '.'))[1],'.',(string_to_array(cn.iliname, '.'))[2]) = ma.ilielement and ma.attr_name = 'ili2db.ili.bidDomain'
WHERE array_length(string_to_array(cn.iliname, '.'),1) > 2 and tp.setting != 'ENUM'
""".format(
schema=self.schema,
Expand All @@ -998,7 +1001,7 @@ def get_topics_info(self):
return cur.fetchall()
return {}

def create_basket(self, dataset_tid, topic):
def create_basket(self, dataset_tid, topic, tilitid_value=None):
if self.schema and self._table_exists(PG_BASKET_TABLE):
cur = self.conn.cursor()
cur.execute(
Expand All @@ -1017,10 +1020,15 @@ def create_basket(self, dataset_tid, topic):
topic
)
try:
if not tilitid_value:
# default value
tilitid_value = "uuid_generate_v4()"
elif not tilitid_value.isnumeric():
tilitid_value = f"'{tilitid_value}'"
cur.execute(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES (nextval('{schema}.{sequence}'), {dataset_tid}, '{topic}', uuid_generate_v4(), 'modelbaker')
VALUES (nextval('{schema}.{sequence}'), {dataset_tid}, '{topic}', {tilitid}, 'modelbaker')
""".format(
schema=self.schema,
sequence="t_ili2db_seq",
Expand All @@ -1029,6 +1037,7 @@ def create_basket(self, dataset_tid, topic):
basket_table=PG_BASKET_TABLE,
dataset_tid=dataset_tid,
topic=topic,
tilitid=tilitid_value,
)
)
self.conn.commit()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dataset_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ def check_dataset_mutations(self, db_connector):
topics = db_connector.get_topics_info()
assert len(topics) == 2

# check the bid_domain
count = 0
for topic in topics:
if topic["topic"] == "Infrastructure":
assert topic["bid_domain"] == "INTERLIS.UUIDOID"
count += 1
if topic["topic"] == "Lines":
assert topic["bid_domain"] == "INTERLIS.UUIDOID"
count += 1
assert count == 2

# Generate the basket for 'Glarus Nord' and the first topic
result = db_connector.create_basket(
glarus_nord_tid, f"{topics[0]['model']}.{topics[0]['topic']}"
Expand Down

0 comments on commit e452e29

Please sign in to comment.