Skip to content

Commit

Permalink
Merge pull request #606 from opengisch/t_object_key
Browse files Browse the repository at this point in the history
Use fetch and increment of T_Id in geopackage
  • Loading branch information
signedav committed Dec 6, 2021
2 parents b15bb87 + 5dc010b commit 5386846
Showing 1 changed file with 83 additions and 5 deletions.
88 changes: 83 additions & 5 deletions QgisModelBaker/libqgsprojectgen/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,23 @@ def create_dataset(self, datasetname):
if self._table_exists(GPKG_DATASET_TABLE):
cur = self.conn.cursor()
try:
(
status,
next_id,
fetch_and_increment_feedback,
) = self._fetch_and_increment_key_object(self.tid)
if not status:
return False, self.tr('Could not create dataset "{}": {}').format(
datasetname, fetch_and_increment_feedback
)

cur.execute(
"""
INSERT INTO {dataset_table} (datasetName) VALUES (:datasetname)
INSERT INTO {dataset_table} ({tid_name},datasetName) VALUES (:next_id, :datasetname)
""".format(
dataset_table=GPKG_DATASET_TABLE
tid_name=self.tid, dataset_table=GPKG_DATASET_TABLE
),
{"datasetname": datasetname},
{"datasetname": datasetname, "next_id": next_id},
)
self.conn.commit()
return True, self.tr('Successfully created dataset "{}".').format(
Expand Down Expand Up @@ -708,13 +718,24 @@ def create_basket(self, dataset_tid, topic):
topic
)
try:
(
status,
next_id,
fetch_and_increment_feedback,
) = self._fetch_and_increment_key_object(self.tid)
if not status:
return False, self.tr(
'Could not create basket for topic "{}": {}'
).format(topic, fetch_and_increment_feedback)
cur.execute(
"""
INSERT INTO {basket_table} (dataset, topic, {tilitid_name}, attachmentkey )
VALUES ({dataset_tid}, '{topic}', '{uuid}', 'Qgis Model Baker')
INSERT INTO {basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES ({next_id}, {dataset_tid}, '{topic}', '{uuid}', 'Qgis Model Baker')
""".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(),
Expand Down Expand Up @@ -747,3 +768,60 @@ def get_tid_handling(self):
if content:
return content[0] == "property"
return False

def _fetch_and_increment_key_object(self, field_name):
next_id = 0
if self._table_exists("T_KEY_OBJECT"):
# fetch last unique id of field_name
cur = self.conn.cursor()
cur.execute(
"""
SELECT T_LastUniqueId, T_CreateDate FROM T_KEY_OBJECT
WHERE T_Key = ?
""",
[field_name],
)

content = cur.fetchone()

create_date = "date('now')"

# increment
if content:
next_id = content[0] + 1
create_date = content[1]

# and update
try:
cur.execute(
"""
INSERT OR REPLACE INTO T_KEY_OBJECT (T_Key, T_LastUniqueId, T_LastChange, T_CreateDate, T_User )
VALUES (:key, :next_id, date('now'), :create_date, 'Qgis Model Baker')
""",
{"key": field_name, "next_id": next_id, "create_date": create_date},
)
self.conn.commit()
return (
True,
next_id,
self.tr("Successfully set the T_LastUniqueId to {}.").format(
next_id
),
)
except sqlite3.Error as e:
error_message = " ".join(e.args)

return (
False,
next_id,
self.tr("Could not set the T_LastUniqueId to {}: {}").format(
next_id, error_message
),
)
return (
False,
next_id,
self.tr(
"Could not fetch T_LastUniqueId because T_KEY_OBJECT does not exist."
),
)

0 comments on commit 5386846

Please sign in to comment.