Optimize AlbumRepository.GetAll and add a GetAllWithoutGenres method specifically for Subsonic API, where multiple-genres are not required
This commit is contained in:
@@ -93,7 +93,18 @@ func (r *albumRepository) Exists(id string) (bool, error) {
|
||||
|
||||
func (r *albumRepository) selectAlbum(options ...model.QueryOptions) SelectBuilder {
|
||||
sql := r.newSelectWithAnnotation("album.id", options...).Columns("album.*")
|
||||
return r.withGenres(sql).GroupBy("album.id")
|
||||
if len(options) > 0 && options[0].Filters != nil {
|
||||
s, _, _ := options[0].Filters.ToSql()
|
||||
// If there's any reference of genre in the filter, joins with genre
|
||||
if strings.Contains(s, "genre") {
|
||||
sql = r.withGenres(sql)
|
||||
// If there's no filter on genre_id, group the results by media_file.id
|
||||
if !strings.Contains(s, "genre_id") {
|
||||
sql = sql.GroupBy("album.id")
|
||||
}
|
||||
}
|
||||
}
|
||||
return sql
|
||||
}
|
||||
|
||||
func (r *albumRepository) Get(id string) (*model.Album, error) {
|
||||
@@ -118,13 +129,21 @@ func (r *albumRepository) Put(m *model.Album) error {
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, error) {
|
||||
res, err := r.GetAllWithoutGenres(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = r.loadAlbumGenres(&res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetAllWithoutGenres(options ...model.QueryOptions) (model.Albums, error) {
|
||||
sq := r.selectAlbum(options...)
|
||||
res := model.Albums{}
|
||||
err := r.queryAll(sq, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = r.loadAlbumGenres(&res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ func (r *sqlRepository) loadMediaFileGenres(mfs *model.MediaFiles) error {
|
||||
m[mf.ID] = mf
|
||||
}
|
||||
|
||||
return utils.RangeByChunks(ids, 100, func(ids []string) error {
|
||||
return utils.RangeByChunks(ids, 900, func(ids []string) error {
|
||||
sql := Select("g.*", "mg.media_file_id").From("genre g").Join("media_file_genres mg on mg.genre_id = g.id").
|
||||
Where(Eq{"mg.media_file_id": ids}).OrderBy("mg.media_file_id", "mg.rowid")
|
||||
var genres []struct {
|
||||
@@ -74,7 +74,7 @@ func (r *sqlRepository) loadAlbumGenres(mfs *model.Albums) error {
|
||||
m[mf.ID] = mf
|
||||
}
|
||||
|
||||
return utils.RangeByChunks(ids, 100, func(ids []string) error {
|
||||
return utils.RangeByChunks(ids, 900, func(ids []string) error {
|
||||
sql := Select("g.*", "ag.album_id").From("genre g").Join("album_genres ag on ag.genre_id = g.id").
|
||||
Where(Eq{"ag.album_id": ids}).OrderBy("ag.album_id", "ag.rowid")
|
||||
var genres []struct {
|
||||
@@ -103,7 +103,7 @@ func (r *sqlRepository) loadArtistGenres(mfs *model.Artists) error {
|
||||
m[mf.ID] = mf
|
||||
}
|
||||
|
||||
return utils.RangeByChunks(ids, 100, func(ids []string) error {
|
||||
return utils.RangeByChunks(ids, 900, func(ids []string) error {
|
||||
sql := Select("g.*", "ag.artist_id").From("genre g").Join("artist_genres ag on ag.genre_id = g.id").
|
||||
Where(Eq{"ag.artist_id": ids}).OrderBy("ag.artist_id", "ag.rowid")
|
||||
var genres []struct {
|
||||
|
||||
Reference in New Issue
Block a user