From 3ef17fcd468d710186d0448424d15ea42786b395 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Fri, 6 Jun 2025 14:28:15 -0400 Subject: [PATCH] Split entity_items and appears query into more granular queries We can be a bit more specific about the information we get this way --- src/app/views/albums.zig | 2 +- src/app/views/artists.zig | 2 +- src/app/views/songs.zig | 6 +-- src/queries.zig | 91 +++++++++++++++++++++++++++------------ 4 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index 713e6d6..03046b3 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -20,7 +20,7 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { const album = try queries.entityQueryResult(request, queries.loadQuery(.album, .entity_info), .{id}); try root.put("album", album); - const songs = try queries.entityQueryResult(request, queries.loadQuery(.album, .entity_items), .{id}); + const songs = try queries.entityQueryResult(request, queries.loadQuery(.album, .get_songs), .{id}); try root.put("songs", songs); const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.album, .firstlast), .{id}); diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index 1f7ff85..3ec8eba 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -21,7 +21,7 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { const artist = try queries.entityQueryResult(request, queries.loadQuery(.artist, .entity_info), .{id}); try root.put("artist", artist); - const albums = try queries.entityQueryResult(request, queries.loadQuery(.artist, .entity_items), .{id}); + const albums = try queries.entityQueryResult(request, queries.loadQuery(.artist, .get_albums), .{id}); try root.put("albums", albums); const appears = try queries.entityQueryResult(request, queries.loadQuery(.artist, .appears), .{id}); diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index b24dbf5..164f928 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -17,11 +17,11 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { const song = try queries.entityQueryResult(request, queries.loadQuery(.song, .entity_info), .{id}); try root.put("song", song); - const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .entity_items), .{id}); + const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_scrobbles), .{id}); try root.put("scrobbles", scrobbles); - const appears = try queries.entityQueryResult(request, queries.loadQuery(.song, .appears), .{id}); - try root.put("appears", appears); + const albums = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_albums), .{id}); + try root.put("albums", albums); const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.song, .firstlast), .{id}); try root.put("firstlast", firstlast); diff --git a/src/queries.zig b/src/queries.zig index 8db7809..5d92869 100644 --- a/src/queries.zig +++ b/src/queries.zig @@ -55,7 +55,7 @@ pub fn entityQueryResult(request: *jetzig.Request, query: GeneratedQuery, args: } const EntityType = enum { scrobble, song, album, artist }; -const QueryTypeEnum = enum { firstlast, timescale, entities, entity_items, appears, entity_info, datestreak }; +const QueryTypeEnum = enum { firstlast, timescale, entities, get_songs, get_albums, get_scrobbles, appears, entity_info, datestreak }; const GeneratedQuery = struct { entity: EntityType, @@ -246,20 +246,9 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery { }, .appears => - //.ResultType = AppearsResult, + // Not sure how I feel about this one switch (entity) { - .scrobble, .album => unreachable, - .song => - \\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles - \\FROM artistalbums - \\INNER JOIN albums ON albums.id = artistalbums.album_id - \\INNER JOIN albumsongs ON albumsongs.album_id = albums.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\WHERE albumsongs.song_id = $1 - \\GROUP BY albums.id - \\ORDER BY scrobbles DESC; - , + .scrobble, .song, .album => unreachable, .artist => \\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles \\FROM artistalbums @@ -273,19 +262,8 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery { , }, - .entity_items => - //.ResultType = EntityItemsResult, - switch (entity) { - .scrobble => unreachable, - .song => - \\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date - \\FROM albumsongs - \\INNER JOIN albums ON albums.id = albumsongs.album_id - \\INNER JOIN songs ON songs.id = albumsongs.song_id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\WHERE songs.id = $1 - \\ORDER BY date ASC - , + .get_songs => switch (entity) { + .scrobble, .song => unreachable, // Might be able to use this with SongGroups? .album => \\SELECT songs.name AS song_name, songs.id AS song_id, COUNT(scrobbles) AS scrobbles \\FROM albumsongs @@ -296,6 +274,34 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery { \\ORDER BY scrobbles DESC , .artist => + \\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\GROUP BY songs.id, albums.id + \\ORDER BY scrobbles DESC + , + }, + + .get_albums => + //.ResultType = EntityItemsResult, + switch (entity) { + .scrobble, .album => unreachable, // Might be able to use this with ReleaseGroups? + .song => + \\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles + \\FROM artistalbums + \\INNER JOIN albums ON albums.id = artistalbums.album_id + \\INNER JOIN albumsongs ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongs.song_id = $1 + \\GROUP BY albums.id + \\ORDER BY scrobbles DESC + , + .artist => \\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles \\FROM artistalbums \\INNER JOIN albums ON albums.id = artistalbums.album_id @@ -307,6 +313,37 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery { , }, + .get_scrobbles => switch (entity) { + .scrobble => unreachable, + .song => + \\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date + \\FROM albumsongs + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE songs.id = $1 + \\ORDER BY date ASC + , + .album => + \\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date + \\FROM albumsongs + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE albums.id = $1 + \\ORDER BY date ASC + , + .artist => + \\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date + \\FROM albumsongs + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE artists.id = $1 + \\ORDER BY date ASC + , + }, + .entity_info => switch (entity) { .scrobble => unreachable, .song =>