diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 26e955f9..86d3d055 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -149,23 +149,6 @@ func (r *albumRepository) getEmbeddedCoversForAlbums(ids []string) (map[string]m return result, nil } -func (r *albumRepository) getGenresForAlbums(ids []string) (map[string]model.Genres, error) { - sql := Select("mf.album_id", "group_concat(mfg.genre_id, ' ') as genre").From("media_file_genres mfg"). - LeftJoin("media_file mf on mf.id = mfg.media_file_id"). - Where(Eq{"mf.album_id": ids}).GroupBy("mf.album_id") - var mfs model.MediaFiles - err := r.queryAll(sql, &mfs) - if err != nil { - return nil, err - } - - result := map[string]model.Genres{} - for _, mf := range mfs { - result[mf.AlbumID] = getGenres(mf.Genre) - } - return result, nil -} - func (r *albumRepository) Refresh(ids ...string) error { chunks := utils.BreakUpStringSlice(ids, 100) for _, chunk := range chunks { @@ -185,6 +168,7 @@ type refreshAlbum struct { SongArtists string SongArtistIds string AlbumArtistIds string + GenreIds string Years string DiscSubtitles string Comments string @@ -205,15 +189,19 @@ func (r *albumRepository) refresh(ids ...string) error { max(f.updated_at) as max_updated_at, max(f.created_at) as max_created_at, a.id as current_id, - group_concat(f.comment, "` + zwsp + `") as comments, + group_concat(f.comment, "`+zwsp+`") as comments, group_concat(f.mbz_album_id, ' ') as mbz_album_id, group_concat(f.disc_subtitle, ' ') as disc_subtitles, group_concat(f.artist, ' ') as song_artists, group_concat(f.artist_id, ' ') as song_artist_ids, group_concat(f.album_artist_id, ' ') as album_artist_ids, - group_concat(f.year, ' ') as years`). + group_concat(f.year, ' ') as years`, + "mg.genre_ids"). From("media_file f"). LeftJoin("album a on f.album_id = a.id"). + LeftJoin(`(select mf.album_id, group_concat(mfg.genre_id, ' ') as genre_ids from media_file_genres mfg + left join media_file mf on mf.id = mfg.media_file_id where mf.album_id in ('` + + strings.Join(ids, "','") + `') group by mf.album_id) mg on mg.album_id = f.album_id`). Where(Eq{"f.album_id": ids}).GroupBy("f.album_id") err := r.queryAll(sel, &albums) if err != nil { @@ -225,11 +213,6 @@ func (r *albumRepository) refresh(ids ...string) error { return nil } - allGenres, err := r.getGenresForAlbums(ids) - if err != nil { - return nil - } - toInsert := 0 toUpdate := 0 for _, al := range albums { @@ -271,7 +254,7 @@ func (r *albumRepository) refresh(ids ...string) error { al.AllArtistIDs = utils.SanitizeStrings(al.SongArtistIds, al.AlbumArtistID, al.ArtistID) al.FullText = getFullText(al.Name, al.Artist, al.AlbumArtist, al.SongArtists, al.SortAlbumName, al.SortArtistName, al.SortAlbumArtistName, al.DiscSubtitles) - al.Genres = allGenres[al.ID] + al.Genres = getGenres(al.GenreIds) err := r.Put(&al.Album) if err != nil { return err diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index 8276967f..23eb5d35 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -175,23 +175,6 @@ func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) { return result, nil } -func (r *artistRepository) getGenresForArtists(ids []string) (map[string]model.Genres, error) { - sql := Select("a.album_artist_id", "group_concat(ag.genre_id, ' ') as genre").From("album_genres ag"). - LeftJoin("album a on a.id = ag.album_id"). - Where(Eq{"a.album_artist_id": ids}).GroupBy("a.album_artist_id") - var als model.Albums - err := r.queryAll(sql, &als) - if err != nil { - return nil, err - } - - result := map[string]model.Genres{} - for _, al := range als { - result[al.AlbumArtistID] = getGenres(al.Genre) - } - return result, nil -} - func (r *artistRepository) Refresh(ids ...string) error { chunks := utils.BreakUpStringSlice(ids, 100) for _, chunk := range chunks { @@ -207,15 +190,19 @@ func (r *artistRepository) refresh(ids ...string) error { type refreshArtist struct { model.Artist CurrentId string + GenreIds string } var artists []refreshArtist sel := Select("f.album_artist_id as id", "f.album_artist as name", "count(*) as album_count", "a.id as current_id", "group_concat(f.mbz_album_artist_id , ' ') as mbz_artist_id", "f.sort_album_artist_name as sort_artist_name", "f.order_album_artist_name as order_artist_name", "sum(f.song_count) as song_count", "sum(f.size) as size", - "(select group_concat(genre_id, ' ') from album_genres where album_id = f.id) as genre_ids"). + "alg.genre_ids"). From("album f"). LeftJoin("artist a on f.album_artist_id = a.id"). + LeftJoin(`(select al.album_artist_id, group_concat(ag.genre_id, ' ') as genre_ids from album_genres ag + left join album al on al.id = ag.album_id where al.album_artist_id in ('` + + strings.Join(ids, "','") + `') group by al.album_artist_id) alg on alg.album_artist_id = f.album_artist_id`). Where(Eq{"f.album_artist_id": ids}). GroupBy("f.album_artist_id").OrderBy("f.id") err := r.queryAll(sel, &artists) @@ -223,11 +210,6 @@ func (r *artistRepository) refresh(ids ...string) error { return err } - allGenres, err := r.getGenresForArtists(ids) - if err != nil { - return err - } - toInsert := 0 toUpdate := 0 for _, ar := range artists { @@ -237,7 +219,7 @@ func (r *artistRepository) refresh(ids ...string) error { toInsert++ } ar.MbzArtistID = getMostFrequentMbzID(r.ctx, ar.MbzArtistID, r.tableName, ar.Name) - ar.Genres = allGenres[ar.ID] + ar.Genres = getGenres(ar.GenreIds) err := r.Put(&ar.Artist) if err != nil { return err