~singpolyma/biboumi

ref: d7427fc9ca4c06fda458e4951559f57163d90b94 biboumi/src/database/database.hpp -rw-r--r-- 9.3 KiB
d7427fc9 — louiz’ Re-connect to postgresql when the connection is lost 4 years ago
                                                                                
81f8f45b louiz’
88ae2599 Florent Le Coz
50cadf3d louiz’
88ae2599 Florent Le Coz
0168b96b louiz’
40db183e louiz’
8ec823be louiz’
50cadf3d louiz’
655151d8 louiz’
8ec823be louiz’
88ae2599 Florent Le Coz
50cadf3d louiz’
8ec823be louiz’
d7055414 louiz’
fd7c3652 louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
dac19da4 louiz’
50cadf3d louiz’
0168b96b louiz’
369ccb03 louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
cf56b60f louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
0168b96b louiz’
369ccb03 louiz’
50cadf3d louiz’
0168b96b louiz’
369ccb03 louiz’
50cadf3d louiz’
03714c6c louiz’
40db183e louiz’
0168b96b louiz’
369ccb03 louiz’
50cadf3d louiz’
369ccb03 louiz’
50cadf3d louiz’
bfcf2945 louiz’
84034ed3 louiz’
b3b90139 louiz’
84034ed3 louiz’
50cadf3d louiz’
bfcf2945 louiz’
50cadf3d louiz’
577984fa louiz’
50cadf3d louiz’
40db183e louiz’
50cadf3d louiz’
84034ed3 louiz’
577984fa louiz’
88ae2599 Florent Le Coz
af420738 louiz’
50cadf3d louiz’
88ae2599 Florent Le Coz
50cadf3d louiz’
577984fa louiz’
99389eef louiz’
0280343c louiz’
99389eef louiz’
50cadf3d louiz’
0280343c louiz’
4a2a280d louiz’
99389eef louiz’
0280343c louiz’
50cadf3d louiz’
8ec823be louiz’
84034ed3 louiz’
88ae2599 Florent Le Coz
50cadf3d louiz’
4c1b9abe louiz’
50cadf3d louiz’
2677ac42 louiz’
50cadf3d louiz’
0168b96b louiz’
50cadf3d louiz’
84034ed3 louiz’
577984fa louiz’
0168b96b louiz’
88ae2599 Florent Le Coz
655151d8 louiz’
0168b96b louiz’
5af0a804 louiz’
0168b96b louiz’
50cadf3d louiz’
8ec823be louiz’
655151d8 louiz’
88ae2599 Florent Le Coz
5af0a804 louiz’
88ae2599 Florent Le Coz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#pragma once

#include <biboumi.h>
#ifdef USE_DATABASE

#include <database/table.hpp>
#include <database/column.hpp>
#include <database/count_query.hpp>

#include <database/engine.hpp>

#include <utils/optional_bool.hpp>

#include <chrono>
#include <string>

#include <memory>
#include <map>


class Database
{
 public:
  using time_point = std::chrono::system_clock::time_point;
  struct RecordNotFound: public std::exception {};
  enum class Paging { first, last };

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  struct EncodingIn: Column<std::string> { static constexpr auto name = "encodingin_"; };

  struct MaxHistoryLength: Column<int> { static constexpr auto name = "maxhistorylength_";
    MaxHistoryLength(): Column<int>(20) {} };

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

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

  struct VerifyCert: Column<bool> { static constexpr auto name = "verifycert_";
    VerifyCert(): Column<bool>(true) {} };

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

  struct GlobalPersistent: Column<bool> { static constexpr auto name = "persistent_";
    GlobalPersistent(); };

  struct LocalJid: Column<std::string> { static constexpr auto name = "local"; };

  struct RemoteJid: Column<std::string> { static constexpr auto name = "remote"; };

  struct Address: Column<std::string> { static constexpr auto name = "address_"; };

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

