database.hpp 5.57 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 11
#include <utils/optional_bool.hpp>

12
#include <chrono>
13 14 15
#include <string>

#include <memory>
16

17 18 19

class Database
{
20
 public:
21
  using time_point = std::chrono::system_clock::time_point;
22

23
  struct Uuid: Column<std::string> { static constexpr auto name = "uuid_"; };
24

25
  struct Owner: Column<std::string> { static constexpr auto name = "owner_"; };
26

27
  struct IrcChanName: Column<std::string> { static constexpr auto name = "ircChanName_"; };
28

29
  struct Channel: Column<std::string> { static constexpr auto name = "channel_"; };
30

31
  struct IrcServerName: Column<std::string> { static constexpr auto name = "ircServerName_"; };
32

33
  struct Server: Column<std::string> { static constexpr auto name = "server_"; };
34

35
  struct Date: Column<time_point::rep> { static constexpr auto name = "date_"; };
36

37
  struct Body: Column<std::string> { static constexpr auto name = "body_"; };
38

39
  struct Nick: Column<std::string> { static constexpr auto name = "nick_"; };
40

41
  struct Pass: Column<std::string> { static constexpr auto name = "pass_"; };
42

43 44
  struct Ports: Column<std::string> { static constexpr auto name = "ports_";
    Ports(): Column<std::string>("6667") {} };
45

46
  struct TlsPorts: Column<std::string> { static constexpr auto name = "tlsPorts_";
47
    TlsPorts(): Column<std::string>("6697;6670") {} };
48

49
  struct Username: Column<std::string> { static constexpr auto name = "username_"; };
50

51
  struct Realname: Column<std::string> { static constexpr auto name = "realname_"; };
52

53
  struct AfterConnectionCommand: Column<std::string> { static constexpr auto name = "afterConnectionCommand_"; };
54

55
  struct TrustedFingerprint: Column<std::string> { static constexpr auto name = "trustedFingerprint_"; };
56

57
  struct EncodingOut: Column<std::string> { static constexpr auto name = "encodingOut_"; };
58

59
  struct EncodingIn: Column<std::string> { static constexpr auto name = "encodingIn_"; };
60 61

  struct MaxHistoryLength: Column<int> { static constexpr auto name = "maxHistoryLength_";
62
    MaxHistoryLength(): Column<int>(20) {} };
63 64

  struct RecordHistory: Column<bool> { static constexpr auto name = "recordHistory_";
65
    RecordHistory(): Column<bool>(true) {}};
66

67
  struct RecordHistoryOptional: Column<OptionalBool> { static constexpr auto name = "recordHistory_"; };
68

69
  struct VerifyCert: Column<bool> { static constexpr auto name = "verifyCert_";
70
    VerifyCert(): Column<bool>(true) {} };
71 72

  struct Persistent: Column<bool> { static constexpr auto name = "persistent_";
73
    Persistent(): Column<bool>(false) {} };
74 75 76 77

  using MucLogLineTable = Table<Id, Uuid, Owner, IrcChanName, IrcServerName, Date, Body, Nick>;
  using MucLogLine = MucLogLineTable::RowType;

78
  using GlobalOptionsTable = Table<Id, Owner, MaxHistoryLength, RecordHistory, Persistent>;
79 80 81 82 83
  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;

84
  using IrcChannelOptionsTable = Table<Id, Owner, Server, Channel, EncodingOut, EncodingIn, MaxHistoryLength, Persistent, RecordHistoryOptional>;
85 86
  using IrcChannelOptions = IrcChannelOptionsTable::RowType;

87 88 89
  Database() = default;
  ~Database() = default;

louiz’'s avatar
louiz’ committed
90 91 92 93 94
  Database(const Database&) = delete;
  Database(Database&&) = delete;
  Database& operator=(const Database&) = delete;
  Database& operator=(Database&&) = delete;

95 96
  static GlobalOptions get_global_options(const std::string& owner);
  static IrcServerOptions get_irc_server_options(const std::string& owner,
97
                                                     const std::string& server);
98 99 100 101 102 103 104 105 106 107 108 109 110
  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);
111

112
  static void close();
113
  static void open(const std::string& filename);
114

115
  template <typename TableType>
116
  static int64_t count(const TableType& table)
117 118 119 120 121 122 123 124 125 126
  {
    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;
127

128
 private:
129
  static std::string gen_uuid();
130 131
};
#endif /* USE_DATABASE */