mpd/src/DatabaseGlue.cxx

152 lines
2.9 KiB
C++
Raw Normal View History

/*
2013-10-30 23:37:06 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "DatabaseGlue.hxx"
#include "DatabaseSimple.hxx"
#include "DatabaseRegistry.hxx"
2013-08-10 19:43:27 +02:00
#include "DatabaseError.hxx"
2013-01-02 22:52:08 +01:00
#include "Directory.hxx"
#include "util/Error.hxx"
#include "ConfigData.hxx"
2013-10-02 12:14:07 +02:00
#include "Stats.hxx"
2012-07-30 07:26:08 +02:00
#include "DatabasePlugin.hxx"
#include "db/SimpleDatabasePlugin.hxx"
#include <assert.h>
#include <string.h>
2013-10-02 12:14:07 +02:00
2012-07-30 07:26:08 +02:00
static Database *db;
static bool db_is_open;
static bool is_simple;
bool
DatabaseGlobalInit(const config_param &param, Error &error)
{
2013-10-19 18:19:03 +02:00
assert(db == nullptr);
assert(!db_is_open);
const char *plugin_name =
param.GetBlockValue("plugin", "simple");
is_simple = strcmp(plugin_name, "simple") == 0;
const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
2013-10-19 18:19:03 +02:00
if (plugin == nullptr) {
error.Format(db_domain,
"No such database plugin: %s", plugin_name);
return false;
}
db = plugin->create(param, error);
2013-10-19 18:19:03 +02:00
return db != nullptr;
}
void
DatabaseGlobalDeinit(void)
{
if (db_is_open)
2012-07-30 07:26:08 +02:00
db->Close();
2013-10-19 18:19:03 +02:00
if (db != nullptr)
2012-07-30 07:26:08 +02:00
delete db;
}
const Database *
GetDatabase()
{
2013-10-19 18:19:03 +02:00
assert(db == nullptr || db_is_open);
return db;
}
const Database *
GetDatabase(Error &error)
{
assert(db == nullptr || db_is_open);
if (db == nullptr)
error.Set(db_domain, DB_DISABLED, "No database");
return db;
}
bool
db_is_simple(void)
{
2013-10-19 18:19:03 +02:00
assert(db == nullptr || db_is_open);
return is_simple;
}
Directory *
db_get_root(void)
{
2013-10-19 18:19:03 +02:00
assert(db != nullptr);
assert(db_is_simple());
2012-07-30 07:26:08 +02:00
return ((SimpleDatabase *)db)->GetRoot();
}
Directory *
db_get_directory(const char *name)
{
2013-10-19 18:19:03 +02:00
if (db == nullptr)
return nullptr;
Directory *music_root = db_get_root();
2013-10-19 18:19:03 +02:00
if (name == nullptr)
return music_root;
2013-01-02 23:06:10 +01:00
return music_root->LookupDirectory(name);
}
bool
db_save(Error &error)
{
2013-10-19 18:19:03 +02:00
assert(db != nullptr);
assert(db_is_open);
assert(db_is_simple());
return ((SimpleDatabase *)db)->Save(error);
}
bool
DatabaseGlobalOpen(Error &error)
{
2013-10-19 18:19:03 +02:00
assert(db != nullptr);
assert(!db_is_open);
2012-07-30 07:26:08 +02:00
if (!db->Open(error))
return false;
db_is_open = true;
return true;
}
bool
db_exists()
{
2013-10-19 18:19:03 +02:00
assert(db != nullptr);
assert(db_is_open);
assert(db_is_simple());
return ((SimpleDatabase *)db)->GetUpdateStamp() > 0;
}