Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get and reset T_Id Sequence #71

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modelbaker/dbconnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,18 @@ def get_ili2db_settings(self):
"""
return {}

def get_ili2db_sequence_value(self):
"""
Returns the current value of the sequence used for the t_id
"""
return None

def set_ili2db_sequence_value(self, value):
"""
Resets the current value of the sequence used for the t_id
"""
return False, None


class DBConnectorError(Exception):
"""This error is raised when DbConnector could not connect to database.
Expand Down
64 changes: 62 additions & 2 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ def create_basket(self, dataset_tid, topic):
cursor.execute(
"""
INSERT INTO {basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES ({next_id}, {dataset_tid}, '{topic}', '{uuid}', 'Qgis Model Baker')
VALUES ({next_id}, {dataset_tid}, '{topic}', '{uuid}', 'modelbaker')
""".format(
tid_name=self.tid,
tilitid_name=self.tilitid,
Expand Down Expand Up @@ -964,7 +964,7 @@ def _fetch_and_increment_key_object(self, field_name):
cursor.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')
VALUES (:key, :next_id, date('now'), :create_date, 'modelbaker')
""",
{"key": field_name, "next_id": next_id, "create_date": create_date},
)
Expand Down Expand Up @@ -994,3 +994,63 @@ def _fetch_and_increment_key_object(self, field_name):
"Could not fetch T_LastUniqueId because T_KEY_OBJECT does not exist."
),
)

def get_ili2db_sequence_value(self):
if self._table_exists("T_KEY_OBJECT"):
cursor = self.conn.cursor()
cursor.execute(
"""
SELECT T_LastUniqueId FROM T_KEY_OBJECT
WHERE T_Key = '{}'
""".format(
self.tid
)
)

content = cursor.fetchone()
cursor.close()

if content:
return content[0]
return None

def set_ili2db_sequence_value(self, value):
if self._table_exists("T_KEY_OBJECT"):
try:
cursor = self.conn.cursor()
if not self.get_ili2db_sequence_value():
# need to create the entry
cursor.execute(
"""
INSERT INTO T_KEY_OBJECT (T_Key, T_LastUniqueId, T_LastChange, T_CreateDate, T_User )
VALUES ('{tid}', {value}, date('now'), date('now'), 'modelbaker')
""".format(
tid=self.tid, value=value
)
)
else:
# just update it
cursor.execute(
"""
UPDATE T_KEY_OBJECT SET
T_LastUniqueID = {value},
T_LastChange = date('now'),
T_User = 'modelbaker'
WHERE T_Key = '{tid}';
""".format(
tid=self.tid, value=value
)
)
self.conn.commit()
cursor.close()
return True, self.tr("Successfully reset T_LastUniqueId to {}.").format(
value
)
except sqlite3.Error as e:
cursor.close()
error_message = " ".join(e.args)
return False, self.tr("Could not reset T_LastUniqueId: {}").format(
error_message
)

return False, self.tr("Could not reset T_LastUniqueId")
40 changes: 39 additions & 1 deletion modelbaker/dbconnector/mssql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ def create_basket(self, dataset_tid, topic):
cur.execute(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', NEWID(), 'Qgis Model Baker')
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', NEWID(), 'modelbaker')
""".format(
schema=self.schema,
sequence="t_ili2db_seq",
Expand Down Expand Up @@ -1033,3 +1033,41 @@ def get_ili2db_settings(self):
)
result = self._get_dict_result(cur)
return result

def get_ili2db_sequence_value(self):
if self.schema:
cur = self.conn.cursor()
cur.execute(
"""
SELECT NEXT VALUE FOR {schema}.{sequence};
""".format(
schema=self.schema, sequence="t_ili2db_seq"
)
)
content = cur.fetchone()
if content:
return content[0]
return None

def set_ili2db_sequence_value(self, value):
if self.schema:
cur = self.conn.cursor()
try:
cur.execute(
"""
ALTER SEQUENCE {schema}.{sequence} RESTART WITH {value};
""".format(
schema=self.schema, sequence="t_ili2db_seq", value=value
)
)
self.conn.commit()
return True, self.tr(
'Successfully reset sequence value to "{}".'
).format(value)
except pyodbc.errors.Error as e:
error_message = " ".join(e.args)
return False, self.tr("Could not reset sequence: {}").format(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not be better to throw an exception instead of returning false? I'm thinking more general that only this case. Or is it a design décision for the library?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oopsi. Haven't seen this before merging. Good input, but I wanted it to make it identical to other functions. Some (e.g.) create_baskets are called for multiple baskets, where in the frontend I 'collect' all the feedbacks and return it in a list. Kind of "5 of 10 baskets are not created" or similar.

error_message
)

return False, self.tr("Could not reset sequence")
40 changes: 39 additions & 1 deletion modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def create_basket(self, dataset_tid, topic):
cur.execute(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES (nextval('{schema}.{sequence}'), {dataset_tid}, '{topic}', uuid_generate_v4(), 'Qgis Model Baker')
VALUES (nextval('{schema}.{sequence}'), {dataset_tid}, '{topic}', uuid_generate_v4(), 'modelbaker')
""".format(
schema=self.schema,
sequence="t_ili2db_seq",
Expand Down Expand Up @@ -1075,6 +1075,44 @@ def get_ili2db_settings(self):
result = cur.fetchall()
return result

def get_ili2db_sequence_value(self):
if self.schema:
cur = self.conn.cursor()
cur.execute(
"""
SELECT last_value FROM {schema}.{sequence};
""".format(
schema=self.schema, sequence="t_ili2db_seq"
)
)
content = cur.fetchone()
if content:
return content[0]
return None

def set_ili2db_sequence_value(self, value):
if self.schema:
cur = self.conn.cursor()
try:
cur.execute(
"""
ALTER SEQUENCE {schema}.{sequence} RESTART WITH {value};
""".format(
schema=self.schema, sequence="t_ili2db_seq", value=value
)
)
self.conn.commit()
return True, self.tr(
'Successfully reset sequence value to "{}".'
).format(value)
except psycopg2.errors.Error as e:
error_message = " ".join(e.args)
return False, self.tr("Could not reset sequence: {}").format(
error_message
)

return False, self.tr("Could not reset sequence")

def get_schemas(self):
cursor = self.conn.cursor()
try:
Expand Down
2 changes: 1 addition & 1 deletion modelbaker/iliwrapper/ili2dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def error_string(self):
if self.java_version:
return QCoreApplication.translate(
"ili2dbutils",
'Wrong java version found. Qgis Model Baker requires at least java version 8. Please <a href="{0}">install Java</a> and or <a href="{1}">configure a custom java path</a>.<br/><br/>Java Version:<br/>{2}',
'Wrong java version found. Model Baker requires at least java version 8. Please <a href="{0}">install Java</a> and or <a href="{1}">configure a custom java path</a>.<br/><br/>Java Version:<br/>{2}',
).format(
JavaNotFoundError.JAVA_DOWNLOAD_URL,
JavaNotFoundError.PLUGIN_CONFIGURATION_URL,
Expand Down
10 changes: 5 additions & 5 deletions modelbaker/iliwrapper/ilicache.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _process_ilisite(self, file):
file=file, exception=str(e)
)
),
self.tr("QGIS Model Baker"),
self.tr("modelbaker"),
)
return

Expand Down Expand Up @@ -206,7 +206,7 @@ def _process_informationfile(self, file, netloc, url):
file=file, exception=str(e)
)
),
self.tr("QGIS Model Baker"),
self.tr("modelbaker"),
)
return

Expand Down Expand Up @@ -300,7 +300,7 @@ def process_ili_file(self, ilifile):
ilifile=ilifile, exception=str(e)
)
),
self.tr("QGIS Model Baker"),
self.tr("modelbaker"),
)
fileModels = list()

Expand Down Expand Up @@ -472,7 +472,7 @@ def _process_informationfile(self, file, netloc, url):
file=file, exception=str(e)
)
),
self.tr("QGIS Model Baker"),
self.tr("modelbaker"),
)
return

Expand Down Expand Up @@ -903,7 +903,7 @@ def _process_informationfile(self, file, netloc, url):
file=file, exception=str(e)
)
),
self.tr("QGIS Model Baker"),
self.tr("modelbaker"),
)
return

Expand Down
4 changes: 2 additions & 2 deletions modelbaker/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def get_db_connector(configuration):
"There was an error connecting to the database. Check connection parameters. Error details: {}".format(
db_connector_error
),
"QGIS Model Baker",
"modelbaker",
)
return None
except FileNotFoundError as file_not_found_error:
QgsMessageLog.logMessage(
"There was an error connecting to the database. Check connection parameters. Error details: {}".format(
file_not_found_error
),
"QGIS Model Baker",
"modelbaker",
)
return None

Expand Down
Loading