|
|
@@ -0,0 +1,96 @@
|
|
|
+import sqlite3
|
|
|
+from abc import abstractmethod
|
|
|
+
|
|
|
+from core import constants
|
|
|
+
|
|
|
+
|
|
|
+class Repository:
|
|
|
+ TABLE_NAME = None
|
|
|
+ MODEL_CLS = None
|
|
|
+
|
|
|
+ @abstractmethod
|
|
|
+ def __init__(self):
|
|
|
+ self.cnn = sqlite3.connect(constants.DB_PATH)
|
|
|
+
|
|
|
+ def execute(self, sql, *parameters):
|
|
|
+ cur = self.cnn.cursor()
|
|
|
+ cur.execute(sql, *parameters)
|
|
|
+ return cur
|
|
|
+
|
|
|
+ def get_by_id(self, id_):
|
|
|
+ cur = self.execute(
|
|
|
+ f"SELECT * FROM {self.TABLE_NAME} WHERE id=?;",
|
|
|
+ id_
|
|
|
+ )
|
|
|
+ return self.MODEL_CLS(**cur.fetchone())
|
|
|
+
|
|
|
+ def get_all(self):
|
|
|
+ cur = self.execute(f"SELECT * FROM {self.TABLE_NAME};", )
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
+
|
|
|
+ def get_by(self, **kwargs):
|
|
|
+ pass
|
|
|
+
|
|
|
+ def get_by_raw_sql(self, sql, parameters=None):
|
|
|
+ parameters = parameters if parameters is not None else []
|
|
|
+ cur = self.execute(sql, parameters)
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
+
|
|
|
+ def create(self, model, commit=False):
|
|
|
+ fields, values = model.as_fields_and_values()
|
|
|
+ self.execute(
|
|
|
+ f"INSERT INTO {self.TABLE_NAME} ({', '.join(fields)}) VALUES (?);",
|
|
|
+ *values
|
|
|
+ )
|
|
|
+ if commit:
|
|
|
+ self.commit()
|
|
|
+
|
|
|
+ def update(self, model, commit=False):
|
|
|
+ fields, values = model.as_fields_and_values()
|
|
|
+ self.execute(
|
|
|
+ f"UPDATE {self.TABLE_NAME} SET ({'=?,'.join(fields)});",
|
|
|
+ *values
|
|
|
+ )
|
|
|
+ if commit:
|
|
|
+ self.commit()
|
|
|
+
|
|
|
+ def delete(self, model, commit=False, permanent=False):
|
|
|
+ if commit:
|
|
|
+ self.commit()
|
|
|
+
|
|
|
+ def commit(self):
|
|
|
+ self.cnn.commit()
|
|
|
+
|
|
|
+ def rollback(self):
|
|
|
+ self.cnn.rollback()
|
|
|
+
|
|
|
+ def __del__(self):
|
|
|
+ self.cnn.close()
|
|
|
+
|
|
|
+
|
|
|
+class MusicFolderRepository(Repository):
|
|
|
+ TABLE_NAME = "MusicFolders"
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+
|
|
|
+class ProfileRepository(Repository):
|
|
|
+ TABLE_NAME = "Profiles"
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+
|
|
|
+class TagRepository(Repository):
|
|
|
+ TABLE_NAME = "Tags"
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+
|
|
|
+class TrackRepository(Repository):
|
|
|
+ TABLE_NAME = "Tracks"
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|