database.hpp 6.26 KB
Newer Older
1 2
#pragma once

3 4 5
#include <biboumi.h>
#ifdef USE_DATABASE

6 7 8
#include <database/table.hpp>
#include <database/column.hpp>
#include <database/count_query.hpp>
9

10
#include <chrono>
11 12 13
#include <string>

#include <memory>
14

15 16 17

class Database
{
18
 public:
19
  using time_point = std::chrono::system_clock::time_point;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  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_";
52 53
    static constexpr auto options = "";
    Ports(): Column<std::string>("6667") {}};
54 55

  struct TlsPorts: Column<std::string> { static constexpr auto name = "ports_";
56 57
    static constexpr auto options = "";
    TlsPorts(): Column<std::string>("6697;6670") {} };
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  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_";
78 79
    static constexpr auto options = "";
    MaxHistoryLength(): Column<int>(20) {} };
80 81

  struct RecordHistory: Column<bool> { static constexpr auto name = "recordHistory_";
82 83
    static constexpr auto options = "";
    RecordHistory(): Column<bool>(true) {}};
84 85

  struct VerifyCert: Column<bool> { static constexpr auto name = "verifyCert_";
86 87
    static constexpr auto options = "";
    VerifyCert(): Column<bool>(true) {} };
88 89

  struct Persistent: Column<bool> { static constexpr auto name = "persistent_";
90 91
    static constexpr auto options = "";
    Persistent(): Column<bool>(false) {} };
92 93 94 95 96 97 98 99 100 101 102 103 104

  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;

105 106 107
  Database() = default;
  ~Database() = default;

louiz’'s avatar
louiz’ committed
108 109 110 111 112
  Database(const Database&) = delete;
  Database(Database&&) = delete;
  Database& operator=(const Database&) = delete;
  Database& operator=(Database&&) = delete;

113 114
  static GlobalOptions get_global_options(const std::string& owner);
  static IrcServerOptions get_irc_server_options(const std::string& owner,
115
                                                     const std::string& server);
116 117 118 119 120 121 122 123 124 125 126 127 128
  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<MucLogLine> 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);
129

130
  static void close();
131
  static void open(const std::string& filename);
132

133 134 135 136 137 138 139 140 141 142 143 144
  template <typename TableType>
  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;
145

146
 private:
147
  static std::string gen_uuid();
148 149
};
#endif /* USE_DATABASE */