mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
refactor: consolidate query executions into two functions queryOne
and queryAll
This commit is contained in:
parent
7e65bb8f20
commit
7c4511e33a
6 changed files with 20 additions and 57 deletions
|
@ -2,8 +2,6 @@ package persistence
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "github.com/Masterminds/squirrel"
|
||||
|
@ -78,12 +76,8 @@ func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums
|
|||
default:
|
||||
sq = sq.OrderBy("RANDOM()")
|
||||
}
|
||||
sql, args, err := r.toSql(sq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := model.Albums{}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
|
||||
err := r.queryAll(sq, &results)
|
||||
return results, err
|
||||
}
|
||||
|
||||
|
@ -94,15 +88,13 @@ func (r *albumRepository) Refresh(ids ...string) error {
|
|||
HasCoverArt bool
|
||||
}
|
||||
var albums []refreshAlbum
|
||||
o := r.ormer
|
||||
sql := fmt.Sprintf(`
|
||||
select album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.compilation, f.genre,
|
||||
sel := Select(`album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.compilation, f.genre,
|
||||
max(f.year) as year, sum(f.duration) as duration, count(*) as song_count, a.id as current_id,
|
||||
f.id as cover_art_id, f.path as cover_art_path, f.has_cover_art
|
||||
from media_file f left outer join album a on f.album_id = a.id
|
||||
where f.album_id in ('%s')
|
||||
group by album_id order by f.id`, strings.Join(ids, "','"))
|
||||
_, err := o.Raw(sql).QueryRows(&albums)
|
||||
f.id as cover_art_id, f.path as cover_art_path, f.has_cover_art`).
|
||||
From("media_file f").
|
||||
LeftJoin("album a on f.album_id = a.id").
|
||||
Where(Eq{"f.album_id": ids}).GroupBy("album_id").OrderBy("f.id")
|
||||
err := r.queryAll(sel, &albums)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package persistence
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
@ -112,18 +111,13 @@ func (r *artistRepository) Refresh(ids ...string) error {
|
|||
Compilation bool
|
||||
}
|
||||
var artists []refreshArtist
|
||||
o := r.ormer
|
||||
sql := fmt.Sprintf(`
|
||||
select f.artist_id as id,
|
||||
f.artist as name,
|
||||
f.album_artist,
|
||||
f.compilation,
|
||||
count(*) as album_count,
|
||||
a.id as current_id
|
||||
from album f
|
||||
left outer join artist a on f.artist_id = a.id
|
||||
where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(ids, "','"))
|
||||
_, err := o.Raw(sql).QueryRows(&artists)
|
||||
sel := Select("f.artist_id as id", "f.artist as name", "f.album_artist", "f.compilation",
|
||||
"count(*) as album_count", "a.id as current_id").
|
||||
From("album f").
|
||||
LeftJoin("artist a on f.artist_id = a.id").
|
||||
Where(Eq{"f.artist_id": ids}).
|
||||
GroupBy("f.artist_id").OrderBy("f.id")
|
||||
err := r.queryAll(sel, &artists)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -23,11 +23,7 @@ func NewGenreRepository(ctx context.Context, o orm.Ormer) model.GenreRepository
|
|||
func (r genreRepository) GetAll() (model.Genres, error) {
|
||||
sq := Select("genre as name", "count(distinct album_id) as album_count", "count(distinct id) as song_count").
|
||||
From("media_file").GroupBy("genre")
|
||||
sql, args, err := r.toSql(sq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := model.Genres{}
|
||||
_, err = r.ormer.Raw(sql, args).QueryRows(&res)
|
||||
err := r.queryAll(sq, &res)
|
||||
return res, err
|
||||
}
|
||||
|
|
|
@ -102,12 +102,8 @@ func (r mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.Med
|
|||
default:
|
||||
sq = sq.OrderBy("RANDOM()")
|
||||
}
|
||||
sql, args, err := r.toSql(sq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := model.MediaFiles{}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
|
||||
err := r.queryAll(sq, &results)
|
||||
return results, err
|
||||
}
|
||||
|
||||
|
|
|
@ -103,28 +103,17 @@ func (r sqlRepository) queryAll(sq Sqlizer, response interface{}) error {
|
|||
|
||||
func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
|
||||
existsQuery = existsQuery.Columns("count(*) as count").From(r.tableName)
|
||||
query, args, err := r.toSql(existsQuery)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var res struct{ Count int64 }
|
||||
err = r.ormer.Raw(query, args...).QueryRow(&res)
|
||||
err := r.queryOne(existsQuery, &res)
|
||||
return res.Count > 0, err
|
||||
}
|
||||
|
||||
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
|
||||
countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
|
||||
countQuery = r.applyFilters(countQuery, options...)
|
||||
query, args, err := r.toSql(countQuery)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var res struct{ Count int64 }
|
||||
err = r.ormer.Raw(query, args...).QueryRow(&res)
|
||||
if err == orm.ErrNoRows {
|
||||
return 0, model.ErrNotFound
|
||||
}
|
||||
return res.Count, nil
|
||||
err := r.queryOne(countQuery, &res)
|
||||
return res.Count, err
|
||||
}
|
||||
|
||||
func (r sqlRepository) put(id string, m interface{}) (newId string, err error) {
|
||||
|
|
|
@ -49,11 +49,7 @@ func (r sqlRepository) doSearch(q string, offset, size int, results interface{},
|
|||
Like{"full_text": "%" + part + "%"},
|
||||
})
|
||||
}
|
||||
sql, args, err := r.toSql(sq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(results)
|
||||
err := r.queryAll(sq, results)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue