refactor: consolidate query executions into two functions queryOne and queryAll
This commit is contained in:
@@ -2,8 +2,6 @@ package persistence
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/Masterminds/squirrel"
|
. "github.com/Masterminds/squirrel"
|
||||||
@@ -78,12 +76,8 @@ func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums
|
|||||||
default:
|
default:
|
||||||
sq = sq.OrderBy("RANDOM()")
|
sq = sq.OrderBy("RANDOM()")
|
||||||
}
|
}
|
||||||
sql, args, err := r.toSql(sq)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
results := model.Albums{}
|
results := model.Albums{}
|
||||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
|
err := r.queryAll(sq, &results)
|
||||||
return results, err
|
return results, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,15 +88,13 @@ func (r *albumRepository) Refresh(ids ...string) error {
|
|||||||
HasCoverArt bool
|
HasCoverArt bool
|
||||||
}
|
}
|
||||||
var albums []refreshAlbum
|
var albums []refreshAlbum
|
||||||
o := r.ormer
|
sel := Select(`album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.compilation, f.genre,
|
||||||
sql := fmt.Sprintf(`
|
|
||||||
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,
|
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
|
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
|
From("media_file f").
|
||||||
where f.album_id in ('%s')
|
LeftJoin("album a on f.album_id = a.id").
|
||||||
group by album_id order by f.id`, strings.Join(ids, "','"))
|
Where(Eq{"f.album_id": ids}).GroupBy("album_id").OrderBy("f.id")
|
||||||
_, err := o.Raw(sql).QueryRows(&albums)
|
err := r.queryAll(sel, &albums)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package persistence
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -112,18 +111,13 @@ func (r *artistRepository) Refresh(ids ...string) error {
|
|||||||
Compilation bool
|
Compilation bool
|
||||||
}
|
}
|
||||||
var artists []refreshArtist
|
var artists []refreshArtist
|
||||||
o := r.ormer
|
sel := Select("f.artist_id as id", "f.artist as name", "f.album_artist", "f.compilation",
|
||||||
sql := fmt.Sprintf(`
|
"count(*) as album_count", "a.id as current_id").
|
||||||
select f.artist_id as id,
|
From("album f").
|
||||||
f.artist as name,
|
LeftJoin("artist a on f.artist_id = a.id").
|
||||||
f.album_artist,
|
Where(Eq{"f.artist_id": ids}).
|
||||||
f.compilation,
|
GroupBy("f.artist_id").OrderBy("f.id")
|
||||||
count(*) as album_count,
|
err := r.queryAll(sel, &artists)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,11 +23,7 @@ func NewGenreRepository(ctx context.Context, o orm.Ormer) model.GenreRepository
|
|||||||
func (r genreRepository) GetAll() (model.Genres, error) {
|
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").
|
sq := Select("genre as name", "count(distinct album_id) as album_count", "count(distinct id) as song_count").
|
||||||
From("media_file").GroupBy("genre")
|
From("media_file").GroupBy("genre")
|
||||||
sql, args, err := r.toSql(sq)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
res := model.Genres{}
|
res := model.Genres{}
|
||||||
_, err = r.ormer.Raw(sql, args).QueryRows(&res)
|
err := r.queryAll(sq, &res)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,12 +102,8 @@ func (r mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.Med
|
|||||||
default:
|
default:
|
||||||
sq = sq.OrderBy("RANDOM()")
|
sq = sq.OrderBy("RANDOM()")
|
||||||
}
|
}
|
||||||
sql, args, err := r.toSql(sq)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
results := model.MediaFiles{}
|
results := model.MediaFiles{}
|
||||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
|
err := r.queryAll(sq, &results)
|
||||||
return results, err
|
return results, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,28 +103,17 @@ func (r sqlRepository) queryAll(sq Sqlizer, response interface{}) error {
|
|||||||
|
|
||||||
func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
|
func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
|
||||||
existsQuery = existsQuery.Columns("count(*) as count").From(r.tableName)
|
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 }
|
var res struct{ Count int64 }
|
||||||
err = r.ormer.Raw(query, args...).QueryRow(&res)
|
err := r.queryOne(existsQuery, &res)
|
||||||
return res.Count > 0, err
|
return res.Count > 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
|
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
|
||||||
countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
|
countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
|
||||||
countQuery = r.applyFilters(countQuery, options...)
|
countQuery = r.applyFilters(countQuery, options...)
|
||||||
query, args, err := r.toSql(countQuery)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
var res struct{ Count int64 }
|
var res struct{ Count int64 }
|
||||||
err = r.ormer.Raw(query, args...).QueryRow(&res)
|
err := r.queryOne(countQuery, &res)
|
||||||
if err == orm.ErrNoRows {
|
return res.Count, err
|
||||||
return 0, model.ErrNotFound
|
|
||||||
}
|
|
||||||
return res.Count, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r sqlRepository) put(id string, m interface{}) (newId string, err error) {
|
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 + "%"},
|
Like{"full_text": "%" + part + "%"},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sql, args, err := r.toSql(sq)
|
err := r.queryAll(sq, results)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = r.ormer.Raw(sql, args...).QueryRows(results)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user