From 7c4511e33a3a094deb4abb35ab1682220930c8d3 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 1 Feb 2020 14:48:22 -0500 Subject: [PATCH] refactor: consolidate query executions into two functions `queryOne` and `queryAll` --- persistence/album_repository.go | 22 +++++++--------------- persistence/artist_repository.go | 20 +++++++------------- persistence/genre_repository.go | 6 +----- persistence/mediafile_repository.go | 6 +----- persistence/sql_base_repository.go | 17 +++-------------- persistence/sql_search.go | 6 +----- 6 files changed, 20 insertions(+), 57 deletions(-) diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 9d027a76b..49126b850 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -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 } diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index 861123140..05b55a634 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -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 } diff --git a/persistence/genre_repository.go b/persistence/genre_repository.go index c4232feb0..e858eb7a0 100644 --- a/persistence/genre_repository.go +++ b/persistence/genre_repository.go @@ -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 } diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 28169c239..e0117401d 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -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 } diff --git a/persistence/sql_base_repository.go b/persistence/sql_base_repository.go index 84b76faf4..7f3bb4590 100644 --- a/persistence/sql_base_repository.go +++ b/persistence/sql_base_repository.go @@ -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) { diff --git a/persistence/sql_search.go b/persistence/sql_search.go index 1d88ed8fc..2f745764d 100644 --- a/persistence/sql_search.go +++ b/persistence/sql_search.go @@ -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 }