summaryrefslogtreecommitdiffstats
path: root/server/src/database.cpp
blob: c1f9ab233b86890324aaac296feced4a6028e90b (about) (plain) (blame)
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
module routemon:database$impl;

import :database;

namespace routemon::database {

namespace schema {

constexpr std::int64_t expected_version = 2;

auto try_to_string(rwgps_notification_cause c) -> std::optional<std::string>
{
  switch (c)
  {
  case rwgps_notification_cause::unknown:
    return std::nullopt;
  case rwgps_notification_cause::created:
    return "created";
  case rwgps_notification_cause::updated:
    return "updated";
  case rwgps_notification_cause::deleted:
    return "deleted";
  case rwgps_notification_cause::added:
    return "added";
  case rwgps_notification_cause::removed:
    return "removed";
  }
}

auto try_to_string(rwgps_notification_item_type t) -> std::optional<std::string>
{
  switch (t)
  {
  case rwgps_notification_item_type::unknown:
    return std::nullopt;
  case rwgps_notification_item_type::trip:
    return "trip";
  case rwgps_notification_item_type::route:
    return "route";
  case rwgps_notification_item_type::event:
    return "event";
  case rwgps_notification_item_type::event_participant:
    return "event_participant";
  case rwgps_notification_item_type::club_member:
    return "club_member";
  case rwgps_notification_item_type::authorization:
    return "authorization";
  }
}

auto try_to_string(rwgps_notification_target_collection_type t)
    -> std::optional<std::string>
{
  switch (t)
  {
  case rwgps_notification_target_collection_type::unknown:
    return std::nullopt;
  case rwgps_notification_target_collection_type::pinned:
    return "pinned";
  }
};

} // namespace schema

auto connection::insert_rwgps_notifications(
    std::vector<std::pair<
        schema::rwgps_notification,
        std::optional<schema::rwgps_notification_target_collection>
    >>& notifs) -> void
{
  auto tx = sqlite3::tx{dbc_};
  for (auto& [notif, mtarget_collection] : notifs)
  {
    tx.query(
          "INSERT INTO rwgps_notification "
          "       (rwgps_timestamp, user_id, item_type, item_id, "
          "        item_user_id, item_url, action_) "
          "VALUES (:rwgps_timestamp, :user_id, :item_type, :item_id, "
          "        :item_user_id, :item_url, :action) "
          "RETURNING id",
          [&notif](auto& s)
          {
            auto const ts = time::iso8601_secs(notif.rwgps_timestamp);
            s.bind(":rwgps_timestamp", std::string_view{ts});
            s.bind(":user_id", notif.user_id);
            s.bind(":item_type", try_to_string(notif.item_type));
            s.bind(":item_id", notif.item_id);
            s.bind(":item_user_id", notif.item_user_id);
            s.bind(":item_url", notif.item_url);
            s.bind(":action", try_to_string(notif.action));
          })
        .scan_row_max1(notif.id);

    if (mtarget_collection)
    {
      tx.exec(
          "INSERT INTO rwgps_notification_target_collection "
          "       (notification_id, id, type_, url) "
          "VALUES (:notification_id, :id, :type, :url)",
          [&notif, &mtarget_collection](auto& s)
          {
            s.bind(":notification_id", notif.id);
            s.bind(":id", mtarget_collection->id);
            s.bind(":type", try_to_string(mtarget_collection->type));
            s.bind(":url", mtarget_collection->url);
          });
    }
  }
}

auto open(util::zstring_view filename) -> std::shared_ptr<connection>
{
  auto dbc = sqlite3::open(filename);
  try
  {
    auto mversion = std::optional<std::int64_t>{};
    dbc.query("SELECT version FROM migration;").scan_row_max1(mversion);
    if (!mversion)
      throw std::runtime_error{"failed to fetch database migration version"};
    if (mversion != schema::expected_version)
    {
      throw std::runtime_error{std::format(
          "database migration version ({}) does not match expected "
          "version ({}), consider running migrations",
          *mversion, schema::expected_version)};
    }
  }
  catch (std::exception const& e)
  {
    throw std::runtime_error{std::format(
        "failed to query database version: {}", e.what())};
  }
  return std::shared_ptr<connection>{new connection{std::move(dbc)}};
}

} // namespace routemon::database