2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-10-30 23:37:06 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-08 11:07:35 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-08 11:07:35 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2012-08-02 18:39:17 +02:00
|
|
|
#include "DatabaseGlue.hxx"
|
2013-01-03 00:25:15 +01:00
|
|
|
#include "DatabaseSimple.hxx"
|
2012-08-08 08:46:16 +02:00
|
|
|
#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"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#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"
|
2008-10-08 11:07:35 +02:00
|
|
|
|
|
|
|
#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;
|
2011-09-05 23:03:05 +02:00
|
|
|
static bool db_is_open;
|
2013-01-02 19:19:40 +01:00
|
|
|
static bool is_simple;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
DatabaseGlobalInit(const config_param ¶m, Error &error)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db == nullptr);
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(!db_is_open);
|
2009-01-18 16:09:01 +01:00
|
|
|
|
2012-08-08 08:46:16 +02:00
|
|
|
const char *plugin_name =
|
2013-08-04 13:51:27 +02:00
|
|
|
param.GetBlockValue("plugin", "simple");
|
2013-01-02 19:19:40 +01:00
|
|
|
is_simple = strcmp(plugin_name, "simple") == 0;
|
|
|
|
|
2012-08-08 08:46:16 +02:00
|
|
|
const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (plugin == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(db_domain,
|
|
|
|
"No such database plugin: %s", plugin_name);
|
2012-08-08 08:46:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
db = plugin->create(param, error);
|
2013-10-19 18:19:03 +02:00
|
|
|
return db != nullptr;
|
2009-01-18 16:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-03 00:24:45 +01:00
|
|
|
DatabaseGlobalDeinit(void)
|
2009-01-18 16:09:01 +01:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db_is_open)
|
2012-07-30 07:26:08 +02:00
|
|
|
db->Close();
|
2009-01-18 16:56:07 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (db != nullptr)
|
2012-07-30 07:26:08 +02:00
|
|
|
delete db;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 18:39:17 +02:00
|
|
|
const Database *
|
|
|
|
GetDatabase()
|
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db == nullptr || db_is_open);
|
2012-08-02 18:39:17 +02:00
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2012-08-22 21:40:20 +02:00
|
|
|
const Database *
|
2013-08-10 18:02:44 +02:00
|
|
|
GetDatabase(Error &error)
|
2012-08-22 21:40:20 +02:00
|
|
|
{
|
|
|
|
assert(db == nullptr || db_is_open);
|
|
|
|
|
|
|
|
if (db == nullptr)
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(db_domain, DB_DISABLED, "No database");
|
2012-08-22 21:40:20 +02:00
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2012-08-08 08:19:30 +02:00
|
|
|
bool
|
|
|
|
db_is_simple(void)
|
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db == nullptr || db_is_open);
|
2012-08-08 08:19:30 +02:00
|
|
|
|
2013-01-02 19:19:40 +01:00
|
|
|
return is_simple;
|
2012-08-08 08:19:30 +02:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_root(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db != nullptr);
|
2012-08-08 08:19:30 +02:00
|
|
|
assert(db_is_simple());
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return ((SimpleDatabase *)db)->GetRoot();
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_directory(const char *name)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
if (db == nullptr)
|
|
|
|
return nullptr;
|
2009-01-18 16:56:07 +01:00
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *music_root = db_get_root();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (name == nullptr)
|
2008-10-08 11:07:35 +02:00
|
|
|
return music_root;
|
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
return music_root->LookupDirectory(name);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
db_save(Error &error)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db != nullptr);
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db_is_open);
|
2012-08-08 08:19:30 +02:00
|
|
|
assert(db_is_simple());
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
return ((SimpleDatabase *)db)->Save(error);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
DatabaseGlobalOpen(Error &error)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db != nullptr);
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(!db_is_open);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
if (!db->Open(error))
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
db_is_open = true;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
return true;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:35:29 +01:00
|
|
|
bool
|
|
|
|
db_exists()
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(db != nullptr);
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db_is_open);
|
2012-08-08 08:19:30 +02:00
|
|
|
assert(db_is_simple());
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2013-11-22 00:35:29 +01:00
|
|
|
return ((SimpleDatabase *)db)->GetUpdateStamp() > 0;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|