From 7f3778e82f74627bfe992fd03193acec06adf9ce Mon Sep 17 00:00:00 2001 From: mitteneer Date: Sat, 24 May 2025 13:59:28 -0400 Subject: [PATCH] Move SQL logic to separate function Idk if this makes any sense, and I don't really like the code atm, but the view .zig files lookk nicer? --- src/app/views/albums.zig | 72 ++------ src/app/views/artists.zig | 164 ++---------------- src/app/views/songs.zig | 44 +---- src/queries.zig | 343 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 370 insertions(+), 253 deletions(-) create mode 100644 src/queries.zig diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index 9c6063e..af72456 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -3,81 +3,31 @@ const jetzig = @import("jetzig"); const jetquery = @import("jetzig").jetquery; const TableRow = @import("../../types.zig").TableRow; const HyperlinkData = @import("../../types.zig").HyperlinkData; +const queries = @import("../../queries.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var albums_view = try root.put("albums", .array); - const query = - \\SELECT albums.name, albums.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongs - \\INNER JOIN albums ON albumsongs.album_id = albums.id - \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong - \\INNER JOIN artistalbums ON artistalbums.album_id = albums.id - \\INNER JOIN artists ON artists.id = artistalbums.artist_id - \\GROUP BY albums.id, artists.id - \\ORDER BY scrobbles DESC - ; - var albums_jq_result = try request.repo.executeSql(query, .{}); - defer albums_jq_result.deinit(); + const albums = try queries.entityQueryResult(request, queries.generateQuery(.album, .entities), .{}, .array); + try root.put("albums", albums); - const Album = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; - - var prev_album_id: ?i32 = null; - - var row: ?TableRow = null; - var artistlist = std.ArrayList(HyperlinkData).init(request.allocator); - - blk: while (try albums_jq_result.postgresql.result.next()) |album_row| { - const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator }); - if (album.id == prev_album_id) { - try artistlist.append(.{ .name = album.artist_name, .id = album.artist_id }); - continue :blk; - } else { - try artistlist.append(.{ .name = album.artist_name, .id = album.artist_id }); - - row = TableRow{ - .album = .{ .name = album.name, .id = album.id }, - .artistlist = try artistlist.toOwnedSlice(), - .scrobbles = album.scrobbles, - }; - - try albums_view.append(row); - } - prev_album_id = album.id; - } return request.render(.ok); } pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var songs_view = try root.put("songs", .array); - const album_name = try jetzig.database.Query(.Album).find(id).select(.{ .id, .name }).execute(request.repo); - _ = try root.put("album", album_name.?.name); - const query = - \\SELECT songs.name, songs.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongs - \\INNER JOIN songs ON albumsongs.song_id = songs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\WHERE albumsongs.album_id = $1 - \\GROUP BY songs.id - \\ORDER BY scrobbles DESC - ; + const album = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_info), .{id}, .object); + try root.put("album", album.get("entity_info")); - var songs_js_result = try request.repo.executeSql(query, .{id}); - defer songs_js_result.deinit(); + const songs = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_items), .{id}, .array); + try root.put("songs", songs); - const Song = struct { name: []const u8, id: i32, scrobbles: i64 }; + const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.album, .firstlast), .{id}, .array); + try root.put("firstlast", firstlast); - while (try songs_js_result.postgresql.result.next()) |song_row| { - const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator }); - const row = TableRow{ - .song = .{ .name = song.name, .id = song.id }, - .scrobbles = song.scrobbles, - }; + const timescale = try queries.entityQueryResult(request, queries.generateQuery(.album, .timescale), .{id}, .array); + try root.put("yearly", timescale); - try songs_view.append(row); - } return request.render(.ok); } diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index 9fd9dcd..37ca155 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -4,35 +4,13 @@ const jetquery = @import("jetzig").jetquery; const TableRow = @import("../../types.zig").TableRow; const dateFmt = @import("../../date_fmt.zig").dateFmt; const ordinalFmt = @import("../../ordinal_fmt.zig").ordinalFmt; +const queries = @import("../../queries.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var artists_view = try root.put("artists", .array); + const artists = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entities), .{}, .array); - const query = - \\SELECT artists.name, artists.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongsartists - \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id - \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\GROUP BY artists.id - \\ORDER BY scrobbles DESC; - ; - - var artists_jq_result = try request.repo.executeSql(query, .{}); - defer artists_jq_result.deinit(); - - const Artist = struct { name: []const u8, id: i32, scrobbles: i64 }; - - while (try artists_jq_result.postgresql.result.next()) |artist_row| { - const artist = try artist_row.to(Artist, .{ .dupe = true, .allocator = request.allocator }); - const row = TableRow{ - .artist = .{ .name = artist.name, .id = artist.id }, - .scrobbles = artist.scrobbles, - }; - - try artists_view.append(row); - } + try root.put("artists", artists); return request.render(.ok); } @@ -40,136 +18,20 @@ pub fn index(request: *jetzig.Request) !jetzig.View { pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - const ArtistResult = struct { name: []const u8, id: i32, scrobbles: i64, rank: i64 }; - const AlbumsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; - const ScrobbleResult = struct { name: []const u8, id: i32, date: i64 }; + const artist = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_info), .{id}, .object); + try root.put("artist", artist.get("entity_info")); - const artist_info_query = - \\SELECT * FROM - \\(SELECT *, ROW_NUMBER() OVER (ORDER BY scrobbles DESC) AS rank FROM - \\(SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongsartists - \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id - \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\GROUP BY artists.id)) - \\WHERE id = $1 - ; + const albums = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_items), .{id}, .array); + try root.put("albums", albums); - var artist_jq_result = try request.repo.executeSql(artist_info_query, .{id}); - defer artist_jq_result.deinit(); + const appears = try queries.entityQueryResult(request, queries.generateQuery(.artist, .appears), .{id}, .array); + try root.put("appears", appears); - if (try artist_jq_result.postgresql.result.next()) |artist_row| { - const artist = try artist_row.to(ArtistResult, .{ .dupe = true, .allocator = request.allocator }); - var artist_view = try root.put("artist", .object); - try artist_view.put("name", artist.name); - try artist_view.put("id", artist.id); - try artist_view.put("scrobbles", artist.scrobbles); - try artist_view.put("rank", ordinalFmt(request.allocator, artist.rank)); - } - try artist_jq_result.drain(); + const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.artist, .firstlast), .{id}, .array); + try root.put("firstlast", firstlast); - const albums_query = - \\SELECT albums.name AS name, albums.id AS 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 - \\WHERE artistalbums.artist_id = $1 - \\GROUP BY albums.id - \\ORDER BY scrobbles DESC - ; - - var albums_view = try root.put("albums", .array); - - var albums_jq_result = try request.repo.executeSql(albums_query, .{id}); - defer albums_jq_result.deinit(); - - while (try albums_jq_result.postgresql.result.next()) |album_row| { - const album = try album_row.to(AlbumsResult, .{ .dupe = true, .allocator = request.allocator }); - const album_table_row = TableRow{ - .album = .{ .name = album.name, .id = album.id }, - .scrobbles = album.scrobbles, - }; - - try albums_view.append(album_table_row); - } - //albums_jq_result.drain(); - - const appears_query = - \\SELECT albums.name AS name, albums.id AS 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 albumsongsartists.artist_id = $1 AND artistalbums.artist_id != $1 - \\GROUP BY albums.id - \\ORDER BY scrobbles DESC; - ; - - var appears_view = try root.put("appears", .array); - - var appears_jq_result = try request.repo.executeSql(appears_query, .{id}); - defer appears_jq_result.deinit(); - - while (try appears_jq_result.postgresql.result.next()) |appears_row| { - const appears = try appears_row.to(AlbumsResult, .{ .dupe = true, .allocator = request.allocator }); - const appears_table_row = TableRow{ - .album = .{ .name = appears.name, .id = appears.id }, - .scrobbles = appears.scrobbles, - }; - - try appears_view.append(appears_table_row); - } - - //appears_jq_result.drain(); - - const first_last_songs_query = - \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date - \\FROM albumsongs - \\INNER JOIN songs ON songs.id = albumsongs.song_id - \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\WHERE albumsongsartists.artist_id = $1 - \\ORDER BY scrobbles.datetime DESC - \\LIMIT 1) - \\ - \\UNION ALL - \\ - \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date - \\FROM albumsongs - \\INNER JOIN songs ON songs.id = albumsongs.song_id - \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\WHERE albumsongsartists.artist_id = $1 - \\ORDER BY scrobbles.datetime ASC - \\LIMIT 1) - ; - - var first_last_songs_jq_result = try request.repo.executeSql(first_last_songs_query, .{id}); - defer first_last_songs_jq_result.deinit(); - - // These look backwards, but it's correct this way - var last_song_view = try root.put("last", .object); - - if (try first_last_songs_jq_result.postgresql.result.next()) |last_song_row| { - const last_song = try last_song_row.to(ScrobbleResult, .{ .dupe = true, .allocator = request.allocator }); - try last_song_view.put("name", last_song.name); - try last_song_view.put("id", last_song.id); - try last_song_view.put("date", (try dateFmt(request.allocator, last_song.date))); - } else unreachable; - - var first_song_view = try root.put("first", .object); - - if (try first_last_songs_jq_result.postgresql.result.next()) |first_song_row| { - const first_song = try first_song_row.to(ScrobbleResult, .{ .dupe = true, .allocator = request.allocator }); - try first_song_view.put("name", first_song.name); - try first_song_view.put("id", first_song.id); - try first_song_view.put("date", (try dateFmt(request.allocator, first_song.date))); - } else unreachable; - - try first_last_songs_jq_result.drain(); + const timescale = try queries.entityQueryResult(request, queries.generateQuery(.artist, .timescale), .{id}, .array); + try root.put("yearly", timescale); return request.render(.ok); } diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index f91b5ed..d5a7944 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -1,51 +1,13 @@ const std = @import("std"); const jetzig = @import("jetzig"); -const TableRow = @import("../../types.zig").TableRow; -const HyperlinkData = @import("../../types.zig").HyperlinkData; +const queries = @import("../../queries.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var songs_view = try root.put("songs", .array); - const query = - \\SELECT songs.name, songs.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongs - \\INNER JOIN songs ON albumsongs.song_id = songs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id - \\GROUP BY songs.id, artists.id - \\ORDER BY scrobbles DESC, songs.name ASC - ; + const songs = try queries.entityQueryResult(request, queries.generateQuery(.song, .entities), .{}, .array); + try root.put("songs", songs); - var songs_js_result = try request.repo.executeSql(query, .{}); - defer songs_js_result.deinit(); - - const Song = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; - - var prev_song_id: ?i32 = null; - - var row: ?TableRow = null; - var artistlist = std.ArrayList(HyperlinkData).init(request.allocator); - - blk: while (try songs_js_result.postgresql.result.next()) |song_row| { - const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator }); - if (song.id == prev_song_id) { - try artistlist.append(.{ .name = song.artist_name, .id = song.artist_id }); - continue :blk; - } else { - try artistlist.append(.{ .name = song.artist_name, .id = song.artist_id }); - - row = TableRow{ - .song = .{ .name = song.name, .id = song.id }, - .artistlist = try artistlist.toOwnedSlice(), - .scrobbles = song.scrobbles, - }; - - try songs_view.append(row); - } - prev_song_id = song.id; - } return request.render(.ok); } diff --git a/src/queries.zig b/src/queries.zig new file mode 100644 index 0000000..cc5137b --- /dev/null +++ b/src/queries.zig @@ -0,0 +1,343 @@ +// Probably the worst code in the project +const jetzig = @import("jetzig"); +const TableRow = @import("types.zig").TableRow; +const HyperlinkData = @import("types.zig").HyperlinkData; +const std = @import("std"); + +pub fn entityQueryResult(request: *jetzig.Request, query: GeneratedQuery, args: anytype, root_type: enum { object, array }) !*jetzig.Data.Value { + var result = try request.repo.executeSql(query.query, args); + + defer result.deinit(); + + var Data = jetzig.Data.init(request.allocator); + var out: *jetzig.Data.Value = switch (root_type) { + .array => try Data.array(), + .object => try Data.object(), + }; + + var artist_list = if (query.ResultType == EntitiesAlbumResult or query.ResultType == EntitiesArtistResult or query.ResultType == EntitiesScrobbleResult or query.ResultType == EntitiesSongResult) + std.ArrayList(HyperlinkData).init(request.allocator); + + blk: while (try result.postgresql.result.next()) |entity_row| { + switch (query.ResultType) { + FirstlastResult, TimescaleResult, EntitiesScrobbleResult, EntitiesSongResult, EntitiesAlbumResult, EntitiesArtistResult, EntityItemsResult, AppearsResult, EntityInfoResult => |T| { + const entity = try entity_row.to(T, .{ .dupe = true, .allocator = request.allocator }); + const item: ?TableRow = switch (query.ResultType) { + EntitiesScrobbleResult, EntitiesSongResult, EntitiesAlbumResult, EntitiesArtistResult => switch (query.entity) { + .artist => TableRow{ .artist = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + .album => album_entities: { + const last_artist = artist_list.getLastOrNull(); + try artist_list.append(.{ .name = entity.artist_name, .id = entity.artist_id }); + if (last_artist) |la| { + if (la.id == entity.artist_id) continue :blk; + } + break :album_entities TableRow{ .album = .{ .name = entity.name, .id = entity.id }, .artistlist = try artist_list.toOwnedSlice(), .scrobbles = entity.scrobbles }; + }, + .song => song_entities: { + const last_artist = artist_list.getLastOrNull(); + try artist_list.append(.{ .name = entity.artist_name, .id = entity.artist_id }); + if (last_artist) |la| { + if (la.id == entity.artist_id) continue :blk; + } + break :song_entities TableRow{ .song = .{ .name = entity.name, .id = entity.id }, .artistlist = try artist_list.toOwnedSlice(), .scrobbles = entity.scrobbles }; + }, + else => unreachable, + }, + EntityItemsResult => switch (query.entity) { + .artist => TableRow{ .album = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + .album => TableRow{ .song = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + else => unreachable, + }, + AppearsResult => switch (query.entity) { + .song, .artist => TableRow{ .album = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + .album, .scrobble => unreachable, + }, + else => null, + }; + + if (item) |itm| { + switch (root_type) { + .array => try out.append(itm), + .object => switch (query.query_type) { + inline else => |qt| try out.put(@tagName(qt), itm), + }, + } + } else { + switch (root_type) { + .array => try out.append(entity), + .object => switch (query.query_type) { + inline else => |qt| try out.put(@tagName(qt), entity), + }, + } + } + }, + else => unreachable, + } + } + if (root_type == .object) {} + return out; +} + +const GeneratedQuery = struct { + entity: EntityType, + query_type: QueryTypeEnum, + ResultType: type, + query: []const u8, +}; + +const EntityType = enum { scrobble, song, album, artist }; +const QueryTypeEnum = enum { firstlast, timescale, entities, entity_items, appears, entity_info }; +const FirstlastResult = struct { name: []const u8, id: i32, date: []const u8 }; +const TimescaleResult = struct { year: []const u8, scrobbles: i64 }; +const EntitiesArtistResult = struct { name: []const u8, id: i32, scrobbles: i64 }; +const EntitiesAlbumResult = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; +const EntitiesSongResult = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; +const EntitiesScrobbleResult = struct { song_name: []const u8, song_id: i32, album_name: []const u8, album_id: i32, artist_name: []const u8, artist_id: i32, s_id: i32, date: i64 }; +const EntityItemsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; +const AppearsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; +const EntityInfoResult = struct { name: []const u8, id: i32, scrobbles: i64, rank: []const u8 }; + +pub fn generateQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery { + return GeneratedQuery{ + .entity = entity, + .query_type = query_type, + .ResultType = switch (query_type) { + .firstlast => FirstlastResult, + .timescale => TimescaleResult, + .entities => switch (entity) { + .scrobble => EntitiesScrobbleResult, + .song => EntitiesSongResult, + .album => EntitiesAlbumResult, + .artist => EntitiesArtistResult, + }, + .entity_items => EntityItemsResult, + .appears => AppearsResult, + .entity_info => EntityInfoResult, + }, + .query = switch (query_type) { + .firstlast => + //.ResultType = FirstlastResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime ASC + \\LIMIT 1) + \\ + \\UNION ALL + \\ + \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime DESC + \\LIMIT 1) + , + + .album => + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\WHERE albums.id = $1 + \\ORDER BY scrobbles.datetime ASC + \\LIMIT 1) + \\ + \\UNION ALL + \\ + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\WHERE albums.id = $1 + \\ORDER BY scrobbles.datetime DESC + \\LIMIT 1) + , + + .artist => + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime ASC + \\LIMIT 1) + \\ + \\UNION ALL + \\ + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime DESC + \\LIMIT 1) + , + }, + + .timescale => + //.ResultType = TimescaleResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\SELECT TO_CHAR(date_trunc('year', datetime), 'YYYY') AS year, COUNT(*) as scrobbles + \\FROM scrobbles + \\INNER JOIN albumsongs ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id + \\WHERE artists.id = $1 + \\GROUP BY year + \\ORDER BY year ASC; + , + + .album => + \\SELECT TO_CHAR(date_trunc('year', datetime), 'YYYY') AS year, COUNT(*) as scrobbles + \\FROM scrobbles + \\INNER JOIN albumsongs ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\WHERE albums.id = $1 + \\GROUP BY year + \\ORDER BY year ASC; + , + + .artist => + \\SELECT TO_CHAR(date_trunc('year', datetime), 'YYYY') AS year, COUNT(*) as scrobbles + \\FROM scrobbles + \\INNER JOIN albumsongs ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id + \\WHERE artists.id = $1 + \\GROUP BY year + \\ORDER BY year ASC; + , + }, + + .entities => + //.ResultType = EntitiesResult, + switch (entity) { + .scrobble => + \\none + , + .song => + \\SELECT songs.name, songs.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN songs ON albumsongs.song_id = songs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id + \\GROUP BY songs.id, artists.id + \\ORDER BY scrobbles DESC, songs.name ASC + , + .album => + \\SELECT albums.name, albums.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN albums ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN artistalbums ON artistalbums.album_id = albums.id + \\INNER JOIN artists ON artists.id = artistalbums.artist_id + \\GROUP BY albums.id, artists.id + \\ORDER BY scrobbles DESC + , + .artist => + \\SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongsartists + \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id + \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\GROUP BY artists.id + \\ORDER BY scrobbles DESC; + , + }, + + .appears => + //.ResultType = AppearsResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\ NOTHING YET + , + .album => + \\nope + , + .artist => + \\SELECT albums.name AS name, albums.id AS 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 albumsongsartists.artist_id = $1 AND artistalbums.artist_id != $1 + \\GROUP BY albums.id + \\ORDER BY scrobbles DESC; + , + }, + + .entity_items => + //.ResultType = EntityItemsResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\get all scrobbles + , + .album => + \\SELECT songs.name, songs.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN songs ON albumsongs.song_id = songs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE albumsongs.album_id = $1 + \\GROUP BY songs.id + \\ORDER BY scrobbles DESC + , + .artist => + \\SELECT albums.name AS name, albums.id AS 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 + \\WHERE artistalbums.artist_id = $1 + \\GROUP BY albums.id + \\ORDER BY scrobbles DESC + , + }, + + .entity_info => switch (entity) { + .scrobble => unreachable, + .song => + \\lol idk + , + .album => + \\SELECT * FROM + \\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM9999th') AS rank FROM + \\(SELECT albums.name, albums.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN albums ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\GROUP BY albums.id)) + \\WHERE id = $1 + , + .artist => + \\SELECT * FROM + \\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM9999th') AS rank FROM + \\(SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongsartists + \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id + \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\GROUP BY artists.id)) + \\WHERE id = $1 + , + }, + }, + }; +}