2021.11 #49

Manually merged
edraft merged 10 commits from 2021.11 into master 2021-11-30 12:03:04 +01:00
22 changed files with 277 additions and 231 deletions
Showing only changes of commit d9f10edbb7 - Show all commits

4
.vscode/launch.json vendored
View File

@ -41,6 +41,10 @@
"cwd": "${workspaceFolder}/src/tests/custom/database/src",
"program": "main.py",
"console": "integratedTerminal",
"env": {
"PYTHON_ENVIRONMENT": "development",
"PYTHONPATH": "${workspaceFolder}/src/:$PYTHONPATH"
}
},
{
"name": "CLI",

View File

@ -3,8 +3,8 @@
"Name": "sh_cpl-core",
"Version": {
"Major": "2021",
"Minor": "10",
"Micro": "2"
"Minor": "11",
"Micro": "0"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -20,9 +20,9 @@ __version__ = '2021.10.2'
from collections import namedtuple
# imports:
from .database_model import DatabaseModel
from .database_settings import DatabaseSettings
from .database_settings_name_enum import DatabaseSettingsNameEnum
from .database_settings import DatabaseSettings
from .table_abc import TableABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2021', minor='10', micro='2')

View File

@ -1,59 +1,40 @@
from typing import Optional
from sqlalchemy import engine, create_engine
from sqlalchemy.orm import Session, sessionmaker
import mysql.connector as sql
from cpl_core.console.console import Console
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.utils import CredentialManager
from mysql.connector.abstracts import MySQLConnectionAbstract
class DatabaseConnection(DatabaseConnectionABC):
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)
self._db_settings = database_settings
self._engine: Optional[engine] = None
self._session: Optional[Session] = None
self._credentials: Optional[str] = None
self._database_server: Optional[MySQLConnectionAbstract] = None
@property
def engine(self) -> engine:
return self._engine
def server(self) -> MySQLConnectionAbstract:
return self._database_server
@property
def session(self) -> Session:
return self._session
def connect(self, connection_string: str):
def connect(self, database_settings: DatabaseSettings):
try:
self._engine = create_engine(connection_string)
if self._db_settings.auth_plugin is not None:
self._engine = create_engine(connection_string, connect_args={'auth_plugin': self._db_settings.auth_plugin})
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()
self._database_server = sql.connect(
host=database_settings.host,
user=database_settings.user,
passwd=CredentialManager.decrypt(database_settings.password),
db=database_settings.database,
charset=database_settings.charset,
use_unicode=database_settings.use_unicode,
buffered=database_settings.buffered,
auth_plugin=database_settings.auth_plugin
)
Console.set_foreground_color(ForegroundColorEnum.green)
Console.write_line(f'[{__name__}] Connected to database')
Console.set_foreground_color(ForegroundColorEnum.default)

View File

@ -1,7 +1,7 @@
from abc import abstractmethod, ABC
from abc import ABC, abstractmethod
from sqlalchemy import engine
from sqlalchemy.orm import Session
from cpl_core.database.database_settings import DatabaseSettings
from mysql.connector.abstracts import MySQLConnectionAbstract
class DatabaseConnectionABC(ABC):
@ -12,14 +12,10 @@ class DatabaseConnectionABC(ABC):
@property
@abstractmethod
def engine(self) -> engine: pass
@property
@abstractmethod
def session(self) -> Session: pass
def server(self) -> MySQLConnectionAbstract: pass
@abstractmethod
def connect(self, connection_string: str):
def connect(self, database_settings: DatabaseSettings):
r"""Connects to a database by connection string
Parameter

View File

@ -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.foreground_color_enum import ForegroundColorEnum
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.database_settings import DatabaseSettings
from cpl_core.database.database_model import DatabaseModel
from cpl_core.database.table_abc import TableABC
class DatabaseContext(DatabaseContextABC):
@ -19,38 +19,24 @@ class DatabaseContext(DatabaseContextABC):
"""
def __init__(self, database_settings: DatabaseSettings):
DatabaseContextABC.__init__(self)
DatabaseContextABC.__init__(self, database_settings)
self._db: DatabaseConnectionABC = DatabaseConnection(database_settings)
self._tables: list[Table] = []
self._db: DatabaseConnectionABC = DatabaseConnection()
self._cursor: Optional[str] = None
self._tables: list[TableABC] = TableABC.__subclasses__()
@property
def engine(self) -> engine:
return self._db.engine
def cursor(self):
return self._cursor
@property
def session(self) -> Session:
return self._db.session
def connect(self, connection_string: str):
self._db.connect(connection_string)
self._create_tables()
def connect(self, database_settings: DatabaseSettings):
self._db.connect(database_settings)
c = self._db.server.cursor()
self._cursor = c
Console.write_line(f"Ts: {self._tables}")
for table in self._tables:
Console.write_line(f"{table}, {table.create_string}")
c.execute(table.create_string)
def save_changes(self):
self._db.session.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()
self._db.server.commit()

View File

@ -1,7 +1,6 @@
from abc import abstractmethod, ABC
from abc import ABC, abstractmethod
from sqlalchemy import engine
from sqlalchemy.orm import Session
from cpl_core.database.database_settings import DatabaseSettings
class DatabaseContextABC(ABC):
@ -13,28 +12,18 @@ class DatabaseContextABC(ABC):
@property
@abstractmethod
def engine(self) -> engine: pass
@property
@abstractmethod
def session(self) -> Session: pass
def cursor(self): pass
@abstractmethod
def connect(self, connection_string: str):
r"""Connects to a database by connection string
def connect(self, database_settings: DatabaseSettings):
r"""Connects to a database by connection settings
Parameter
---------
connection_string: :class:`str`
Database connection string, see: https://docs.sqlalchemy.org/en/14/core/engines.html
database_settings :class:`cpl_core.database.database_settings.DatabaseSettings`
"""
pass
def save_changes(self):
r"""Saves changes of the database"""
pass
@abstractmethod
def _create_tables(self):
r"""Create all tables for application from database model"""
pass

View File

@ -1,3 +0,0 @@
from sqlalchemy.ext.declarative import declarative_base
DatabaseModel: declarative_base = declarative_base()

View File

@ -13,61 +13,47 @@ class DatabaseSettings(ConfigurationModelABC):
def __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._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 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
@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):
r"""Sets attributes from given dict
@ -76,20 +62,22 @@ class DatabaseSettings(ConfigurationModelABC):
settings: :class:`dict`
"""
try:
self._connection_string = settings[DatabaseSettingsNameEnum.connection_string.value]
self._credentials = settings[DatabaseSettingsNameEnum.credentials.value]
self._host = settings[DatabaseSettingsNameEnum.host.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:
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:
Console.set_foreground_color(ForegroundColorEnum.red)
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')

View File

@ -3,9 +3,11 @@ from enum import Enum
class DatabaseSettingsNameEnum(Enum):
connection_string = 'ConnectionString'
credentials = 'Credentials'
encoding = 'Encoding'
case_sensitive = 'CaseSensitive'
echo = 'Echo'
host = 'Host'
user = 'User'
password = 'Password'
database = 'Database'
charset = 'Charset'
use_unicode = 'UseUnicode'
buffered = 'Buffered'
auth_plugin = 'AuthPlugin'

View 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

View File

@ -40,7 +40,7 @@ class ServiceCollection(ServiceCollectionABC):
def add_db_context(self, db_context_type: Type[DatabaseContextABC], db_settings: DatabaseSettings):
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):
self.add_singleton(LoggerABC, Logger)

View File

@ -15,15 +15,13 @@ class ServiceCollectionABC(ABC):
pass
@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
Parameter
---------
db_context: Type[:class:`cpl_core.database.context.database_context_abc.DatabaseContextABC`]
Database context
db_settings: :class:`cpl_core.database.database_settings.DatabaseSettings`
Database settings
"""
pass

View File

@ -29,8 +29,8 @@ class Application(ApplicationABC):
user_repo.add_test_user()
Console.write_line('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:')
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)

View File

@ -14,9 +14,13 @@
},
"DatabaseSettings": {
"AuthPlugin": "mysql_native_password",
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
"Encoding": "utf8mb4"
"Host": "localhost",
"User": "sh_cpl",
"Password": "MHZhc0Y2bjhKc1VUMWV0Qw==",
"Database": "sh_cpl",
"Charset": "utf8mb4",
"UseUnicode": "true",
"Buffered": "true",
"AuthPlugin": "mysql_native_password"
}
}

View File

@ -1,14 +1,46 @@
from sqlalchemy import Column, Integer, String
from cpl_core.database import DatabaseModel
from cpl_core.database import TableABC
class CityModel(DatabaseModel):
__tablename__ = 'Cities'
Id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
Name = Column(String(64), nullable=False)
ZIP = Column(String(5), nullable=False)
class CityModel(TableABC):
def __init__(self, name: str, zip_code: str):
self.CityId = 0
self.Name = name
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};
"""

View File

@ -1,18 +1,50 @@
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from cpl_core.database import TableABC
from cpl_core.database import DatabaseModel
from .city_model import CityModel
class UserModel(DatabaseModel):
__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")
class UserModel(TableABC):
def __init__(self, name: str, city: CityModel):
self.UserId = 0
self.Name = name
self.City_Id = city.Id
self.CityId = city.CityId
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};
"""

View File

@ -9,21 +9,21 @@ class UserRepo(UserRepoABC):
def __init__(self, db_context: DatabaseContextABC):
UserRepoABC.__init__(self)
self._session = db_context.session
self._user_query = db_context.session.query(UserModel)
self._db: DatabaseContextABC = db_context
def create(self): pass
def add_test_user(self):
city = CityModel('Haren', '49733')
city2 = CityModel('Meppen', '49716')
self._session.add(city2)
self._db.cursor.execute(city2.insert_string)
user = UserModel('TestUser', city)
self._session.add(user)
self._session.commit()
self._db.cursor.execute(user.insert_string)
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]:
return self._session.query(CityModel).all()
self._db.cursor.execute(f"""SELECT * FROM `City`""")
return self._db.cursor.fetchall()

View File

@ -1,8 +1,11 @@
from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.database import DatabaseSettings
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
from cpl_core.logging import LoggerABC, Logger
from cpl_core.dependency_injection import (ServiceCollectionABC,
ServiceProviderABC)
from cpl_core.environment import ApplicationEnvironmentABC
from cpl_core.logging import Logger, LoggerABC
from model.db_context import DBContext
from model.user_repo import UserRepo
from model.user_repo_abc import UserRepoABC
@ -10,30 +13,29 @@ from model.user_repo_abc import UserRepoABC
class Startup(StartupABC):
def __init__(self, config: ConfigurationABC, services: ServiceCollectionABC):
def __init__(self):
StartupABC.__init__(self)
self._configuration = config
self._environment = self._configuration.environment
self._services = services
self._configuration = None
def configure_configuration(self) -> ConfigurationABC:
self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_')
self._configuration.add_console_arguments()
self._configuration.add_json_file(f'appsettings.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC) -> ConfigurationABC:
configuration.add_environment_variables('PYTHON_')
configuration.add_environment_variables('CPL_')
configuration.add_console_arguments()
configuration.add_json_file(f'appsettings.json')
configuration.add_json_file(f'appsettings.{configuration.environment.environment_name}.json')
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
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)
self._services.add_singleton(LoggerABC, Logger)
return self._services.build_service_provider()
services.add_singleton(UserRepoABC, UserRepo)
services.add_singleton(LoggerABC, Logger)
return services.build_service_provider()