From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/database/connection.cpp | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 server/src/database/connection.cpp (limited to 'server/src/database/connection.cpp') 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 @@ +module routemon:database.connection$impl; + +import :database.connection; + +namespace routemon::database { + +auto connection::insert_rwgps_notifications( + std::vector + >>& 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", + [¬if](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)", + [¬if, &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 +{ + auto dbc = sqlite3::open(filename); + try + { + auto mversion = std::optional{}; + 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{new connection{std::move(dbc)}}; +} + +} // namespace routemon::database -- cgit v1.3.1