Skip to content

Commit

Permalink
Merge branch 'main' of github.com:opengisch/QgisModelBakerLibrary int…
Browse files Browse the repository at this point in the history
…o variousfixes
  • Loading branch information
signedav committed Oct 7, 2024
2 parents 5aeeb6e + 654b29b commit db04c90
Show file tree
Hide file tree
Showing 16 changed files with 829 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ jobs:
run: |
sudo ./scripts/package_pip_packages.sh
- name: Test on QGIS with PG 13
run: docker-compose -f .docker/docker-compose.gh.yml run -e "PGHOST=postgres13" qgis /usr/src/.docker/run-docker-tests.sh
run: docker compose -f .docker/docker-compose.gh.yml run -e "PGHOST=postgres13" qgis /usr/src/.docker/run-docker-tests.sh
- name: Test on QGIS with PG 15
run: docker-compose -f .docker/docker-compose.gh.yml run -e "PGHOST=postgres15" qgis /usr/src/.docker/run-docker-tests.sh
run: docker compose -f .docker/docker-compose.gh.yml run -e "PGHOST=postgres15" qgis /usr/src/.docker/run-docker-tests.sh

release:
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
Expand Down
25 changes: 15 additions & 10 deletions modelbaker/db_factory/pg_command_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,23 @@ def get_uri(self, su: bool = False, qgis: bool = False) -> str:
uri += ["dbname='{}'".format(self.configuration.database)]

# only provide authcfg to the uri when it's needed for QGIS specific things
if (
qgis
and self.configuration.dbauthid
and (
not service_config
or not (
service_config.get("user", None)
and service_config.get("password", None)
)
if self.configuration.dbauthid and (
not service_config
or not (
service_config.get("user", None)
and service_config.get("password", None)
)
):
uri += ["authcfg={}".format(self.configuration.dbauthid)]
if qgis:
uri += ["authcfg={}".format(self.configuration.dbauthid)]
else:
# Operations like Export do not require superuser
# login and may be run with authconfig
authconfig_map = db_utils.get_authconfig_map(
self.configuration.dbauthid
)
uri += ["user={}".format(authconfig_map.get("username"))]
uri += ["password={}".format(authconfig_map.get("password"))]
else:
if not service_config or not service_config.get("user", None):
uri += ["user={}".format(self.configuration.dbusr)]
Expand Down
19 changes: 18 additions & 1 deletion modelbaker/dbconnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,29 @@ def get_classes_relevance(self):
"""
return []

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
"""
Returns the state and the errormessage
"""
return False, None

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
"""
Returns the state and the errormessage
The basket_config must have the following keys:
dataset_t_id
datasetname
topic
bid_value
attachmentkey
basket_t_id
"""
return False, None

def get_tid_handling(self):
"""
Returns `True` if a tid handling is enabled according to the settings table (when the database has been created with `--createTidCol`).
Expand Down
52 changes: 50 additions & 2 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, uri, schema):
self.tid = "T_Id"
self.tilitid = "T_Ili_Tid"
self.dispName = "dispName"
self.attachmentKey = "attachmentKey"
self.basket_table_name = GPKG_BASKET_TABLE
self.dataset_table_name = GPKG_DATASET_TABLE

Expand Down Expand Up @@ -652,6 +653,9 @@ def get_attrili_attrdb_mapping_by_owner(self, owners):
return cursor

def get_models(self):
if not self._table_exists("t_ili2db_trafo"):
return {}

# Get MODELS
cursor = self.conn.cursor()

Expand Down Expand Up @@ -915,7 +919,9 @@ def get_classes_relevance(self):
return contents
return []

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
if self._table_exists(GPKG_BASKET_TABLE):
cursor = self.conn.cursor()
cursor.execute(
Expand Down Expand Up @@ -947,7 +953,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
cursor.execute(
"""
INSERT INTO "{basket_table}" ("{tid_name}", dataset, topic, "{tilitid_name}", attachmentkey )
VALUES (?, ?, ?, ?, 'modelbaker')
VALUES (?, ?, ?, ?, ?)
""".format(
tid_name=self.tid,
tilitid_name=self.tilitid,
Expand All @@ -958,6 +964,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
dataset_tid,
topic,
tilitid_value,
attachment_key,
),
)
self.conn.commit()
Expand All @@ -973,6 +980,47 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
).format(topic, error_message)
return False, self.tr('Could not create basket for topic "{}".').format(topic)

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
if self._table_exists(GPKG_BASKET_TABLE):
cursor = self.conn.cursor()
try:
cursor.execute(
"""
UPDATE {basket_table}
SET dataset = ?,
{t_ili_tid} = ?,
{attachment_key} = ?
WHERE {tid_name} = ?
""".format(
basket_table=GPKG_BASKET_TABLE,
t_ili_tid=self.tilitid,
attachment_key=self.attachmentKey,
tid_name=self.tid,
),
(
basket_config["dataset_t_id"],
basket_config["bid_value"],
basket_config["attachmentkey"],
basket_config["basket_t_id"],
),
)
self.conn.commit()
cursor.close()
return True, self.tr(
'Successfully edited basket for topic "{}" and dataset "{}".'
).format(basket_config["topic"], basket_config["datasetname"])
except sqlite3.Error as e:
cursor.close()
error_message = " ".join(e.args)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}": {}'
).format(
basket_config["topic"], basket_config["datasetname"], error_message
)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}"'
).format(basket_config["topic"], basket_config["datasetname"])

