diff --git a/backend/src/module/database/bangumi.py b/backend/src/module/database/bangumi.py index 6d0919f5..97efe1b4 100644 --- a/backend/src/module/database/bangumi.py +++ b/backend/src/module/database/bangumi.py @@ -1,9 +1,6 @@ import logging from module.database.orm import Connector - -from module.ab_decorator import locked -from module.database.connector import DataConnector from module.models import BangumiData from module.conf import DATA_PATH @@ -91,26 +88,10 @@ class BangumiDatabase(Connector): location = {"title_raw": title_raw} set_value = {"poster_link": poster_link} self.update.value(location, set_value) - # self._cursor.execute( - # """ - # UPDATE bangumi - # SET poster_link = :poster_link - # WHERE title_raw = :title_raw - # """, - # {"poster_link": poster_link, "title_raw": title_raw}, - # ) - # self._conn.commit() logger.debug(f"[Database] Update {title_raw} poster_link to {poster_link}.") def delete_one(self, _id: int): self.delete.one(_id) - # self._cursor.execute( - # """ - # DELETE FROM bangumi WHERE id = :id - # """, - # {"id": _id}, - # ) - # self._conn.commit() logger.debug(f"[Database] Delete bangumi id: {_id}.") def delete_all(self): @@ -122,8 +103,6 @@ class BangumiDatabase(Connector): def search_id(self, _id: int) -> BangumiData | None: dict_data = self.select.one(conditions={"id": _id}) - # condition = {"id": _id} - # dict_data = self._search_data(table_name=self.__table_name, condition=condition) if dict_data is None: logger.warning(f"[Database] Cannot find bangumi id: {_id}.") return None diff --git a/backend/src/module/database/orm/connector.py b/backend/src/module/database/orm/connector.py index d30088a9..255aa290 100644 --- a/backend/src/module/database/orm/connector.py +++ b/backend/src/module/database/orm/connector.py @@ -57,3 +57,6 @@ class Connector: if keys: return [dict(zip(keys, data)) for data in datas] return [dict(zip(self._columns, data)) for data in datas] + + def fetch(self): + return self._cursor.fetchall() diff --git a/backend/src/module/database/orm/update.py b/backend/src/module/database/orm/update.py index 96dc8fa6..7b022418 100644 --- a/backend/src/module/database/orm/update.py +++ b/backend/src/module/database/orm/update.py @@ -9,6 +9,15 @@ class Update: self._table_name = table_name self._example_data = data + def __table_exists(self) -> bool: + self._connector.execute( + f""" + SELECT name FROM sqlite_master + WHERE type='table' AND name='{self._table_name}' + """ + ) + return self._connector.fetch() is not None + def table(self): columns = ", ".join( [ @@ -18,8 +27,9 @@ class Update: ) create_table_sql = f"CREATE TABLE IF NOT EXISTS {self._table_name} ({columns});" self._connector.execute(create_table_sql) + logger.debug(f"Create table {self._table_name}.") self._connector.execute(f"PRAGMA table_info({self._table_name})") - existing_columns = self._connector._columns + existing_columns = [x[1] for x in self._connector.fetch()] for key, value in self._example_data.items(): if key not in existing_columns: insert_column = self.__python_to_sqlite_type(value) @@ -27,7 +37,7 @@ class Update: value = "NULL" add_column_sql = f"ALTER TABLE {self._table_name} ADD COLUMN {key} {insert_column} DEFAULT {value};" self._connector.execute(add_column_sql) - logger.debug(f"Create / Update table {self._table_name}.") + logger.debug(f"Update table {self._table_name}.") def one(self, data: dict) -> bool: _id = data["id"] diff --git a/backend/src/test/test_database.py b/backend/src/test/test_database.py index f7fc94f5..31446ba3 100644 --- a/backend/src/test/test_database.py +++ b/backend/src/test/test_database.py @@ -28,6 +28,7 @@ def test_database(): with BangumiDatabase(database=TEST_PATH) as database: # create table database.update_table() + with BangumiDatabase(database=TEST_PATH) as database: # insert database.insert_one(test_data) assert database.search_id(1) == test_data