REFACTOR: Moved more functionality into database base type
This commit is contained in:
@@ -8,7 +8,6 @@ class DatabaseController(ABC):
|
||||
""" Abstract Base Class to handle database access for each component
|
||||
in the web app
|
||||
"""
|
||||
|
||||
__data_dir = "./data/"
|
||||
__db_name = "wmgzon.db"
|
||||
|
||||
@@ -48,8 +47,64 @@ class DatabaseController(ABC):
|
||||
setattr(obj, attr, value)
|
||||
return obj
|
||||
|
||||
def do(self, query: str, params: list[str]):
|
||||
""" Function to run a query that returns no response """
|
||||
self._conn.execute(
|
||||
query,
|
||||
params
|
||||
)
|
||||
self._conn.commit()
|
||||
|
||||
def get_one(self, query: str, params: list[str], type: type = None
|
||||
) -> type | None:
|
||||
""" Returns one item from the given query """
|
||||
if type is None:
|
||||
type = self.TYPE
|
||||
|
||||
cursor = self._conn.execute(
|
||||
query,
|
||||
params
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
|
||||
if row is None:
|
||||
return None
|
||||
|
||||
# Construct the row into a single object
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
obj = self.new_instance(type, params)
|
||||
|
||||
return obj
|
||||
|
||||
def get_many(self, query: str, params: list[str], type: type = None
|
||||
) -> list[type] | None:
|
||||
""" Returns all items matching the given query """
|
||||
if type is None:
|
||||
type = self.TYPE
|
||||
|
||||
cursor = self._conn.execute(
|
||||
query,
|
||||
params
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
|
||||
if rows is None or len(rows) == 0:
|
||||
return None
|
||||
|
||||
objs = list()
|
||||
|
||||
# Construct the row into a list of object
|
||||
for row in rows:
|
||||
params = dict(zip(self.FIELDS, row))
|
||||
obj = self.new_instance(type, params)
|
||||
objs.append(obj)
|
||||
|
||||
return objs
|
||||
|
||||
"""
|
||||
Set of CRUD methods to allow for Data manipulation on the backend
|
||||
|
||||
These items MUST be implemented by anything inheriting from this class
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
|
||||
Reference in New Issue
Block a user