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?
This commit is contained in:
parent
09f542e26e
commit
7f3778e82f
4 changed files with 370 additions and 253 deletions
343
src/queries.zig
Normal file
343
src/queries.zig
Normal file
|
|
@ -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
|
||||
,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue