Fix "too many SQL variables" error in GetStarred endpoint

This commit is contained in:
Deluan 2021-07-21 10:45:52 -04:00
parent 4f9d546da4
commit 4fcb238295
2 changed files with 63 additions and 45 deletions

View file

@ -3,6 +3,7 @@ package persistence
import (
. "github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
)
func (r sqlRepository) withGenres(sql SelectBuilder) SelectBuilder {
@ -37,6 +38,7 @@ func (r *sqlRepository) loadMediaFileGenres(mfs *model.MediaFiles) error {
m[mf.ID] = mf
}
return utils.RangeByChunks(ids, 100, 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 {
@ -53,6 +55,7 @@ func (r *sqlRepository) loadMediaFileGenres(mfs *model.MediaFiles) error {
mf.Genres = append(mf.Genres, g.Genre)
}
return nil
})
}
func (r *sqlRepository) loadAlbumGenres(mfs *model.Albums) error {
@ -64,6 +67,7 @@ func (r *sqlRepository) loadAlbumGenres(mfs *model.Albums) error {
m[mf.ID] = mf
}
return utils.RangeByChunks(ids, 100, 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 {
@ -80,6 +84,7 @@ func (r *sqlRepository) loadAlbumGenres(mfs *model.Albums) error {
mf.Genres = append(mf.Genres, g.Genre)
}
return nil
})
}
func (r *sqlRepository) loadArtistGenres(mfs *model.Artists) error {
@ -91,6 +96,7 @@ func (r *sqlRepository) loadArtistGenres(mfs *model.Artists) error {
m[mf.ID] = mf
}
return utils.RangeByChunks(ids, 100, 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 {
@ -107,4 +113,5 @@ func (r *sqlRepository) loadArtistGenres(mfs *model.Artists) error {
mf.Genres = append(mf.Genres, g.Genre)
}
return nil
})
}

View file

@ -53,6 +53,17 @@ func BreakUpStringSlice(items []string, chunkSize int) [][]string {
return chunks
}
func RangeByChunks(items []string, chunkSize int, cb func([]string) error) error {
chunks := BreakUpStringSlice(items, chunkSize)
for _, chunk := range chunks {
err := cb(chunk)
if err != nil {
return err
}
}
return nil
}
func LongestCommonPrefix(list []string) string {
if len(list) == 0 {
return ""