diff --git a/components/MusicTable.py b/components/MusicTable.py index cc5d121..63c5286 100644 --- a/components/MusicTable.py +++ b/components/MusicTable.py @@ -113,7 +113,7 @@ class MusicTable(QTableView): selected_indices = self.get_selected_rows() for file in selected_filepaths: with DBA.DBAccess() as db: - db.execute("DELETE FROM library WHERE filepath = ?", (file,)) + db.execute("DELETE FROM song WHERE filepath = ?", (file,)) for index in selected_indices: self.model.removeRow(index) @@ -264,7 +264,7 @@ class MusicTable(QTableView): # Update the db with DBA.DBAccess() as db: db.query( - "UPDATE library SET filepath = ? WHERE filepath = ?", + "UPDATE song SET filepath = ? WHERE filepath = ?", (new_path, filepath), ) print(f"Moved: {filepath} -> {new_path}") @@ -296,7 +296,7 @@ class MusicTable(QTableView): try: with DBA.DBAccess() as db: data = db.query( - "SELECT id, title, artist, album, genre, codec, album_date, filepath FROM library;", + "SELECT id, title, artist, album, genre, codec, album_date, filepath FROM song;", (), ) except Exception as e: diff --git a/main.py b/main.py index 07f79ed..df7fee2 100644 --- a/main.py +++ b/main.py @@ -384,7 +384,7 @@ if __name__ == "__main__": os.makedirs(path_as_string) # Create database on first run with DBA.DBAccess() as db: - with open("utils/delete_and_create_library.sql", "r") as file: + with open("utils/init.sql", "r") as file: lines = file.read() for statement in lines.split(";"): print(f"executing [{statement}]") diff --git a/utils/add_files_to_library.py b/utils/add_files_to_library.py index 8026673..5200d67 100644 --- a/utils/add_files_to_library.py +++ b/utils/add_files_to_library.py @@ -64,7 +64,7 @@ def add_files_to_library(files): if len(insert_data) >= 1000: with DBA.DBAccess() as db: db.executemany( - "INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT OR IGNORE INTO song (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", insert_data, ) insert_data = [] # Reset the insert_data list @@ -72,7 +72,7 @@ def add_files_to_library(files): if insert_data: with DBA.DBAccess() as db: db.executemany( - "INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT OR IGNORE INTO song (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", insert_data, ) return True diff --git a/utils/delete_and_create_library.sql b/utils/delete_and_create_library.sql index 34ed8ea..1703f62 100644 --- a/utils/delete_and_create_library.sql +++ b/utils/delete_and_create_library.sql @@ -1,6 +1,6 @@ -DROP TABLE IF EXISTS library; +DROP TABLE IF EXISTS song; -CREATE TABLE library( +CREATE TABLE song( -- In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer. id integer primary key, filepath varchar(511) UNIQUE, diff --git a/utils/init.sql b/utils/init.sql new file mode 100644 index 0000000..9865b5e --- /dev/null +++ b/utils/init.sql @@ -0,0 +1,32 @@ +DROP TABLE IF EXISTS song_playlist; +DROP TABLE IF EXISTS song; +DROP TABLE IF EXISTS playlist; + +CREATE TABLE song( + -- In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer. + id integer primary key, + filepath varchar(511) UNIQUE, + title varchar(255), + album varchar(255), + artist varchar(255), + genre varchar(255), + codec varchar(15), + album_date date, + bitrate int, + date_added TIMESTAMP default CURRENT_TIMESTAMP +); + +CREATE TABLE playlist( + id integer primary key, + name varchar(64), + date_created TIMESTAMP default CURRENT_TIMESTAMP +); + +CREATE TABLE song_playlist( + playlist_id integer, + song_id integer, + date_created TIMESTAMP default CURRENT_TIMESTAMP, + primary key (playlist_id, song_id), + foreign key (playlist_id) references playlist(id), + foreign key (song_id) references song(id) +); diff --git a/utils/update_song_in_library.py b/utils/update_song_in_library.py index d857bb1..fc310cb 100644 --- a/utils/update_song_in_library.py +++ b/utils/update_song_in_library.py @@ -2,7 +2,9 @@ import DBA from components.ErrorDialog import ErrorDialog -def update_song_in_library(library_id: int, edited_column_name: str, user_input_data: str): +def update_song_in_library( + library_id: int, edited_column_name: str, user_input_data: str +): """Updates a field in the library database based on an ID Args: @@ -15,10 +17,14 @@ def update_song_in_library(library_id: int, edited_column_name: str, user_input_ try: with DBA.DBAccess() as db: # yeah yeah this is bad... the column names are defined in the program by me so im ok with it because it works - db.execute(f'UPDATE library SET {edited_column_name} = ? WHERE id = ?', (user_input_data, library_id)) + db.execute( + f"UPDATE song SET {edited_column_name} = ? WHERE id = ?", + (user_input_data, library_id), + ) except Exception as e: - dialog = ErrorDialog(f'Unable to update [{edited_column_name}] to [{user_input_data}]. ID: {library_id} | {e}') + dialog = ErrorDialog( + f"Unable to update [{edited_column_name}] to [{user_input_data}]. ID: {library_id} | {e}" + ) dialog.exec_() return False return True -