2020-11-29 21:36:16 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from sqlalchemy import engine, create_engine
|
|
|
|
from sqlalchemy.orm import session, sessionmaker
|
|
|
|
|
|
|
|
from sh_edraft.database.base.database_connection_base import DatabaseConnectionBase
|
|
|
|
from sh_edraft.database.model.database_settings import DatabaseSettings
|
2020-11-29 21:48:17 +01:00
|
|
|
from sh_edraft.utils.console import Console
|
2020-11-29 21:36:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DatabaseConnection(DatabaseConnectionBase):
|
|
|
|
|
|
|
|
def __init__(self, database_settings: DatabaseSettings):
|
|
|
|
DatabaseConnectionBase.__init__(self)
|
|
|
|
|
|
|
|
self._db_settings = database_settings
|
|
|
|
|
|
|
|
self._engine: Optional[engine] = None
|
|
|
|
self._session: Optional[session] = None
|
|
|
|
self._credentials: Optional[str] = None
|
|
|
|
|
|
|
|
def create(self):
|
2020-11-29 21:48:17 +01:00
|
|
|
try:
|
|
|
|
self._engine = create_engine(self._db_settings.decrypted_connection_string)
|
2020-11-29 21:36:16 +01:00
|
|
|
|
2020-11-29 21:48:17 +01:00
|
|
|
if self._db_settings.encoding is not None:
|
|
|
|
self._engine.encoding = self._db_settings.encoding
|
2020-11-29 21:36:16 +01:00
|
|
|
|
2020-11-29 21:48:17 +01:00
|
|
|
if self._db_settings.case_sensitive is not None:
|
|
|
|
self._engine.case_sensitive = self._db_settings.case_sensitive
|
2020-11-29 21:36:16 +01:00
|
|
|
|
2020-11-29 21:48:17 +01:00
|
|
|
if self._db_settings.echo is not None:
|
|
|
|
self._engine.echo = self._db_settings.echo
|
2020-11-29 21:36:16 +01:00
|
|
|
|
2020-11-29 21:48:17 +01:00
|
|
|
self._engine.connect()
|
|
|
|
|
|
|
|
db_session = sessionmaker(bind=self._engine)
|
|
|
|
self._session = db_session()
|
|
|
|
Console.write_line(f'[{__name__}] Connected to database', 'green')
|
|
|
|
except Exception as e:
|
|
|
|
Console.write_line(f'[{__name__}] Database connection failed -> {e}', 'red')
|
|
|
|
exit()
|
2020-11-29 21:36:16 +01:00
|
|
|
|