From 88ae2599f6dbf655e8806c9b4619ec089425683b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 18 Sep 2015 21:49:54 +0200 Subject: Introduce an optional Database module Uses litesql --- src/database/database.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/database/database.hpp | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/database/database.cpp create mode 100644 src/database/database.hpp (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp new file mode 100644 index 0000000..e16465c --- /dev/null +++ b/src/database/database.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +using namespace std::string_literals; + +std::unique_ptr Database::db; + +db::BibouDB& Database::get_db() +{ + if (!Database::db) + { + const std::string db_filename = Config::get("db_name", + xdg_data_path("biboumi.sqlite")); + // log_info("Opening database: " << db_filename); + std::cout << "Opening database: " << db_filename << std::endl; + Database::db = std::make_unique("sqlite3", + "database="s + db_filename); + } + + if (Database::db->needsUpgrade()) + Database::db->upgrade(); + + return *Database::db.get(); +} + +void Database::set_verbose(const bool val) +{ + Database::get_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(), + db::IrcServerOptions::Owner == owner && + db::IrcServerOptions::Server == server).one(); + return options; + } catch (const litesql::NotFound& e) { + db::IrcServerOptions options(Database::get_db()); + options.owner = owner; + options.server = server; + // options.update(); + return options; + } +} + +void Database::close() +{ + Database::db.reset(nullptr); +} diff --git a/src/database/database.hpp b/src/database/database.hpp new file mode 100644 index 0000000..d8dc735 --- /dev/null +++ b/src/database/database.hpp @@ -0,0 +1,47 @@ +#ifndef DATABASE_HPP_INCLUDED +#define DATABASE_HPP_INCLUDED + +#include +#ifdef USE_DATABASE + +#include "biboudb.hpp" + +#include + +#include + +class Database +{ +public: + Database() = default; + ~Database() = default; + + static void set_verbose(const bool val); + + template + static size_t count() + { + return litesql::select(Database::get_db()).count(); + } + /** + * Return the object from the db. Create it beforehand (with all default + * values) if it is not already present. + */ + static db::IrcServerOptions get_irc_server_options(const std::string& owner, + const std::string& server); + + static void close(); + +private: + static std::unique_ptr db; + + static db::BibouDB& get_db(); + + Database(const Database&) = delete; + Database(Database&&) = delete; + Database& operator=(const Database&) = delete; + Database& operator=(Database&&) = delete; +}; +#endif /* USE_DATABASE */ + +#endif /* DATABASE_HPP_INCLUDED */ -- cgit v1.2.3 From 74c73799ab83ac1569ddee7a3dddb12df3bc8a6d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 24 Oct 2015 15:17:53 +0200 Subject: Only compile database.cpp if configured with litesql --- src/database/database.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index e16465c..8d09788 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -1,3 +1,6 @@ +#include "biboumi.h" +#ifdef USE_DATABASE + #include #include #include @@ -52,3 +55,5 @@ void Database::close() { Database::db.reset(nullptr); } + +#endif -- cgit v1.2.3 From 580b721ba580d27be94b8977e8bbadf359feb2a3 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 29 Oct 2015 03:35:25 +0100 Subject: =?UTF-8?q?Remove=20a=20write=20to=20std::cout=20from=20Database?= =?UTF-8?q?=E2=80=99s=20constructor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/database.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 8d09788..fd18417 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -17,8 +17,6 @@ db::BibouDB& Database::get_db() { const std::string db_filename = Config::get("db_name", xdg_data_path("biboumi.sqlite")); - // log_info("Opening database: " << db_filename); - std::cout << "Opening database: " << db_filename << std::endl; Database::db = std::make_unique("sqlite3", "database="s + db_filename); } -- cgit v1.2.3 From 421c960df501b40e836a783400ab00dc60c3fdae Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 24 Dec 2015 21:39:53 +0100 Subject: Add a ChannelOptions table in the DB And a way to retrieve its values, defaulting on the ServerOptions for unset values. --- src/database/database.cpp | 33 +++++++++++++++++++++++++++++++++ src/database/database.hpp | 6 ++++++ 2 files changed, 39 insertions(+) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index fd18417..0c7f425 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -49,6 +49,39 @@ db::IrcServerOptions Database::get_irc_server_options(const std::string& owner, } } +db::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner, + const std::string& server, + const std::string& channel) +{ + try { + auto options = litesql::select(Database::get_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()); + options.owner = owner; + options.server = server; + options.channel = channel; + return options; + } +} + +db::IrcChannelOptions Database::get_irc_channel_options_with_server_default(const std::string& owner, + const std::string& server, + const std::string& channel) +{ + auto coptions = Database::get_irc_channel_options(owner, server, channel); + auto soptions = Database::get_irc_server_options(owner, server); + if (coptions.encodingIn.value().empty()) + coptions.encodingIn = soptions.encodingIn; + if (coptions.encodingOut.value().empty()) + coptions.encodingOut = soptions.encodingOut; + + return coptions; +} + void Database::close() { Database::db.reset(nullptr); diff --git a/src/database/database.hpp b/src/database/database.hpp index d8dc735..fc957bd 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -29,6 +29,12 @@ public: */ static db::IrcServerOptions get_irc_server_options(const std::string& owner, const std::string& server); + static db::IrcChannelOptions get_irc_channel_options(const std::string& owner, + const std::string& server, + const std::string& channel); + static db::IrcChannelOptions get_irc_channel_options_with_server_default(const std::string& owner, + const std::string& server, + const std::string& channel); static void close(); -- cgit v1.2.3 From af42073830087d97385e507f27f601e8769541b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 4 May 2016 14:16:40 +0200 Subject: Style fix Move all constructors at the top of classes --- src/database/database.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/database') diff --git a/src/database/database.hpp b/src/database/database.hpp index fc957bd..7bd41a3 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -16,6 +16,11 @@ public: Database() = default; ~Database() = default; + Database(const Database&) = delete; + Database(Database&&) = delete; + Database& operator=(const Database&) = delete; + Database& operator=(Database&&) = delete; + static void set_verbose(const bool val); template @@ -42,11 +47,6 @@ private: static std::unique_ptr db; static db::BibouDB& get_db(); - - Database(const Database&) = delete; - Database(Database&&) = delete; - Database& operator=(const Database&) = delete; - Database& operator=(Database&&) = delete; }; #endif /* USE_DATABASE */ -- cgit v1.2.3 From 81f8f45b371d1a0ef72c2768fbd1f9188fe83616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 4 Jul 2016 17:53:53 +0200 Subject: Replace all include guards by #pragma once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s $CURRENT_YEAR --- src/database/database.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/database') diff --git a/src/database/database.hpp b/src/database/database.hpp index 7bd41a3..0131669 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -1,5 +1,5 @@ -#ifndef DATABASE_HPP_INCLUDED -#define DATABASE_HPP_INCLUDED +#pragma once + #include #ifdef USE_DATABASE @@ -50,4 +50,4 @@ private: }; #endif /* USE_DATABASE */ -#endif /* DATABASE_HPP_INCLUDED */ + -- cgit v1.2.3 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