db_interactor/config.py
Frederik Jacobsen 8f736f58f3 Added a way to run the same query on multiple schemas and logging.
Need to work on running update queries, that combine plsql and normal sql statements and potentially multiple extracts for one sql file, to make multiple files, and probably to also export to xlsx file format.
2024-12-12 21:50:37 +01:00

113 lines
2.9 KiB
Python

import logging
from enum import Enum
import toml
from dataclasses import dataclass
class DatabaseType(Enum):
PSQL = 1
ORCL = 2
SQLITE = 3
@dataclass
class DatabaseConfig:
_host: str
_database_type: DatabaseType | None
_database_name: str
_database_ssid: str
_database_port: str
def __init__(self, config: dict):
self._host = config["HOST"]
type = config["DATABASE_TYPE"]
match type:
case 'PSQL':
self._database_type = DatabaseType.PSQL
case 'ORCL':
self._database_type = DatabaseType.ORCL
case 'SQLITE':
self._database_type = DatabaseType.SQLITE
case _:
self._database_type = None
self._database_name = config["DATABASE_NAME"]
self._database_ssid = config["DATABASE_SSID"]
self._database_port = config["DATABASE_PORT"]
@classmethod
def create(cls, database_name: str, database_type: DatabaseType, database_ssid: str, database_port: str, host: str):
config = {
"HOST": host,
"DATABASE_TYPE": database_type,
"DATABASE_NAME": database_name,
"DATABASE_SSID": database_ssid,
"DATABASE_PORT": database_port,
}
obj = cls.__new__(cls)
cls.__init__(obj, config)
return obj
@property
def host(self) -> str:
return self._host
@property
def type(self) -> DatabaseType:
return self._database_type
@property
def name(self) -> str:
return self._database_name
@property
def ssid(self) -> str:
return self._database_ssid
@property
def port(self) -> str:
return self._database_port
@dataclass
class KeePassConfig:
_path: str
_db_credentials_name: str
_db_credentials_group: str
def __init__(self, config: dict):
self._path: str = config["PATH"]
self._db_credentials_name: str = config["DB_CREDENTIALS_NAME"]
self._db_credentials_group: str = config["DB_CREDENTIALS_GROUP"]
@property
def path(self) -> str:
return self._path
@property
def db_credentials_name(self) -> str:
return self._db_credentials_name
@property
def db_credentials_group(self) -> str:
return self._db_credentials_group
@dataclass
class Config:
_config: dict
_kee_pass_config: KeePassConfig
_database_config: DatabaseConfig
def __init__(self):
with open('config.toml', 'r') as f:
self._config: dict = toml.load(f)
self._kee_pass_config: KeePassConfig = KeePassConfig(self._config['keepass'])
self._database_config: DatabaseConfig = DatabaseConfig(self._config['database'])
@property
def kee_pass(self) -> KeePassConfig:
return self._kee_pass_config
@property
def database(self) -> DatabaseConfig:
return self._database_config