From 426c12f5cb0e8984a8396a8bf3a4243ee50d5ae7 Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Fri, 4 Jul 2025 08:18:36 +0200 Subject: [PATCH] add get_signature function for old signatures that are already in the database for all tables --- src/database.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/database.py b/src/database.py index 334f5c3..c07a335 100644 --- a/src/database.py +++ b/src/database.py @@ -63,12 +63,29 @@ class Database: # print(f"Error: Unknown signature type '{table}'. Please use 'ma', 'mb', or 'mc'.") return self.insert_ma(old_signature) + def get_signature(self, old_signature: str, table: str) -> str | None: + """Get the new signature for the given old signature from the specified table.""" + cursor = self.connection.cursor() + cursor.execute( + f"SELECT new_signature FROM {table} WHERE old_signature = ?", + (old_signature,), + ) + result = cursor.fetchone() + if result: + return result[0] + else: + print( + f"Error: The old_signature '{old_signature}' does not exist in the {table} table." + ) + return None + def insert_ma(self, old_signature: str) -> str|None: """Insert an old signature into the ma table and return the new signature. New Signatures is generated by using 01 / as prefix, then incrementing the number by 1 for each new entry.""" if self.signature_exists(old_signature, "ma"): print(f"Error: The old_signature '{old_signature}' already exists in the ma table.") - return None + signature = self.get_signature(old_signature, "ma") + return signature new_signature = self.create_new_signature("ma") try: cursor = self.connection.cursor() @@ -84,6 +101,12 @@ class Database: """Insert an old signature into the mb table and return the new signature. New Signatures is generated by using 02 / as prefix, then incrementing the number by 1 for each new entry.""" new_signature = self.create_new_signature("mb") + if self.signature_exists(old_signature, "mb"): + print( + f"Error: The old_signature '{old_signature}' already exists in the mb table." + ) + signature = self.get_signature(old_signature, "mb") + return signature try: cursor = self.connection.cursor() cursor.execute("INSERT INTO mb (old_signature, new_signature) VALUES (?, ?)", (old_signature, new_signature)) @@ -97,6 +120,12 @@ class Database: """Insert an old signature into the mc table and return the new signature. New Signatures is generated by using 03 / as prefix, then incrementing the number by 1 for each new entry.""" new_signature = self.create_new_signature("mc") + if self.signature_exists(old_signature, "mc"): + print( + f"Error: The old_signature '{old_signature}' already exists in the mc table." + ) + signature = self.get_signature(old_signature, "mc") + return signature try: cursor = self.connection.cursor() cursor.execute("INSERT INTO mc (old_signature, new_signature) VALUES (?, ?)", (old_signature, new_signature))