29 lines
829 B
Python
29 lines
829 B
Python
from database import Database
|
|
|
|
|
|
class BaseStorage:
|
|
|
|
def __init__(self, db: Database):
|
|
self.db = db
|
|
|
|
def __fetch_all(self, sql, *args):
|
|
return self.db.fetch_all(sql, *args)
|
|
|
|
def __fetch_one(self, sql, *args):
|
|
return self.db.fetch_one(sql, *args)
|
|
|
|
def __form_query(self, table_name: str, kwargs) -> str:
|
|
sql = f'select * from {table_name}'
|
|
|
|
if len(kwargs) != 0:
|
|
filters = [f"{key}='{val}'" for key, val in kwargs.items()]
|
|
sql = f"select * from {table_name} where {' and '.join(filters)}"
|
|
|
|
return sql
|
|
|
|
def get_by(self, table_name: str, **kwargs):
|
|
return self.__fetch_one(self.__form_query(table_name, kwargs))
|
|
|
|
def list_by(self, table_name: str, **kwargs):
|
|
return self.__fetch_all(self.__form_query(table_name, kwargs))
|