db/DatabasePlaylist: pass Database reference around
Reduce global variable usage, move to frontend code.
This commit is contained in:
parent
db69ceade6
commit
29072797ca
|
@ -92,11 +92,12 @@ SongLoader::LoadSong(const char *uri_utf8, Error &error) const
|
||||||
/* URI relative to the music directory */
|
/* URI relative to the music directory */
|
||||||
|
|
||||||
#ifdef ENABLE_DATABASE
|
#ifdef ENABLE_DATABASE
|
||||||
return DatabaseDetachSong(uri_utf8, error);
|
if (db != nullptr)
|
||||||
#else
|
return DatabaseDetachSong(*db, uri_utf8, error);
|
||||||
|
#endif
|
||||||
|
|
||||||
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG),
|
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG),
|
||||||
"No database");
|
"No database");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,13 @@
|
||||||
#ifndef MPD_SONG_LOADER_HXX
|
#ifndef MPD_SONG_LOADER_HXX
|
||||||
#define MPD_SONG_LOADER_HXX
|
#define MPD_SONG_LOADER_HXX
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
class Client;
|
class Client;
|
||||||
|
class Database;
|
||||||
class DetachedSong;
|
class DetachedSong;
|
||||||
class Error;
|
class Error;
|
||||||
|
|
||||||
|
@ -35,11 +39,26 @@ class Error;
|
||||||
class SongLoader {
|
class SongLoader {
|
||||||
const Client *const client;
|
const Client *const client;
|
||||||
|
|
||||||
|
#ifdef ENABLE_DATABASE
|
||||||
|
const Database *const db;
|
||||||
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
#ifdef ENABLE_DATABASE
|
||||||
|
SongLoader(const Client *_client, const Database *_db=nullptr)
|
||||||
|
:client(_client), db(_db) {}
|
||||||
|
explicit SongLoader(const Client &_client)
|
||||||
|
:client(&_client), db(nullptr) {}
|
||||||
|
explicit SongLoader(const Database *_db)
|
||||||
|
:client(nullptr), db(_db) {}
|
||||||
|
explicit SongLoader(std::nullptr_t)
|
||||||
|
:client(nullptr), db(nullptr) {}
|
||||||
|
#else
|
||||||
explicit SongLoader(const Client &_client)
|
explicit SongLoader(const Client &_client)
|
||||||
:client(&_client) {}
|
:client(&_client) {}
|
||||||
explicit SongLoader(const Client *_client)
|
explicit SongLoader(const Client *_client)
|
||||||
:client(_client) {}
|
:client(_client) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
gcc_nonnull_all
|
gcc_nonnull_all
|
||||||
DetachedSong *LoadSong(const char *uri_utf8, Error &error) const;
|
DetachedSong *LoadSong(const char *uri_utf8, Error &error) const;
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "DatabaseCommands.hxx"
|
#include "DatabaseCommands.hxx"
|
||||||
|
#include "db/DatabaseGlue.hxx"
|
||||||
#include "db/DatabaseQueue.hxx"
|
#include "db/DatabaseQueue.hxx"
|
||||||
#include "db/DatabasePlaylist.hxx"
|
#include "db/DatabasePlaylist.hxx"
|
||||||
#include "db/DatabasePrint.hxx"
|
#include "db/DatabasePrint.hxx"
|
||||||
|
@ -119,7 +120,11 @@ handle_searchaddpl(Client &client, int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
Error error;
|
Error error;
|
||||||
return search_add_to_playlist("", playlist, &filter, error)
|
const Database *db = GetDatabase(error);
|
||||||
|
if (db == nullptr)
|
||||||
|
return print_error(client, error);
|
||||||
|
|
||||||
|
return search_add_to_playlist(*db, "", playlist, &filter, error)
|
||||||
? CommandResult::OK
|
? CommandResult::OK
|
||||||
: print_error(client, error);
|
: print_error(client, error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "PlaylistCommands.hxx"
|
#include "PlaylistCommands.hxx"
|
||||||
|
#include "db/DatabaseGlue.hxx"
|
||||||
#include "db/DatabasePlaylist.hxx"
|
#include "db/DatabasePlaylist.hxx"
|
||||||
#include "CommandError.hxx"
|
#include "CommandError.hxx"
|
||||||
#include "PlaylistPrint.hxx"
|
#include "PlaylistPrint.hxx"
|
||||||
|
@ -192,7 +193,11 @@ handle_playlistadd(Client &client, gcc_unused int argc, char *argv[])
|
||||||
success = spl_append_uri(playlist, loader, uri, error);
|
success = spl_append_uri(playlist, loader, uri, error);
|
||||||
} else {
|
} else {
|
||||||
#ifdef ENABLE_DATABASE
|
#ifdef ENABLE_DATABASE
|
||||||
success = search_add_to_playlist(uri, playlist, nullptr,
|
const Database *db = GetDatabase(error);
|
||||||
|
if (db == nullptr)
|
||||||
|
return print_error(client, error);
|
||||||
|
|
||||||
|
success = search_add_to_playlist(*db, uri, playlist, nullptr,
|
||||||
error);
|
error);
|
||||||
#else
|
#else
|
||||||
success = false;
|
success = false;
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
#include "DatabasePlaylist.hxx"
|
#include "DatabasePlaylist.hxx"
|
||||||
#include "Selection.hxx"
|
#include "Selection.hxx"
|
||||||
#include "PlaylistFile.hxx"
|
#include "PlaylistFile.hxx"
|
||||||
#include "DatabaseGlue.hxx"
|
|
||||||
#include "DatabasePlugin.hxx"
|
#include "DatabasePlugin.hxx"
|
||||||
#include "DetachedSong.hxx"
|
#include "DetachedSong.hxx"
|
||||||
#include "Mapper.hxx"
|
#include "Mapper.hxx"
|
||||||
|
@ -37,17 +36,14 @@ AddSong(const char *playlist_path_utf8,
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
search_add_to_playlist(const char *uri, const char *playlist_path_utf8,
|
search_add_to_playlist(const Database &db,
|
||||||
|
const char *uri, const char *playlist_path_utf8,
|
||||||
const SongFilter *filter,
|
const SongFilter *filter,
|
||||||
Error &error)
|
Error &error)
|
||||||
{
|
{
|
||||||
const Database *db = GetDatabase(error);
|
|
||||||
if (db == nullptr)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const DatabaseSelection selection(uri, true, filter);
|
const DatabaseSelection selection(uri, true, filter);
|
||||||
|
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
const auto f = std::bind(AddSong, playlist_path_utf8, _1, _2);
|
const auto f = std::bind(AddSong, playlist_path_utf8, _1, _2);
|
||||||
return db->Visit(selection, f, error);
|
return db.Visit(selection, f, error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,14 @@
|
||||||
|
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
class Database;
|
||||||
class SongFilter;
|
class SongFilter;
|
||||||
class Error;
|
class Error;
|
||||||
|
|
||||||
gcc_nonnull(1,2)
|
gcc_nonnull(2,3)
|
||||||
bool
|
bool
|
||||||
search_add_to_playlist(const char *uri, const char *path_utf8,
|
search_add_to_playlist(const Database &db,
|
||||||
|
const char *uri, const char *path_utf8,
|
||||||
const SongFilter *filter,
|
const SongFilter *filter,
|
||||||
Error &error);
|
Error &error);
|
||||||
|
|
||||||
|
|
|
@ -19,23 +19,18 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "DatabaseSong.hxx"
|
#include "DatabaseSong.hxx"
|
||||||
#include "DatabaseGlue.hxx"
|
|
||||||
#include "DatabasePlugin.hxx"
|
#include "DatabasePlugin.hxx"
|
||||||
#include "DetachedSong.hxx"
|
#include "DetachedSong.hxx"
|
||||||
#include "Mapper.hxx"
|
#include "Mapper.hxx"
|
||||||
|
|
||||||
DetachedSong *
|
DetachedSong *
|
||||||
DatabaseDetachSong(const char *uri, Error &error)
|
DatabaseDetachSong(const Database &db, const char *uri, Error &error)
|
||||||
{
|
{
|
||||||
const Database *db = GetDatabase(error);
|
const LightSong *tmp = db.GetSong(uri, error);
|
||||||
if (db == nullptr)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
const LightSong *tmp = db->GetSong(uri, error);
|
|
||||||
if (tmp == nullptr)
|
if (tmp == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
DetachedSong *song = new DetachedSong(map_song_detach(*tmp));
|
DetachedSong *song = new DetachedSong(map_song_detach(*tmp));
|
||||||
db->ReturnSong(tmp);
|
db.ReturnSong(tmp);
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
class Database;
|
||||||
class DetachedSong;
|
class DetachedSong;
|
||||||
class Error;
|
class Error;
|
||||||
|
|
||||||
|
@ -33,6 +34,6 @@ class Error;
|
||||||
*/
|
*/
|
||||||
gcc_malloc gcc_nonnull_all
|
gcc_malloc gcc_nonnull_all
|
||||||
DetachedSong *
|
DetachedSong *
|
||||||
DatabaseDetachSong(const char *uri, Error &error);
|
DatabaseDetachSong(const Database &db, const char *uri, Error &error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -117,7 +117,8 @@ static const char *uri1 = "/foo/bar.ogg";
|
||||||
static const char *uri2 = "foo/bar.ogg";
|
static const char *uri2 = "foo/bar.ogg";
|
||||||
|
|
||||||
DetachedSong *
|
DetachedSong *
|
||||||
DatabaseDetachSong(const char *uri, gcc_unused Error &error)
|
DatabaseDetachSong(gcc_unused const Database &db, const char *uri,
|
||||||
|
gcc_unused Error &error)
|
||||||
{
|
{
|
||||||
if (strcmp(uri, uri2) == 0)
|
if (strcmp(uri, uri2) == 0)
|
||||||
return new DetachedSong(uri, MakeTag2a());
|
return new DetachedSong(uri, MakeTag2a());
|
||||||
|
@ -236,7 +237,7 @@ class TranslateSongTest : public CppUnit::TestFixture {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestInDatabase() {
|
void TestInDatabase() {
|
||||||
const SongLoader loader(nullptr);
|
const SongLoader loader(reinterpret_cast<const Database *>(1));
|
||||||
|
|
||||||
DetachedSong song1("doesntexist");
|
DetachedSong song1("doesntexist");
|
||||||
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
|
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
|
||||||
|
@ -258,8 +259,9 @@ class TranslateSongTest : public CppUnit::TestFixture {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestRelative() {
|
void TestRelative() {
|
||||||
const SongLoader secure_loader(nullptr);
|
const Database &db = *reinterpret_cast<const Database *>(1);
|
||||||
const SongLoader insecure_loader(reinterpret_cast<const Client *>(1));
|
const SongLoader secure_loader(&db);
|
||||||
|
const SongLoader insecure_loader(reinterpret_cast<const Client *>(1), &db);
|
||||||
|
|
||||||
/* map to music_directory */
|
/* map to music_directory */
|
||||||
DetachedSong song1("bar.ogg", MakeTag2b());
|
DetachedSong song1("bar.ogg", MakeTag2b());
|
||||||
|
|
Loading…
Reference in New Issue