def get_tid_handling(self):
if self._table_exists(GPKG_SETTINGS_TABLE):
cursor = self.conn.cursor()
Expand Down
51 changes: 49 additions & 2 deletions modelbaker/dbconnector/mssql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, uri, schema):
self.iliCodeName = "iliCode"
self.tid = "T_Id"
self.tilitid = "T_Ili_Tid"
self.attachmentKey = "attachmentkey"
self.dispName = "dispName"
self.basket_table_name = BASKET_TABLE
self.dataset_table_name = DATASET_TABLE
Expand Down Expand Up @@ -737,6 +738,9 @@ def get_iliname_dbname_mapping(self, sqlnames=list()):
return result

def get_models(self):
if not self._table_exists("t_ili2db_trafo"):
return {}

# Get MODELS
if self.schema:
cur = self.conn.cursor()
Expand Down Expand Up @@ -1053,7 +1057,9 @@ def get_classes_relevance(self):
result = self._get_dict_result(cur)
return result

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
if self.schema and self._table_exists(BASKET_TABLE):
cur = self.conn.cursor()
cur.execute(
Expand All @@ -1080,7 +1086,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
cur.execute(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', {tilitid}, 'modelbaker')
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', {tilitid}, {attachment_key})
""".format(
schema=self.schema,
sequence="t_ili2db_seq",
Expand All @@ -1090,6 +1096,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
dataset_tid=dataset_tid,
topic=topic,
tilitid=tilitid_value,
attachment_key=attachment_key,
)
)
self.conn.commit()
Expand All @@ -1103,6 +1110,46 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
).format(topic, error_message)
return False, self.tr('Could not create basket for topic "{}".').format(topic)

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
if self.schema and self._table_exists(BASKET_TABLE):
cur = self.conn.cursor()
try:
cur.execute(
"""
UPDATE {schema}.{basket_table}
SET dataset = ?,
{t_ili_tid} = ?,
{attachment_key} = ?
WHERE {tid_name} = ?
""".format(
schema=self.schema,
basket_table=BASKET_TABLE,
t_ili_tid=self.tilitid,
attachment_key=self.attachmentKey,
tid_name=self.tid,
),
(
basket_config["dataset_t_id"],
basket_config["bid_value"],
basket_config["attachmentkey"],
basket_config["basket_t_id"],
),
)
self.conn.commit()
return True, self.tr(
'Successfully edited basket for topic "{}" and dataset "{}".'
).format(basket_config["topic"], basket_config["datasetname"])
except pyodbc.errors.Error as e:
error_message = " ".join(e.args)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}": {}'
).format(
basket_config["topic"], basket_config["datasetname"], error_message
)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}"'
).format(basket_config["topic"], basket_config["datasetname"])

def get_tid_handling(self):
if self.schema and self._table_exists(SETTINGS_TABLE):
cur = self.conn.cursor()
Expand Down
53 changes: 51 additions & 2 deletions modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, uri, schema):
self.iliCodeName = "ilicode"
self.tid = "t_id"
self.tilitid = "t_ili_tid"
self.attachmentKey = "attachmentkey"
self.dispName = "dispname"
self.basket_table_name = PG_BASKET_TABLE
self.dataset_table_name = PG_DATASET_TABLE
Expand Down Expand Up @@ -844,6 +845,9 @@ def get_attrili_attrdb_mapping_by_owner(self, owners):
return {}

def get_models(self):
if not self._table_exists("t_ili2db_trafo"):
return {}

# Get MODELS
if self.schema:
cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
Expand Down Expand Up @@ -1100,7 +1104,9 @@ def get_classes_relevance(self):
return cur.fetchall()
return []

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
if self.schema and self._table_exists(PG_BASKET_TABLE):
cur = self.conn.cursor()
cur.execute(
Expand Down Expand Up @@ -1128,7 +1134,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
sql.SQL(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey)
VALUES (nextval(%s), %s, %s, %s, 'modelbaker')
VALUES (nextval(%s), %s, %s, %s, %s)
"""
).format(
schema=sql.Identifier(self.schema),
Expand All @@ -1141,6 +1147,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
dataset_tid,
topic,
tilitid_value,
attachment_key,
),
)
self.conn.commit()
Expand All @@ -1154,6 +1161,48 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
).format(topic, error_message)
return False, self.tr('Could not create basket for topic "{}".').format(topic)

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
if self.schema and self._table_exists(PG_BASKET_TABLE):
cur = self.conn.cursor()
try:
cur.execute(
sql.SQL(
"""
UPDATE {schema}.{basket_table}
SET dataset = %s,
{t_ili_tid} = %s,
{attachment_key} = %s
WHERE {tid_name} = %s
"""
).format(
schema=sql.Identifier(self.schema),
basket_table=sql.Identifier(PG_BASKET_TABLE),
t_ili_tid=sql.Identifier(self.tilitid),
attachment_key=sql.Identifier(self.attachmentKey),
tid_name=sql.Identifier(self.tid),
),
(
basket_config["dataset_t_id"],
basket_config["bid_value"],
basket_config["attachmentkey"],
basket_config["basket_t_id"],
),
)
self.conn.commit()
return True, self.tr(
'Successfully edited basket for topic "{}" and dataset "{}".'
).format(basket_config["topic"], basket_config["datasetname"])
except psycopg2.errors.Error as e:
error_message = " ".join(e.args)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}": {}'
).format(
basket_config["topic"], basket_config["datasetname"], error_message
)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}"'
).format(basket_config["topic"], basket_config["datasetname"])

def get_tid_handling(self):
if self.schema and self._table_exists(PG_SETTINGS_TABLE):
cur = self.conn.cursor()
Expand Down
Loading

0 comments on commit db04c90

Please sign in to comment.