[WIP] Added native mysql support
This commit is contained in:
parent
7616cd4c69
commit
d9f10edbb7
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -41,6 +41,10 @@
|
|||||||
"cwd": "${workspaceFolder}/src/tests/custom/database/src",
|
"cwd": "${workspaceFolder}/src/tests/custom/database/src",
|
||||||
"program": "main.py",
|
"program": "main.py",
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
|
"env": {
|
||||||
|
"PYTHON_ENVIRONMENT": "development",
|
||||||
|
"PYTHONPATH": "${workspaceFolder}/src/:$PYTHONPATH"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "CLI",
|
"name": "CLI",
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
"Name": "sh_cpl-core",
|
"Name": "sh_cpl-core",
|
||||||
"Version": {
|
"Version": {
|
||||||
"Major": "2021",
|
"Major": "2021",
|
||||||
"Minor": "10",
|
"Minor": "11",
|
||||||
"Micro": "2"
|
"Micro": "0"
|
||||||
},
|
},
|
||||||
"Author": "Sven Heidemann",
|
"Author": "Sven Heidemann",
|
||||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||||
|
@ -20,9 +20,9 @@ __version__ = '2021.10.2'
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
# imports:
|
# imports:
|
||||||
from .database_model import DatabaseModel
|
|
||||||
from .database_settings import DatabaseSettings
|
|
||||||
from .database_settings_name_enum import DatabaseSettingsNameEnum
|
from .database_settings_name_enum import DatabaseSettingsNameEnum
|
||||||
|
from .database_settings import DatabaseSettings
|
||||||
|
from .table_abc import TableABC
|
||||||
|
|
||||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||||
version_info = VersionInfo(major='2021', minor='10', micro='2')
|
version_info = VersionInfo(major='2021', minor='10', micro='2')
|
||||||
|
@ -1,59 +1,40 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import engine, create_engine
|
import mysql.connector as sql
|
||||||
from sqlalchemy.orm import Session, sessionmaker
|
|
||||||
|
|
||||||
from cpl_core.console.console import Console
|
from cpl_core.console.console import Console
|
||||||
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
|
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
|
||||||
from cpl_core.database.connection.database_connection_abc import DatabaseConnectionABC
|
from cpl_core.database.connection.database_connection_abc import \
|
||||||
|
DatabaseConnectionABC
|
||||||
from cpl_core.database.database_settings import DatabaseSettings
|
from cpl_core.database.database_settings import DatabaseSettings
|
||||||
|
from cpl_core.utils import CredentialManager
|
||||||
|
from mysql.connector.abstracts import MySQLConnectionAbstract
|
||||||
|
|
||||||
|
|
||||||
class DatabaseConnection(DatabaseConnectionABC):
|
class DatabaseConnection(DatabaseConnectionABC):
|
||||||
r"""Representation of the database connection
|
r"""Representation of the database connection
|
||||||
|
|
||||||
Parameter
|
|
||||||
---------
|
|
||||||
database_settings: :class:`cpl_core.database.database_settings.DatabaseSettings`
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, database_settings: DatabaseSettings):
|
def __init__(self):
|
||||||
DatabaseConnectionABC.__init__(self)
|
DatabaseConnectionABC.__init__(self)
|
||||||
|
|
||||||
self._db_settings = database_settings
|
self._database_server: Optional[MySQLConnectionAbstract] = None
|
||||||
|
|
||||||
self._engine: Optional[engine] = None
|
|
||||||
self._session: Optional[Session] = None
|
|
||||||
self._credentials: Optional[str] = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def engine(self) -> engine:
|
def server(self) -> MySQLConnectionAbstract:
|
||||||
return self._engine
|
return self._database_server
|
||||||
|
|
||||||
@property
|
def connect(self, database_settings: DatabaseSettings):
|
||||||
def session(self) -> Session:
|
|
||||||
return self._session
|
|
||||||
|
|
||||||
def connect(self, connection_string: str):
|
|
||||||
try:
|
try:
|
||||||
self._engine = create_engine(connection_string)
|
self._database_server = sql.connect(
|
||||||
|
host=database_settings.host,
|
||||||
if self._db_settings.auth_plugin is not None:
|
user=database_settings.user,
|
||||||
self._engine = create_engine(connection_string, connect_args={'auth_plugin': self._db_settings.auth_plugin})
|
passwd=CredentialManager.decrypt(database_settings.password),
|
||||||
|
db=database_settings.database,
|
||||||
if self._db_settings.encoding is not None:
|
charset=database_settings.charset,
|
||||||
self._engine.encoding = self._db_settings.encoding
|
use_unicode=database_settings.use_unicode,
|
||||||
|
buffered=database_settings.buffered,
|
||||||
if self._db_settings.case_sensitive is not None:
|
auth_plugin=database_settings.auth_plugin
|
||||||
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(ForegroundColorEnum.green)
|
Console.set_foreground_color(ForegroundColorEnum.green)
|
||||||
Console.write_line(f'[{__name__}] Connected to database')
|
Console.write_line(f'[{__name__}] Connected to database')
|
||||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
Console.set_foreground_color(ForegroundColorEnum.default)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from abc import abstractmethod, ABC
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from sqlalchemy import engine
|
from cpl_core.database.database_settings import DatabaseSettings
|
||||||
from sqlalchemy.orm import Session
|
from mysql.connector.abstracts import MySQLConnectionAbstract
|
||||||
|
|
||||||
|
|
||||||
class DatabaseConnectionABC(ABC):
|
class DatabaseConnectionABC(ABC):
|
||||||
@ -12,14 +12,10 @@ class DatabaseConnectionABC(ABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def engine(self) -> engine: pass
|
def server(self) -> MySQLConnectionAbstract: pass
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def session(self) -> Session: pass
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def connect(self, connection_string: str):
|
def connect(self, database_settings: DatabaseSettings):
|
||||||
r"""Connects to a database by connection string
|
r"""Connects to a database by connection string
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
from sqlalchemy import engine, Table
|
|
||||||
from sqlalchemy.orm import Session
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.console.console import Console
|
from cpl_core.console.console import Console
|
||||||
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
|
|
||||||
from cpl_core.database.connection.database_connection import DatabaseConnection
|
from cpl_core.database.connection.database_connection import DatabaseConnection
|
||||||
from cpl_core.database.connection.database_connection_abc import DatabaseConnectionABC
|
from cpl_core.database.connection.database_connection_abc import \
|
||||||
|
DatabaseConnectionABC
|
||||||
from cpl_core.database.context.database_context_abc import DatabaseContextABC
|
from cpl_core.database.context.database_context_abc import DatabaseContextABC
|
||||||
from cpl_core.database.database_settings import DatabaseSettings
|
from cpl_core.database.database_settings import DatabaseSettings
|
||||||
from cpl_core.database.database_model import DatabaseModel
|
from cpl_core.database.table_abc import TableABC
|
||||||
|
|
||||||
|
|
||||||
class DatabaseContext(DatabaseContextABC):
|
class DatabaseContext(DatabaseContextABC):
|
||||||
@ -19,38 +19,24 @@ class DatabaseContext(DatabaseContextABC):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, database_settings: DatabaseSettings):
|
def __init__(self, database_settings: DatabaseSettings):
|
||||||
DatabaseContextABC.__init__(self)
|
DatabaseContextABC.__init__(self, database_settings)
|
||||||
|
|
||||||
self._db: DatabaseConnectionABC = DatabaseConnection(database_settings)
|
self._db: DatabaseConnectionABC = DatabaseConnection()
|
||||||
self._tables: list[Table] = []
|
self._cursor: Optional[str] = None
|
||||||
|
self._tables: list[TableABC] = TableABC.__subclasses__()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def engine(self) -> engine:
|
def cursor(self):
|
||||||
return self._db.engine
|
return self._cursor
|
||||||
|
|
||||||
@property
|
def connect(self, database_settings: DatabaseSettings):
|
||||||
def session(self) -> Session:
|
self._db.connect(database_settings)
|
||||||
return self._db.session
|
c = self._db.server.cursor()
|
||||||
|
self._cursor = c
|
||||||
def connect(self, connection_string: str):
|
Console.write_line(f"Ts: {self._tables}")
|
||||||
self._db.connect(connection_string)
|
for table in self._tables:
|
||||||
self._create_tables()
|
Console.write_line(f"{table}, {table.create_string}")
|
||||||
|
c.execute(table.create_string)
|
||||||
|
|
||||||
def save_changes(self):
|
def save_changes(self):
|
||||||
self._db.session.commit()
|
self._db.server.commit()
|
||||||
|
|
||||||
def _create_tables(self):
|
|
||||||
try:
|
|
||||||
for subclass in DatabaseModel.__subclasses__():
|
|
||||||
self._tables.append(subclass.__table__)
|
|
||||||
|
|
||||||
DatabaseModel.metadata.drop_all(self._db.engine, self._tables)
|
|
||||||
DatabaseModel.metadata.create_all(self._db.engine, self._tables, checkfirst=True)
|
|
||||||
Console.set_foreground_color(ForegroundColorEnum.green)
|
|
||||||
Console.write_line(f'[{__name__}] Created tables')
|
|
||||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
|
||||||
except Exception as e:
|
|
||||||
Console.set_foreground_color(ForegroundColorEnum.red)
|
|
||||||
Console.write_line(f'[{__name__}] Creating tables failed -> {e}')
|
|
||||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
|
||||||
exit()
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from abc import abstractmethod, ABC
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from sqlalchemy import engine
|
from cpl_core.database.database_settings import DatabaseSettings
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
|
|
||||||
|
|
||||||
class DatabaseContextABC(ABC):
|
class DatabaseContextABC(ABC):
|
||||||
@ -13,28 +12,18 @@ class DatabaseContextABC(ABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def engine(self) -> engine: pass
|
def cursor(self): pass
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def session(self) -> Session: pass
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def connect(self, connection_string: str):
|
def connect(self, database_settings: DatabaseSettings):
|
||||||
r"""Connects to a database by connection string
|
r"""Connects to a database by connection settings
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
---------
|
---------
|
||||||
connection_string: :class:`str`
|
database_settings :class:`cpl_core.database.database_settings.DatabaseSettings`
|
||||||
Database connection string, see: https://docs.sqlalchemy.org/en/14/core/engines.html
|
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def save_changes(self):
|
def save_changes(self):
|
||||||
r"""Saves changes of the database"""
|
r"""Saves changes of the database"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def _create_tables(self):
|
|
||||||
r"""Create all tables for application from database model"""
|
|
||||||
pass
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
from sqlalchemy.ext.declarative import declarative_base
|
|
||||||
|
|
||||||
DatabaseModel: declarative_base = declarative_base()
|
|
@ -13,61 +13,47 @@ class DatabaseSettings(ConfigurationModelABC):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
ConfigurationModelABC.__init__(self)
|
ConfigurationModelABC.__init__(self)
|
||||||
|
|
||||||
|
self._host: Optional[str] = None
|
||||||
|
self._user: Optional[str] = None
|
||||||
|
self._password: Optional[str] = None
|
||||||
|
self._databse: Optional[str] = None
|
||||||
|
self._charset: Optional[str] = None
|
||||||
|
self._use_unicode: Optional[bool] = None
|
||||||
|
self._buffered: Optional[bool] = None
|
||||||
self._auth_plugin: Optional[str] = None
|
self._auth_plugin: Optional[str] = None
|
||||||
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
|
@property
|
||||||
def auth_plugin(self) -> str:
|
def host(self) -> Optional[str]:
|
||||||
|
return self._host
|
||||||
|
|
||||||
|
@property
|
||||||
|
def user(self) -> Optional[str]:
|
||||||
|
return self._user
|
||||||
|
|
||||||
|
@property
|
||||||
|
def password(self) -> Optional[str]:
|
||||||
|
return self._password
|
||||||
|
|
||||||
|
@property
|
||||||
|
def database(self) -> Optional[str]:
|
||||||
|
return self._databse
|
||||||
|
|
||||||
|
@property
|
||||||
|
def charset(self) -> Optional[str]:
|
||||||
|
return self._charset
|
||||||
|
|
||||||
|
@property
|
||||||
|
def use_unicode(self) -> Optional[bool]:
|
||||||
|
return self._use_unicode
|
||||||
|
|
||||||
|
@property
|
||||||
|
def buffered(self) -> Optional[bool]:
|
||||||
|
return self._buffered
|
||||||
|
|
||||||
|
@property
|
||||||
|
def auth_plugin(self) -> Optional[str]:
|
||||||
return self._auth_plugin
|
return self._auth_plugin
|
||||||
|
|
||||||
@auth_plugin.setter
|
|
||||||
def auth_plugin(self, auth_plugin: str):
|
|
||||||
self._auth_plugin = auth_plugin
|
|
||||||
|
|
||||||
@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):
|
def from_dict(self, settings: dict):
|
||||||
r"""Sets attributes from given dict
|
r"""Sets attributes from given dict
|
||||||
|
|
||||||
@ -76,20 +62,22 @@ class DatabaseSettings(ConfigurationModelABC):
|
|||||||
settings: :class:`dict`
|
settings: :class:`dict`
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self._connection_string = settings[DatabaseSettingsNameEnum.connection_string.value]
|
self._host = settings[DatabaseSettingsNameEnum.host.value]
|
||||||
self._credentials = settings[DatabaseSettingsNameEnum.credentials.value]
|
self._user = settings[DatabaseSettingsNameEnum.user.value]
|
||||||
|
self._password = settings[DatabaseSettingsNameEnum.password.value]
|
||||||
|
self._databse = settings[DatabaseSettingsNameEnum.database.value]
|
||||||
|
|
||||||
|
if DatabaseSettingsNameEnum.charset.value in settings:
|
||||||
|
self._charset = settings[DatabaseSettingsNameEnum.charset.value]
|
||||||
|
|
||||||
|
if DatabaseSettingsNameEnum.buffered.value in settings:
|
||||||
|
self._use_unicode = bool(settings[DatabaseSettingsNameEnum.use_unicode.value])
|
||||||
|
|
||||||
|
if DatabaseSettingsNameEnum.buffered.value in settings:
|
||||||
|
self._buffered = bool(settings[DatabaseSettingsNameEnum.buffered.value])
|
||||||
|
|
||||||
if DatabaseSettingsNameEnum.auth_plugin.value in settings:
|
if DatabaseSettingsNameEnum.auth_plugin.value in settings:
|
||||||
self._auth_plugin = settings[DatabaseSettingsNameEnum.auth_plugin.value]
|
self._auth_plugin = settings[DatabaseSettingsNameEnum.auth_plugin.value]
|
||||||
|
|
||||||
if DatabaseSettingsNameEnum.encoding.value in settings:
|
|
||||||
self._encoding = settings[DatabaseSettingsNameEnum.encoding.value]
|
|
||||||
|
|
||||||
if DatabaseSettingsNameEnum.case_sensitive.value in settings:
|
|
||||||
self._case_sensitive = bool(settings[DatabaseSettingsNameEnum.case_sensitive.value])
|
|
||||||
|
|
||||||
if DatabaseSettingsNameEnum.echo.value in settings:
|
|
||||||
self._echo = bool(settings[DatabaseSettingsNameEnum.echo.value])
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
Console.set_foreground_color(ForegroundColorEnum.red)
|
Console.set_foreground_color(ForegroundColorEnum.red)
|
||||||
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
||||||
|
@ -3,9 +3,11 @@ from enum import Enum
|
|||||||
|
|
||||||
class DatabaseSettingsNameEnum(Enum):
|
class DatabaseSettingsNameEnum(Enum):
|
||||||
|
|
||||||
connection_string = 'ConnectionString'
|
host = 'Host'
|
||||||
credentials = 'Credentials'
|
user = 'User'
|
||||||
encoding = 'Encoding'
|
password = 'Password'
|
||||||
case_sensitive = 'CaseSensitive'
|
database = 'Database'
|
||||||
echo = 'Echo'
|
charset = 'Charset'
|
||||||
|
use_unicode = 'UseUnicode'
|
||||||
|
buffered = 'Buffered'
|
||||||
auth_plugin = 'AuthPlugin'
|
auth_plugin = 'AuthPlugin'
|
||||||
|
35
src/cpl_core/database/table_abc.py
Normal file
35
src/cpl_core/database/table_abc.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
class TableABC(ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __init__(self):
|
||||||
|
self._created_at: Optional[datetime] = None
|
||||||
|
self._modified_at: Optional[datetime] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def CreatedAt(self) -> datetime:
|
||||||
|
return self._created_at
|
||||||
|
|
||||||
|
@property
|
||||||
|
def LastModifiedAt(self) -> datetime:
|
||||||
|
return self._modified_at
|
||||||
|
|
||||||
|
@property
|
||||||
|
@abstractmethod
|
||||||
|
def create_string(self) -> str: pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
@abstractmethod
|
||||||
|
def insert_string(self) -> str: pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
@abstractmethod
|
||||||
|
def udpate_string(self) -> str: pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
@abstractmethod
|
||||||
|
def delete_string(self) -> str: pass
|
@ -40,7 +40,7 @@ class ServiceCollection(ServiceCollectionABC):
|
|||||||
|
|
||||||
def add_db_context(self, db_context_type: Type[DatabaseContextABC], db_settings: DatabaseSettings):
|
def add_db_context(self, db_context_type: Type[DatabaseContextABC], db_settings: DatabaseSettings):
|
||||||
self._database_context = db_context_type(db_settings)
|
self._database_context = db_context_type(db_settings)
|
||||||
self._database_context.connect(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
|
self._database_context.connect(db_settings)
|
||||||
|
|
||||||
def add_logging(self):
|
def add_logging(self):
|
||||||
self.add_singleton(LoggerABC, Logger)
|
self.add_singleton(LoggerABC, Logger)
|
||||||
|
@ -15,15 +15,13 @@ class ServiceCollectionABC(ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_db_context(self, db_context: Type[DatabaseContextABC], db_settings: DatabaseSettings):
|
def add_db_context(self, db_context: Type[DatabaseContextABC]):
|
||||||
r"""Adds database context
|
r"""Adds database context
|
||||||
|
|
||||||
Parameter
|
Parameter
|
||||||
---------
|
---------
|
||||||
db_context: Type[:class:`cpl_core.database.context.database_context_abc.DatabaseContextABC`]
|
db_context: Type[:class:`cpl_core.database.context.database_context_abc.DatabaseContextABC`]
|
||||||
Database context
|
Database context
|
||||||
db_settings: :class:`cpl_core.database.database_settings.DatabaseSettings`
|
|
||||||
Database settings
|
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@ class Application(ApplicationABC):
|
|||||||
user_repo.add_test_user()
|
user_repo.add_test_user()
|
||||||
Console.write_line('Users:')
|
Console.write_line('Users:')
|
||||||
for user in user_repo.get_users():
|
for user in user_repo.get_users():
|
||||||
Console.write_line(user.Id, user.Name, user.City_Id, user.City.Id, user.City.Name, user.City.ZIP)
|
Console.write_line(user.UserId, user.Name, user.CityId, user.City.CityId, user.City.Name, user.City.ZIP)
|
||||||
|
|
||||||
Console.write_line('Cities:')
|
Console.write_line('Cities:')
|
||||||
for city in user_repo.get_cities():
|
for city in user_repo.get_cities():
|
||||||
Console.write_line(city.Id, city.Name, city.ZIP)
|
Console.write_line(city.CityId, city.Name, city.ZIP)
|
||||||
|
@ -14,9 +14,13 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"DatabaseSettings": {
|
"DatabaseSettings": {
|
||||||
"AuthPlugin": "mysql_native_password",
|
"Host": "localhost",
|
||||||
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
|
"User": "sh_cpl",
|
||||||
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
"Password": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
||||||
"Encoding": "utf8mb4"
|
"Database": "sh_cpl",
|
||||||
|
"Charset": "utf8mb4",
|
||||||
|
"UseUnicode": "true",
|
||||||
|
"Buffered": "true",
|
||||||
|
"AuthPlugin": "mysql_native_password"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,14 +1,46 @@
|
|||||||
from sqlalchemy import Column, Integer, String
|
from cpl_core.database import TableABC
|
||||||
|
|
||||||
from cpl_core.database import DatabaseModel
|
|
||||||
|
|
||||||
|
|
||||||
class CityModel(DatabaseModel):
|
class CityModel(TableABC):
|
||||||
__tablename__ = 'Cities'
|
|
||||||
Id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
|
|
||||||
Name = Column(String(64), nullable=False)
|
|
||||||
ZIP = Column(String(5), nullable=False)
|
|
||||||
|
|
||||||
def __init__(self, name: str, zip_code: str):
|
def __init__(self, name: str, zip_code: str):
|
||||||
|
self.CityId = 0
|
||||||
self.Name = name
|
self.Name = name
|
||||||
self.ZIP = zip_code
|
self.ZIP = zip_code
|
||||||
|
|
||||||
|
@property
|
||||||
|
def create_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `City` (
|
||||||
|
`CityId` INT(30) NOT NULL AUTO_INCREMENT,
|
||||||
|
`Name` VARCHAR NOT NULL,
|
||||||
|
`ZIP` VARCHAR NOT NULL,
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def insert_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
INSERT INTO `City` (
|
||||||
|
`Name`, `ZIP`
|
||||||
|
) VALUES (
|
||||||
|
'{self.Name}',
|
||||||
|
'{self.ZIP}'
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def udpate_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
UPDATE `City`
|
||||||
|
SET `Name` = '{self.Name}',
|
||||||
|
`ZIP` = '{self.ZIP}',
|
||||||
|
WHERE `CityId` = {self.Id};
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def delete_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
DELETE FROM `City`
|
||||||
|
WHERE `CityId` = {self.Id};
|
||||||
|
"""
|
||||||
|
@ -1,18 +1,50 @@
|
|||||||
from sqlalchemy import Column, Integer, String, ForeignKey
|
from cpl_core.database import TableABC
|
||||||
from sqlalchemy.orm import relationship
|
|
||||||
|
|
||||||
from cpl_core.database import DatabaseModel
|
|
||||||
from .city_model import CityModel
|
from .city_model import CityModel
|
||||||
|
|
||||||
|
|
||||||
class UserModel(DatabaseModel):
|
class UserModel(TableABC):
|
||||||
__tablename__ = 'Users'
|
|
||||||
Id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
|
|
||||||
Name = Column(String(64), nullable=False)
|
|
||||||
City_Id = Column(Integer, ForeignKey('Cities.Id'), nullable=False)
|
|
||||||
City = relationship("CityModel")
|
|
||||||
|
|
||||||
def __init__(self, name: str, city: CityModel):
|
def __init__(self, name: str, city: CityModel):
|
||||||
|
self.UserId = 0
|
||||||
self.Name = name
|
self.Name = name
|
||||||
self.City_Id = city.Id
|
self.CityId = city.CityId
|
||||||
self.City = city
|
self.City = city
|
||||||
|
|
||||||
|
@property
|
||||||
|
def create_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS `User` (
|
||||||
|
`UserId` INT(30) NOT NULL AUTO_INCREMENT,
|
||||||
|
`Name` VARCHAR NOT NULL,
|
||||||
|
`CityId` VARCHAR NOT NULL,
|
||||||
|
FOREIGN KEY (`UserId`) REFERENCES City(`CityId`),
|
||||||
|
PRIMARY KEY(`UserId`)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def insert_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
INSERT INTO `User` (
|
||||||
|
`Name`
|
||||||
|
) VALUES (
|
||||||
|
'{self.Name}'
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def udpate_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
UPDATE `User`
|
||||||
|
SET `Name` = '{self.Name}',
|
||||||
|
`CityId` = {self.CityId},
|
||||||
|
WHERE `UserId` = {self.UserId};
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def delete_string(self) -> str:
|
||||||
|
return f"""
|
||||||
|
DELETE FROM `User`
|
||||||
|
WHERE `UserId` = {self.UserId};
|
||||||
|
"""
|
||||||
|
@ -9,21 +9,21 @@ class UserRepo(UserRepoABC):
|
|||||||
def __init__(self, db_context: DatabaseContextABC):
|
def __init__(self, db_context: DatabaseContextABC):
|
||||||
UserRepoABC.__init__(self)
|
UserRepoABC.__init__(self)
|
||||||
|
|
||||||
self._session = db_context.session
|
self._db: DatabaseContextABC = db_context
|
||||||
self._user_query = db_context.session.query(UserModel)
|
|
||||||
|
|
||||||
def create(self): pass
|
def create(self): pass
|
||||||
|
|
||||||
def add_test_user(self):
|
def add_test_user(self):
|
||||||
city = CityModel('Haren', '49733')
|
city = CityModel('Haren', '49733')
|
||||||
city2 = CityModel('Meppen', '49716')
|
city2 = CityModel('Meppen', '49716')
|
||||||
self._session.add(city2)
|
self._db.cursor.execute(city2.insert_string)
|
||||||
user = UserModel('TestUser', city)
|
user = UserModel('TestUser', city)
|
||||||
self._session.add(user)
|
self._db.cursor.execute(user.insert_string)
|
||||||
self._session.commit()
|
|
||||||
|
|
||||||
def get_users(self) -> list[UserModel]:
|
def get_users(self) -> list[UserModel]:
|
||||||
return self._session.query(UserModel).all()
|
self._db.cursor.execute(f"""SELECT * FROM `User`""")
|
||||||
|
return self._db.cursor.fetchall()
|
||||||
|
|
||||||
def get_cities(self) -> list[CityModel]:
|
def get_cities(self) -> list[CityModel]:
|
||||||
return self._session.query(CityModel).all()
|
self._db.cursor.execute(f"""SELECT * FROM `City`""")
|
||||||
|
return self._db.cursor.fetchall()
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
from cpl_core.application import StartupABC
|
from cpl_core.application import StartupABC
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
from cpl_core.database import DatabaseSettings
|
from cpl_core.database import DatabaseSettings
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
from cpl_core.dependency_injection import (ServiceCollectionABC,
|
||||||
from cpl_core.logging import LoggerABC, Logger
|
ServiceProviderABC)
|
||||||
|
from cpl_core.environment import ApplicationEnvironmentABC
|
||||||
|
from cpl_core.logging import Logger, LoggerABC
|
||||||
|
|
||||||
from model.db_context import DBContext
|
from model.db_context import DBContext
|
||||||
from model.user_repo import UserRepo
|
from model.user_repo import UserRepo
|
||||||
from model.user_repo_abc import UserRepoABC
|
from model.user_repo_abc import UserRepoABC
|
||||||
@ -10,30 +13,29 @@ from model.user_repo_abc import UserRepoABC
|
|||||||
|
|
||||||
class Startup(StartupABC):
|
class Startup(StartupABC):
|
||||||
|
|
||||||
def __init__(self, config: ConfigurationABC, services: ServiceCollectionABC):
|
def __init__(self):
|
||||||
StartupABC.__init__(self)
|
StartupABC.__init__(self)
|
||||||
|
|
||||||
self._configuration = config
|
self._configuration = None
|
||||||
self._environment = self._configuration.environment
|
|
||||||
self._services = services
|
|
||||||
|
|
||||||
def configure_configuration(self) -> ConfigurationABC:
|
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC) -> ConfigurationABC:
|
||||||
self._configuration.add_environment_variables('PYTHON_')
|
configuration.add_environment_variables('PYTHON_')
|
||||||
self._configuration.add_environment_variables('CPL_')
|
configuration.add_environment_variables('CPL_')
|
||||||
self._configuration.add_console_arguments()
|
configuration.add_console_arguments()
|
||||||
self._configuration.add_json_file(f'appsettings.json')
|
configuration.add_json_file(f'appsettings.json')
|
||||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
|
configuration.add_json_file(f'appsettings.{configuration.environment.environment_name}.json')
|
||||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
|
configuration.add_json_file(f'appsettings.{configuration.environment.host_name}.json', optional=True)
|
||||||
|
|
||||||
return self._configuration
|
self._configuration = configuration
|
||||||
|
|
||||||
def configure_services(self) -> ServiceProviderABC:
|
return configuration
|
||||||
|
|
||||||
|
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironmentABC) -> ServiceProviderABC:
|
||||||
# Create and connect to database
|
# Create and connect to database
|
||||||
db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings)
|
db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings)
|
||||||
self._services.add_db_context(DBContext, db_settings)
|
services.add_db_context(DBContext, db_settings)
|
||||||
|
|
||||||
self._services.add_singleton(UserRepoABC, UserRepo)
|
services.add_singleton(UserRepoABC, UserRepo)
|
||||||
|
|
||||||
self._services.add_singleton(LoggerABC, Logger)
|
|
||||||
return self._services.build_service_provider()
|
|
||||||
|
|
||||||
|
services.add_singleton(LoggerABC, Logger)
|
||||||
|
return services.build_service_provider()
|
||||||
|
Loading…
Reference in New Issue
Block a user