From 0744cc8606a26324bf5940379f50e7be74f48b25 Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 2 Dec 2022 12:58:18 +0100 Subject: [PATCH 1/2] started new database implementation, the old one was way to bad --- src/music_kraken/database/new_database.py | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/music_kraken/database/new_database.py diff --git a/src/music_kraken/database/new_database.py b/src/music_kraken/database/new_database.py new file mode 100644 index 0000000..468c961 --- /dev/null +++ b/src/music_kraken/database/new_database.py @@ -0,0 +1,37 @@ +import sqlite3 +import os +import logging + +logger = logging.getLogger("database") + + +class Database: + def __init__(self, structure_file: str, database_file: str): + self.structure_file: str = structure_file + self.database_file: str = database_file + + self.connection = sqlite3.connect(self.database_file) + self.cursor = self.connection.cursor() + + def reset(self): + """ + Deletes all Data from the database if it exists + and resets the schema defined in self.structure_file + """ + logger.info(f"resetting the database \"{self.__name__}\"") + + # deleting the database + del self.connection + del self.cursor + os.remove(self.database_file) + + # newly creating the database + self.connection = sqlite3.connect(self.database_file) + self.cursor = self.connection.cursor() + with open(self.structure_file, "r") as structure_file_obj: + query = structure_file_obj.read() + + # fill the database with the schematic + self.cursor.executescript(query) + self.connection.commit() + \ No newline at end of file From 7ac5b8a6d3ecd449c232ee37a622f9c16c39c74b Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 2 Dec 2022 14:39:47 +0100 Subject: [PATCH 2/2] added push method to sync db objects, needs to be implemented --- src/goof.py | 19 +++++++ src/music_kraken/database/new_database.py | 68 +++++++++++++++++++++-- src/music_kraken/static_files/new_db.sql | 4 -- 3 files changed, 81 insertions(+), 10 deletions(-) diff --git a/src/goof.py b/src/goof.py index 2966c77..77cd00a 100644 --- a/src/goof.py +++ b/src/goof.py @@ -1,5 +1,23 @@ import music_kraken +import music_kraken.database.new_database as db + +cache = music_kraken.database.new_database.Database("test.db") +cache.reset() + +artist = music_kraken.Artist( + name="I'm in a Coffin" +) + +song = music_kraken.Song( + title="Vein Deep in the Solution", + release="One Final Action", + artists=[artist] +) + +cache.push([artist, song]) + +""" music_kraken.clear_cache() artist = music_kraken.Artist( @@ -16,3 +34,4 @@ print(song) print(song.id) # music_kraken.fetch_sources([song]) +""" diff --git a/src/music_kraken/database/new_database.py b/src/music_kraken/database/new_database.py index 468c961..d68ccb2 100644 --- a/src/music_kraken/database/new_database.py +++ b/src/music_kraken/database/new_database.py @@ -1,13 +1,23 @@ import sqlite3 import os import logging +from typing import List +from pkg_resources import resource_string + +from .song import ( + Song, + Lyrics, + Metadata, + Target, + Artist, + Source +) logger = logging.getLogger("database") class Database: - def __init__(self, structure_file: str, database_file: str): - self.structure_file: str = structure_file + def __init__(self, database_file: str): self.database_file: str = database_file self.connection = sqlite3.connect(self.database_file) @@ -18,7 +28,7 @@ class Database: Deletes all Data from the database if it exists and resets the schema defined in self.structure_file """ - logger.info(f"resetting the database \"{self.__name__}\"") + logger.info(f"resetting the database") # deleting the database del self.connection @@ -28,10 +38,56 @@ class Database: # newly creating the database self.connection = sqlite3.connect(self.database_file) self.cursor = self.connection.cursor() - with open(self.structure_file, "r") as structure_file_obj: - query = structure_file_obj.read() + query = resource_string("music_kraken", "static_files/new_db.sql").decode('utf-8') # fill the database with the schematic self.cursor.executescript(query) self.connection.commit() - \ No newline at end of file + + def push_one(self, db_object: Song | Lyrics | Target | Artist | Source): + if type(db_object) == Song: + return self.push_song(song=db_object) + + if type(db_object) == Lyrics: + return self.push_lyrics(lyrics=db_object) + + if type(db_object) == Target: + return self.push_target(target=db_object) + + if type(db_object) == Artist: + return self.push_artist(artist=db_object) + + if type(db_object) == Source: + return self.push_source(source=db_object) + + def push(self, db_object_list: List[Song | Lyrics | Target | Artist | Source]): + """ + This function is used to Write the data of any db_object to the database + + It syncs a whole list of db_objects to the database and is meant + as the primary method to add to the database. + + :param db_object_list: + """ + + for db_object in db_object_list: + self.push_one(db_object) + + def push_song(self, song: Song): + pass + + def push_lyrics(self, lyrics: Lyrics): + pass + + def push_target(self, target: Target): + pass + + def push_artist(self, artist: Artist): + pass + + def push_source(self, source: Source): + pass + + +if __name__ == "__main__": + cache = Database("") diff --git a/src/music_kraken/static_files/new_db.sql b/src/music_kraken/static_files/new_db.sql index c72bd82..7cc54c6 100644 --- a/src/music_kraken/static_files/new_db.sql +++ b/src/music_kraken/static_files/new_db.sql @@ -40,10 +40,6 @@ CREATE TABLE Lyrics FOREIGN KEY(song_id) REFERENCES Song(id) ); -CREATE TABLE EasyId3 -( -); - CREATE TABLE SongArtist ( song_id BIGINT,