And create both permanent and temporary maps for tracks. The temporary map is intended to be used for the New Tracks and Previous playlists, since we don't store the state across restarts. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
# Copyright 2021 (c) Anna Schumaker.
|
|
#
|
|
# Table: playlists
|
|
# +------ ---+------+------+
|
|
# | playlistid | name | sort |
|
|
# +-------- -+------+------+
|
|
#
|
|
# Index: playlist_index
|
|
# +--------------------+
|
|
# | name -> playlistid |
|
|
# +--------------------+
|
|
from . import execute
|
|
from . import executemany
|
|
from . import objects
|
|
from . import track
|
|
|
|
Default = [ "Collection", "Favorites", "New Tracks",
|
|
"Previous", "Queued Tracks" ]
|
|
|
|
|
|
class Playlist(objects.Tag):
|
|
def do_get_column(self, column):
|
|
return execute(f"SELECT {column} FROM playlists "
|
|
"WHERE playlistid=?", [ self.rowid ])
|
|
|
|
|
|
class PlaylistTable(objects.Table):
|
|
def __init__(self):
|
|
objects.Table.__init__(self, "playlists", Playlist)
|
|
|
|
def do_create(self):
|
|
execute("CREATE TABLE IF NOT EXISTS playlists "
|
|
"(playlistid INTEGER PRIMARY KEY, "
|
|
" name TEXT UNIQUE, "
|
|
" sort TEXT)")
|
|
execute("CREATE INDEX IF NOT EXISTS playlist_index "
|
|
"ON playlists(name)")
|
|
executemany("INSERT OR IGNORE INTO playlists (name, sort) "
|
|
"VALUES (?, ?)",
|
|
[ (p, p.casefold()) for p in Default ])
|
|
|
|
def do_insert(self, name):
|
|
return execute("INSERT INTO playlists (name, sort) "
|
|
"VALUES (?, ?)", (name, name.casefold()))
|
|
|
|
def do_delete(self, playlist):
|
|
Map.delete_playlist(playlist)
|
|
TempMap.delete_playlist(playlist)
|
|
return execute("DELETE FROM playlists WHERE playlistid=?", [ int(playlist) ])
|
|
|
|
def do_get(self, rowid):
|
|
return execute("SELECT playlistid FROM playlists "
|
|
"WHERE playlistid=?", [ rowid ])
|
|
|
|
def do_lookup(self, name):
|
|
return execute("SELECT playlistid FROM playlists "
|
|
"WHERE name=?", [ name ])
|
|
|
|
|
|
class PlaylistMap(objects.Map):
|
|
def __init__(self, temp=False):
|
|
name = "playlist_map" if temp==False else "temp_playlist_map"
|
|
self.temporary = temp
|
|
|
|
objects.Map.__init__(self, name, Playlist, track.Track)
|
|
self.lookup_tracks = self.lookup_rhs
|
|
self.lookup_playlists = self.lookup_lhs
|
|
|
|
def do_create(self):
|
|
temp = "" if self.temporary == False else "TEMPORARY"
|
|
execute(f"CREATE {temp} TABLE IF NOT EXISTS {self.map_name} "
|
|
"(playlistid INTEGER, "
|
|
" trackid INTEGER, "
|
|
" FOREIGN KEY(playlistid) REFERENCES playlists(playlistid), "
|
|
" FOREIGN KEY(trackid) REFERENCES tracks(trackid), "
|
|
" UNIQUE(playlistid, trackid))")
|
|
|
|
def do_insert(self, playlist, track):
|
|
execute(f"INSERT INTO {self.map_name} (playlistid, trackid) "
|
|
"VALUES (?, ?)", [ int(playlist), int(track) ])
|
|
|
|
def do_delete(self, playlist, track):
|
|
return execute(f"DELETE FROM {self.map_name} "
|
|
"WHERE playlistid=? AND trackid=?",
|
|
[ int(playlist), int(track) ])
|
|
|
|
def do_lookup_rhs(self, playlist):
|
|
return execute(f"SELECT trackid FROM {self.map_name} "
|
|
"WHERE playlistid=?", [ int(playlist) ])
|
|
|
|
def do_lookup_lhs(self, track):
|
|
return execute(f"SELECT playlistid FROM {self.map_name} "
|
|
"WHERE trackid=?", [ int(track) ])
|
|
|
|
def delete_track(self, track):
|
|
for playlist in self.lookup_playlists(track):
|
|
self.delete(playlist, track)
|
|
|
|
def delete_playlist(self, playlist):
|
|
for track in self.lookup_tracks(playlist):
|
|
self.delete(playlist, track)
|
|
|
|
|
|
Table = PlaylistTable()
|
|
Map = PlaylistMap(temp=False)
|
|
TempMap = PlaylistMap(temp=True)
|