From 4c1b9abe7e230a39b119bdc45ebcd5e677fad488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 12 Jul 2016 00:31:57 +0200 Subject: Properly catch and handle database errors Do not use a singleton for the database. fix #3203 --- src/database/database.cpp | 33 +++++++++++++++------------------ src/database/database.hpp | 6 +++--- 2 files changed, 18 insertions(+), 21 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 0c7f425..61e1b47 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -2,8 +2,6 @@ #ifdef USE_DATABASE #include -#include -#include #include #include @@ -11,37 +9,36 @@ using namespace std::string_literals; std::unique_ptr Database::db; -db::BibouDB& Database::get_db() +void Database::open(const std::string& filename, const std::string& db_type) { - if (!Database::db) + try { - const std::string db_filename = Config::get("db_name", - xdg_data_path("biboumi.sqlite")); - Database::db = std::make_unique("sqlite3", - "database="s + db_filename); + auto new_db = std::make_unique(db_type, + "database="s + filename); + if (new_db->needsUpgrade()) + new_db->upgrade(); + Database::db.reset(new_db.release()); + } catch (const litesql::DatabaseError& e) { + log_error("Failed to open database ", filename, ". ", e.what()); + throw; } - - if (Database::db->needsUpgrade()) - Database::db->upgrade(); - - return *Database::db.get(); } void Database::set_verbose(const bool val) { - Database::get_db().verbose = val; + Database::db->verbose = val; } db::IrcServerOptions Database::get_irc_server_options(const std::string& owner, const std::string& server) { try { - auto options = litesql::select(Database::get_db(), + auto options = litesql::select(*Database::db, db::IrcServerOptions::Owner == owner && db::IrcServerOptions::Server == server).one(); return options; } catch (const litesql::NotFound& e) { - db::IrcServerOptions options(Database::get_db()); + db::IrcServerOptions options(*Database::db); options.owner = owner; options.server = server; // options.update(); @@ -54,13 +51,13 @@ db::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner const std::string& channel) { try { - auto options = litesql::select(Database::get_db(), + auto options = litesql::select(*Database::db, db::IrcChannelOptions::Owner == owner && db::IrcChannelOptions::Server == server && db::IrcChannelOptions::Channel == channel).one(); return options; } catch (const litesql::NotFound& e) { - db::IrcChannelOptions options(Database::get_db()); + db::IrcChannelOptions options(*Database::db); options.owner = owner; options.server = server; options.channel = channel; diff --git a/src/database/database.hpp b/src/database/database.hpp index 0131669..7173bcd 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -26,7 +26,7 @@ public: template static size_t count() { - return litesql::select(Database::get_db()).count(); + return litesql::select(*Database::db).count(); } /** * Return the object from the db. Create it beforehand (with all default @@ -42,11 +42,11 @@ public: const std::string& channel); static void close(); + static void open(const std::string& filename, const std::string& db_type="sqlite3"); + private: static std::unique_ptr db; - - static db::BibouDB& get_db(); }; #endif /* USE_DATABASE */ -- cgit v1.2.3