From 36f95734cb811fa2b4961ff0b079564398887745 Mon Sep 17 00:00:00 2001 From: user01 Date: Sun, 25 Oct 2026 17:28:56 +0900 Subject: [PATCH] =?UTF-8?q?=EC=98=A4=EB=9E=9C=EB=A7=8C=EC=9D=B4=EB=9D=BC?= =?UTF-8?q?=20=EB=AD=90=EA=B0=80=20=EC=88=98=EC=A0=95=EB=90=9C=EC=A7=80=20?= =?UTF-8?q?=EC=9E=98=20=EB=AA=B0=EB=9D=BC=EC=84=9C=20=EC=9D=BC=EB=8B=A8=20?= =?UTF-8?q?=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataClass_Pupil.py | 47 ++++++++++++++++++++++---------------------- MgrCalibreDB.py | 10 +++++----- MgrPupilColDB.py | 17 ++++++++-------- MgrSQLiteDB.py | 49 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 78 insertions(+), 45 deletions(-) diff --git a/DataClass_Pupil.py b/DataClass_Pupil.py index a0cc339..23a17ca 100644 --- a/DataClass_Pupil.py +++ b/DataClass_Pupil.py @@ -145,46 +145,45 @@ class PupilData: return retInfo """ + def _get_gallery_data(self) -> dict: + if not isinstance(self.m_data, dict): + return {} + + gallery_data = self.m_data.get(GALINFO) + if not isinstance(gallery_data, dict): + gallery_data = self.m_data.get(GALBLOCK) + + return gallery_data if isinstance(gallery_data, dict) else {} + # def GetTitle(self) -> str: - retStr : str = "" - if None != self.m_data: - retStr = self.m_data[GALINFO]["title"] - - return retStr + return self._get_gallery_data().get("title", "") # def GetHitomiID(self) -> str: - retStr : str = "" - if None != self.m_data: - retStr = self.m_data[GALINFO]["id"] - - return retStr + return self._get_gallery_data().get("id", "") # def GetImgFileCount(self) -> int: - if None == self.m_data: - return 0 - - return len(self.m_data[GALINFO]["files"]) + return len(self._get_gallery_data().get("files", [])) # pupil 의 JSON 을 파싱해서 ImageFileList 를 반환한다. def GetImgFileList(self) -> list[info.ImageFileInfo]: listRet = list[info.ImageFileInfo]() - if None != self.m_data: - listFiles = self.m_data[GALINFO]["files"] - for item in listFiles: - tempInfo = info.ImageFileInfo(item["name"], - item["height"], - item["width"], - item["hash"], - item["haswebp"]) - listRet.append(tempInfo) + listFiles = self._get_gallery_data().get("files", []) + for item in listFiles: + tempInfo = info.ImageFileInfo( + item["name"], + item["height"], + item["width"], + item["hash"], + item["haswebp"], + ) + listRet.append(tempInfo) return listRet # def GetText(self) -> str: return util.PrintJSONTree(self.m_data) - diff --git a/MgrCalibreDB.py b/MgrCalibreDB.py index e36751c..0a0a2d5 100644 --- a/MgrCalibreDB.py +++ b/MgrCalibreDB.py @@ -24,7 +24,7 @@ class MgrCalibreDB(MyDB.MgrSQLiteDB): if True == util.IsEmptyStr(strTableName): return -1 - strSQL = f"SELECT MAX(id) FROM \'{strTableName}\';" + strSQL = f"SELECT MAX(id) FROM \"{strTableName}\";" listMaxID = self.SQLExecute_Get(self.cursor, strSQL) if listMaxID is None or 0 >= len(listMaxID): util.DbgOut("Error : Invalid Table Name") @@ -142,8 +142,8 @@ class MgrCalibreDB(MyDB.MgrSQLiteDB): return strAuthorSort = strAuthor.replace(" ", ",") - strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strAuthor}\',\'{strAuthorSort}\', \'\');" - self.SQLExecute(self.cursor, strSQL, True) + strSQL = f"INSERT OR IGNORE INTO \"{strTableName}\" VALUES (?, ?, ?, ?);" + self.SQLExecute_Params(self.cursor, strSQL, (nMaxID, strAuthor, strAuthorSort, ""), True) # @@ -157,8 +157,8 @@ class MgrCalibreDB(MyDB.MgrSQLiteDB): util.DbgOut("Error : Invalid Table Name") return - strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strTag}\', \'\');" - self.SQLExecute(self.cursor, strSQL, True) + strSQL = f"INSERT OR IGNORE INTO \"{strTableName}\" VALUES (?, ?, ?);" + self.SQLExecute_Params(self.cursor, strSQL, (nMaxID, strTag, ""), True) """ #Example usage diff --git a/MgrPupilColDB.py b/MgrPupilColDB.py index dc00014..727b131 100644 --- a/MgrPupilColDB.py +++ b/MgrPupilColDB.py @@ -37,14 +37,13 @@ class MgrPupilColDB(MyDB.MgrSQLiteDB): if True == util.IsEmptyStr(strName) or True == util.IsEmptyStr(strTableName): return -1 - strSQL = f"SELECT id FROM \'{strTableName}\' WHERE name = \'{strName}\';" - self.SQLExecute(self.cursor, strSQL, True) - result = self.cursor.fetchone() + strSQL = f"SELECT id FROM \"{strTableName}\" WHERE name = ?;" + listRet = self.SQLExecute_Get_Params(self.cursor, strSQL, (strName,)) - if result is None: + if listRet is None or 0 >= len(listRet): return -1 - return result[0] + return listRet[0][0] # def GetAuthorIDByName(self, strName: str) -> int: @@ -129,7 +128,7 @@ class MgrPupilColDB(MyDB.MgrSQLiteDB): strTableName = "authors" try: - strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' (name, group, language, desc) VALUES (?, ?, ?, ?)" + strSQL = f"INSERT OR IGNORE INTO \"{strTableName}\" (name, group, language, desc) VALUES (?, ?, ?, ?)" self.cursor.execute(strSQL, (strName, strGroup, strLanguage, strDesc)) nID = self.cursor.lastrowid self.conn.commit() @@ -163,7 +162,7 @@ class MgrPupilColDB(MyDB.MgrSQLiteDB): strTableName = "tags" try: - strSQL = f"INSERT INTO \'{strTableName}\' (name, sep_title, value) VALUES (?)" + strSQL = f"INSERT INTO \"{strTableName}\" (name, sep_title, value) VALUES (?, ?, ?)" self.cursor.execute(strSQL, (strName, strSep, strValue,)) nID = self.cursor.lastrowid self.conn.commit() @@ -183,7 +182,7 @@ class MgrPupilColDB(MyDB.MgrSQLiteDB): strTableName = "groups" try: - strSQL = f"INSERT INTO \'{strTableName}\' (name) VALUES (?)" + strSQL = f"INSERT INTO \"{strTableName}\" (name) VALUES (?)" self.cursor.execute(strSQL, (strName,)) nID = self.cursor.lastrowid self.conn.commit() @@ -201,7 +200,7 @@ class MgrPupilColDB(MyDB.MgrSQLiteDB): def AddFileInfo(self, strName: str, strHash: str, bHasAvif: bool, nWidth: int, nHeight: int) -> int: strTableName = "files" try: - strSQL = f"INSERT INTO \'{strTableName}\' (filename, hash, hasavif, width, height) VALUES (?)" + strSQL = f"INSERT INTO \"{strTableName}\" (filename, hash, hasavif, width, height) VALUES (?, ?, ?, ?, ?)" self.cursor.execute(strSQL, (strName, strHash, bHasAvif, nWidth, nHeight)) nID = self.cursor.lastrowid self.conn.commit() diff --git a/MgrSQLiteDB.py b/MgrSQLiteDB.py index 83c0c82..f06fbeb 100644 --- a/MgrSQLiteDB.py +++ b/MgrSQLiteDB.py @@ -27,7 +27,24 @@ class MgrSQLiteDB: return None try: - self.cursor.execute(strSQL) + cursor.execute(strSQL) + + if True == bCommit: + self.conn.commit() + + except sqlite3.Error as e: + util.DbgOut(f"SQLite Error occurred: {e}") + + # 파라미터 바인딩으로 쿼리를 실행한다. SQL 인젝션 방지용. + def SQLExecute_Params(self, cursor: sqlite3.Cursor, strSQL: str, tupleParams: tuple, bCommit: bool = True): + if not cursor: + return None + + if True == util.IsEmptyStr(strSQL): + return None + + try: + cursor.execute(strSQL, tupleParams) if True == bCommit: self.conn.commit() @@ -47,8 +64,26 @@ class MgrSQLiteDB: listRet = [] try: - self.cursor.execute(strSQL) - listRet = self.cursor.fetchall() + cursor.execute(strSQL) + listRet = cursor.fetchall() + except sqlite3.Error as e: + util.DbgOut(f"SQLite Error occurred: {e}") + + return listRet + + # 파라미터 바인딩으로 쿼리를 실행해서 결과를 가져온다. + def SQLExecute_Get_Params(self, cursor: sqlite3.Cursor, strSQL: str, tupleParams: tuple): + if not cursor: + return None + + if True == util.IsEmptyStr(strSQL): + return None + + listRet = [] + + try: + cursor.execute(strSQL, tupleParams) + listRet = cursor.fetchall() except sqlite3.Error as e: util.DbgOut(f"SQLite Error occurred: {e}") @@ -78,8 +113,8 @@ class MgrSQLiteDB: if 0 >= nID or True == util.IsEmptyStr(strTableName) or True == util.IsEmptyStr(strColName): return - strSQL = f"UPDATE {strTableName} SET \'{strColName}\' = \'{strValue}\' WHERE id = {nID};" - self.SQLExecute(self.cursor, strSQL) + strSQL = f"UPDATE {strTableName} SET \"{strColName}\" = ? WHERE id = ?;" + self.SQLExecute_Params(self.cursor, strSQL, (strValue, nID)) # 하나의 조건에 맞는 항목의 한 컬럼을 지정해서 값을 가져온다. @@ -87,8 +122,8 @@ class MgrSQLiteDB: if True == util.IsEmptyStr(strTableName) or True == util.IsEmptyStr(strGetCol) or True == util.IsEmptyStr(strCompCol): return [] - strSQL = f"SELECT {strGetCol} FROM {strTableName} WHERE {strCompCol} = {strCompValue};" - listRet = self.SQLExecute_Get(self.cursor, strSQL) + strSQL = f"SELECT {strGetCol} FROM {strTableName} WHERE {strCompCol} = ?;" + listRet = self.SQLExecute_Get_Params(self.cursor, strSQL, (strCompValue,)) if listRet is None or 0 >= len(listRet): return []