Commit 50cadf3d authored by louiz’'s avatar louiz’

Implement our own database ORM, and update the whole code to use it

Entirely replace LiteSQL

fix #3271
parent 7ca95a09
<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "litesql.dtd">
<database name="BibouDB" namespace="db">
<object name="GlobalOptions">
<field name="owner" type="string" length="3071"/>
<field name="maxHistoryLength" type="integer" default="20"/>
<field name="recordHistory" type="boolean" default="true"/>
<index unique="true">
<indexfield name="owner"/>
</index>
</object>
<object name="IrcServerOptions">
<field name="owner" type="string" length="3071"/>
<field name="server" type="string" length="3071"/>
<field name="pass" type="string" length="1024" default=""/>
<field name="afterConnectionCommand" type="string" length="510" default=""/>
<field name="tlsPorts" type="string" length="4096" default="6697;6670" />
<field name="ports" type="string" length="4096" default="6667" />
<field name="username" type="string" length="1024" default=""/>
<field name="realname" type="string" length="1024" default=""/>
<field name="verifyCert" type="boolean" default="true"/>
<field name="trustedFingerprint" type="string"/>
<field name="encodingOut" type="string" default="ISO-8859-1"/>
<field name="encodingIn" type="string" default="ISO-8859-1"/>
<field name="maxHistoryLength" type="integer" default="20"/>
<index unique="true">
<indexfield name="owner"/>
<indexfield name="server"/>
</index>
</object>
<object name="IrcChannelOptions">
<field name="owner" type="string" length="3071"/>
<field name="server" type="string" length="3071"/>
<field name="channel" type="string" length="1024"/>
<field name="encodingOut" type="string"/>
<field name="encodingIn" type="string"/>
<field name="maxHistoryLength" type="integer" default="20"/>
<field name="persistent" type="boolean" default="false"/>
<index unique="true">
<indexfield name="owner"/>
<indexfield name="server"/>
<indexfield name="channel"/>
</index>
</object>
<object name="MucLogLine">
<field name="uuid" type="string" length="36" />
<!-- The bare JID of the user for which we stored the line. It's
the JID associated with the Bridge -->
<field name="owner" type="string" length="4096" />
<!-- The room IID -->
<field name="ircChanName" type="string" length="4096" />
<field name="ircServerName" type="string" length="4096" />
<field name="date" type="datetime" />
<field name="body" type="string" length="65536"/>
<field name="nick" type="string" length="4096" />
</object>
</database>
......@@ -23,7 +23,7 @@ static std::string in_encoding_for(const Bridge& bridge, const Iid& iid)
#ifdef USE_DATABASE
const auto jid = bridge.get_bare_jid();
auto options = Database::get_irc_channel_options_with_server_default(jid, iid.get_server(), iid.get_local());
return options.encodingIn.value();
return options.col<Database::EncodingIn>();
#else
(void)bridge;
(void)iid;
......@@ -32,13 +32,13 @@ static std::string in_encoding_for(const Bridge& bridge, const Iid& iid)
}
Bridge::Bridge(std::string user_jid, BiboumiComponent& xmpp, std::shared_ptr<Poller>& poller):
user_jid(std::move(user_jid)),
user_jid(std::move(user_jid)),
xmpp(xmpp),
poller(poller)
{
#ifdef USE_DATABASE
const auto options = Database::get_global_options(this->user_jid);
this->set_record_history(options.recordHistory.value());
this->set_record_history(options.col<Database::RecordHistory>());
#endif
}
......@@ -258,7 +258,7 @@ void Bridge::send_channel_message(const Iid& iid, const std::string& body)
#ifdef USE_DATABASE
const auto xmpp_body = this->make_xmpp_body(line);
if (this->record_history)
uuid = Database::store_muc_message(this->get_bare_jid(), iid, std::chrono::system_clock::now(),
uuid = Database::store_muc_message(this->get_bare_jid(), iid.get_local(), iid.get_server(), std::chrono::system_clock::now(),
std::get<0>(xmpp_body), irc->get_own_nick());
#endif
for (const auto& resource: this->resources_in_chan[iid.to_tuple()])
......@@ -436,7 +436,7 @@ void Bridge::leave_irc_channel(Iid&& iid, const std::string& status_message, con
#ifdef USE_DATABASE
const auto coptions = Database::get_irc_channel_options_with_server_default(this->user_jid,
iid.get_server(), iid.get_local());
persistent = coptions.persistent.value();
persistent = coptions.col<Database::Persistent>();
#endif
if (channel->joined && !channel->parting && !persistent)
{
......@@ -837,7 +837,7 @@ void Bridge::send_message(const Iid& iid, const std::string& nick, const std::st
#ifdef USE_DATABASE
const auto xmpp_body = this->make_xmpp_body(body, encoding);
if (!nick.empty() && this->record_history)
Database::store_muc_message(this->get_bare_jid(), iid, std::chrono::system_clock::now(),
Database::store_muc_message(this->get_bare_jid(), iid.get_local(), iid.get_server(), std::chrono::system_clock::now(),
std::get<0>(xmpp_body), nick);
#endif
for (const auto& resource: this->resources_in_chan[iid.to_tuple()])
......@@ -994,12 +994,12 @@ void Bridge::send_room_history(const std::string& hostname, std::string chan_nam
{
#ifdef USE_DATABASE
const auto coptions = Database::get_irc_channel_options_with_server_and_global_default(this->user_jid, hostname, chan_name);
const auto lines = Database::get_muc_logs(this->user_jid, chan_name, hostname, coptions.maxHistoryLength.value());
const auto lines = Database::get_muc_logs(this->user_jid, chan_name, hostname, coptions.col<Database::MaxHistoryLength>());
chan_name.append(utils::empty_if_fixed_server("%" + hostname));
for (const auto& line: lines)
{
const auto seconds = line.date.value().timeStamp();
this->xmpp.send_history_message(chan_name, line.nick.value(), line.body.value(),
const auto seconds = line.col<Database::Date>();
this->xmpp.send_history_message(chan_name, line.col<Database::Nick>(), line.col<Database::Body>(),
this->user_jid + "/" + resource, seconds);
}
#else
......
#pragma once
#include <cstdint>
template <typename T>
struct Column
{
using real_type = T;
T value;
};
struct Id: Column<std::size_t> { static constexpr auto name = "id_";
static constexpr auto options = "PRIMARY KEY AUTOINCREMENT"; };
#pragma once
#include <database/query.hpp>
#include <database/table.hpp>
#include <string>
#include <sqlite3.h>
struct CountQuery: public Query
{
CountQuery(std::string name):
Query("SELECT count(*) FROM ")
{
this->body += std::move(name);
}
std::size_t execute(sqlite3* db)
{
auto statement = this->prepare(db);
std::size_t res = 0;
if (sqlite3_step(statement) == SQLITE_ROW)
res = sqlite3_column_int64(statement, 0);
else
{
log_error("Count request didn’t return a result");
return 0;
}
if (sqlite3_step(statement) != SQLITE_DONE)
log_warning("Count request returned more than one result.");
log_debug("Returning count: ", res);
return res;
}
};
......@@ -2,175 +2,166 @@
#ifdef USE_DATABASE
#include <database/database.hpp>
#include <logger/logger.hpp>
#include <irc/iid.hpp>
#include <uuid/uuid.h>
#include <utils/get_first_non_empty.hpp>
#include <utils/time.hpp>
using namespace std::string_literals;
#include <sqlite3.h>
std::unique_ptr<db::BibouDB> Database::db;
sqlite3* Database::db;
Database::MucLogLineTable Database::muc_log_lines("MucLogLine_");
Database::GlobalOptionsTable Database::global_options("GlobalOptions_");
Database::IrcServerOptionsTable Database::irc_server_options("IrcServerOptions_");
Database::IrcChannelOptionsTable Database::irc_channel_options("IrcChannelOptions_");
void Database::open(const std::string& filename, const std::string& db_type)
void Database::open(const std::string& filename)
{
try
{
auto new_db = std::make_unique<db::BibouDB>(db_type,
"database="s + filename);
if (new_db->needsUpgrade())
new_db->upgrade();
Database::db = std::move(new_db);
} catch (const litesql::DatabaseError& e) {
log_error("Failed to open database ", filename, ". ", e.what());
throw;
}
auto res = sqlite3_open_v2(filename.data(), &Database::db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
log_debug("open: ", res);
Database::muc_log_lines.create(Database::db);
Database::global_options.create(Database::db);
Database::irc_server_options.create(Database::db);
Database::irc_channel_options.create(Database::db);
}
void Database::set_verbose(const bool val)
{
Database::db->verbose = val;
}
db::GlobalOptions Database::get_global_options(const std::string& owner)
Database::GlobalOptions Database::get_global_options(const std::string& owner)
{
try {
auto options = litesql::select<db::GlobalOptions>(*Database::db,
db::GlobalOptions::Owner == owner).one();
return options;
} catch (const litesql::NotFound& e) {
db::GlobalOptions options(*Database::db);
options.owner = owner;
return options;
}
auto request = Database::global_options.select().where() << Owner{} << "=" << owner;
Database::GlobalOptions options{Database::global_options.get_name()};
auto result = request.execute(Database::db);
if (result.size() == 1)
options = result.front();
else
options.col<Owner>() = owner;
return options;
}
db::IrcServerOptions Database::get_irc_server_options(const std::string& owner,
const std::string& server)
Database::IrcServerOptions Database::get_irc_server_options(const std::string& owner, const std::string& server)
{
try {
auto options = litesql::select<db::IrcServerOptions>(*Database::db,
db::IrcServerOptions::Owner == owner &&
db::IrcServerOptions::Server == server).one();
return options;
} catch (const litesql::NotFound& e) {
db::IrcServerOptions options(*Database::db);
options.owner = owner;
options.server = server;
// options.update();
return options;
}
auto request = Database::irc_server_options.select().where() << Owner{} << "=" << owner << " and " << Server{} << "=" << server;
Database::IrcServerOptions options{Database::irc_server_options.get_name()};
auto result = request.execute(Database::db);
if (result.size() == 1)
options = result.front();
else
{
options.col<Owner>() = owner;
options.col<Server>() = server;
}
return options;
}
db::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner,
const std::string& server,
const std::string& channel)
Database::IrcChannelOptions Database::get_irc_channel_options(const std::string& owner, const std::string& server, const std::string& channel)
{
try {
auto options = litesql::select<db::IrcChannelOptions>(*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::db);
options.owner = owner;
options.server = server;
options.channel = channel;
return options;
}
auto request = Database::irc_channel_options.select().where() << Owner{} << "=" << owner <<\
" and " << Server{} << "=" << server <<\
" and " << Channel{} << "=" << channel;
Database::IrcChannelOptions options{Database::irc_channel_options.get_name()};
auto result = request.execute(Database::db);
if (result.size() == 1)
options = result.front();
else
{
options.col<Owner>() = owner;
options.col<Server>() = server;
options.col<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)
Database::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);
coptions.encodingIn = get_first_non_empty(coptions.encodingIn.value(),
soptions.encodingIn.value());
coptions.encodingOut = get_first_non_empty(coptions.encodingOut.value(),
soptions.encodingOut.value());
coptions.col<EncodingIn>() = get_first_non_empty(coptions.col<EncodingIn>(),
soptions.col<EncodingIn>());
coptions.col<EncodingOut>() = get_first_non_empty(coptions.col<EncodingOut>(),
soptions.col<EncodingOut>());
coptions.maxHistoryLength = get_first_non_empty(coptions.maxHistoryLength.value(),
soptions.maxHistoryLength.value());
coptions.col<MaxHistoryLength>() = get_first_non_empty(coptions.col<MaxHistoryLength>(),
soptions.col<MaxHistoryLength>());
return coptions;
}
db::IrcChannelOptions Database::get_irc_channel_options_with_server_and_global_default(const std::string& owner,
const std::string& server,
const std::string& channel)
Database::IrcChannelOptions Database::get_irc_channel_options_with_server_and_global_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);
auto goptions = Database::get_global_options(owner);
coptions.encodingIn = get_first_non_empty(coptions.encodingIn.value(),
soptions.encodingIn.value());
coptions.encodingOut = get_first_non_empty(coptions.encodingOut.value(),
soptions.encodingOut.value());
coptions.col<EncodingIn>() = get_first_non_empty(coptions.col<EncodingIn>(),
soptions.col<EncodingIn>());
coptions.maxHistoryLength = get_first_non_empty(coptions.maxHistoryLength.value(),
soptions.maxHistoryLength.value(),
goptions.maxHistoryLength.value());
coptions.col<EncodingOut>() = get_first_non_empty(coptions.col<EncodingOut>(),
soptions.col<EncodingOut>());
coptions.col<MaxHistoryLength>() = get_first_non_empty(coptions.col<MaxHistoryLength>(),
soptions.col<MaxHistoryLength>(),
goptions.col<MaxHistoryLength>());
return coptions;
}
std::string Database::store_muc_message(const std::string& owner, const Iid& iid,
Database::time_point date,
const std::string& body,
const std::string& nick)
std::string Database::store_muc_message(const std::string& owner, const std::string& chan_name,
const std::string& server_name, Database::time_point date,
const std::string& body, const std::string& nick)
{
db::MucLogLine line(*Database::db);
auto line = Database::muc_log_lines.row();
auto uuid = Database::gen_uuid();
line.uuid = uuid;
line.owner = owner;
line.ircChanName = iid.get_local();
line.ircServerName = iid.get_server();
line.date = std::chrono::duration_cast<std::chrono::seconds>(date.time_since_epoch()).count();
line.body = body;
line.nick = nick;
line.col<Uuid>() = uuid;
line.col<Owner>() = owner;
line.col<IrcChanName>() = chan_name;
line.col<IrcServerName>() = server_name;
line.col<Date>() = std::chrono::duration_cast<std::chrono::seconds>(date.time_since_epoch()).count();
line.col<Body>() = body;
line.col<Nick>() = nick;
line.update();
line.save(Database::db);
return uuid;
}
std::vector<db::MucLogLine> Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server,
std::vector<Database::MucLogLine> Database::get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server,
int limit, const std::string& start, const std::string& end)
{
auto request = litesql::select<db::MucLogLine>(*Database::db,
db::MucLogLine::Owner == owner &&
db::MucLogLine::IrcChanName == chan_name &&
db::MucLogLine::IrcServerName == server);
request.orderBy(db::MucLogLine::Id, false);
auto request = Database::muc_log_lines.select().where() << Database::Owner{} << "=" << owner << \
" and " << Database::IrcChanName{} << "=" << chan_name << \
" and " << Database::IrcServerName{} << "=" << server;
if (limit >= 0)
request.limit(limit);
if (!start.empty())
{
const auto start_time = utils::parse_datetime(start);
if (start_time != -1)
request.where(db::MucLogLine::Date >= start_time);
request << " and " << Database::Date{} << ">=" << start_time;
}
if (!end.empty())
{
const auto end_time = utils::parse_datetime(end);
if (end_time != -1)
request.where(db::MucLogLine::Date <= end_time);
request << " and " << Database::Date{} << "<=" << end_time;
}
const auto& res = request.all();
return {res.crbegin(), res.crend()};
request.order_by() << Id{} << " DESC ";
if (limit >= 0)
request.limit() << limit;
auto result = request.execute(Database::db);
return {result.crbegin(), result.crend()};
}
void Database::close()
{
Database::db.reset(nullptr);
sqlite3_close_v2(Database::db);
}
std::string Database::gen_uuid()
......@@ -182,5 +173,4 @@ std::string Database::gen_uuid()
return uuid_str;
}
#endif
#endif
\ No newline at end of file
#pragma once
#include <biboumi.h>
#ifdef USE_DATABASE
#include "biboudb.hpp"
#include <memory>
#include <database/table.hpp>
#include <database/column.hpp>
#include <database/count_query.hpp>
#include <litesql.hpp>
#include <chrono>
#include <string>
#include <memory>
class Iid;
class Database
{
public:
public:
using time_point = std::chrono::system_clock::time_point;
struct Uuid: Column<std::string> { static constexpr auto name = "uuid_";
static constexpr auto options = ""; };
struct Owner: Column<std::string> { static constexpr auto name = "owner_";
static constexpr auto options = ""; };
struct IrcChanName: Column<std::string> { static constexpr auto name = "ircChanName_";
static constexpr auto options = ""; };
struct Channel: Column<std::string> { static constexpr auto name = "channel_";
static constexpr auto options = ""; };
struct IrcServerName: Column<std::string> { static constexpr auto name = "ircServerName_";
static constexpr auto options = ""; };
struct Server: Column<std::string> { static constexpr auto name = "server_";
static constexpr auto options = ""; };
struct Date: Column<time_point::rep> { static constexpr auto name = "date_";
static constexpr auto options = ""; };
struct Body: Column<std::string> { static constexpr auto name = "body_";
static constexpr auto options = ""; };
struct Nick: Column<std::string> { static constexpr auto name = "nick_";
static constexpr auto options = ""; };
struct Pass: Column<std::string> { static constexpr auto name = "pass_";
static constexpr auto options = ""; };
struct Ports: Column<std::string> { static constexpr auto name = "tlsPorts_";
static constexpr auto options = ""; };
struct TlsPorts: Column<std::string> { static constexpr auto name = "ports_";
static constexpr auto options = ""; };
struct Username: Column<std::string> { static constexpr auto name = "username_";
static constexpr auto options = ""; };
struct Realname: Column<std::string> { static constexpr auto name = "realname_";
static constexpr auto options = ""; };
struct AfterConnectionCommand: Column<std::string> { static constexpr auto name = "afterConnectionCommand_";
static constexpr auto options = ""; };
struct TrustedFingerprint: Column<std::string> { static constexpr auto name = "trustedFingerprint_";
static constexpr auto options = ""; };
struct EncodingOut: Column<std::string> { static constexpr auto name = "encodingOut_";
static constexpr auto options = ""; };
struct EncodingIn: Column<std::string> { static constexpr auto name = "encodingIn_";
static constexpr auto options = ""; };
struct MaxHistoryLength: Column<int> { static constexpr auto name = "maxHistoryLength_";
static constexpr auto options = ""; };
struct RecordHistory: Column<bool> { static constexpr auto name = "recordHistory_";
static constexpr auto options = ""; };
struct VerifyCert: Column<bool> { static constexpr auto name = "verifyCert_";
static constexpr auto options = ""; };
struct Persistent: Column<bool> { static constexpr auto name = "persistent_";
static constexpr auto options = ""; };
using MucLogLineTable = Table<Id, Uuid, Owner, IrcChanName, IrcServerName, Date, Body, Nick>;
using MucLogLine = MucLogLineTable::RowType;
using GlobalOptionsTable = Table<Id, Owner, MaxHistoryLength, RecordHistory>;
using GlobalOptions = GlobalOptionsTable::RowType;
using IrcServerOptionsTable = Table<Id, Owner, Server, Pass, AfterConnectionCommand, TlsPorts, Ports, Username, Realname, VerifyCert, TrustedFingerprint, EncodingOut, EncodingIn, MaxHistoryLength>;
using IrcServerOptions = IrcServerOptionsTable::RowType;
using IrcChannelOptionsTable = Table<Id, Owner, Server, Channel, EncodingOut, EncodingIn, MaxHistoryLength, Persistent>;
using IrcChannelOptions = IrcChannelOptionsTable::RowType;
Database() = default;
~Database() = default;
......@@ -25,42 +104,40 @@ public:
Database& operator=(const Database&) = delete;
Database& operator=(Database&&) = delete;
static void set_verbose(const bool val);
template<typename PersistentType>
static size_t count()
{
return litesql::select<PersistentType>(*Database::db).count();
}
/**
* Return the object from the db. Create it beforehand (with all default
* values) if it is not already present.
*/
static db::GlobalOptions get_global_options(const std::string& owner);
static db::IrcServerOptions get_irc_server_options(const std::string& owner,
static GlobalOptions get_global_options(const std::string& owner);
static 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);