Refactored code
This commit is contained in:
25
src_old/sh_edraft/database/__init__.py
Normal file
25
src_old/sh_edraft/database/__init__.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.database
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.database'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
26
src_old/sh_edraft/database/connection/__init__.py
Normal file
26
src_old/sh_edraft/database/connection/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.database.connection
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.database.connection'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .database_connection import DatabaseConnection
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
26
src_old/sh_edraft/database/connection/base/__init__.py
Normal file
26
src_old/sh_edraft/database/connection/base/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.database.connection.base
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.database.connection.base'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .database_connection_base import DatabaseConnectionBase
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
@@ -0,0 +1,21 @@
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
from sqlalchemy import engine
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
|
||||
class DatabaseConnectionBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def engine(self) -> engine: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def session(self) -> Session: pass
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, connection_string: str): pass
|
56
src_old/sh_edraft/database/connection/database_connection.py
Normal file
56
src_old/sh_edraft/database/connection/database_connection.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import engine, create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from sh_edraft.database.connection.base.database_connection_base import DatabaseConnectionBase
|
||||
from sh_edraft.database.model.database_settings import DatabaseSettings
|
||||
from sh_edraft.console.console import Console
|
||||
from sh_edraft.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
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
|
||||
|
||||
@property
|
||||
def engine(self) -> engine:
|
||||
return self._engine
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
return self._session
|
||||
|
||||
def connect(self, connection_string: str):
|
||||
try:
|
||||
self._engine = create_engine(connection_string)
|
||||
|
||||
if self._db_settings.encoding is not None:
|
||||
self._engine.encoding = self._db_settings.encoding
|
||||
|
||||
if self._db_settings.case_sensitive is not None:
|
||||
self._engine.case_sensitive = self._db_settings.case_sensitive
|
||||
|
||||
if self._db_settings.echo is not None:
|
||||
self._engine.echo = self._db_settings.echo
|
||||
|
||||
self._engine.connect()
|
||||
|
||||
db_session = sessionmaker(bind=self._engine)
|
||||
self._session = db_session()
|
||||
Console.set_foreground_color(ForegroundColor.green)
|
||||
Console.write_line(f'[{__name__}] Connected to database')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(f'[{__name__}] Database connection failed -> {e}')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
exit()
|
||||
|
26
src_old/sh_edraft/database/context/__init__.py
Normal file
26
src_old/sh_edraft/database/context/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.database.context
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.database.context'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .database_context import DatabaseContext
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
26
src_old/sh_edraft/database/context/base/__init__.py
Normal file
26
src_old/sh_edraft/database/context/base/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.database.context.base
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.database.context.base'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .database_context_base import DatabaseContextBase
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
@@ -0,0 +1,27 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from sqlalchemy import engine
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
|
||||
|
||||
class DatabaseContextBase(ServiceBase):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def engine(self) -> engine: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def session(self) -> Session: pass
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, connection_string: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def _create_tables(self): pass
|
50
src_old/sh_edraft/database/context/database_context.py
Normal file
50
src_old/sh_edraft/database/context/database_context.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from sqlalchemy import engine, Table
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from sh_edraft.database.connection.database_connection import DatabaseConnection
|
||||
from sh_edraft.database.connection.base.database_connection_base import DatabaseConnectionBase
|
||||
from sh_edraft.database.context.base.database_context_base import DatabaseContextBase
|
||||
from sh_edraft.database.model.dbmodel import DBModel
|
||||
from sh_edraft.database.model.database_settings import DatabaseSettings
|
||||
from sh_edraft.console.console import Console
|
||||
from sh_edraft.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
class DatabaseContext(DatabaseContextBase):
|
||||
|
||||
def __init__(self, database_settings: DatabaseSettings):
|
||||
DatabaseContextBase.__init__(self)
|
||||
|
||||
self._db: DatabaseConnectionBase = DatabaseConnection(database_settings)
|
||||
self._tables: list[Table] = []
|
||||
|
||||
@property
|
||||
def engine(self) -> engine:
|
||||
return self._db.engine
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
return self._db.session
|
||||
|
||||
def create(self):
|
||||
pass
|
||||
|
||||
def connect(self, connection_string: str):
|
||||
self._db.connect(connection_string)
|
||||
self._create_tables()
|
||||
|
||||
def _create_tables(self):
|
||||
try:
|
||||
for subclass in DBModel.__subclasses__():
|
||||
self._tables.append(subclass.__table__)
|
||||
|
||||
DBModel.metadata.drop_all(self._db.engine, self._tables)
|
||||
DBModel.metadata.create_all(self._db.engine, self._tables, checkfirst=True)
|
||||
Console.set_foreground_color(ForegroundColor.green)
|
||||
Console.write_line(f'[{__name__}] Created tables')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(f'[{__name__}] Creating tables failed -> {e}')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
exit()
|
28
src_old/sh_edraft/database/model/__init__.py
Normal file
28
src_old/sh_edraft/database/model/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.database.model
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.database.model'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .database_settings import DatabaseSettings
|
||||
from .database_settings_name import DatabaseSettingsName
|
||||
from .dbmodel import DBModel
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
78
src_old/sh_edraft/database/model/database_settings.py
Normal file
78
src_old/sh_edraft/database/model/database_settings.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import traceback
|
||||
from typing import Optional
|
||||
|
||||
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
||||
from sh_edraft.database.model.database_settings_name import DatabaseSettingsName
|
||||
from sh_edraft.console.console import Console
|
||||
from sh_edraft.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
class DatabaseSettings(ConfigurationModelBase):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelBase.__init__(self)
|
||||
|
||||
self._connection_string: Optional[str] = None
|
||||
self._credentials: Optional[str] = None
|
||||
self._encoding: Optional[str] = None
|
||||
self._case_sensitive: Optional[bool] = None
|
||||
self._echo: Optional[bool] = None
|
||||
|
||||
@property
|
||||
def connection_string(self) -> str:
|
||||
return self._connection_string
|
||||
|
||||
@connection_string.setter
|
||||
def connection_string(self, connection_string: str):
|
||||
self._connection_string = connection_string
|
||||
|
||||
@property
|
||||
def credentials(self) -> str:
|
||||
return self._credentials
|
||||
|
||||
@credentials.setter
|
||||
def credentials(self, credentials: str):
|
||||
self._credentials = credentials
|
||||
|
||||
@property
|
||||
def encoding(self) -> str:
|
||||
return self._encoding
|
||||
|
||||
@encoding.setter
|
||||
def encoding(self, encoding: str) -> None:
|
||||
self._encoding = encoding
|
||||
|
||||
@property
|
||||
def case_sensitive(self) -> bool:
|
||||
return self._case_sensitive
|
||||
|
||||
@case_sensitive.setter
|
||||
def case_sensitive(self, case_sensitive: bool) -> None:
|
||||
self._case_sensitive = case_sensitive
|
||||
|
||||
@property
|
||||
def echo(self) -> bool:
|
||||
return self._echo
|
||||
|
||||
@echo.setter
|
||||
def echo(self, echo: bool) -> None:
|
||||
self._echo = echo
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._connection_string = settings[DatabaseSettingsName.connection_string.value]
|
||||
self._credentials = settings[DatabaseSettingsName.credentials.value]
|
||||
|
||||
if DatabaseSettingsName.encoding.value in settings:
|
||||
self._encoding = settings[DatabaseSettingsName.encoding.value]
|
||||
|
||||
if DatabaseSettingsName.case_sensitive.value in settings:
|
||||
self._case_sensitive = bool(settings[DatabaseSettingsName.case_sensitive.value])
|
||||
|
||||
if DatabaseSettingsName.echo.value in settings:
|
||||
self._echo = bool(settings[DatabaseSettingsName.echo.value])
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
||||
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
10
src_old/sh_edraft/database/model/database_settings_name.py
Normal file
10
src_old/sh_edraft/database/model/database_settings_name.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class DatabaseSettingsName(Enum):
|
||||
|
||||
connection_string = 'ConnectionString'
|
||||
credentials = 'Credentials'
|
||||
encoding = 'Encoding'
|
||||
case_sensitive = 'CaseSensitive'
|
||||
echo = 'Echo'
|
3
src_old/sh_edraft/database/model/dbmodel.py
Normal file
3
src_old/sh_edraft/database/model/dbmodel.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
DBModel: declarative_base = declarative_base()
|
Reference in New Issue
Block a user