2023-09-06 22:01:51 +01:00
|
|
|
import sqlite3
|
|
|
|
|
from ipaddress import IPv4Address
|
|
|
|
|
from sqlite3 import OperationalError
|
|
|
|
|
from typing import Dict, Optional, Any, List, Union
|
2023-08-29 13:21:34 +01:00
|
|
|
|
2023-09-06 22:01:51 +01:00
|
|
|
from prettytable import PrettyTable, MARKDOWN
|
|
|
|
|
|
|
|
|
|
from primaite.simulator.file_system.file_system import File
|
|
|
|
|
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
|
|
|
|
from primaite.simulator.network.transmission.transport_layer import Port
|
|
|
|
|
from primaite.simulator.system.core.software_manager import SoftwareManager
|
2023-08-25 15:29:53 +01:00
|
|
|
from primaite.simulator.system.services.service import Service
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseService(Service):
|
2023-09-06 11:35:41 +01:00
|
|
|
"""A generic SQL Server Service."""
|
2023-09-06 22:01:51 +01:00
|
|
|
backup_server: Optional[IPv4Address] = None
|
|
|
|
|
"The IP Address of the server the "
|
2023-09-06 11:35:41 +01:00
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2023-09-06 22:01:51 +01:00
|
|
|
kwargs["name"] = "Database"
|
|
|
|
|
kwargs["port"] = Port.POSTGRES_SERVER
|
|
|
|
|
kwargs["protocol"] = IPProtocol.TCP
|
2023-09-06 11:35:41 +01:00
|
|
|
super().__init__(**kwargs)
|
2023-09-06 22:01:51 +01:00
|
|
|
self._db_file: File
|
|
|
|
|
self._create_db_file()
|
|
|
|
|
self._conn = sqlite3.connect(self._db_file.sim_path)
|
|
|
|
|
self._cursor = self._conn.cursor()
|
|
|
|
|
|
|
|
|
|
def tables(self) -> List[str]:
|
|
|
|
|
sql = "SELECT name FROM sqlite_master WHERE type='table' AND name != 'sqlite_sequence';"
|
|
|
|
|
results = self._process_sql(sql)
|
|
|
|
|
return [row[0] for row in results["data"]]
|
|
|
|
|
|
|
|
|
|
def show(self, markdown: bool = False):
|
|
|
|
|
"""Prints a Table names in the Database."""
|
|
|
|
|
table = PrettyTable(["Table"])
|
|
|
|
|
if markdown:
|
|
|
|
|
table.set_style(MARKDOWN)
|
|
|
|
|
table.align = "l"
|
|
|
|
|
table.title = f"{self.file_system.sys_log.hostname} Database"
|
|
|
|
|
for row in self.tables():
|
|
|
|
|
table.add_row([row])
|
|
|
|
|
print(table)
|
|
|
|
|
|
|
|
|
|
def _create_db_file(self):
|
|
|
|
|
self._db_file: File = self.file_system.create_file(folder_name="database", file_name="database.db", real=True)
|
|
|
|
|
self.folder = self._db_file.folder
|
|
|
|
|
|
|
|
|
|
def _process_sql(self, query: str) -> Dict[str, Union[int, List[Any]]]:
|
|
|
|
|
try:
|
|
|
|
|
self._cursor.execute(query)
|
|
|
|
|
self._conn.commit()
|
|
|
|
|
except OperationalError:
|
|
|
|
|
# Handle the case where the table does not exist.
|
|
|
|
|
return {"status_code": 404, "data": []}
|
|
|
|
|
|
|
|
|
|
return {"status_code": 200, "data": self._cursor.fetchall()}
|
2023-08-25 15:29:53 +01:00
|
|
|
|
2023-08-29 13:21:34 +01:00
|
|
|
def describe_state(self) -> Dict:
|
2023-08-31 11:32:11 +01:00
|
|
|
"""
|
|
|
|
|
Produce a dictionary describing the current state of this object.
|
|
|
|
|
|
|
|
|
|
Please see :py:meth:`primaite.simulator.core.SimComponent.describe_state` for a more detailed explanation.
|
|
|
|
|
|
|
|
|
|
:return: Current state of this object and child objects.
|
|
|
|
|
:rtype: Dict
|
|
|
|
|
"""
|
2023-08-29 13:21:34 +01:00
|
|
|
return super().describe_state()
|
|
|
|
|
|
2023-09-06 22:01:51 +01:00
|
|
|
def receive(self, payload: Any, session_id: str, **kwargs) -> bool:
|
|
|
|
|
result = self._process_sql(payload)
|
|
|
|
|
software_manager: SoftwareManager = self.software_manager
|
|
|
|
|
software_manager.send_payload_to_session_manager(payload=result, session_id=session_id)
|
2023-09-06 11:35:41 +01:00
|
|
|
|
2023-09-06 22:01:51 +01:00
|
|
|
return result["status_code"]
|
2023-08-29 14:15:49 +01:00
|
|
|
|
2023-09-06 22:01:51 +01:00
|
|
|
def send(self, payload: Any, session_id: str, **kwargs) -> bool:
|
|
|
|
|
pass
|