Updated docs
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
cpl-core sh-edraft Common Python library
|
||||
cpl-core CPL core
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
sh-edraft Common Python library
|
||||
CPL core package
|
||||
|
||||
:copyright: (c) 2020 - 2023 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'cpl_core.database'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 - 2023 sh-edraft.de'
|
||||
__version__ = '2022.12.1'
|
||||
__title__ = "cpl_core.database"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2020 - 2023 sh-edraft.de"
|
||||
__version__ = "2023.2.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -25,5 +25,5 @@ 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='2022', minor='12', micro='1')
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="2023", minor="2", micro="0")
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
cpl-core sh-edraft Common Python library
|
||||
cpl-core CPL core
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
sh-edraft Common Python library
|
||||
CPL core package
|
||||
|
||||
:copyright: (c) 2020 - 2023 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'cpl_core.database.connection'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 - 2023 sh-edraft.de'
|
||||
__version__ = '2022.12.1'
|
||||
__title__ = "cpl_core.database.connection"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2020 - 2023 sh-edraft.de"
|
||||
__version__ = "2023.2.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -24,5 +24,5 @@ from collections import namedtuple
|
||||
from .database_connection import DatabaseConnection
|
||||
from .database_connection_abc import DatabaseConnectionABC
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='2022', minor='12', micro='1')
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="2023", minor="2", micro="0")
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from typing import Optional
|
||||
|
||||
import mysql.connector as sql
|
||||
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.credential_manager import CredentialManager
|
||||
from mysql.connector.abstracts import MySQLConnectionAbstract
|
||||
@@ -10,8 +9,7 @@ from mysql.connector.cursor import MySQLCursorBuffered
|
||||
|
||||
|
||||
class DatabaseConnection(DatabaseConnectionABC):
|
||||
r"""Representation of the database connection
|
||||
"""
|
||||
r"""Representation of the database connection"""
|
||||
|
||||
def __init__(self):
|
||||
DatabaseConnectionABC.__init__(self)
|
||||
@@ -36,10 +34,9 @@ class DatabaseConnection(DatabaseConnectionABC):
|
||||
charset=database_settings.charset,
|
||||
use_unicode=database_settings.use_unicode,
|
||||
buffered=database_settings.buffered,
|
||||
auth_plugin=database_settings.auth_plugin
|
||||
auth_plugin=database_settings.auth_plugin,
|
||||
)
|
||||
connection.cursor().execute(
|
||||
f'CREATE DATABASE IF NOT EXISTS `{database_settings.database}`;')
|
||||
connection.cursor().execute(f"CREATE DATABASE IF NOT EXISTS `{database_settings.database}`;")
|
||||
self._database = sql.connect(
|
||||
host=database_settings.host,
|
||||
port=database_settings.port,
|
||||
@@ -49,6 +46,6 @@ class DatabaseConnection(DatabaseConnectionABC):
|
||||
charset=database_settings.charset,
|
||||
use_unicode=database_settings.use_unicode,
|
||||
buffered=database_settings.buffered,
|
||||
auth_plugin=database_settings.auth_plugin
|
||||
auth_plugin=database_settings.auth_plugin,
|
||||
)
|
||||
self._cursor = self._database.cursor()
|
||||
|
||||
@@ -9,22 +9,24 @@ class DatabaseConnectionABC(ABC):
|
||||
r"""ABC for the :class:`cpl_core.database.connection.database_connection.DatabaseConnection`"""
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def server(self) -> MySQLConnectionAbstract: pass
|
||||
|
||||
def server(self) -> MySQLConnectionAbstract:
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def cursor(self) -> MySQLCursorBuffered: pass
|
||||
|
||||
def cursor(self) -> MySQLCursorBuffered:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
r"""Connects to a database by connection string
|
||||
|
||||
Parameter
|
||||
---------
|
||||
Parameter:
|
||||
connection_string: :class:`str`
|
||||
Database connection string, see: https://docs.sqlalchemy.org/en/14/core/engines.html
|
||||
"""
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
cpl-core sh-edraft Common Python library
|
||||
cpl-core CPL core
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
sh-edraft Common Python library
|
||||
CPL core package
|
||||
|
||||
:copyright: (c) 2020 - 2023 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'cpl_core.database.context'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 - 2023 sh-edraft.de'
|
||||
__version__ = '2022.12.1'
|
||||
__title__ = "cpl_core.database.context"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2020 - 2023 sh-edraft.de"
|
||||
__version__ = "2023.2.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -24,5 +24,5 @@ from collections import namedtuple
|
||||
from .database_context import DatabaseContext
|
||||
from .database_context_abc import DatabaseContextABC
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='2022', minor='12', micro='1')
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="2023", minor="2", micro="0")
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import mysql
|
||||
|
||||
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.table_abc import TableABC
|
||||
@@ -15,8 +13,7 @@ from mysql.connector.cursor import MySQLCursorBuffered
|
||||
class DatabaseContext(DatabaseContextABC):
|
||||
r"""Representation of the database context
|
||||
|
||||
Parameter
|
||||
---------
|
||||
Parameter:
|
||||
database_settings: :class:`cpl_core.database.database_settings.DatabaseSettings`
|
||||
"""
|
||||
|
||||
@@ -38,7 +35,7 @@ class DatabaseContext(DatabaseContextABC):
|
||||
except mysql.connector.Error as err:
|
||||
# reconnect your cursor as you did in __init__ or wherever
|
||||
if self._settings is None:
|
||||
raise Exception('Call DatabaseContext.connect first')
|
||||
raise Exception("Call DatabaseContext.connect first")
|
||||
self.connect(self._settings)
|
||||
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
@@ -53,7 +50,7 @@ class DatabaseContext(DatabaseContextABC):
|
||||
def save_changes(self):
|
||||
self._ping_and_reconnect()
|
||||
self._db.server.commit()
|
||||
|
||||
|
||||
def select(self, statement: str) -> list[tuple]:
|
||||
self._ping_and_reconnect()
|
||||
self._db.cursor.execute(statement)
|
||||
|
||||
@@ -13,33 +13,31 @@ class DatabaseContextABC(ABC):
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def cursor(self) -> MySQLCursorBuffered: pass
|
||||
|
||||
def cursor(self) -> MySQLCursorBuffered:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
r"""Connects to a database by connection settings
|
||||
|
||||
Parameter
|
||||
---------
|
||||
Parameter:
|
||||
database_settings :class:`cpl_core.database.database_settings.DatabaseSettings`
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def save_changes(self):
|
||||
r"""Saves changes of the database"""
|
||||
pass
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def select(self, statement: str) -> list[tuple]:
|
||||
r"""Runs SQL Statements
|
||||
|
||||
Parameter
|
||||
---------
|
||||
|
||||
Parameter:
|
||||
statement: :class:`str`
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
Returns:
|
||||
list: Fetched list of selected elements
|
||||
"""
|
||||
pass
|
||||
|
||||
@@ -62,8 +62,7 @@ class DatabaseSettings(ConfigurationModelABC):
|
||||
def from_dict(self, settings: dict):
|
||||
r"""Sets attributes from given dict
|
||||
|
||||
Parameter
|
||||
---------
|
||||
Parameter:
|
||||
settings: :class:`dict`
|
||||
"""
|
||||
try:
|
||||
@@ -75,20 +74,20 @@ class DatabaseSettings(ConfigurationModelABC):
|
||||
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]
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColorEnum.red)
|
||||
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.write_line(f"[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings")
|
||||
Console.write_line(f"[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}")
|
||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
||||
|
||||
@@ -2,13 +2,12 @@ from enum import Enum
|
||||
|
||||
|
||||
class DatabaseSettingsNameEnum(Enum):
|
||||
|
||||
host = 'Host'
|
||||
port = 'Port'
|
||||
user = 'User'
|
||||
password = 'Password'
|
||||
database = 'Database'
|
||||
charset = 'Charset'
|
||||
use_unicode = 'UseUnicode'
|
||||
buffered = 'Buffered'
|
||||
auth_plugin = 'AuthPlugin'
|
||||
host = "Host"
|
||||
port = "Port"
|
||||
user = "User"
|
||||
password = "Password"
|
||||
database = "Database"
|
||||
charset = "Charset"
|
||||
use_unicode = "UseUnicode"
|
||||
buffered = "Buffered"
|
||||
auth_plugin = "AuthPlugin"
|
||||
|
||||
@@ -4,16 +4,15 @@ from typing import Optional
|
||||
|
||||
|
||||
class TableABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
self._created_at: Optional[datetime] = datetime.now().isoformat()
|
||||
self._modified_at: Optional[datetime] = datetime.now().isoformat()
|
||||
|
||||
|
||||
@property
|
||||
def created_at(self) -> datetime:
|
||||
return self._created_at
|
||||
|
||||
|
||||
@property
|
||||
def modified_at(self) -> datetime:
|
||||
return self._modified_at
|
||||
@@ -21,15 +20,18 @@ class TableABC(ABC):
|
||||
@modified_at.setter
|
||||
def modified_at(self, value: datetime):
|
||||
self._modified_at = value
|
||||
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def insert_string(self) -> str: pass
|
||||
|
||||
def insert_string(self) -> str:
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def udpate_string(self) -> str: pass
|
||||
|
||||
def udpate_string(self) -> str:
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def delete_string(self) -> str: pass
|
||||
def delete_string(self) -> str:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user