Changed DatabaseConnection

This commit is contained in:
Sven Heidemann 2020-12-11 19:42:09 +01:00
parent d2dc57bc76
commit ee60be9880
4 changed files with 23 additions and 4 deletions

View File

@ -1 +1 @@
# imports:

View File

@ -1,5 +1,8 @@
from abc import abstractmethod
from sqlalchemy import engine
from sqlalchemy.orm import session
from sh_edraft.service.base.service_base import ServiceBase
@ -9,5 +12,13 @@ class DatabaseConnectionBase(ServiceBase):
def __init__(self):
ServiceBase.__init__(self)
@property
@abstractmethod
def use_mysql(self, connection_string: str): pass
def engine(self) -> engine: pass
@property
@abstractmethod
def session(self) -> session: pass
@abstractmethod
def connect(self, connection_string: str): pass

View File

@ -21,9 +21,17 @@ class DatabaseConnection(DatabaseConnectionBase):
self.create()
@property
def engine(self) -> engine:
return self._engine
@property
def session(self) -> session:
return self._session
def create(self): pass
def use_mysql(self, connection_string: str):
def connect(self, connection_string: str):
try:
self._engine = create_engine(connection_string)

View File

@ -40,7 +40,7 @@ class Program(ApplicationBase):
db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings)
self._services.add_singleton(DatabaseConnectionBase, DatabaseConnection)
db: DatabaseConnectionBase = self._services.get_service(DatabaseConnectionBase)
db.use_mysql(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
db.connect(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
# Add and create logger
self._services.add_singleton(LoggerBase, Logger)