feat(db): add paged + count queries for library reads

This commit is contained in:
2026-04-21 00:01:15 -04:00
parent 62e48642e2
commit fb49a39492
6 changed files with 136 additions and 0 deletions
+11
View File
@@ -11,6 +11,17 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const countAlbumsMatching = `-- name: CountAlbumsMatching :one
SELECT COUNT(*) FROM albums WHERE title ILIKE '%' || $1::text || '%'
`
func (q *Queries) CountAlbumsMatching(ctx context.Context, dollar_1 string) (int64, error) {
row := q.db.QueryRow(ctx, countAlbumsMatching, dollar_1)
var count int64
err := row.Scan(&count)
return count, err
}
const getAlbumByArtistAndTitle = `-- name: GetAlbumByArtistAndTitle :one
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums WHERE artist_id = $1 AND title = $2 LIMIT 1
`
+95
View File
@@ -11,6 +11,28 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const countArtists = `-- name: CountArtists :one
SELECT COUNT(*) FROM artists
`
func (q *Queries) CountArtists(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, countArtists)
var count int64
err := row.Scan(&count)
return count, err
}
const countArtistsMatching = `-- name: CountArtistsMatching :one
SELECT COUNT(*) FROM artists WHERE name ILIKE '%' || $1::text || '%'
`
func (q *Queries) CountArtistsMatching(ctx context.Context, dollar_1 string) (int64, error) {
row := q.db.QueryRow(ctx, countArtistsMatching, dollar_1)
var count int64
err := row.Scan(&count)
return count, err
}
const getArtistByID = `-- name: GetArtistByID :one
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists WHERE id = $1
`
@@ -78,6 +100,79 @@ func (q *Queries) ListArtists(ctx context.Context) ([]Artist, error) {
return items, nil
}
const listArtistsAlpha = `-- name: ListArtistsAlpha :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists ORDER BY sort_name, name, id LIMIT $1 OFFSET $2
`
type ListArtistsAlphaParams struct {
Limit int32
Offset int32
}
func (q *Queries) ListArtistsAlpha(ctx context.Context, arg ListArtistsAlphaParams) ([]Artist, error) {
rows, err := q.db.Query(ctx, listArtistsAlpha, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Artist
for rows.Next() {
var i Artist
if err := rows.Scan(
&i.ID,
&i.Name,
&i.SortName,
&i.Mbid,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listArtistsNewest = `-- name: ListArtistsNewest :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists ORDER BY created_at DESC, id DESC LIMIT $1 OFFSET $2
`
type ListArtistsNewestParams struct {
Limit int32
Offset int32
}
// Secondary id tiebreaker keeps pagination stable when created_at ties.
func (q *Queries) ListArtistsNewest(ctx context.Context, arg ListArtistsNewestParams) ([]Artist, error) {
rows, err := q.db.Query(ctx, listArtistsNewest, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Artist
for rows.Next() {
var i Artist
if err := rows.Scan(
&i.ID,
&i.Name,
&i.SortName,
&i.Mbid,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const searchArtists = `-- name: SearchArtists :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists
WHERE name ILIKE '%' || $1 || '%'
+11
View File
@@ -22,6 +22,17 @@ func (q *Queries) CountTracksByAlbum(ctx context.Context, albumID pgtype.UUID) (
return count, err
}
const countTracksMatching = `-- name: CountTracksMatching :one
SELECT COUNT(*) FROM tracks WHERE title ILIKE '%' || $1::text || '%'
`
func (q *Queries) CountTracksMatching(ctx context.Context, dollar_1 string) (int64, error) {
row := q.db.QueryRow(ctx, countTracksMatching, dollar_1)
var count int64
err := row.Scan(&count)
return count, err
}
const getTrackByID = `-- name: GetTrackByID :one
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at FROM tracks WHERE id = $1
`
+3
View File
@@ -53,3 +53,6 @@ SELECT * FROM albums
WHERE title ILIKE '%' || $1 || '%'
ORDER BY sort_title
LIMIT $2 OFFSET $3;
-- name: CountAlbumsMatching :one
SELECT COUNT(*) FROM albums WHERE title ILIKE '%' || $1::text || '%';
+13
View File
@@ -24,3 +24,16 @@ SELECT * FROM artists
WHERE name ILIKE '%' || $1 || '%'
ORDER BY sort_name
LIMIT $2 OFFSET $3;
-- name: ListArtistsAlpha :many
SELECT * FROM artists ORDER BY sort_name, name, id LIMIT $1 OFFSET $2;
-- name: ListArtistsNewest :many
-- Secondary id tiebreaker keeps pagination stable when created_at ties.
SELECT * FROM artists ORDER BY created_at DESC, id DESC LIMIT $1 OFFSET $2;
-- name: CountArtists :one
SELECT COUNT(*) FROM artists;
-- name: CountArtistsMatching :one
SELECT COUNT(*) FROM artists WHERE name ILIKE '%' || $1::text || '%';
+3
View File
@@ -36,3 +36,6 @@ SELECT * FROM tracks
WHERE title ILIKE '%' || $1 || '%'
ORDER BY title
LIMIT $2 OFFSET $3;
-- name: CountTracksMatching :one
SELECT COUNT(*) FROM tracks WHERE title ILIKE '%' || $1::text || '%';