diff --git a/cocluto/SimpaDbUtil.py b/cocluto/SimpaDbUtil.py index 35d8429..09a389d 100644 --- a/cocluto/SimpaDbUtil.py +++ b/cocluto/SimpaDbUtil.py @@ -79,6 +79,13 @@ class ISqlDatabaseBackend(object): """ raise NotImplementedError() + @abc.abstractmethod + def get_last_insert_id(self) -> int: + """ + :return: the id of the last inserted row + """ + raise NotImplementedError() + @abc.abstractmethod def table_exists(self, table_name: str) -> bool: """returns true if the given table exists in the database @@ -139,6 +146,12 @@ class RemoteMysqlDb(ISqlDatabaseBackend): rows = self._conn.store_result() return rows + def get_last_insert_id(self) -> int: + """ + :return: the id of the last inserted row + """ + return int(self.query("SELECT last_insert_id();")[0][0]) + def table_exists(self, table_name: str) -> bool: rows = self.query(f"SHOW TABLES LIKE '{table_name}';") assert len(rows) <= 1, f'Unexpected case: more than one ({len(rows)}) tables match the table name {table_name}.' @@ -190,6 +203,12 @@ class SshAccessedMysqlDb(ISqlDatabaseBackend): rows = completed_process.stdout.decode('utf-8').split('\n') return rows + def get_last_insert_id(self) -> int: + """ + :return: the id of the last inserted row + """ + return int(self.query("SELECT last_insert_id();")[0][0]) + def table_exists(self, table_name: str) -> bool: rows = self.query(f"SHOW TABLES LIKE '{table_name}';") logging.debug('len(rows) = %d', len(rows)) @@ -274,6 +293,12 @@ class SqliteDb(ISqlDatabaseBackend): self._con.commit() return rows + def get_last_insert_id(self) -> int: + """ + :return: the id of the last inserted row + """ + return int(self.query("SELECT last_insert_rowid();")[0][0]) + def table_exists(self, table_name: str) -> bool: rows = self.query(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}';") assert len(rows) <= 1, f'Unexpected case: more than one ({len(rows)}) tables match the table name {table_name}.' diff --git a/cocluto/quman.py b/cocluto/quman.py index b4bc242..973dd67 100755 --- a/cocluto/quman.py +++ b/cocluto/quman.py @@ -259,7 +259,7 @@ class QueueManager: sql_query = f"INSERT INTO log (timestamp, user_id, host_fqdn, queue_machines, action, disable_id, reason) VALUES ('{timestamp}', '{userid}', '{host_fqdn}', '{','.join(queue_machines)}', '{action}', '{disable_id}', '{reason}');" self.db_backend.query(sql_query) # get the log id of the disable action that was just inserted - log_id = self.db_backend.query("SELECT last_insert_rowid();")[0][0] + log_id = self.db_backend.get_last_insert_id() return log_id def get_disable_requests(self, queue_machine: QueueMachineId) -> Dict[int, DisableRequest]: diff --git a/cocluto/version.py b/cocluto/version.py index 7aa46e9..8d2e3cb 100644 --- a/cocluto/version.py +++ b/cocluto/version.py @@ -1,4 +1,4 @@ -__version__ = '1.0.21' +__version__ = '1.0.22' class Version(object):