diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index 95e9eda2b..5c0bc47a8 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -231,15 +231,12 @@ handle_playlistadd_position(Client &client, const char *playlist_name, editor.Insert(position, uri); } else { #ifdef ENABLE_DATABASE - const auto &db = client.GetDatabaseOrThrow(); - const auto *storage = client.GetStorage(); const DatabaseSelection selection(uri, true, nullptr); - db.Visit(selection, [&editor, &position, storage](const auto &song){ - editor.Insert(position, - DatabaseDetachSong(storage, song)); - ++position; - }); + SearchInsertIntoPlaylist(client.GetDatabaseOrThrow(), + client.GetStorage(), + selection, + editor, position); #else (void)client; r.Error(ACK_ERROR_NO_EXIST, "No database"); diff --git a/src/db/DatabasePlaylist.cxx b/src/db/DatabasePlaylist.cxx index 9c55c2966..9d72d32a8 100644 --- a/src/db/DatabasePlaylist.cxx +++ b/src/db/DatabasePlaylist.cxx @@ -41,3 +41,18 @@ search_add_to_playlist(const Database &db, const Storage *storage, const auto f = [=](auto && arg1) { return AddSong(storage, playlist_path_utf8, arg1); }; db.Visit(selection, f); } + +void +SearchInsertIntoPlaylist(const Database &db, const Storage *storage, + const DatabaseSelection &selection, + PlaylistFileEditor &playlist, + unsigned position) +{ + assert(position <= playlist.size()); + + db.Visit(selection, [&playlist, &position, storage](const auto &song){ + playlist.Insert(position, + DatabaseDetachSong(storage, song)); + ++position; + }); +} diff --git a/src/db/DatabasePlaylist.hxx b/src/db/DatabasePlaylist.hxx index c2c462362..bbe3e2f29 100644 --- a/src/db/DatabasePlaylist.hxx +++ b/src/db/DatabasePlaylist.hxx @@ -25,6 +25,7 @@ class Database; class Storage; struct DatabaseSelection; +class PlaylistFileEditor; gcc_nonnull(3) void @@ -32,4 +33,10 @@ search_add_to_playlist(const Database &db, const Storage *storage, const char *playlist_path_utf8, const DatabaseSelection &selection); +void +SearchInsertIntoPlaylist(const Database &db, const Storage *storage, + const DatabaseSelection &selection, + PlaylistFileEditor &playlist, + unsigned position); + #endif