summaryrefslogtreecommitdiffstats
path: root/server/src/database/connection.cpp
blob: fcdf15fe3bca60d8b0d541c3e793e86394570454 (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
module routemon:database.connection$impl;

import :database.connection;

namespace routemon::database {

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(std::filesystem::path const& 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