Implement db searches using raw sql
This commit is contained in:
parent
09d4453665
commit
27358fe217
4 changed files with 226 additions and 109 deletions
|
|
@ -5,60 +5,91 @@ const jetquery = @import("jetzig").jetquery;
|
|||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var albums_view = try root.put("albums", .array);
|
||||
const albums = try jetzig.database.Query(.Album)
|
||||
.select(.{ .id, .name })
|
||||
.include(.albumartists, .{ .select = .{.artist_id} })
|
||||
.include(.scrobbles, .{ .select = .{.id} })
|
||||
.orderBy(.{ .name = .asc })
|
||||
.all(request.repo);
|
||||
//const albums = try request.repo.all(query);
|
||||
//const albums = try jetzig.database.Query(.Album)
|
||||
// .select(.{ .id, .name })
|
||||
// .include(.albumartists, .{ .select = .{.artist_id} })
|
||||
// .include(.scrobbles, .{ .select = .{.id} })
|
||||
// .orderBy(.{ .name = .asc })
|
||||
// .all(request.repo);
|
||||
////const albums = try request.repo.all(query);
|
||||
|
||||
for (albums) |album| {
|
||||
const query =
|
||||
\\SELECT albums.name, albums.id, COUNT(scrobbles) AS scrobbles
|
||||
\\FROM albumsongs
|
||||
\\INNER JOIN albums ON albumsongs.album_id = albums.id
|
||||
\\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong
|
||||
\\GROUP BY albums.id
|
||||
\\ORDER BY scrobbles DESC
|
||||
;
|
||||
|
||||
var albums_jq_result = try request.repo.executeSql(query, .{});
|
||||
defer albums_jq_result.deinit();
|
||||
|
||||
const Album = struct { name: []const u8, id: i32, scrobbles: i64 };
|
||||
|
||||
while (try albums_jq_result.postgresql.result.next()) |album_row| {
|
||||
const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator });
|
||||
var album_view = try albums_view.append(.object);
|
||||
|
||||
var artist_infos = try album_view.put("artist_info", .array);
|
||||
for (album.albumartists) |artist| {
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
const artist_data = try jetzig.database.Query(.Artist)
|
||||
.find(artist.artist_id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
try artist_info.put("name", artist_data.?.name);
|
||||
try artist_info.put("id", artist_data.?.id);
|
||||
}
|
||||
|
||||
try album_view.put("name", album.name);
|
||||
try album_view.put("url", album.id);
|
||||
try album_view.put("scrobbles", (album.scrobbles).len);
|
||||
try album_view.put("scrobbles", album.scrobbles);
|
||||
}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||
const album = try jetzig.database.Query(.Album)
|
||||
.find(id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
var root = try request.data(.object);
|
||||
try root.put("album", album.?.name);
|
||||
var songs_view = try root.put("songs", .array);
|
||||
const query = jetzig.database.Query(.Albumsong)
|
||||
.select(.{.id})
|
||||
.include(.song, .{ .select = .{ .name, .id } })
|
||||
.join(.inner, .album)
|
||||
.where(.{ .album = .{ .id = id } });
|
||||
const album_name = try jetzig.database.Query(.Album).find(id).select(.{ .id, .name }).execute(request.repo);
|
||||
_ = try root.put("album", album_name.?.name);
|
||||
|
||||
const songs = try request.repo.all(query);
|
||||
for (songs) |song| {
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
.where(.{ .song_id = song.song.id })
|
||||
.count()
|
||||
.execute(request.repo);
|
||||
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
|
||||
;
|
||||
|
||||
var songs_js_result = try request.repo.executeSql(query, .{id});
|
||||
defer songs_js_result.deinit();
|
||||
|
||||
const Song = struct { name: []const u8, id: i32, scrobbles: i64 };
|
||||
|
||||
while (try songs_js_result.postgresql.result.next()) |song_row| {
|
||||
const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator });
|
||||
var song_view = try songs_view.append(.object);
|
||||
try song_view.put("name", song.song.name);
|
||||
try song_view.put("url", song.song.id);
|
||||
try song_view.put("scrobbles", scrobbles);
|
||||
try song_view.put("name", song.name);
|
||||
try song_view.put("url", song.id);
|
||||
try song_view.put("scrobbles", song.scrobbles);
|
||||
}
|
||||
|
||||
//const album = try jetzig.database.Query(.Album)
|
||||
// .find(id)
|
||||
// .select(.{ .id, .name })
|
||||
// .execute(request.repo);
|
||||
//var root = try request.data(.object);
|
||||
//try root.put("album", album.?.name);
|
||||
//var songs_view = try root.put("songs", .array);
|
||||
//const query = jetzig.database.Query(.Albumsong)
|
||||
// .select(.{.id})
|
||||
// .include(.song, .{ .select = .{ .name, .id } })
|
||||
// .join(.inner, .album)
|
||||
// .where(.{ .album = .{ .id = id } });
|
||||
|
||||
//const songs = try request.repo.all(query);
|
||||
//for (songs) |song| {
|
||||
// const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
// .where(.{ .song_id = song.song.id })
|
||||
// .count()
|
||||
// .execute(request.repo);
|
||||
// var song_view = try songs_view.append(.object);
|
||||
// try song_view.put("name", song.song.name);
|
||||
// try song_view.put("url", song.song.id);
|
||||
// try song_view.put("scrobbles", scrobbles);
|
||||
//}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,46 +5,85 @@ const jetquery = @import("jetzig").jetquery;
|
|||
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 jetzig.database.Query(.Artist)
|
||||
.select(.{ .id, .name })
|
||||
.include(.scrobbleartists, .{ .select = .{.id} })
|
||||
.orderBy(.{ .name = .asc })
|
||||
.all(request.repo);
|
||||
for (artists) |artist| {
|
||||
|
||||
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 });
|
||||
var artist_view = try artists_view.append(.object);
|
||||
try artist_view.put("name", artist.name);
|
||||
try artist_view.put("url", artist.id);
|
||||
try artist_view.put("scrobbles", (artist.scrobbleartists).len);
|
||||
try artist_view.put("scrobbles", artist.scrobbles);
|
||||
}
|
||||
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||
const artist = try jetzig.database.Query(.Artist)
|
||||
.find(id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
var root = try request.data(.object);
|
||||
try root.put("artist", artist.?.name);
|
||||
var albums_view = try root.put("albums", .array);
|
||||
const query = jetzig.database.Query(.Albumartist)
|
||||
.select(.{.id})
|
||||
.include(.album, .{ .select = .{ .name, .id } })
|
||||
.join(.inner, .artist)
|
||||
.where(.{ .artist = .{ .id = id } });
|
||||
const artist_name = try jetzig.database.Query(.Artist).find(id).select(.{ .id, .name }).execute(request.repo);
|
||||
_ = try root.put("artist", artist_name.?.name);
|
||||
const query =
|
||||
\\SELECT albums.name, albums.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
|
||||
;
|
||||
|
||||
const albums = try request.repo.all(query);
|
||||
for (albums) |album| {
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
.where(.{ .album_id = album.album.id })
|
||||
.count()
|
||||
.execute(request.repo);
|
||||
var albums_jq_result = try request.repo.executeSql(query, .{id});
|
||||
defer albums_jq_result.deinit();
|
||||
|
||||
const Album = struct { name: []const u8, id: i32, scrobbles: i64 };
|
||||
|
||||
while (try albums_jq_result.postgresql.result.next()) |album_row| {
|
||||
const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator });
|
||||
var album_view = try albums_view.append(.object);
|
||||
try album_view.put("name", album.album.name);
|
||||
try album_view.put("url", album.album.id);
|
||||
try album_view.put("scrobbles", scrobbles);
|
||||
try album_view.put("name", album.name);
|
||||
try album_view.put("url", album.id);
|
||||
try album_view.put("scrobbles", album.scrobbles);
|
||||
}
|
||||
//const artist = try jetzig.database.Query(.Artist)
|
||||
// .find(id)
|
||||
// .select(.{ .id, .name })
|
||||
// .execute(request.repo);
|
||||
//var root = try request.data(.object);
|
||||
//try root.put("artist", artist.?.name);
|
||||
//var albums_view = try root.put("albums", .array);
|
||||
//const query = jetzig.database.Query(.Albumartist)
|
||||
// .select(.{.id})
|
||||
// .include(.album, .{ .select = .{ .name, .id } })
|
||||
// .join(.inner, .artist)
|
||||
// .where(.{ .artist = .{ .id = id } });
|
||||
|
||||
//const albums = try request.repo.all(query);
|
||||
//for (albums) |album| {
|
||||
// const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
// .where(.{ .album_id = album.album.id })
|
||||
// .count()
|
||||
// .execute(request.repo);
|
||||
// var album_view = try albums_view.append(.object);
|
||||
// try album_view.put("name", album.album.name);
|
||||
// try album_view.put("url", album.album.id);
|
||||
// try album_view.put("scrobbles", scrobbles);
|
||||
//}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,33 +4,58 @@ const jetzig = @import("jetzig");
|
|||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var scrobbles_view = try root.put("scrobbles", .array);
|
||||
const query = jetzig.database.Query(.Scrobble)
|
||||
.select(.{ .id, .date })
|
||||
.include(.song, .{ .select = .{ .id, .name } })
|
||||
.include(.album, .{ .select = .{ .id, .name } })
|
||||
.include(.scrobbleartists, .{ .select = .{.artist_id} })
|
||||
.orderBy(.{ .date = .desc });
|
||||
const scrobbles = try request.repo.all(query);
|
||||
for (scrobbles) |scrobble| {
|
||||
//const query = jetzig.database.Query(.Scrobble)
|
||||
// .select(.{ .id, .date })
|
||||
// .include(.song, .{ .select = .{ .id, .name } })
|
||||
// .include(.album, .{ .select = .{ .id, .name } })
|
||||
// .include(.scrobbleartists, .{ .select = .{.artist_id} })
|
||||
// .orderBy(.{ .date = .desc });
|
||||
//const scrobbles = try request.repo.all(query);
|
||||
|
||||
const query =
|
||||
\\SELECT songs.name, songs.id, albums.name, albums.id, scrobbles.datetime
|
||||
\\FROM albumsongs
|
||||
\\INNER JOIN songs ON songs.id = albumsongs.song_id
|
||||
\\INNER JOIN albums ON albums.id = albumsongs.album_id
|
||||
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
|
||||
\\ORDER BY scrobbles.datetime ASC
|
||||
;
|
||||
|
||||
var scrobbles_js_result = try request.repo.executeSql(query, .{});
|
||||
defer scrobbles_js_result.deinit();
|
||||
|
||||
const Scrobble = struct { song_name: []const u8, song_id: i32, album_name: []const u8, album_id: i32, date: i64 };
|
||||
|
||||
while (try scrobbles_js_result.postgresql.result.next()) |scrobble_row| {
|
||||
const scrobble = try scrobble_row.to(Scrobble, .{ .dupe = true, .allocator = request.allocator });
|
||||
var scrobble_view = try scrobbles_view.append(.object);
|
||||
|
||||
var artist_infos = try scrobble_view.put("artist_info", .array);
|
||||
for (scrobble.scrobbleartists) |artist| {
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
const artist_data = try jetzig.database.Query(.Artist)
|
||||
.find(artist.artist_id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
try artist_info.put("name", artist_data.?.name);
|
||||
try artist_info.put("id", artist_data.?.id);
|
||||
}
|
||||
|
||||
try scrobble_view.put("song_name", scrobble.song.name);
|
||||
try scrobble_view.put("song_id", scrobble.song.id);
|
||||
try scrobble_view.put("album_name", scrobble.album.name);
|
||||
try scrobble_view.put("album_id", scrobble.album.id);
|
||||
try scrobble_view.put("song_name", scrobble.song_name);
|
||||
try scrobble_view.put("song_id", scrobble.song_id);
|
||||
try scrobble_view.put("album_name", scrobble.album_name);
|
||||
try scrobble_view.put("album_id", scrobble.album_id);
|
||||
try scrobble_view.put("date", scrobble.date);
|
||||
}
|
||||
|
||||
//for (scrobbles) |scrobble| {
|
||||
// var scrobble_view = try scrobbles_view.append(.object);
|
||||
|
||||
// var artist_infos = try scrobble_view.put("artist_info", .array);
|
||||
// for (scrobble.scrobbleartists) |artist| {
|
||||
// var artist_info = try artist_infos.append(.object);
|
||||
// const artist_data = try jetzig.database.Query(.Artist)
|
||||
// .find(artist.artist_id)
|
||||
// .select(.{ .id, .name })
|
||||
// .execute(request.repo);
|
||||
// try artist_info.put("name", artist_data.?.name);
|
||||
// try artist_info.put("id", artist_data.?.id);
|
||||
// }
|
||||
|
||||
// try scrobble_view.put("song_name", scrobble.song.name);
|
||||
// try scrobble_view.put("song_id", scrobble.song.id);
|
||||
// try scrobble_view.put("album_name", scrobble.album.name);
|
||||
// try scrobble_view.put("album_id", scrobble.album.id);
|
||||
// try scrobble_view.put("date", scrobble.date);
|
||||
//}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,30 +4,52 @@ const jetzig = @import("jetzig");
|
|||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var songs_view = try root.put("songs", .array);
|
||||
const songs = try jetzig.database.Query(.Song)
|
||||
.select(.{ .id, .name })
|
||||
.include(.songartists, .{ .select = .{.artist_id} })
|
||||
.include(.scrobbles, .{ .select = .{.id} })
|
||||
.orderBy(.{ .name = .asc })
|
||||
.all(request.repo);
|
||||
//const songs = try jetzig.database.Query(.Song)
|
||||
// .select(.{ .id, .name })
|
||||
// .include(.songartists, .{ .select = .{.artist_id} })
|
||||
// .include(.scrobbles, .{ .select = .{.id} })
|
||||
// .orderBy(.{ .name = .asc })
|
||||
// .all(request.repo);
|
||||
|
||||
for (songs) |song| {
|
||||
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
|
||||
\\GROUP BY songs.id
|
||||
\\ORDER BY scrobbles DESC
|
||||
;
|
||||
|
||||
var songs_js_result = try request.repo.executeSql(query, .{});
|
||||
defer songs_js_result.deinit();
|
||||
|
||||
const Song = struct { name: []const u8, id: i32, scrobbles: i64 };
|
||||
|
||||
while (try songs_js_result.postgresql.result.next()) |song_row| {
|
||||
const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator });
|
||||
var song_view = try songs_view.append(.object);
|
||||
|
||||
var artist_infos = try song_view.put("artist_info", .array);
|
||||
for (song.songartists) |artist| {
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
const artist_data = try jetzig.database.Query(.Artist)
|
||||
.find(artist.artist_id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
try artist_info.put("name", artist_data.?.name);
|
||||
try artist_info.put("id", artist_data.?.id);
|
||||
}
|
||||
try song_view.put("name", song.name);
|
||||
try song_view.put("url", song.id);
|
||||
try song_view.put("scrobbles", (song.scrobbles).len);
|
||||
try song_view.put("scrobbles", song.scrobbles);
|
||||
}
|
||||
|
||||
//for (songs) |song| {
|
||||
// var song_view = try songs_view.append(.object);
|
||||
|
||||
// var artist_infos = try song_view.put("artist_info", .array);
|
||||
// for (song.songartists) |artist| {
|
||||
// var artist_info = try artist_infos.append(.object);
|
||||
// const artist_data = try jetzig.database.Query(.Artist)
|
||||
// .find(artist.artist_id)
|
||||
// .select(.{ .id, .name })
|
||||
// .execute(request.repo);
|
||||
// try artist_info.put("name", artist_data.?.name);
|
||||
// try artist_info.put("id", artist_data.?.id);
|
||||
// }
|
||||
// try song_view.put("name", song.name);
|
||||
// try song_view.put("url", song.id);
|
||||
// try song_view.put("scrobbles", (song.scrobbles).len);
|
||||
//}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue