replace sql mostly with sqlc

This commit is contained in:
2025-07-16 22:10:49 +02:00
parent a3440326e8
commit 7c29f64f6d
14 changed files with 418 additions and 741 deletions

View File

@@ -10,54 +10,6 @@ import (
"database/sql"
)
const getArtists = `-- name: GetArtists :many
SELECT Artist, COUNT(Artist) AS count
FROM Beatmap
WHERE Artist LIKE ? OR Title LIKE ?
GROUP BY Artist
LIMIT ? OFFSET ?
`
type GetArtistsParams struct {
Artist sql.NullString `json:"artist"`
Title sql.NullString `json:"title"`
Limit int64 `json:"limit"`
Offset int64 `json:"offset"`
}
type GetArtistsRow struct {
Artist sql.NullString `json:"artist"`
Count int64 `json:"count"`
}
func (q *Queries) GetArtists(ctx context.Context, arg GetArtistsParams) ([]GetArtistsRow, error) {
rows, err := q.db.QueryContext(ctx, getArtists,
arg.Artist,
arg.Title,
arg.Limit,
arg.Offset,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetArtistsRow{}
for rows.Next() {
var i GetArtistsRow
if err := rows.Scan(&i.Artist, &i.Count); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getBeatmapByHash = `-- name: GetBeatmapByHash :one
SELECT beatmapid, artist, artistunicode, title, titleunicode, creator, difficulty, audio, md5hash, file, rankedstatus, lastmodifiedtime, totaltime, audiopreviewtime, beatmapsetid, source, tags, lastplayed, folder FROM Beatmap WHERE MD5Hash = ?
`
@@ -100,8 +52,19 @@ func (q *Queries) GetBeatmapCount(ctx context.Context) (int64, error) {
return count, err
}
const getBeatmapSetCount = `-- name: GetBeatmapSetCount :one
SELECT COUNT(*) FROM Beatmap GROUP BY BeatmapSetId
`
func (q *Queries) GetBeatmapSetCount(ctx context.Context) (int64, error) {
row := q.db.QueryRowContext(ctx, getBeatmapSetCount)
var count int64
err := row.Scan(&count)
return count, err
}
const getRecentBeatmaps = `-- name: GetRecentBeatmaps :many
SELECT BeatmapId, MD5Hash, Title, Artist, Creator, Folder, File, Audio, TotalTime
SELECT beatmapid, artist, artistunicode, title, titleunicode, creator, difficulty, audio, md5hash, file, rankedstatus, lastmodifiedtime, totaltime, audiopreviewtime, beatmapsetid, source, tags, lastplayed, folder
FROM Beatmap GROUP BY Folder ORDER BY LastModifiedTime DESC LIMIT ? OFFSET ?
`
@@ -110,37 +73,35 @@ type GetRecentBeatmapsParams struct {
Offset int64 `json:"offset"`
}
type GetRecentBeatmapsRow struct {
Beatmapid sql.NullInt64 `json:"beatmapid"`
Md5hash sql.NullString `json:"md5hash"`
Title sql.NullString `json:"title"`
Artist sql.NullString `json:"artist"`
Creator sql.NullString `json:"creator"`
Folder sql.NullString `json:"folder"`
File sql.NullString `json:"file"`
Audio sql.NullString `json:"audio"`
Totaltime sql.NullInt64 `json:"totaltime"`
}
func (q *Queries) GetRecentBeatmaps(ctx context.Context, arg GetRecentBeatmapsParams) ([]GetRecentBeatmapsRow, error) {
func (q *Queries) GetRecentBeatmaps(ctx context.Context, arg GetRecentBeatmapsParams) ([]Beatmap, error) {
rows, err := q.db.QueryContext(ctx, getRecentBeatmaps, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetRecentBeatmapsRow{}
items := []Beatmap{}
for rows.Next() {
var i GetRecentBeatmapsRow
var i Beatmap
if err := rows.Scan(
&i.Beatmapid,
&i.Md5hash,
&i.Title,
&i.Artist,
&i.Artistunicode,
&i.Title,
&i.Titleunicode,
&i.Creator,
&i.Folder,
&i.File,
&i.Difficulty,
&i.Audio,
&i.Md5hash,
&i.File,
&i.Rankedstatus,
&i.Lastmodifiedtime,
&i.Totaltime,
&i.Audiopreviewtime,
&i.Beatmapsetid,
&i.Source,
&i.Tags,
&i.Lastplayed,
&i.Folder,
); err != nil {
return nil, err
}
@@ -176,13 +137,13 @@ type InsertBeatmapParams struct {
Md5hash sql.NullString `json:"md5hash"`
File sql.NullString `json:"file"`
Rankedstatus sql.NullString `json:"rankedstatus"`
Lastmodifiedtime sql.NullTime `json:"lastmodifiedtime"`
Lastmodifiedtime sql.NullInt64 `json:"lastmodifiedtime"`
Totaltime sql.NullInt64 `json:"totaltime"`
Audiopreviewtime sql.NullInt64 `json:"audiopreviewtime"`
Beatmapsetid sql.NullInt64 `json:"beatmapsetid"`
Source sql.NullString `json:"source"`
Tags sql.NullString `json:"tags"`
Lastplayed sql.NullTime `json:"lastplayed"`
Lastplayed sql.NullInt64 `json:"lastplayed"`
Folder sql.NullString `json:"folder"`
}
@@ -211,8 +172,56 @@ func (q *Queries) InsertBeatmap(ctx context.Context, arg InsertBeatmapParams) er
return err
}
const searchArtists = `-- name: SearchArtists :many
SELECT Artist, COUNT(Artist) AS count
FROM Beatmap
WHERE Artist LIKE ? OR Title LIKE ?
GROUP BY Artist
LIMIT ? OFFSET ?
`
type SearchArtistsParams struct {
Artist sql.NullString `json:"artist"`
Title sql.NullString `json:"title"`
Limit int64 `json:"limit"`
Offset int64 `json:"offset"`
}
type SearchArtistsRow struct {
Artist sql.NullString `json:"artist"`
Count int64 `json:"count"`
}
func (q *Queries) SearchArtists(ctx context.Context, arg SearchArtistsParams) ([]SearchArtistsRow, error) {
rows, err := q.db.QueryContext(ctx, searchArtists,
arg.Artist,
arg.Title,
arg.Limit,
arg.Offset,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []SearchArtistsRow{}
for rows.Next() {
var i SearchArtistsRow
if err := rows.Scan(&i.Artist, &i.Count); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const searchBeatmaps = `-- name: SearchBeatmaps :many
SELECT BeatmapId, MD5Hash, Title, Artist, Creator, Folder, File, Audio, TotalTime
SELECT beatmapid, artist, artistunicode, title, titleunicode, creator, difficulty, audio, md5hash, file, rankedstatus, lastmodifiedtime, totaltime, audiopreviewtime, beatmapsetid, source, tags, lastplayed, folder
FROM Beatmap
WHERE Title LIKE ? OR Artist LIKE ?
LIMIT ? OFFSET ?
@@ -225,19 +234,7 @@ type SearchBeatmapsParams struct {
Offset int64 `json:"offset"`
}
type SearchBeatmapsRow struct {
Beatmapid sql.NullInt64 `json:"beatmapid"`
Md5hash sql.NullString `json:"md5hash"`
Title sql.NullString `json:"title"`
Artist sql.NullString `json:"artist"`
Creator sql.NullString `json:"creator"`
Folder sql.NullString `json:"folder"`
File sql.NullString `json:"file"`
Audio sql.NullString `json:"audio"`
Totaltime sql.NullInt64 `json:"totaltime"`
}
func (q *Queries) SearchBeatmaps(ctx context.Context, arg SearchBeatmapsParams) ([]SearchBeatmapsRow, error) {
func (q *Queries) SearchBeatmaps(ctx context.Context, arg SearchBeatmapsParams) ([]Beatmap, error) {
rows, err := q.db.QueryContext(ctx, searchBeatmaps,
arg.Title,
arg.Artist,
@@ -248,19 +245,29 @@ func (q *Queries) SearchBeatmaps(ctx context.Context, arg SearchBeatmapsParams)
return nil, err
}
defer rows.Close()
items := []SearchBeatmapsRow{}
items := []Beatmap{}
for rows.Next() {
var i SearchBeatmapsRow
var i Beatmap
if err := rows.Scan(
&i.Beatmapid,
&i.Md5hash,
&i.Title,
&i.Artist,
&i.Artistunicode,
&i.Title,
&i.Titleunicode,
&i.Creator,
&i.Folder,
&i.File,
&i.Difficulty,
&i.Audio,
&i.Md5hash,
&i.File,
&i.Rankedstatus,
&i.Lastmodifiedtime,
&i.Totaltime,
&i.Audiopreviewtime,
&i.Beatmapsetid,
&i.Source,
&i.Tags,
&i.Lastplayed,
&i.Folder,
); err != nil {
return nil, err
}

View File

@@ -10,82 +10,6 @@ import (
"database/sql"
)
const getCollection = `-- name: GetCollection :many
SELECT
c.Name,
b.BeatmapId,
b.MD5Hash,
b.Title,
b.Artist,
b.Creator,
b.Folder,
b.File,
b.Audio,
b.TotalTime
FROM Collection c
JOIN Beatmap b ON c.MD5Hash = b.MD5Hash
JOIN (
SELECT DISTINCT Name
FROM Collection
ORDER BY Name
LIMIT 1 OFFSET ?
) selected_name ON c.Name = selected_name.Name
LIMIT ? OFFSET ?
`
type GetCollectionParams struct {
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
Offset_2 int64 `json:"offset_2"`
}
type GetCollectionRow struct {
Name sql.NullString `json:"name"`
Beatmapid sql.NullInt64 `json:"beatmapid"`
Md5hash sql.NullString `json:"md5hash"`
Title sql.NullString `json:"title"`
Artist sql.NullString `json:"artist"`
Creator sql.NullString `json:"creator"`
Folder sql.NullString `json:"folder"`
File sql.NullString `json:"file"`
Audio sql.NullString `json:"audio"`
Totaltime sql.NullInt64 `json:"totaltime"`
}
func (q *Queries) GetCollection(ctx context.Context, arg GetCollectionParams) ([]GetCollectionRow, error) {
rows, err := q.db.QueryContext(ctx, getCollection, arg.Offset, arg.Limit, arg.Offset_2)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetCollectionRow{}
for rows.Next() {
var i GetCollectionRow
if err := rows.Scan(
&i.Name,
&i.Beatmapid,
&i.Md5hash,
&i.Title,
&i.Artist,
&i.Creator,
&i.Folder,
&i.File,
&i.Audio,
&i.Totaltime,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCollectionByName = `-- name: GetCollectionByName :many
SELECT c.Name, b.BeatmapId, b.MD5Hash, b.Title, b.Artist, b.Creator, b.Folder, b.File, b.Audio, b.TotalTime
FROM Collection c
@@ -147,6 +71,83 @@ func (q *Queries) GetCollectionByName(ctx context.Context, arg GetCollectionByNa
return items, nil
}
const getCollectionByOffset = `-- name: GetCollectionByOffset :many
SELECT
c.Name,
b.BeatmapId,
b.MD5Hash,
b.Title,
b.Artist,
b.Creator,
b.Folder,
b.File,
b.Audio,
b.TotalTime
FROM Collection c
JOIN Beatmap b ON c.MD5Hash = b.MD5Hash
WHERE c.Name = (
SELECT Name
FROM Collection
GROUP BY Name
ORDER BY Name
LIMIT 1 OFFSET ?1
)
LIMIT ?2 OFFSET ?3
`
type GetCollectionByOffsetParams struct {
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
Offset_2 int64 `json:"offset_2"`
}
type GetCollectionByOffsetRow struct {
Name sql.NullString `json:"name"`
Beatmapid sql.NullInt64 `json:"beatmapid"`
Md5hash sql.NullString `json:"md5hash"`
Title sql.NullString `json:"title"`
Artist sql.NullString `json:"artist"`
Creator sql.NullString `json:"creator"`
Folder sql.NullString `json:"folder"`
File sql.NullString `json:"file"`
Audio sql.NullString `json:"audio"`
Totaltime sql.NullInt64 `json:"totaltime"`
}
func (q *Queries) GetCollectionByOffset(ctx context.Context, arg GetCollectionByOffsetParams) ([]GetCollectionByOffsetRow, error) {
rows, err := q.db.QueryContext(ctx, getCollectionByOffset, arg.Offset, arg.Limit, arg.Offset_2)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetCollectionByOffsetRow{}
for rows.Next() {
var i GetCollectionByOffsetRow
if err := rows.Scan(
&i.Name,
&i.Beatmapid,
&i.Md5hash,
&i.Title,
&i.Artist,
&i.Creator,
&i.Folder,
&i.File,
&i.Audio,
&i.Totaltime,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCollectionCountByName = `-- name: GetCollectionCountByName :one
SELECT COUNT(*) FROM Collection WHERE Name = ?
`
@@ -158,37 +159,55 @@ func (q *Queries) GetCollectionCountByName(ctx context.Context, name sql.NullStr
return count, err
}
const getCollections = `-- name: GetCollections :many
SELECT c.Name, COUNT(b.MD5Hash) AS Count, MIN(b.Folder) AS Folder, MIN(b.File) AS File
FROM Collection c
const insertCollection = `-- name: InsertCollection :exec
INSERT INTO Collection (Name, MD5Hash) VALUES (?, ?)
`
type InsertCollectionParams struct {
Name sql.NullString `json:"name"`
Md5hash sql.NullString `json:"md5hash"`
}
func (q *Queries) InsertCollection(ctx context.Context, arg InsertCollectionParams) error {
_, err := q.db.ExecContext(ctx, insertCollection, arg.Name, arg.Md5hash)
return err
}
const searchCollection = `-- name: SearchCollection :many
SELECT
c.Name,
COUNT(b.MD5Hash) AS Count,
b.Folder,
b.File
FROM Collection c
JOIN Beatmap b ON c.MD5Hash = b.MD5Hash
WHERE c.Name LIKE ?
GROUP BY c.Name
LIMIT ? OFFSET ?
`
type GetCollectionsParams struct {
type SearchCollectionParams struct {
Name sql.NullString `json:"name"`
Limit int64 `json:"limit"`
Offset int64 `json:"offset"`
}
type GetCollectionsRow struct {
type SearchCollectionRow struct {
Name sql.NullString `json:"name"`
Count int64 `json:"count"`
Folder interface{} `json:"folder"`
File interface{} `json:"file"`
Folder sql.NullString `json:"folder"`
File sql.NullString `json:"file"`
}
func (q *Queries) GetCollections(ctx context.Context, arg GetCollectionsParams) ([]GetCollectionsRow, error) {
rows, err := q.db.QueryContext(ctx, getCollections, arg.Name, arg.Limit, arg.Offset)
func (q *Queries) SearchCollection(ctx context.Context, arg SearchCollectionParams) ([]SearchCollectionRow, error) {
rows, err := q.db.QueryContext(ctx, searchCollection, arg.Name, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetCollectionsRow{}
items := []SearchCollectionRow{}
for rows.Next() {
var i GetCollectionsRow
var i SearchCollectionRow
if err := rows.Scan(
&i.Name,
&i.Count,
@@ -207,17 +226,3 @@ func (q *Queries) GetCollections(ctx context.Context, arg GetCollectionsParams)
}
return items, nil
}
const insertCollection = `-- name: InsertCollection :exec
INSERT INTO Collection (Name, MD5Hash) VALUES (?, ?)
`
type InsertCollectionParams struct {
Name sql.NullString `json:"name"`
Md5hash sql.NullString `json:"md5hash"`
}
func (q *Queries) InsertCollection(ctx context.Context, arg InsertCollectionParams) error {
_, err := q.db.ExecContext(ctx, insertCollection, arg.Name, arg.Md5hash)
return err
}

View File

@@ -20,13 +20,13 @@ type Beatmap struct {
Md5hash sql.NullString `json:"md5hash"`
File sql.NullString `json:"file"`
Rankedstatus sql.NullString `json:"rankedstatus"`
Lastmodifiedtime sql.NullTime `json:"lastmodifiedtime"`
Lastmodifiedtime sql.NullInt64 `json:"lastmodifiedtime"`
Totaltime sql.NullInt64 `json:"totaltime"`
Audiopreviewtime sql.NullInt64 `json:"audiopreviewtime"`
Beatmapsetid sql.NullInt64 `json:"beatmapsetid"`
Source sql.NullString `json:"source"`
Tags sql.NullString `json:"tags"`
Lastplayed sql.NullTime `json:"lastplayed"`
Lastplayed sql.NullInt64 `json:"lastplayed"`
Folder sql.NullString `json:"folder"`
}

View File

@@ -10,17 +10,18 @@ import (
)
type Querier interface {
GetArtists(ctx context.Context, arg GetArtistsParams) ([]GetArtistsRow, error)
GetBeatmapByHash(ctx context.Context, md5hash sql.NullString) (Beatmap, error)
GetBeatmapCount(ctx context.Context) (int64, error)
GetCollection(ctx context.Context, arg GetCollectionParams) ([]GetCollectionRow, error)
GetBeatmapSetCount(ctx context.Context) (int64, error)
GetCollectionByName(ctx context.Context, arg GetCollectionByNameParams) ([]GetCollectionByNameRow, error)
GetCollectionByOffset(ctx context.Context, arg GetCollectionByOffsetParams) ([]GetCollectionByOffsetRow, error)
GetCollectionCountByName(ctx context.Context, name sql.NullString) (int64, error)
GetCollections(ctx context.Context, arg GetCollectionsParams) ([]GetCollectionsRow, error)
GetRecentBeatmaps(ctx context.Context, arg GetRecentBeatmapsParams) ([]GetRecentBeatmapsRow, error)
GetRecentBeatmaps(ctx context.Context, arg GetRecentBeatmapsParams) ([]Beatmap, error)
InsertBeatmap(ctx context.Context, arg InsertBeatmapParams) error
InsertCollection(ctx context.Context, arg InsertCollectionParams) error
SearchBeatmaps(ctx context.Context, arg SearchBeatmapsParams) ([]SearchBeatmapsRow, error)
SearchArtists(ctx context.Context, arg SearchArtistsParams) ([]SearchArtistsRow, error)
SearchBeatmaps(ctx context.Context, arg SearchBeatmapsParams) ([]Beatmap, error)
SearchCollection(ctx context.Context, arg SearchCollectionParams) ([]SearchCollectionRow, error)
}
var _ Querier = (*Queries)(nil)