mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
41 lines
854 B
Python
41 lines
854 B
Python
|
from __future__ import annotations
|
||
|
|
||
|
import abc
|
||
|
|
||
|
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
|
||
|
|
||
|
from ..core import app
|
||
|
|
||
|
|
||
|
preregistered_managers: list[type[BaseDatabaseManager]] = []
|
||
|
|
||
|
def manager_class(name: str) -> None:
|
||
|
"""注册一个数据库管理类"""
|
||
|
|
||
|
def decorator(cls: type[BaseDatabaseManager]) -> type[BaseDatabaseManager]:
|
||
|
cls.name = name
|
||
|
preregistered_managers.append(cls)
|
||
|
return cls
|
||
|
|
||
|
return decorator
|
||
|
|
||
|
|
||
|
class BaseDatabaseManager(abc.ABC):
|
||
|
"""基础数据库管理类"""
|
||
|
|
||
|
name: str
|
||
|
|
||
|
ap: app.Application
|
||
|
|
||
|
engine: sqlalchemy_asyncio.AsyncEngine
|
||
|
|
||
|
def __init__(self, ap: app.Application) -> None:
|
||
|
self.ap = ap
|
||
|
|
||
|
@abc.abstractmethod
|
||
|
async def initialize(self) -> None:
|
||
|
pass
|
||
|
|
||
|
def get_engine(self) -> sqlalchemy_asyncio.AsyncEngine:
|
||
|
return self.engine
|