diff --git a/backup.py b/backup.py index 32c0328..edcd38e 100644 --- a/backup.py +++ b/backup.py @@ -241,6 +241,7 @@ class DataBase: """) self.conn.row_factory = dict_factory + # self.conn.set_trace_callback(print) self.conn.commit() @@ -272,9 +273,25 @@ class DataBase: try: crypt_id_list.append(cursor.fetchone()['crypt_id']) except TypeError: - cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""") - return cursor.fetchone()['crypt_id'] - return most_frequent(crypt_id_list) + pass + try: + id = most_frequent(crypt_id_list) + except ValueError: + cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""") + return cursor.fetchone()['crypt_id'] + params = {'id': id, + 'name': ', '.join([f"'{file['name']}'" for file in list_file]), + 'path': ', '.join([f"'{file['path']}'" for file in list_file])} + cursor.execute("""SELECT 1 FROM files + WHERE crypt_id={id} + AND name NOT IN ({name}) + AND path NOT IN ({path})""".format(**params)) + neighbour = cursor.fetchall() + if len(neighbour) > 0: + cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""") + return cursor.fetchone()['crypt_id'] + else: + return id def exist(self, file): cursor = self.conn.cursor() @@ -313,7 +330,7 @@ class DataBase: file_id += 1 proceed = True if proceed: - cursor.execute("""INSERT INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode)) + cursor.execute("""INSERT OR IGNORE INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode)) self.conn.commit() return str(crypt_id).zfill(ZFILL) else: