[WIP] Added native mysql support
This commit is contained in:
@@ -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)
|
||||
|
@@ -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"
|
||||
}
|
||||
}
|
@@ -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};
|
||||
"""
|
||||
|
@@ -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};
|
||||
"""
|
||||
|
@@ -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()
|
||||
|
@@ -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 = None
|
||||
|
||||
self._configuration = config
|
||||
self._environment = self._configuration.environment
|
||||
self._services = services
|
||||
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)
|
||||
|
||||
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)
|
||||
self._configuration = configuration
|
||||
|
||||
return configuration
|
||||
|
||||
return self._configuration
|
||||
|
||||
def configure_services(self) -> ServiceProviderABC:
|
||||
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()
|
||||
|
Reference in New Issue
Block a user