75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
# Copyright 2021 (c) Anna Schumaker.
|
|
#
|
|
# Table: albums
|
|
# +---------+----------+-----------+------+------+
|
|
# | albumid | artistid | plstateid | name | sort |
|
|
# +---------+----------+-----------+------+------+
|
|
#
|
|
# Index: album_index
|
|
# +-----------------------------+
|
|
# | (artistid, name) -> albumid |
|
|
# +-----------------------------+
|
|
from gi.repository import GObject
|
|
from . import artist
|
|
from . import disc
|
|
from . import execute
|
|
from . import objects
|
|
from . import state
|
|
|
|
class Album(objects.Tag):
|
|
def do_get_column(self, column):
|
|
return execute(f"SELECT {column} FROM albums "
|
|
"WHERE albumid=?", [ self.rowid ])
|
|
|
|
@GObject.Property
|
|
def artist(self):
|
|
return artist.Artist(self.get_column("artistid"))
|
|
|
|
@GObject.Property
|
|
def playlist_state(self):
|
|
return state.PlaylistState(self.get_column("plstateid"))
|
|
|
|
def discs(self):
|
|
cursor = execute(f"SELECT discid FROM discs "
|
|
"WHERE albumid=?", [ self.rowid ])
|
|
return [ disc.Disc(row["discid"]) for row in cursor.fetchall() ]
|
|
|
|
|
|
class AlbumTable(objects.Table):
|
|
def __init__(self):
|
|
objects.Table.__init__(self, "albums", Album)
|
|
|
|
def do_create(self):
|
|
execute("CREATE TABLE IF NOT EXISTS albums "
|
|
"(albumid INTEGER PRIMARY KEY, "
|
|
" artistid INTEGER, "
|
|
" plstateid INTEGER NOT NULL, "
|
|
" name TEXT, "
|
|
" sort TEXT, "
|
|
" FOREIGN KEY(artistid) REFERENCES artists(artistid), "
|
|
" FOREIGN KEY(plstateid) REFERENCES playlist_states(plstateid), "
|
|
" UNIQUE(artistid, name))")
|
|
execute("CREATE INDEX IF NOT EXISTS artist_index "
|
|
"ON albums(artistid, name)")
|
|
|
|
def do_insert(self, artist, name):
|
|
plstate = state.Table.insert(random=False, loop=False)
|
|
return execute("INSERT INTO albums (artistid, plstateid, name, sort) "
|
|
"VALUES (?, ?, ?, ?)",
|
|
[ int(artist), int(plstate), name, name.casefold() ])
|
|
|
|
def do_delete(self, album):
|
|
state.Table.delete(album.playlist_state)
|
|
return execute("DELETE FROM albums WHERE albumid=?", [ int(album) ])
|
|
|
|
def do_get(self, rowid):
|
|
return execute("SELECT albumid FROM albums "
|
|
"WHERE albumid=?", [ rowid ])
|
|
|
|
def do_lookup(self, artist, name):
|
|
return execute("SELECT albumid FROM albums "
|
|
"WHERE (artistid=? AND name=?)", [ int(artist), name ])
|
|
|
|
|
|
Table = AlbumTable()
|