continued the database
This commit is contained in:
@@ -43,6 +43,7 @@ Artist = database.Artist
|
||||
Source = database.Source
|
||||
Target = database.Target
|
||||
Lyrics = database.Lyrics
|
||||
Album = database.Album
|
||||
MetadataSearch = metadata.MetadataSearch
|
||||
MetadataDownload = metadata.MetadataDownload
|
||||
|
||||
|
@@ -10,4 +10,6 @@ Target = objects.Target
|
||||
Metadata = objects.Metadata
|
||||
Lyrics = objects.Lyrics
|
||||
|
||||
Album = objects.Album
|
||||
|
||||
# cache = temp_database.TempDatabase()
|
||||
|
@@ -11,7 +11,8 @@ from .objects import (
|
||||
Metadata,
|
||||
Target,
|
||||
Artist,
|
||||
Source
|
||||
Source,
|
||||
Album
|
||||
)
|
||||
|
||||
logger = logging.getLogger("database")
|
||||
@@ -21,11 +22,11 @@ logger = logging.getLogger("database")
|
||||
# use complicated query builder
|
||||
SONG_QUERY = """
|
||||
SELECT
|
||||
Song.id AS song_id, Song.name AS title, Song.isrc AS isrc, Song.length AS length,
|
||||
Song.id AS song_id, Song.name AS title, Song.isrc AS isrc, Song.length AS length, Song.album_id,
|
||||
Target.id AS target_id, Target.file AS file, Target.path AS path
|
||||
FROM Song
|
||||
LEFT JOIN Target ON Song.id=Target.song_id
|
||||
WHERE Song.id="{song_id}";
|
||||
WHERE {where};
|
||||
"""
|
||||
SOURCE_QUERY = """
|
||||
SELECT id, src, url, song_id
|
||||
@@ -74,7 +75,7 @@ class Database:
|
||||
self.cursor = self.connection.cursor()
|
||||
return self.connection, self.cursor
|
||||
|
||||
def push_one(self, db_object: Song | Lyrics | Target | Artist | Source):
|
||||
def push_one(self, db_object: Song | Lyrics | Target | Artist | Source | Album):
|
||||
if type(db_object) == Song:
|
||||
return self.push_song(song=db_object)
|
||||
|
||||
@@ -90,7 +91,12 @@ class Database:
|
||||
if type(db_object) == Source:
|
||||
return self.push_source(source=db_object)
|
||||
|
||||
def push(self, db_object_list: List[Song | Lyrics | Target | Artist | Source]):
|
||||
if type(db_object) == Album:
|
||||
return self.push_album(album=db_object)
|
||||
|
||||
logger.warning(f"type {type(db_object)} isn't yet supported by the db")
|
||||
|
||||
def push(self, db_object_list: List[Song | Lyrics | Target | Artist | Source | Album]):
|
||||
"""
|
||||
This function is used to Write the data of any db_object to the database
|
||||
|
||||
@@ -103,6 +109,24 @@ class Database:
|
||||
for db_object in db_object_list:
|
||||
self.push_one(db_object)
|
||||
|
||||
def push_album(self, album: Album):
|
||||
table = "Album"
|
||||
query = f"INSERT OR REPLACE INTO {table} (id, title, copyright, album_status, language, year, date, country, barcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
|
||||
|
||||
values = (
|
||||
album.id,
|
||||
album.title,
|
||||
album.copyright,
|
||||
album.album_status,
|
||||
album.language,
|
||||
album.year,
|
||||
album.date,
|
||||
album.country,
|
||||
album.barcode
|
||||
)
|
||||
self.cursor.execute(query, values)
|
||||
self.connection.commit()
|
||||
|
||||
def push_song(self, song: Song):
|
||||
# ADDING THE DATA FOR THE SONG OBJECT
|
||||
"""
|
||||
@@ -112,13 +136,15 @@ class Database:
|
||||
name - title
|
||||
"""
|
||||
table = "Song"
|
||||
query = f"INSERT OR REPLACE INTO {table} (id, name, isrc, length) VALUES (?, ?, ?, ?);"
|
||||
|
||||
values = (
|
||||
song.id,
|
||||
song.title,
|
||||
song.isrc,
|
||||
song.length
|
||||
song.length,
|
||||
song.get_album_id()
|
||||
)
|
||||
query = f"INSERT OR REPLACE INTO {table} (id, name, isrc, length, album_id) VALUES (?, ?, ?, ?, ?);"
|
||||
|
||||
self.cursor.execute(query, values)
|
||||
self.connection.commit()
|
||||
@@ -237,28 +263,11 @@ class Database:
|
||||
url=source_row['url']
|
||||
) for source_row in source_rows]
|
||||
|
||||
def pull_single_song(self, song_ref: Reference = None) -> Song:
|
||||
"""
|
||||
This function is used to get one song (including its children like Sources etc)
|
||||
from one song id (a reference object)
|
||||
:param song_ref:
|
||||
:return requested_song:
|
||||
"""
|
||||
if song_ref.id is None:
|
||||
raise ValueError("The Song ref doesn't point anywhere. Remember to use the debugger.")
|
||||
query = SONG_QUERY.format(song_id=song_ref.id)
|
||||
self.cursor.execute(query)
|
||||
|
||||
song_rows = self.cursor.fetchall()
|
||||
if len(song_rows) == 0:
|
||||
logger.warning(f"No song found for the id {song_ref.id}")
|
||||
return Song()
|
||||
if len(song_rows) > 1:
|
||||
logger.warning(f"Multiple Songs found for the id {song_ref.id}. Defaulting to the first one.")
|
||||
song_result = song_rows[0]
|
||||
|
||||
def get_song_from_row(self, song_result) -> Song:
|
||||
song_id = song_result['song_id']
|
||||
|
||||
return Song(
|
||||
id_=song_result['song_id'],
|
||||
id_=song_id,
|
||||
title=song_result['title'],
|
||||
isrc=song_result['isrc'],
|
||||
length=song_result['length'],
|
||||
@@ -267,10 +276,44 @@ class Database:
|
||||
file=song_result['file'],
|
||||
path=song_result['path']
|
||||
),
|
||||
sources=self.pull_sources(song_ref=song_ref),
|
||||
lyrics=self.pull_lyrics(song_ref=song_ref)
|
||||
sources=self.pull_sources(song_ref=Reference(id_=song_id)),
|
||||
lyrics=self.pull_lyrics(song_ref=Reference(id_=song_id)),
|
||||
album_ref=Reference(song_result['album_id'])
|
||||
)
|
||||
|
||||
def pull_songs(self, song_ref: Reference = None, album_ref: Reference = None) -> List[Song]:
|
||||
"""
|
||||
This function is used to get one song (including its children like Sources etc)
|
||||
from one song id (a reference object)
|
||||
:param song_ref:
|
||||
:return requested_song:
|
||||
"""
|
||||
"""
|
||||
if song_ref is None:
|
||||
raise ValueError("The Song ref doesn't point anywhere. Remember to use the debugger.")
|
||||
"""
|
||||
where = "1=1"
|
||||
if song_ref is not None:
|
||||
where = f"Song.id=\"{song_ref.id}\""
|
||||
elif album_ref is not None:
|
||||
where = f"Song.album_id=\"{album_ref.id}\""
|
||||
|
||||
query = SONG_QUERY.format(where=where)
|
||||
self.cursor.execute(query)
|
||||
|
||||
song_rows = self.cursor.fetchall()
|
||||
"""
|
||||
if len(song_rows) == 0:
|
||||
logger.warning(f"No song found for the id {song_ref.id}")
|
||||
return Song()
|
||||
if len(song_rows) > 1:
|
||||
logger.warning(f"Multiple Songs found for the id {song_ref.id}. Defaulting to the first one.")
|
||||
"""
|
||||
|
||||
return [self.get_song_from_row(song_result=song_result) for song_result in song_rows]
|
||||
|
||||
def pull_albums(self, album_ref: Reference = None) -> List[Album]:
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
cache = Database("")
|
||||
|
@@ -10,3 +10,5 @@ Source = song.Source
|
||||
Target = song.Target
|
||||
Metadata = song.Metadata
|
||||
Lyrics = song.Lyrics
|
||||
|
||||
Album = song.Album
|
||||
|
@@ -12,6 +12,11 @@ class Reference:
|
||||
def __str__(self):
|
||||
return f"references to an object with the id: {self.id}"
|
||||
|
||||
def __eq__(self, __o: object) -> bool:
|
||||
if type(__o) != type(self):
|
||||
return False
|
||||
return self.id == __o.id
|
||||
|
||||
|
||||
class DatabaseObject:
|
||||
def __init__(self, id_: str = None) -> None:
|
||||
|
@@ -12,6 +12,10 @@ from .database_object import (
|
||||
)
|
||||
|
||||
|
||||
"""
|
||||
All Objects dependent
|
||||
"""
|
||||
|
||||
class SongAttribute:
|
||||
def __init__(self, song_ref: Reference = None):
|
||||
# the reference to the song the lyrics belong to
|
||||
@@ -137,7 +141,7 @@ class Song(DatabaseObject):
|
||||
id_: str = None,
|
||||
mb_id: str = None,
|
||||
title: str = None,
|
||||
release_name: str = None,
|
||||
album_name: str = None,
|
||||
artist_names: List[str] = [],
|
||||
isrc: str = None,
|
||||
length: int = None,
|
||||
@@ -145,7 +149,7 @@ class Song(DatabaseObject):
|
||||
target: Target = None,
|
||||
lyrics: List[Lyrics] = None,
|
||||
metadata: dict = {},
|
||||
release_ref: str = None,
|
||||
album_ref: Reference = None,
|
||||
artist_refs: List[Reference] = None
|
||||
) -> None:
|
||||
"""
|
||||
@@ -159,7 +163,7 @@ class Song(DatabaseObject):
|
||||
# self.id_: str | None = id_
|
||||
self.mb_id: str | None = mb_id
|
||||
self.title: str | None = title
|
||||
self.release_name: str | None = release_name
|
||||
self.album_name: str | None = album_name
|
||||
self.isrc: str | None = isrc
|
||||
self.length_: int | None = length
|
||||
self.artist_names = artist_names
|
||||
@@ -183,7 +187,7 @@ class Song(DatabaseObject):
|
||||
for lyrics_ in self.lyrics:
|
||||
lyrics_.add_song(self.reference)
|
||||
|
||||
self.release_ref = release_ref
|
||||
self.album_ref = album_ref
|
||||
self.artist_refs = artist_refs
|
||||
|
||||
def __str__(self) -> str:
|
||||
@@ -211,9 +215,66 @@ class Song(DatabaseObject):
|
||||
raise TypeError(f"length of a song must be of the type int not {type(length)}")
|
||||
self.length_ = length
|
||||
|
||||
def get_album_id(self) -> str | None:
|
||||
if self.album_ref is None:
|
||||
return None
|
||||
return self.album_ref.id
|
||||
|
||||
length = property(fget=get_length, fset=set_length)
|
||||
|
||||
|
||||
"""
|
||||
All objects dependend on Album
|
||||
"""
|
||||
|
||||
class Album(DatabaseObject):
|
||||
"""
|
||||
-------DB-FIELDS-------
|
||||
title TEXT,
|
||||
copyright TEXT,
|
||||
album_status TEXT,
|
||||
language TEXT,
|
||||
year TEXT,
|
||||
date TEXT,
|
||||
country TEXT,
|
||||
barcode TEXT,
|
||||
song_id BIGINT,
|
||||
"""
|
||||
def __init__(
|
||||
self,
|
||||
id_: str = None,
|
||||
title: str = None,
|
||||
copyright_: str = None,
|
||||
album_status: str = None,
|
||||
language: str = None,
|
||||
year: str = None,
|
||||
date: str = None,
|
||||
country: str = None,
|
||||
barcode: str = None,
|
||||
song_ref_list: List[Reference] = []
|
||||
) -> None:
|
||||
DatabaseObject.__init__(self, id_=id_)
|
||||
self.title: str = title
|
||||
self.copyright: str = copyright_
|
||||
self.album_status: str = album_status
|
||||
self.language: str = language
|
||||
self.year: str = year
|
||||
self.date: str = date
|
||||
self.country: str = country
|
||||
self.barcode: str = barcode
|
||||
|
||||
self.song_ref_list: List[Reference] = song_ref_list
|
||||
|
||||
def add_song(self, song_ref: Reference):
|
||||
for existing_song_ref in self.song_ref_list:
|
||||
if song_ref == existing_song_ref:
|
||||
return
|
||||
self.song_ref_list.append(song_ref)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""
|
||||
Example for creating a Song object
|
||||
|
@@ -1,9 +1,11 @@
|
||||
CREATE TABLE Song
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name TEXT,
|
||||
isrc TEXT,
|
||||
length INT -- length is in milliseconds (could be wrong)
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name TEXT,
|
||||
isrc TEXT,
|
||||
length INT, -- length is in milliseconds (could be wrong)
|
||||
album_id BIGINT,
|
||||
FOREIGN KEY(album_id) REFERENCES Album(id)
|
||||
);
|
||||
|
||||
|
||||
@@ -27,15 +29,21 @@ CREATE TABLE Artist
|
||||
|
||||
CREATE TABLE Album
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
song_id BIGINT,
|
||||
FOREIGN KEY(song_id) REFERENCES Song(id)
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
title TEXT,
|
||||
copyright TEXT,
|
||||
album_status TEXT,
|
||||
language TEXT,
|
||||
year TEXT,
|
||||
date TEXT,
|
||||
country TEXT,
|
||||
barcode TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE Target
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
file TEXT NOT NULL,
|
||||
file TEXT,
|
||||
path TEXT,
|
||||
song_id BIGINT UNIQUE,
|
||||
FOREIGN KEY(song_id) REFERENCES Song(id)
|
||||
|
Reference in New Issue
Block a user