Initial Commit
This commit is contained in:
50
utilities/database/database.py
Normal file
50
utilities/database/database.py
Normal file
@@ -0,0 +1,50 @@
|
||||
class Database():
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def query(self, query: str, parameters: list=None) -> tuple[int, list[dict]]:
|
||||
pass
|
||||
|
||||
def select(self, table: str, columns: list[str]=None, where: list[dict]=None) -> list[dict]:
|
||||
query_string = "SELECT {columns} FROM {table}{where}".format(
|
||||
columns = "*" if columns is None else ",".join(columns),
|
||||
table = table,
|
||||
where = self.__generate_basic_where_clause(where) if not where is None else ""
|
||||
)
|
||||
|
||||
return self.query(query_string, [element["value"] for element in where] if not where is None else None)[1]
|
||||
|
||||
def insert(self, table: str, columns: list[str], values: list) -> int:
|
||||
query_string = "INSERT INTO {table} ({columns}) VALUES ({values})".format(
|
||||
table = table,
|
||||
columns = ",".join(columns),
|
||||
values = ("?," * len(values))[:-1]
|
||||
)
|
||||
|
||||
return self.query(query_string, values)[0]
|
||||
|
||||
def update(self, table: str, columns: list[str], values: list, where: list[dict]=None) -> int:
|
||||
query_string = "UPDATE {table} SET {set_rules}{where}".format(
|
||||
table = table,
|
||||
set_rules = ",".join([element + "=?" for element in columns]),
|
||||
where = self.__generate_basic_where_clause(where) if not where is None else ""
|
||||
)
|
||||
|
||||
return self.query(query_string, values)[0]
|
||||
|
||||
def delete(self, table: str, where: list[dict]=None) -> int:
|
||||
query_string = "DELETE FROM {table}{where}".format(
|
||||
table = table,
|
||||
where = self.__generate_basic_where_clause(where) if not where is None else ""
|
||||
)
|
||||
|
||||
return self.query(query_string)[0]
|
||||
|
||||
def __generate_basic_where_clause(self, where: list[dict]):
|
||||
return " WHERE {clauses}".format(
|
||||
clauses = "".join([
|
||||
element["name"] + " " + element["compare"] + " ?" + (" " + element["boolean_op"] + " " if "boolean_op" in element else "")
|
||||
for element in where
|
||||
])
|
||||
)
|
||||
|
||||
0
utilities/database/mysql.py
Normal file
0
utilities/database/mysql.py
Normal file
0
utilities/database/sqlite.py
Normal file
0
utilities/database/sqlite.py
Normal file
Reference in New Issue
Block a user