diff options
Diffstat (limited to '')
-rw-r--r-- | src/DatabaseGlue.cxx (renamed from src/database.c) | 128 |
1 files changed, 63 insertions, 65 deletions
diff --git a/src/database.c b/src/DatabaseGlue.cxx index 8c903bb45..853e67b07 100644 --- a/src/database.c +++ b/src/DatabaseGlue.cxx @@ -18,17 +18,21 @@ */ #include "config.h" -#include "database.h" +#include "DatabaseGlue.hxx" +#include "DatabaseSimple.hxx" +#include "DatabaseRegistry.hxx" +#include "DatabaseSave.hxx" +#include "Directory.hxx" +#include "conf.h" + +extern "C" { #include "db_error.h" -#include "db_save.h" -#include "db_selection.h" -#include "db_visitor.h" -#include "db_plugin.h" -#include "db/simple_db_plugin.h" -#include "directory.h" #include "stats.h" -#include "conf.h" #include "glib_compat.h" +} + +#include "DatabasePlugin.hxx" +#include "db/SimpleDatabasePlugin.hxx" #include <glib.h> @@ -42,97 +46,89 @@ #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "database" -static struct db *db; +static Database *db; static bool db_is_open; +static bool is_simple; bool -db_init(const struct config_param *path, GError **error_r) +DatabaseGlobalInit(const config_param *param, GError **error_r) { assert(db == NULL); assert(!db_is_open); - if (path == NULL) - return true; + const char *plugin_name = + config_get_block_string(param, "plugin", "simple"); + is_simple = strcmp(plugin_name, "simple") == 0; - struct config_param *param = config_new_param("database", path->line); - config_add_block_param(param, "path", path->value, path->line); - - db = db_plugin_new(&simple_db_plugin, param, error_r); - - config_param_free(param); + const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name); + if (plugin == NULL) { + g_set_error(error_r, db_quark(), 0, + "No such database plugin: %s", plugin_name); + return false; + } + db = plugin->create(param, error_r); return db != NULL; } void -db_finish(void) +DatabaseGlobalDeinit(void) { if (db_is_open) - db_plugin_close(db); + db->Close(); if (db != NULL) - db_plugin_free(db); + delete db; } -struct directory * -db_get_root(void) +const Database * +GetDatabase() { - assert(db != NULL); + assert(db == NULL || db_is_open); - return simple_db_get_root(db); + return db; } -struct directory * -db_get_directory(const char *name) +const Database * +GetDatabase(GError **error_r) { - if (db == NULL) - return NULL; + assert(db == nullptr || db_is_open); - struct directory *music_root = db_get_root(); - if (name == NULL) - return music_root; + if (db == nullptr) + g_set_error_literal(error_r, db_quark(), DB_DISABLED, + "No database"); - struct directory *directory = - directory_lookup_directory(music_root, name); - return directory; + return db; } -struct song * -db_get_song(const char *file) +bool +db_is_simple(void) { - assert(file != NULL); + assert(db == NULL || db_is_open); - g_debug("get song: %s", file); - - if (db == NULL) - return NULL; - - return db_plugin_get_song(db, file, NULL); + return is_simple; } -bool -db_visit(const struct db_selection *selection, - const struct db_visitor *visitor, void *ctx, - GError **error_r) +Directory * +db_get_root(void) { - if (db == NULL) { - g_set_error_literal(error_r, db_quark(), DB_DISABLED, - "No database"); - return false; - } + assert(db != NULL); + assert(db_is_simple()); - return db_plugin_visit(db, selection, visitor, ctx, error_r); + return ((SimpleDatabase *)db)->GetRoot(); } -bool -db_walk(const char *uri, - const struct db_visitor *visitor, void *ctx, - GError **error_r) +Directory * +db_get_directory(const char *name) { - struct db_selection selection; - db_selection_init(&selection, uri, true); + if (db == NULL) + return NULL; + + Directory *music_root = db_get_root(); + if (name == NULL) + return music_root; - return db_visit(&selection, visitor, ctx, error_r); + return music_root->LookupDirectory(name); } bool @@ -140,17 +136,18 @@ db_save(GError **error_r) { assert(db != NULL); assert(db_is_open); + assert(db_is_simple()); - return simple_db_save(db, error_r); + return ((SimpleDatabase *)db)->Save(error_r); } bool -db_load(GError **error) +DatabaseGlobalOpen(GError **error) { assert(db != NULL); assert(!db_is_open); - if (!db_plugin_open(db, error)) + if (!db->Open(error)) return false; db_is_open = true; @@ -165,6 +162,7 @@ db_get_mtime(void) { assert(db != NULL); assert(db_is_open); + assert(db_is_simple()); - return simple_db_get_mtime(db); + return ((SimpleDatabase *)db)->GetLastModified(); } |