From 50cadf3dac0d56ef8181d1800cc30f8dcb749141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 13 Jun 2017 10:38:39 +0200 Subject: Implement our own database ORM, and update the whole code to use it Entirely replace LiteSQL fix #3271 --- src/database/database.hpp | 153 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 115 insertions(+), 38 deletions(-) (limited to 'src/database/database.hpp') diff --git a/src/database/database.hpp b/src/database/database.hpp index b08a175..ebc4878 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -1,22 +1,101 @@ #pragma once - #include #ifdef USE_DATABASE -#include "biboudb.hpp" - -#include +#include +#include +#include -#include #include +#include + +#include -class Iid; class Database { -public: + public: using time_point = std::chrono::system_clock::time_point; + + struct Uuid: Column { static constexpr auto name = "uuid_"; + static constexpr auto options = ""; }; + + struct Owner: Column { static constexpr auto name = "owner_"; + static constexpr auto options = ""; }; + + struct IrcChanName: Column { static constexpr auto name = "ircChanName_"; + static constexpr auto options = ""; }; + + struct Channel: Column { static constexpr auto name = "channel_"; + static constexpr auto options = ""; }; + + struct IrcServerName: Column { static constexpr auto name = "ircServerName_"; + static constexpr auto options = ""; }; + + struct Server: Column { static constexpr auto name = "server_"; + static constexpr auto options = ""; }; + + struct Date: Column { static constexpr auto name = "date_"; + static constexpr auto options = ""; }; + + struct Body: Column { static constexpr auto name = "body_"; + static constexpr auto options = ""; }; + + struct Nick: Column { static constexpr auto name = "nick_"; + static constexpr auto options = ""; }; + + struct Pass: Column { static constexpr auto name = "pass_"; + static constexpr auto options = ""; }; + + struct Ports: Column { static constexpr auto name = "tlsPorts_"; + static constexpr auto options = ""; }; + + struct TlsPorts: Column { static constexpr auto name = "ports_"; + static constexpr auto options = ""; }; + + struct Username: Column { static constexpr auto name = "username_"; + static constexpr auto options = ""; }; + + struct Realname: Column { static constexpr auto name = "realname_"; + static constexpr auto options = ""; }; + + struct AfterConnectionCommand: Column { static constexpr auto name = "afterConnectionCommand_"; + static constexpr auto options = ""; }; + + struct TrustedFingerprint: Column { static constexpr auto name = "trustedFingerprint_"; + static constexpr auto options = ""; }; + + struct EncodingOut: Column { static constexpr auto name = "encodingOut_"; + static constexpr auto options = ""; }; + + struct EncodingIn: Column { static constexpr auto name = "encodingIn_"; + static constexpr auto options = ""; }; + + struct MaxHistoryLength: Column { static constexpr auto name = "maxHistoryLength_"; + static constexpr auto options = ""; }; + + struct RecordHistory: Column { static constexpr auto name = "recordHistory_"; + static constexpr auto options = ""; }; + + struct VerifyCert: Column { static constexpr auto name = "verifyCert_"; + static constexpr auto options = ""; }; + + struct Persistent: Column { static constexpr auto name = "persistent_"; + static constexpr auto options = ""; }; + + using MucLogLineTable = Table; + using MucLogLine = MucLogLineTable::RowType; + + using GlobalOptionsTable = Table; + using GlobalOptions = GlobalOptionsTable::RowType; + + using IrcServerOptionsTable = Table; + using IrcServerOptions = IrcServerOptionsTable::RowType; + + using IrcChannelOptionsTable = Table; + 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 - static size_t count() - { - return litesql::select(*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); - static db::IrcChannelOptions get_irc_channel_options_with_server_default(const std::string& owner, - const std::string& server, - const std::string& channel); - static db::IrcChannelOptions get_irc_channel_options_with_server_and_global_default(const std::string& owner, - const std::string& server, - const std::string& channel); - static std::vector get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, - int limit=-1, const std::string& start="", const std::string& end=""); - static std::string store_muc_message(const std::string& owner, const Iid& iid, - time_point date, const std::string& body, const std::string& nick); + static IrcChannelOptions get_irc_channel_options(const std::string& owner, + const std::string& server, + const std::string& channel); + static IrcChannelOptions get_irc_channel_options_with_server_default(const std::string& owner, + const std::string& server, + const std::string& channel); + static IrcChannelOptions get_irc_channel_options_with_server_and_global_default(const std::string& owner, + const std::string& server, + const std::string& channel); + static std::vector get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, + int limit=-1, const std::string& start="", const std::string& end=""); + static std::string store_muc_message(const std::string& owner, const std::string& chan_name, const std::string& server_name, + time_point date, const std::string& body, const std::string& nick); static void close(); - static void open(const std::string& filename, const std::string& db_type="sqlite3"); + static void open(const std::string& filename); + template + static std::size_t count(const TableType& table) + { + CountQuery query{table.get_name()}; + return query.execute(Database::db); + } + + static MucLogLineTable muc_log_lines; + static GlobalOptionsTable global_options; + static IrcServerOptionsTable irc_server_options; + static IrcChannelOptionsTable irc_channel_options; + static sqlite3* db; -private: + private: static std::string gen_uuid(); - static std::unique_ptr db; }; #endif /* USE_DATABASE */ - - -- cgit v1.2.3 From 369ccb037619871403b14c959bbb359332133810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 13 Jun 2017 11:18:05 +0200 Subject: Add default values for the database columns --- src/database/database.hpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/database/database.hpp') diff --git a/src/database/database.hpp b/src/database/database.hpp index ebc4878..a0611c1 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -49,10 +49,12 @@ class Database static constexpr auto options = ""; }; struct Ports: Column { static constexpr auto name = "tlsPorts_"; - static constexpr auto options = ""; }; + static constexpr auto options = ""; + Ports(): Column("6667") {}}; struct TlsPorts: Column { static constexpr auto name = "ports_"; - static constexpr auto options = ""; }; + static constexpr auto options = ""; + TlsPorts(): Column("6697;6670") {} }; struct Username: Column { static constexpr auto name = "username_"; static constexpr auto options = ""; }; @@ -73,16 +75,20 @@ class Database static constexpr auto options = ""; }; struct MaxHistoryLength: Column { static constexpr auto name = "maxHistoryLength_"; - static constexpr auto options = ""; }; + static constexpr auto options = ""; + MaxHistoryLength(): Column(20) {} }; struct RecordHistory: Column { static constexpr auto name = "recordHistory_"; - static constexpr auto options = ""; }; + static constexpr auto options = ""; + RecordHistory(): Column(true) {}}; struct VerifyCert: Column { static constexpr auto name = "verifyCert_"; - static constexpr auto options = ""; }; + static constexpr auto options = ""; + VerifyCert(): Column(true) {} }; struct Persistent: Column { static constexpr auto name = "persistent_"; - static constexpr auto options = ""; }; + static constexpr auto options = ""; + Persistent(): Column(false) {} }; using MucLogLineTable = Table; using MucLogLine = MucLogLineTable::RowType; -- cgit v1.2.3 From dac19da4c791a6c16cde09e61841fd7f6b6268d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 14 Jun 2017 10:09:03 +0200 Subject: Fix an inversion of tlsPorts_ and ports_ --- src/database/database.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/database/database.hpp') diff --git a/src/database/database.hpp b/src/database/database.hpp index a0611c1..3a99867 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -48,11 +48,11 @@ class Database struct Pass: Column { static constexpr auto name = "pass_"; static constexpr auto options = ""; }; - struct Ports: Column { static constexpr auto name = "tlsPorts_"; + struct Ports: Column { static constexpr auto name = "ports_"; static constexpr auto options = ""; - Ports(): Column("6667") {}}; + Ports(): Column("6667") {} }; - struct TlsPorts: Column { static constexpr auto name = "ports_"; + struct TlsPorts: Column { static constexpr auto name = "tlsPorts_"; static constexpr auto options = ""; TlsPorts(): Column("6697;6670") {} }; -- cgit v1.2.3 From 2677ac42e8d2e1cf162fec773a9acb453bef8b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 14 Jun 2017 10:31:45 +0200 Subject: Fix compilation (many warnings, and a linkage error) with clang++ --- src/database/database.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/database/database.hpp') diff --git a/src/database/database.hpp b/src/database/database.hpp index 3a99867..1ad62fc 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -131,7 +131,7 @@ class Database static void open(const std::string& filename); template - static std::size_t count(const TableType& table) + static int64_t count(const TableType& table) { CountQuery query{table.get_name()}; return query.execute(Database::db); -- cgit v1.2.3 From 40db183e3753486deaa43e950fff38579c5ced6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 16 Jun 2017 09:52:40 +0200 Subject: Using OptionalBool, add RecordHistoryOptional col into IrcChannelOptions table ref #3269 --- src/database/database.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/database/database.hpp') diff --git a/src/database/database.hpp b/src/database/database.hpp index 1ad62fc..28b6b1b 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -7,6 +7,8 @@ #include #include +#include + #include #include @@ -82,6 +84,9 @@ class Database static constexpr auto options = ""; RecordHistory(): Column(true) {}}; + struct RecordHistoryOptional: Column { static constexpr auto name = "recordHistory_"; + static constexpr auto options = ""; }; + struct VerifyCert: Column { static constexpr auto name = "verifyCert_"; static constexpr auto options = ""; VerifyCert(): Column(true) {} }; @@ -99,7 +104,7 @@ class Database using IrcServerOptionsTable = Table; using IrcServerOptions = IrcServerOptionsTable::RowType; - using IrcChannelOptionsTable = Table; + using IrcChannelOptionsTable = Table; using IrcChannelOptions = IrcChannelOptionsTable::RowType; Database() = default; -- cgit v1.2.3