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