diff --git a/gsuid_core/utils/database/base_models.py b/gsuid_core/utils/database/base_models.py index 95a7b44..721d35d 100644 --- a/gsuid_core/utils/database/base_models.py +++ b/gsuid_core/utils/database/base_models.py @@ -17,6 +17,7 @@ from sqlalchemy.ext.asyncio import async_sessionmaker # type: ignore from sqlmodel import Field, SQLModel, col, and_, delete, select, update from gsuid_core.data_store import get_res_path +from gsuid_core.utils.plugins_config.gs_config import database_config T_BaseModel = TypeVar('T_BaseModel', bound='BaseModel') T_BaseIDModel = TypeVar('T_BaseIDModel', bound='BaseIDModel') @@ -27,13 +28,61 @@ T_Cache = TypeVar('T_Cache', bound='Cache') P = ParamSpec("P") R = TypeVar("R") -DB_PATH = get_res_path() / 'GsData.db' -db_url = str(DB_PATH) -url = f'sqlite+aiosqlite:///{db_url}' -engine = create_async_engine(url, pool_recycle=1500) -async_maker = async_sessionmaker( - engine, expire_on_commit=False, class_=AsyncSession -) + +db_host: str = database_config.get_config('db_host').data +db_port: str = database_config.get_config('db_port').data +db_user: str = database_config.get_config('db_user').data +db_password: str = database_config.get_config('db_password').data +db_name: str = database_config.get_config('db_name').data + +db_pool_size: Optional[int] = database_config.get_config('db_pool_size').data +db_echo: bool = database_config.get_config('db_echo').data +db_pool_recycle: int = database_config.get_config('db_pool_recycle').data + +db_custom_url = database_config.get_config('db_custom_url').data +db_type: str = database_config.get_config('db_type').data + +_db_type = db_type.lower() +db_config = { + 'pool_recycle': db_pool_recycle, + 'pool_size': db_pool_size, + 'echo': db_echo, +} + +if _db_type == 'sqlite': + base_url = 'sqlite+aiosqlite:///' + DB_PATH = get_res_path() / 'GsData.db' + db_url = str(DB_PATH) + del db_config['pool_size'] +elif _db_type == 'mysql': + base_url = 'mysql+aiomysql://' + db_hp = f'{db_host}:{db_port}' if db_port else db_host + db_url = f'{db_user}:{db_password}@{db_hp}/{db_name}' +elif _db_type == 'postgresql': + base_url = 'postgresql+asyncpg://' + db_hp = f'{db_host}:{db_port}' if db_port else db_host + db_url = f'{db_user}:{db_password}@{db_hp}/{db_name}' +elif _db_type == '自定义': + base_url = '' + db_url = db_custom_url +else: + base_url = db_type + db_url = db_custom_url + + +url = f'{base_url}{db_url}' + +try: + engine = create_async_engine(url, **db_config) + async_maker = async_sessionmaker( + engine, + expire_on_commit=False, + class_=AsyncSession, + ) +except: # noqa: E722 + raise ValueError( + f'[GsCore] [数据库] [{base_url}] 连接失败, 请检查配置文件!' + ) def with_session( diff --git a/gsuid_core/utils/plugins_config/database_config.py b/gsuid_core/utils/plugins_config/database_config.py new file mode 100644 index 0000000..95a43c2 --- /dev/null +++ b/gsuid_core/utils/plugins_config/database_config.py @@ -0,0 +1,65 @@ +from typing import Dict + +from .models import GSC, GsIntConfig, GsStrConfig, GsBoolConfig + +DATABASE_CONIFG: Dict[str, GSC] = { + 'db_type': GsStrConfig( + '数据库类型', + '设置喜欢的数据库类型', + 'SQLite', + ['SQLite', 'MySql', 'PostgreSQL', '自定义'], + ), + 'db_custom_url': GsStrConfig( + '自定义数据库连接地址 (一般无需填写)', + '设置自定义数据库连接', + '', + ['sqlite+aiosqlite:///E://GenshinUID//gsdata.db'], + ), + 'db_host': GsStrConfig( + '数据库地址', + '设置数据库地址', + 'localhost', + ['localhost'], + ), + 'db_port': GsStrConfig( + '数据库端口', + '设置数据库端口', + '3306', + ['3306'], + ), + 'db_user': GsStrConfig( + '数据库用户名', + '设置数据库用户名', + 'root', + ['root', 'admin'], + ), + 'db_password': GsStrConfig( + '数据库密码', + '设置数据库密码', + 'root', + ['root', '123456'], + ), + 'db_name': GsStrConfig( + '数据库名称', + '设置数据库名称', + 'GsData', + ['GsData'], + ), + 'db_pool_size': GsIntConfig( + '数据库连接池大小', + '设置数据库连接池大小', + 5, + options=[5, 10, 20, 30, 40, 50], + ), + 'db_echo': GsBoolConfig( + '数据库调试模式', + '设置数据库调试模式', + False, + ), + 'db_pool_recycle': GsIntConfig( + '数据库连接池回收时间', + '设置数据库连接池回收时间', + 1500, + options=[1500, 3600, 7200, 14400, 28800], + ), +} diff --git a/gsuid_core/utils/plugins_config/gs_config.py b/gsuid_core/utils/plugins_config/gs_config.py index 291ebd9..56fbc59 100644 --- a/gsuid_core/utils/plugins_config/gs_config.py +++ b/gsuid_core/utils/plugins_config/gs_config.py @@ -9,6 +9,7 @@ from gsuid_core.data_store import get_res_path from .sp_config import SP_CONIFG from .config_default import CONIFG_DEFAULT from .pic_gen_config import PIC_GEN_CONIFG +from .database_config import DATABASE_CONIFG from .security_config import SECURITY_CONFIG from .send_pic_config import SEND_PIC_CONIFG from .pic_server_config import PIC_UPLOAD_CONIFG @@ -194,3 +195,9 @@ sp_config = StringConfig( RES / 'sp_config.json', SP_CONIFG, ) + +database_config = StringConfig( + 'GsCore数据库配置', + RES / 'database_config.json', + DATABASE_CONIFG, +) diff --git a/gsuid_core/utils/plugins_config/models.py b/gsuid_core/utils/plugins_config/models.py index 7d8c2b4..b314f0c 100644 --- a/gsuid_core/utils/plugins_config/models.py +++ b/gsuid_core/utils/plugins_config/models.py @@ -33,6 +33,7 @@ class GsListConfig(GsConfig, tag=True): class GsIntConfig(GsConfig, tag=True): data: int max_value: Optional[int] = None + options: List[int] = [] GSC = Union[