Instance: add GetDatabaseOrThrow()

This commit is contained in:
Max Kellermann 2016-10-26 18:47:19 +02:00
parent 6135f0763b
commit 086652dd50
12 changed files with 65 additions and 40 deletions

View File

@ -44,6 +44,16 @@ Instance::GetDatabase(Error &error)
return database;
}
const Database &
Instance::GetDatabaseOrThrow() const
{
if (database == nullptr)
throw DatabaseError(DatabaseErrorCode::DISABLED,
"No database");
return *database;
}
void
Instance::OnDatabaseModified()
{

View File

@ -110,6 +110,14 @@ struct Instance final
* music_directory was configured).
*/
Database *GetDatabase(Error &error);
/**
* Returns the global #Database instance. Throws
* DatabaseError if this MPD configuration has no database (no
* music_directory was configured).
*/
gcc_pure
const Database &GetDatabaseOrThrow() const;
#endif
private:

View File

@ -50,6 +50,12 @@ Partition::GetDatabase(Error &error) const
return instance.GetDatabase(error);
}
const Database &
Partition::GetDatabaseOrThrow() const
{
return instance.GetDatabaseOrThrow();
}
void
Partition::DatabaseModified(const Database &db)
{

View File

@ -185,6 +185,9 @@ struct Partition final : QueueListener, PlayerListener, MixerListener {
*/
const Database *GetDatabase(Error &error) const;
gcc_pure
const Database &GetDatabaseOrThrow() const;
/**
* The database has been modified. Propagate the change to
* all subsystems.

View File

@ -33,6 +33,12 @@ Client::GetDatabase(Error &error) const
return partition.instance.GetDatabase(error);
}
const Database &
Client::GetDatabaseOrThrow() const
{
return partition.instance.GetDatabaseOrThrow();
}
const Storage *
Client::GetStorage() const
{

View File

@ -187,6 +187,12 @@ public:
gcc_pure
const Database *GetDatabase(Error &error) const;
/**
* Wrapper for Instance::GetDatabaseOrThrow().
*/
gcc_pure
const Database &GetDatabaseOrThrow() const;
gcc_pure
const Storage *GetStorage() const;

View File

@ -143,11 +143,9 @@ handle_searchaddpl(Client &client, Request args, Response &r)
}
Error error;
const Database *db = client.GetDatabase(error);
if (db == nullptr)
return print_error(r, error);
const Database &db = client.GetDatabaseOrThrow();
return search_add_to_playlist(*db, *client.GetStorage(),
return search_add_to_playlist(db, *client.GetStorage(),
"", playlist, &filter, error)
? CommandResult::OK
: print_error(r, error);

View File

@ -168,11 +168,9 @@ handle_playlistadd(Client &client, Request args, Response &r)
success = spl_append_uri(playlist, loader, uri, error);
} else {
#ifdef ENABLE_DATABASE
const Database *db = client.GetDatabase(error);
if (db == nullptr)
return print_error(r, error);
const Database &db = client.GetDatabaseOrThrow();
success = search_add_to_playlist(*db, *client.GetStorage(),
success = search_add_to_playlist(db, *client.GetStorage(),
uri, playlist, nullptr,
error);
#else

View File

@ -54,19 +54,17 @@ static CommandResult
handle_sticker_song(Response &r, Partition &partition, Request args)
{
Error error;
const Database *db = partition.GetDatabase(error);
if (db == nullptr)
return print_error(r, error);
const Database &db = partition.GetDatabaseOrThrow();
const char *const cmd = args.front();
/* get song song_id key */
if (args.size == 4 && StringIsEqual(cmd, "get")) {
const LightSong *song = db->GetSong(args[2]);
const LightSong *song = db.GetSong(args[2]);
const auto value = sticker_song_get_value(*song, args[3],
error);
db->ReturnSong(song);
db.ReturnSong(song);
if (value.empty()) {
if (error.IsDefined())
return print_error(r, error);
@ -80,11 +78,11 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
return CommandResult::OK;
/* list song song_id */
} else if (args.size == 3 && StringIsEqual(cmd, "list")) {
const LightSong *song = db->GetSong(args[2]);
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
Sticker *sticker = sticker_song_get(*song, error);
db->ReturnSong(song);
db.ReturnSong(song);
if (sticker) {
sticker_print(r, *sticker);
sticker_free(sticker);
@ -94,12 +92,12 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
return CommandResult::OK;
/* set song song_id id key */
} else if (args.size == 5 && StringIsEqual(cmd, "set")) {
const LightSong *song = db->GetSong(args[2]);
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
bool ret = sticker_song_set_value(*song, args[3], args[4],
error);
db->ReturnSong(song);
db.ReturnSong(song);
if (!ret) {
if (error.IsDefined())
return print_error(r, error);
@ -113,13 +111,13 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
/* delete song song_id [key] */
} else if ((args.size == 3 || args.size == 4) &&
StringIsEqual(cmd, "delete")) {
const LightSong *song = db->GetSong(args[2]);
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
bool ret = args.size == 3
? sticker_song_delete(*song, error)
: sticker_song_delete_value(*song, args[3], error);
db->ReturnSong(song);
db.ReturnSong(song);
if (!ret) {
if (error.IsDefined())
return print_error(r, error);
@ -163,7 +161,7 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
args[3],
};
if (!sticker_song_find(*db, base_uri, data.name,
if (!sticker_song_find(db, base_uri, data.name,
op, value,
sticker_song_find_print_cb, &data,
error)) {

View File

@ -113,9 +113,7 @@ PrintSongCount(Response &r, const Partition &partition, const char *name,
TagType group,
Error &error)
{
const Database *db = partition.GetDatabase(error);
if (db == nullptr)
return false;
const Database &db = partition.GetDatabaseOrThrow();
const DatabaseSelection selection(name, true, filter);
@ -127,7 +125,7 @@ PrintSongCount(Response &r, const Partition &partition, const char *name,
using namespace std::placeholders;
const auto f = std::bind(stats_visitor_song, std::ref(stats),
_1);
if (!db->Visit(selection, f, error))
if (!db.Visit(selection, f, error))
return false;
PrintSearchStats(r, stats);
@ -140,7 +138,7 @@ PrintSongCount(Response &r, const Partition &partition, const char *name,
using namespace std::placeholders;
const auto f = std::bind(GroupCountVisitor, std::ref(map),
group, _1);
if (!db->Visit(selection, f, error))
if (!db.Visit(selection, f, error))
return false;
Print(r, group, map);

View File

@ -155,9 +155,7 @@ db_selection_print(Response &r, Partition &partition,
unsigned window_start, unsigned window_end,
Error &error)
{
const Database *db = partition.GetDatabase(error);
if (db == nullptr)
return false;
const Database &db = partition.GetDatabaseOrThrow();
unsigned i = 0;
@ -182,7 +180,7 @@ db_selection_print(Response &r, Partition &partition,
return !in_window || s(song, error2);
};
return db->Visit(selection, d, s, p, error);
return db.Visit(selection, d, s, p, error);
}
bool
@ -226,9 +224,7 @@ PrintUniqueTags(Response &r, Partition &partition,
const SongFilter *filter,
Error &error)
{
const Database *db = partition.GetDatabase(error);
if (db == nullptr)
return false;
const Database &db = partition.GetDatabaseOrThrow();
const DatabaseSelection selection("", true, filter);
@ -236,15 +232,15 @@ PrintUniqueTags(Response &r, Partition &partition,
using namespace std::placeholders;
const auto f = std::bind(PrintSongURIVisitor,
std::ref(r), std::ref(partition), _1);
return db->Visit(selection, f, error);
return db.Visit(selection, f, error);
} else {
assert(type < TAG_NUM_OF_ITEM_TYPES);
using namespace std::placeholders;
const auto f = std::bind(PrintUniqueTag, std::ref(r),
(TagType)type, _1);
return db->VisitUniqueTags(selection, (TagType)type,
group_mask,
f, error);
return db.VisitUniqueTags(selection, (TagType)type,
group_mask,
f, error);
}
}

View File

@ -41,11 +41,9 @@ bool
AddFromDatabase(Partition &partition, const DatabaseSelection &selection,
Error &error)
{
const Database *db = partition.instance.GetDatabase(error);
if (db == nullptr)
return false;
const Database &db = partition.instance.GetDatabaseOrThrow();
using namespace std::placeholders;
const auto f = std::bind(AddToQueue, std::ref(partition), _1);
return db->Visit(selection, f, error);
return db.Visit(selection, f, error);
}