  using GlobalOptionsTable = Table<Id, Owner, MaxHistoryLength, RecordHistory, GlobalPersistent>;
  using GlobalOptions = GlobalOptionsTable::RowType;

  using IrcServerOptionsTable = Table<Id, Owner, Server, Pass, TlsPorts, Ports, Username, Realname, VerifyCert, TrustedFingerprint, EncodingOut, EncodingIn, MaxHistoryLength, Address>;
  using IrcServerOptions = IrcServerOptionsTable::RowType;

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

  using RosterTable = Table<LocalJid, RemoteJid>;
  using RosterItem = RosterTable::RowType;

  using AfterConnectionCommandsTable = Table<Id, ForeignKey, AfterConnectionCommand>;
  using AfterConnectionCommands = std::vector<AfterConnectionCommandsTable::RowType>;

  Database() = default;
  ~Database() = default;

  Database(const Database&) = delete;
  Database(Database&&) = delete;
  Database& operator=(const Database&) = delete;
  Database& operator=(Database&&) = delete;

  static GlobalOptions get_global_options(const std::string& owner);
  static IrcServerOptions get_irc_server_options(const std::string& owner,
                                                     const std::string& server);
  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 AfterConnectionCommands get_after_connection_commands(const IrcServerOptions& server_options);
  static void set_after_connection_commands(const IrcServerOptions& server_options, AfterConnectionCommands& commands);

  /**
   * Get all the lines between (optional) start and end dates, with a (optional) limit.
   * If after_id is set, only the records after it will be returned.
   */
  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="",
                                              const Id::real_type reference_record_id=Id::unset_value, Paging=Paging::first);

  /**
   * Get just one single record matching the given uuid, between (optional) end and start.
   * If it does not exist (or is not between end and start), throw a RecordNotFound exception.
   */
  static MucLogLine get_muc_log(const std::string& owner, const std::string& chan_name, const std::string& server, const std::string& uuid, 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 add_roster_item(const std::string& local, const std::string& remote);
  static bool has_roster_item(const std::string& local, const std::string& remote);
  static void delete_roster_item(const std::string& local, const std::string& remote);
  static std::vector<Database::RosterItem> get_contact_list(const std::string& local);
  static std::vector<Database::RosterItem> get_full_roster();

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

  template <typename TableType>
  static int64_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 RosterTable roster;
  static AfterConnectionCommandsTable after_connection_commands;

  static std::unique_ptr<DatabaseEngine> db;

  /**
   * Some caches, to avoid doing very frequent query requests for a few options.
   */
  using CacheKey = std::tuple<std::string, std::string, std::string>;

  static EncodingIn::real_type get_encoding_in(const std::string& owner,
                                        const std::string& server,
                                        const std::string& channel)
  {
    CacheKey channel_key{owner, server, channel};
    auto it = Database::encoding_in_cache.find(channel_key);
    if (it == Database::encoding_in_cache.end())
      {
        auto options = Database::get_irc_channel_options_with_server_default(owner, server, channel);
        EncodingIn::real_type result = options.col<Database::EncodingIn>();
        if (result.empty())
          result = "ISO-8859-1";
        it = Database::encoding_in_cache.insert(std::make_pair(channel_key, result)).first;
      }
    return it->second;
  }
  static void invalidate_encoding_in_cache(const std::string& owner,
                                           const std::string& server,
                                           const std::string& channel)
  {
    CacheKey channel_key{owner, server, channel};
    Database::encoding_in_cache.erase(channel_key);
  }
  static void invalidate_encoding_in_cache()
  {
    Database::encoding_in_cache.clear();
  }

  static auto raw_exec(const std::string& query)
  {
    return Database::db->raw_exec(query);
  }

 private:
  static std::string gen_uuid();
  static std::map<CacheKey, EncodingIn::real_type> encoding_in_cache;
};

class Transaction
{
public:
  Transaction();
  ~Transaction();
  bool success{false};
};

#endif /* USE_DATABASE */