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
|
2024-01-09 20:53:55 +00:00
|
|
|
import os
|
2024-01-01 20:19:57 +00:00
|
|
|
|
|
|
|
class DatabaseController(ABC):
|
2024-01-09 20:53:55 +00:00
|
|
|
__data_dir = "./data/"
|
|
|
|
__db_name = "wmgzon.db"
|
|
|
|
|
|
|
|
# Use test file if necessary
|
|
|
|
if os.environ.get("ENVIRON") == "test":
|
|
|
|
__db_name = "test_" + __db_name
|
|
|
|
|
|
|
|
__sqlitefile = __data_dir + __db_name
|
|
|
|
|
2024-01-01 20:19:57 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._conn = None
|
|
|
|
try:
|
2024-01-10 23:44:59 +00:00
|
|
|
# Creates a connection and specifies a flag to parse all types back down into
|
|
|
|
# Python declared types e.g. date & time
|
|
|
|
self._conn = sqlite3.connect(self.__sqlitefile, detect_types=sqlite3.PARSE_DECLTYPES)
|
2024-01-01 20:19:57 +00:00
|
|
|
except sqlite3.Error as e:
|
|
|
|
# Close the connection if still open
|
|
|
|
if self._conn:
|
|
|
|
self._conn.close()
|
|
|
|
print(e)
|
|
|
|
|
2024-01-18 21:11:01 +00:00
|
|
|
def __del__(self):
|
|
|
|
if self._conn != None:
|
|
|
|
self._conn.close()
|
|
|
|
|
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-05 14:11:46 +00:00
|
|
|
"""
|
|
|
|
Set of CRUD methods to allow for Data manipulation on the backend
|
|
|
|
"""
|
|
|
|
|
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
|