mirror of
https://github.com/283375/arcaea-offline.git
synced 2025-04-21 15:00:18 +00:00
fix: singleton Database
behavior
This commit is contained in:
parent
a6c1e594c4
commit
262495a580
@ -1,3 +1,6 @@
|
|||||||
|
import logging
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import Engine, inspect, select
|
from sqlalchemy import Engine, inspect, select
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
@ -6,14 +9,35 @@ from .models.scores import *
|
|||||||
from .models.songs import *
|
from .models.songs import *
|
||||||
from .singleton import Singleton
|
from .singleton import Singleton
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Database(metaclass=Singleton):
|
class Database(metaclass=Singleton):
|
||||||
def __init__(self, engine: Engine):
|
def __init__(self, engine: Optional[Engine]):
|
||||||
self.engine = engine
|
try:
|
||||||
|
self.__engine
|
||||||
|
except AttributeError:
|
||||||
|
self.__engine = None
|
||||||
|
|
||||||
|
if engine is None:
|
||||||
|
if isinstance(self.engine, Engine):
|
||||||
|
return
|
||||||
|
raise ValueError("No sqlalchemy.Engine instance specified before.")
|
||||||
|
elif isinstance(engine, Engine):
|
||||||
|
if isinstance(self.engine, Engine):
|
||||||
|
logger.warning(
|
||||||
|
f"A sqlalchemy.Engine instance {self.engine} has been specified "
|
||||||
|
f"and will be replaced to {engine}"
|
||||||
|
)
|
||||||
|
self.engine = engine
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
f"A sqlalchemy.Engine instance expected, not {repr(engine)}"
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def engine(self):
|
def engine(self) -> Engine:
|
||||||
return self.__engine
|
return self.__engine # type: ignore
|
||||||
|
|
||||||
@engine.setter
|
@engine.setter
|
||||||
def engine(self, value: Engine):
|
def engine(self, value: Engine):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user