Added demo for SQL Support
This commit is contained in:
@@ -8,6 +8,7 @@ from cpl_core.database.connection.database_connection_abc import \
|
||||
from cpl_core.database.database_settings import DatabaseSettings
|
||||
from cpl_core.utils import CredentialManager
|
||||
from mysql.connector.abstracts import MySQLConnectionAbstract
|
||||
from mysql.connector.cursor import MySQLCursorBuffered
|
||||
|
||||
|
||||
class DatabaseConnection(DatabaseConnectionABC):
|
||||
@@ -17,15 +18,30 @@ class DatabaseConnection(DatabaseConnectionABC):
|
||||
def __init__(self):
|
||||
DatabaseConnectionABC.__init__(self)
|
||||
|
||||
self._database_server: Optional[MySQLConnectionAbstract] = None
|
||||
self._database: Optional[MySQLConnectionAbstract] = None
|
||||
self._cursor: Optional[MySQLCursorBuffered] = None
|
||||
|
||||
@property
|
||||
def server(self) -> MySQLConnectionAbstract:
|
||||
return self._database_server
|
||||
return self._database
|
||||
|
||||
@property
|
||||
def cursor(self) -> MySQLCursorBuffered:
|
||||
return self._cursor
|
||||
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
try:
|
||||
self._database_server = sql.connect(
|
||||
connection = sql.connect(
|
||||
host=database_settings.host,
|
||||
user=database_settings.user,
|
||||
passwd=CredentialManager.decrypt(database_settings.password),
|
||||
charset=database_settings.charset,
|
||||
use_unicode=database_settings.use_unicode,
|
||||
buffered=database_settings.buffered,
|
||||
auth_plugin=database_settings.auth_plugin
|
||||
)
|
||||
connection.cursor().execute(f'CREATE DATABASE IF NOT EXISTS `{database_settings.database}`;')
|
||||
self._database = sql.connect(
|
||||
host=database_settings.host,
|
||||
user=database_settings.user,
|
||||
passwd=CredentialManager.decrypt(database_settings.password),
|
||||
@@ -35,6 +51,7 @@ class DatabaseConnection(DatabaseConnectionABC):
|
||||
buffered=database_settings.buffered,
|
||||
auth_plugin=database_settings.auth_plugin
|
||||
)
|
||||
self._cursor = self._database.cursor()
|
||||
Console.set_foreground_color(ForegroundColorEnum.green)
|
||||
Console.write_line(f'[{__name__}] Connected to database')
|
||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
||||
|
@@ -2,6 +2,7 @@ from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_core.database.database_settings import DatabaseSettings
|
||||
from mysql.connector.abstracts import MySQLConnectionAbstract
|
||||
from mysql.connector.cursor import MySQLCursorBuffered
|
||||
|
||||
|
||||
class DatabaseConnectionABC(ABC):
|
||||
@@ -14,6 +15,10 @@ class DatabaseConnectionABC(ABC):
|
||||
@abstractmethod
|
||||
def server(self) -> MySQLConnectionAbstract: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def cursor(self) -> MySQLCursorBuffered: pass
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
r"""Connects to a database by connection string
|
||||
|
@@ -8,6 +8,7 @@ from cpl_core.database.connection.database_connection_abc import \
|
||||
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
|
||||
from mysql.connector.cursor import MySQLCursorBuffered
|
||||
|
||||
|
||||
class DatabaseContext(DatabaseContextABC):
|
||||
@@ -22,21 +23,22 @@ class DatabaseContext(DatabaseContextABC):
|
||||
DatabaseContextABC.__init__(self, database_settings)
|
||||
|
||||
self._db: DatabaseConnectionABC = DatabaseConnection()
|
||||
self._cursor: Optional[str] = None
|
||||
self._tables: list[TableABC] = TableABC.__subclasses__()
|
||||
|
||||
@property
|
||||
def cursor(self):
|
||||
return self._cursor
|
||||
|
||||
def cursor(self) -> MySQLCursorBuffered:
|
||||
return self._db.cursor
|
||||
|
||||
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)
|
||||
Console.write_line(f"{table}, {table.get_create_string()}")
|
||||
self._db.cursor.execute(table.get_create_string())
|
||||
|
||||
def save_changes(self):
|
||||
self._db.server.commit()
|
||||
|
||||
def select(self, statement: str) -> list:
|
||||
self._db.cursor.execute(statement)
|
||||
return self._db.cursor.fetchall()
|
||||
|
@@ -1,6 +1,7 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_core.database.database_settings import DatabaseSettings
|
||||
from mysql.connector.cursor import MySQLCursorBuffered
|
||||
|
||||
|
||||
class DatabaseContextABC(ABC):
|
||||
@@ -11,9 +12,9 @@ class DatabaseContextABC(ABC):
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def cursor(self): pass
|
||||
|
||||
def cursor(self) -> MySQLCursorBuffered:
|
||||
return self._cursor
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
r"""Connects to a database by connection settings
|
||||
@@ -23,7 +24,22 @@ class DatabaseContextABC(ABC):
|
||||
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:
|
||||
r"""Runs SQL Statements
|
||||
|
||||
Parameter
|
||||
---------
|
||||
statement: :class:`str`
|
||||
|
||||
Returns
|
||||
-------
|
||||
list: Fetched list of selected elements
|
||||
"""
|
||||
pass
|
||||
|
@@ -18,9 +18,9 @@ class TableABC(ABC):
|
||||
def LastModifiedAt(self) -> datetime:
|
||||
return self._modified_at
|
||||
|
||||
@property
|
||||
@staticmethod
|
||||
@abstractmethod
|
||||
def create_string(self) -> str: pass
|
||||
def get_create_string() -> str: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
|
Reference in New Issue
Block a user