From f768b79c4ba068aa2c552874ac44352927c00e61 Mon Sep 17 00:00:00 2001 From: KimigaiiWuyi <444835641@qq.com> Date: Sun, 8 Sep 2024 05:15:02 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=BD=91=E9=A1=B5=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=88=96?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E8=AE=BE=E7=BD=AE=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=20(#69)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gsuid_core/utils/database/base_models.py | 63 ++++++++++++++++-- .../utils/plugins_config/database_config.py | 65 +++++++++++++++++++ gsuid_core/utils/plugins_config/gs_config.py | 7 ++ gsuid_core/utils/plugins_config/models.py | 1 + 4 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 gsuid_core/utils/plugins_config/database_config.py 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[