diff options
Diffstat (limited to 'server/src/database.cpp')
| -rw-r--r-- | server/src/database.cpp | 136 |
1 files changed, 0 insertions, 136 deletions
diff --git a/server/src/database.cpp b/server/src/database.cpp deleted file mode 100644 index c1f9ab2..0000000 --- a/server/src/database.cpp +++ /dev/null | |||
| @@ -1,136 +0,0 @@ | |||
| 1 | module routemon:database$impl; | ||
| 2 | |||
| 3 | import :database; | ||
| 4 | |||
| 5 | namespace routemon::database { | ||
| 6 | |||
| 7 | namespace schema { | ||
| 8 | |||
| 9 | constexpr std::int64_t expected_version = 2; | ||
| 10 | |||
| 11 | auto try_to_string(rwgps_notification_cause c) -> std::optional<std::string> | ||
| 12 | { | ||
| 13 | switch (c) | ||
| 14 | { | ||
| 15 | case rwgps_notification_cause::unknown: | ||
| 16 | return std::nullopt; | ||
| 17 | case rwgps_notification_cause::created: | ||
| 18 | return "created"; | ||
| 19 | case rwgps_notification_cause::updated: | ||
| 20 | return "updated"; | ||
| 21 | case rwgps_notification_cause::deleted: | ||
| 22 | return "deleted"; | ||
| 23 | case rwgps_notification_cause::added: | ||
| 24 | return "added"; | ||
| 25 | case rwgps_notification_cause::removed: | ||
| 26 | return "removed"; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | auto try_to_string(rwgps_notification_item_type t) -> std::optional<std::string> | ||
| 31 | { | ||
| 32 | switch (t) | ||
| 33 | { | ||
| 34 | case rwgps_notification_item_type::unknown: | ||
| 35 | return std::nullopt; | ||
| 36 | case rwgps_notification_item_type::trip: | ||
| 37 | return "trip"; | ||
| 38 | case rwgps_notification_item_type::route: | ||
| 39 | return "route"; | ||
| 40 | case rwgps_notification_item_type::event: | ||
| 41 | return "event"; | ||
| 42 | case rwgps_notification_item_type::event_participant: | ||
| 43 | return "event_participant"; | ||
| 44 | case rwgps_notification_item_type::club_member: | ||
| 45 | return "club_member"; | ||
| 46 | case rwgps_notification_item_type::authorization: | ||
| 47 | return "authorization"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | auto try_to_string(rwgps_notification_target_collection_type t) | ||
| 52 | -> std::optional<std::string> | ||
| 53 | { | ||
| 54 | switch (t) | ||
| 55 | { | ||
| 56 | case rwgps_notification_target_collection_type::unknown: | ||
| 57 | return std::nullopt; | ||
| 58 | case rwgps_notification_target_collection_type::pinned: | ||
| 59 | return "pinned"; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | } // namespace schema | ||
| 64 | |||
| 65 | auto connection::insert_rwgps_notifications( | ||
| 66 | std::vector<std::pair< | ||
| 67 | schema::rwgps_notification, | ||
| 68 | std::optional<schema::rwgps_notification_target_collection> | ||
| 69 | >>& notifs) -> void | ||
| 70 | { | ||
| 71 | auto tx = sqlite3::tx{dbc_}; | ||
| 72 | for (auto& [notif, mtarget_collection] : notifs) | ||
| 73 | { | ||
| 74 | tx.query( | ||
| 75 | "INSERT INTO rwgps_notification " | ||
| 76 | " (rwgps_timestamp, user_id, item_type, item_id, " | ||
| 77 | " item_user_id, item_url, action_) " | ||
| 78 | "VALUES (:rwgps_timestamp, :user_id, :item_type, :item_id, " | ||
| 79 | " :item_user_id, :item_url, :action) " | ||
| 80 | "RETURNING id", | ||
| 81 | [¬if](auto& s) | ||
| 82 | { | ||
| 83 | auto const ts = time::iso8601_secs(notif.rwgps_timestamp); | ||
| 84 | s.bind(":rwgps_timestamp", std::string_view{ts}); | ||
| 85 | s.bind(":user_id", notif.user_id); | ||
| 86 | s.bind(":item_type", try_to_string(notif.item_type)); | ||
| 87 | s.bind(":item_id", notif.item_id); | ||
| 88 | s.bind(":item_user_id", notif.item_user_id); | ||
| 89 | s.bind(":item_url", notif.item_url); | ||
| 90 | s.bind(":action", try_to_string(notif.action)); | ||
| 91 | }) | ||
| 92 | .scan_row_max1(notif.id); | ||
| 93 | |||
| 94 | if (mtarget_collection) | ||
| 95 | { | ||
| 96 | tx.exec( | ||
| 97 | "INSERT INTO rwgps_notification_target_collection " | ||
| 98 | " (notification_id, id, type_, url) " | ||
| 99 | "VALUES (:notification_id, :id, :type, :url)", | ||
| 100 | [¬if, &mtarget_collection](auto& s) | ||
| 101 | { | ||
| 102 | s.bind(":notification_id", notif.id); | ||
| 103 | s.bind(":id", mtarget_collection->id); | ||
| 104 | s.bind(":type", try_to_string(mtarget_collection->type)); | ||
| 105 | s.bind(":url", mtarget_collection->url); | ||
| 106 | }); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | auto open(util::zstring_view filename) -> std::shared_ptr<connection> | ||
| 112 | { | ||
| 113 | auto dbc = sqlite3::open(filename); | ||
| 114 | try | ||
| 115 | { | ||
| 116 | auto mversion = std::optional<std::int64_t>{}; | ||
| 117 | dbc.query("SELECT version FROM migration;").scan_row_max1(mversion); | ||
| 118 | if (!mversion) | ||
| 119 | throw std::runtime_error{"failed to fetch database migration version"}; | ||
| 120 | if (mversion != schema::expected_version) | ||
| 121 | { | ||
| 122 | throw std::runtime_error{std::format( | ||
| 123 | "database migration version ({}) does not match expected " | ||
| 124 | "version ({}), consider running migrations", | ||
| 125 | *mversion, schema::expected_version)}; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | catch (std::exception const& e) | ||
| 129 | { | ||
| 130 | throw std::runtime_error{std::format( | ||
| 131 | "failed to query database version: {}", e.what())}; | ||
| 132 | } | ||
| 133 | return std::shared_ptr<connection>{new connection{std::move(dbc)}}; | ||
| 134 | } | ||
| 135 | |||
| 136 | } // namespace routemon::database | ||