module routemon:database$impl; import :database; namespace routemon::database { namespace schema { constexpr std::int64_t expected_version = 2; auto try_to_string(rwgps_notification_cause c) -> std::optional { switch (c) { case rwgps_notification_cause::unknown: return std::nullopt; case rwgps_notification_cause::created: return "created"; case rwgps_notification_cause::updated: return "updated"; case rwgps_notification_cause::deleted: return "deleted"; case rwgps_notification_cause::added: return "added"; case rwgps_notification_cause::removed: return "removed"; } } auto try_to_string(rwgps_notification_item_type t) -> std::optional { switch (t) { case rwgps_notification_item_type::unknown: return std::nullopt; case rwgps_notification_item_type::trip: return "trip"; case rwgps_notification_item_type::route: return "route"; case rwgps_notification_item_type::event: return "event"; case rwgps_notification_item_type::event_participant: return "event_participant"; case rwgps_notification_item_type::club_member: return "club_member"; case rwgps_notification_item_type::authorization: return "authorization"; } } auto try_to_string(rwgps_notification_target_collection_type t) -> std::optional { switch (t) { case rwgps_notification_target_collection_type::unknown: return std::nullopt; case rwgps_notification_target_collection_type::pinned: return "pinned"; } }; } // namespace schema 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(util::zstring_view 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