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