Introduce an optional Database module
Uses litesql
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include <database/database.hpp>
|
||||
#include <config/config.hpp>
|
||||
#include <utils/xdg.hpp>
|
||||
#include <logger/logger.hpp>
|
||||
#include <string>
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
std::unique_ptr<db::BibouDB> 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<db::BibouDB>("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<db::IrcServerOptions>(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);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef DATABASE_HPP_INCLUDED
|
||||
#define DATABASE_HPP_INCLUDED
|
||||
|
||||
#include <biboumi.h>
|
||||
#ifdef USE_DATABASE
|
||||
|
||||
#include "biboudb.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <litesql.hpp>
|
||||
|
||||
class Database
|
||||
{
|
||||
public:
|
||||
Database() = default;
|
||||
~Database() = default;
|
||||
|
||||
static void set_verbose(const bool val);
|
||||
|
||||
template<typename PersistentType>
|
||||
static size_t count()
|
||||
{
|
||||
return litesql::select<PersistentType>(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::BibouDB> 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 */
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <xmpp/xmpp_component.hpp>
|
||||
#include <utils/timed_events.hpp>
|
||||
#include <database/database.hpp>
|
||||
#include <xmpp/xmpp_parser.hpp>
|
||||
#include <utils/encoding.hpp>
|
||||
#include <logger/logger.hpp>
|
||||
@@ -16,12 +17,15 @@
|
||||
#include <utils/xdg.hpp>
|
||||
#include <xmpp/jid.hpp>
|
||||
#include <irc/iid.hpp>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "biboumi.h"
|
||||
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
|
||||
@@ -403,8 +407,39 @@ int main()
|
||||
assert(iid6.is_channel);
|
||||
assert(!iid6.is_user);
|
||||
}
|
||||
#ifdef USE_DATABASE
|
||||
{
|
||||
std::cout << color << "Testing the Database…" << reset << std::endl;
|
||||
// Remove any potential existing db
|
||||
unlink("./test.db");
|
||||
Config::set("db_name", "test.db");
|
||||
Database::set_verbose(true);
|
||||
auto o = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
|
||||
o.requireTls = false;
|
||||
o.update();
|
||||
auto a = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
|
||||
assert(a.requireTls == false);
|
||||
auto b = Database::get_irc_server_options("moumou@example.com", "irc.example.com");
|
||||
assert(b.requireTls == true);
|
||||
|
||||
// b does not yet exist in the db, the object is created but not yet
|
||||
// inserted
|
||||
assert(1 == Database::count<db::IrcServerOptions>());
|
||||
|
||||
b.update();
|
||||
assert(2 == Database::count<db::IrcServerOptions>());
|
||||
|
||||
assert(b.pass == "");
|
||||
assert(b.pass.value() == "");
|
||||
|
||||
std::vector<litesql::FieldType> ftypes;
|
||||
db::IrcServerOptions::getFieldTypes(ftypes);
|
||||
for (const auto& type: ftypes)
|
||||
{
|
||||
std::cout << type.type() << std::endl;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
{
|
||||
std::cout << color << "Testing the xdg_path function…" << reset << std::endl;
|
||||
std::string res;
|
||||
|
||||
Reference in New Issue
Block a user