diff options
| author | Rutger Broekhoff | 2026-11-30 21:27:55 +0100 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-11-30 21:27:55 +0100 |
| commit | 7bba7176194c4d1d9c7066f8eda7a94491d924e6 (patch) | |
| tree | a9a171e8608786e09d6342729ee13b8471c0c9c2 /server/src/database/connection.cpp | |
| parent | 02475e05ccd1aa15c6a6163834f28bfebbef796f (diff) | |
| download | routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip | |
Cleanups and updates
Diffstat (limited to 'server/src/database/connection.cpp')
| -rw-r--r-- | server/src/database/connection.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/server/src/database/connection.cpp b/server/src/database/connection.cpp new file mode 100644 index 0000000..fcdf15f --- /dev/null +++ b/server/src/database/connection.cpp | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | module routemon:database.connection$impl; | ||
| 2 | |||
| 3 | import :database.connection; | ||
| 4 | |||
| 5 | namespace routemon::database { | ||
| 6 | |||
| 7 | auto connection::insert_rwgps_notifications( | ||
| 8 | std::vector<std::pair< | ||
| 9 | schema::rwgps_notification, | ||
| 10 | std::optional<schema::rwgps_notification_target_collection> | ||
| 11 | >>& notifs) -> void | ||
| 12 | { | ||
| 13 | auto tx = sqlite3::tx{dbc_}; | ||
| 14 | for (auto& [notif, mtarget_collection] : notifs) | ||
| 15 | { | ||
| 16 | tx.query( | ||
| 17 | "INSERT INTO rwgps_notification " | ||
| 18 | " (rwgps_timestamp, user_id, item_type, item_id, " | ||
| 19 | " item_user_id, item_url, action_) " | ||
| 20 | "VALUES (:rwgps_timestamp, :user_id, :item_type, :item_id, " | ||
| 21 | " :item_user_id, :item_url, :action) " | ||
| 22 | "RETURNING id", | ||
| 23 | [¬if](auto& s) | ||
| 24 | { | ||
| 25 | auto const ts = time::iso8601_secs(notif.rwgps_timestamp); | ||
| 26 | s.bind(":rwgps_timestamp", std::string_view{ts}); | ||
| 27 | s.bind(":user_id", notif.user_id); | ||
| 28 | s.bind(":item_type", try_to_string(notif.item_type)); | ||
| 29 | s.bind(":item_id", notif.item_id); | ||
| 30 | s.bind(":item_user_id", notif.item_user_id); | ||
| 31 | s.bind(":item_url", notif.item_url); | ||
| 32 | s.bind(":action", try_to_string(notif.action)); | ||
| 33 | }) | ||
| 34 | .scan_row_max1(notif.id); | ||
| 35 | |||
| 36 | if (mtarget_collection) | ||
| 37 | { | ||
| 38 | tx.exec( | ||
| 39 | "INSERT INTO rwgps_notification_target_collection " | ||
| 40 | " (notification_id, id, type_, url) " | ||
| 41 | "VALUES (:notification_id, :id, :type, :url)", | ||
| 42 | [¬if, &mtarget_collection](auto& s) | ||
| 43 | { | ||
| 44 | s.bind(":notification_id", notif.id); | ||
| 45 | s.bind(":id", mtarget_collection->id); | ||
| 46 | s.bind(":type", try_to_string(mtarget_collection->type)); | ||
| 47 | s.bind(":url", mtarget_collection->url); | ||
| 48 | }); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | auto open(std::filesystem::path const& filename) -> std::shared_ptr<connection> | ||
| 54 | { | ||
| 55 | auto dbc = sqlite3::open(filename); | ||
| 56 | try | ||
| 57 | { | ||
| 58 | auto mversion = std::optional<std::int64_t>{}; | ||
| 59 | dbc.query("SELECT version FROM migration;").scan_row_max1(mversion); | ||
| 60 | if (!mversion) | ||
| 61 | throw std::runtime_error{"failed to fetch database migration version"}; | ||
| 62 | if (mversion != schema::expected_version) | ||
| 63 | { | ||
| 64 | throw std::runtime_error{std::format( | ||
| 65 | "database migration version ({}) does not match expected " | ||
| 66 | "version ({}), consider running migrations", | ||
| 67 | *mversion, schema::expected_version)}; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | catch (std::exception const& e) | ||
| 71 | { | ||
| 72 | throw std::runtime_error{std::format( | ||
| 73 | "failed to query database version: {}", e.what())}; | ||
| 74 | } | ||
| 75 | return std::shared_ptr<connection>{new connection{std::move(dbc)}}; | ||
| 76 | } | ||
| 77 | |||
| 78 | } // namespace routemon::database | ||