将ProjectConfig加入Architecture
This commit is contained in:
@@ -5,8 +5,9 @@ from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor
|
||||
from ..Convention.Runtime.Architecture import Architecture
|
||||
from ..Convention.Runtime.File import ToolFile
|
||||
|
||||
config = ProjectConfig()
|
||||
DATABASE_PATH = config.GetFile(config.FindItem("database_path", "db.db"), False).GetFullPath()
|
||||
logger: ProjectConfig = Architecture.Get(ProjectConfig)
|
||||
DATABASE_PATH = logger.GetFile(logger.FindItem("database_path", "db.db"), False).GetFullPath()
|
||||
logger.SaveProperties()
|
||||
|
||||
class Database:
|
||||
"""数据库管理类"""
|
||||
@@ -47,9 +48,9 @@ class Database:
|
||||
self._conn.execute("PRAGMA cache_size=1000")
|
||||
self._conn.execute("PRAGMA temp_store=MEMORY")
|
||||
|
||||
config.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接成功: {self.db_path}{ConsoleFrontColor.RESET}")
|
||||
logger.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接成功: {self.db_path}{ConsoleFrontColor.RESET}")
|
||||
except Exception as e:
|
||||
config.Log("Error", f"{ConsoleFrontColor.RED}数据库连接失败: {e}{ConsoleFrontColor.RESET}")
|
||||
logger.Log("Error", f"{ConsoleFrontColor.RED}数据库连接失败: {e}{ConsoleFrontColor.RESET}")
|
||||
raise
|
||||
return self._conn
|
||||
|
||||
@@ -75,7 +76,7 @@ class Database:
|
||||
if not self._table_exists(table_name):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute(f"CREATE TABLE IF NOT EXISTS {table_name} (id INTEGER PRIMARY KEY AUTOINCREMENT)")
|
||||
config.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 创建{ConsoleFrontColor.RESET}")
|
||||
logger.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 创建{ConsoleFrontColor.RESET}")
|
||||
return self
|
||||
|
||||
def _column_exists(self, table_name: str, column_name: str) -> bool:
|
||||
@@ -105,9 +106,9 @@ class Database:
|
||||
try:
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute(f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_def}")
|
||||
config.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 添加列 {column_name}{ConsoleFrontColor.RESET}")
|
||||
logger.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 添加列 {column_name}{ConsoleFrontColor.RESET}")
|
||||
except Exception as e:
|
||||
config.Log("Warning", f"{ConsoleFrontColor.YELLOW}添加列失败: {e}{ConsoleFrontColor.RESET}")
|
||||
logger.Log("Warning", f"{ConsoleFrontColor.YELLOW}添加列失败: {e}{ConsoleFrontColor.RESET}")
|
||||
|
||||
def define_column(self, table_name: str, column_name: str, column_def: str):
|
||||
"""定义列
|
||||
@@ -129,7 +130,7 @@ class Database:
|
||||
if self._conn:
|
||||
self._conn.close()
|
||||
self._conn = None
|
||||
config.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接已关闭{ConsoleFrontColor.RESET}")
|
||||
logger.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接已关闭{ConsoleFrontColor.RESET}")
|
||||
|
||||
def get_db() -> Database:
|
||||
"""获取全局数据库实例(单例模式)"""
|
||||
|
||||
Reference in New Issue
Block a user