2024-01-01 20:19:57 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2024-01-02 22:22:14 +00:00
|
|
|
from typing import Mapping, Any
|
2024-01-01 20:19:57 +00:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
class DatabaseController(ABC):
|
|
|
|
__sqlitefile = "./data/wmgzon.db"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._conn = None
|
|
|
|
try:
|
|
|
|
self._conn = sqlite3.connect(self.__sqlitefile)
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
# Close the connection if still open
|
|
|
|
if self._conn:
|
|
|
|
self._conn.close()
|
|
|
|
print(e)
|
|
|
|
|
2024-01-02 22:26:03 +00:00
|
|
|
""" Takes a dictionary of fields and returns the object
|
|
|
|
with those fields populated """
|
2024-01-02 22:22:14 +00:00
|
|
|
def new_instance(self, of: type, with_fields: Mapping[str, Any]):
|
|
|
|
obj = of.__new__(of)
|
|
|
|
for attr, value in with_fields.items():
|
|
|
|
setattr(obj, attr, value)
|
|
|
|
return obj
|
|
|
|
|
2024-01-01 20:19:57 +00:00
|
|
|
@abstractmethod
|
|
|
|
def create(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def read(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def update(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def delete(self):
|
|
|
|
pass
|