This commit is contained in:
‘KimigaiiWuyi’ 2023-04-27 17:42:37 +08:00
parent 5a45e56757
commit 10b666d691

View File

@ -8,13 +8,22 @@ from gsuid_core.utils.database.dal import SQLA
is_wal = False is_wal = False
active_sqla: Dict[str, SQLA] = {} active_sqla: Dict[str, SQLA] = {}
active_sr_sqla: Dict[str, SQLA] = {}
db_url = str(get_res_path().parent / 'GsData.db') db_url = str(get_res_path().parent / 'GsData.db')
def get_sqla(bot_id) -> SQLA: class DBSqla:
if bot_id not in active_sqla: def __init__(self, is_sr: bool = False) -> None:
sqla = SQLA(db_url, bot_id) self.is_sr = is_sr
active_sqla[bot_id] = sqla
def get_sqla(self, bot_id) -> SQLA:
return self._get_sqla(bot_id, self.is_sr)
def _get_sqla(self, bot_id, is_sr: bool = False) -> SQLA:
sqla_list = active_sr_sqla if is_sr else active_sqla
if bot_id not in sqla_list:
sqla = SQLA(db_url, bot_id, is_sr)
sqla_list[bot_id] = sqla
sqla.create_all() sqla.create_all()
@event.listens_for(sqla.engine.sync_engine, 'connect') @event.listens_for(sqla.engine.sync_engine, 'connect')
@ -24,4 +33,10 @@ def get_sqla(bot_id) -> SQLA:
cursor.execute('PRAGMA journal_mode=WAL') cursor.execute('PRAGMA journal_mode=WAL')
cursor.close() cursor.close()
return active_sqla[bot_id] return sqla_list[bot_id]
def get_gs_sqla(self, bot_id):
return self._get_sqla(bot_id, False)
def get_sr_sqla(self, bot_id):
return self._get_sqla(bot_id, True)