From 3c987e12b3f231035989d1b4a3ca35af71faa38b Mon Sep 17 00:00:00 2001 From: Jeroen Vermeulen Date: Wed, 18 Dec 2024 04:53:07 +0100 Subject: [PATCH] Wire up notification mechanism to new handlers. --- NEWS | 1 + include/pqxx/connection.hxx | 1 + include/pqxx/notification.hxx | 3 +++ src/connection.cxx | 16 ++++++++++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 6d9a646d5..9d11cd0ff 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ 7.10.0 - Deprecate `errorhandler`; replace with lambda-friendly "notice handlers." + - Deprecate `notification_receiver`; replace with "notification handlers" - Bump minimum CMake version to 3.28. (#874) - Fixed error message on clashing transaction focuses. (#879) - Don't call`/bin/true`; macOS doesn't have it. Just call `true`. (#885) diff --git a/include/pqxx/connection.hxx b/include/pqxx/connection.hxx index 54245e6f1..1fcc36a89 100644 --- a/include/pqxx/connection.hxx +++ b/include/pqxx/connection.hxx @@ -1283,6 +1283,7 @@ private: /// 9.0: Replace with just notice handler. std::shared_ptr m_notice_waiters; + // TODO: Remove these when we retire notification_receiver. // TODO: Can we make these movable? using receiver_list = std::multimap; diff --git a/include/pqxx/notification.hxx b/include/pqxx/notification.hxx index 7dd3befe1..4ea3a5afc 100644 --- a/include/pqxx/notification.hxx +++ b/include/pqxx/notification.hxx @@ -61,10 +61,13 @@ public: * @param cx Connnection to operate on. * @param channel Name of the notification to listen for. */ + [[deprecated("Use pqxx::connection::listen() instead.")]] notification_receiver(connection &cx, std::string_view channel); /// Register the receiver with a connection. + [[deprecated("Use pqxx::connection::listen() instead.")]] notification_receiver(notification_receiver const &) = delete; /// Register the receiver with a connection. + [[deprecated("Use pqxx::connection::listen() instead.")]] notification_receiver &operator=(notification_receiver const &) = delete; /// Deregister the receiver. virtual ~notification_receiver(); diff --git a/src/connection.cxx b/src/connection.cxx index d39cc6034..329a00488 100644 --- a/src/connection.cxx +++ b/src/connection.cxx @@ -608,16 +608,20 @@ int pqxx::connection::get_notifs() // Even if somehow we receive notifications during our transaction, don't // deliver them. - if (m_trans) + if (m_trans != nullptr) PQXX_UNLIKELY return 0; int notifs = 0; + + // Old mechanism. This is going away. for (auto N{get_notif(m_conn)}; N.get(); N = get_notif(m_conn)) { notifs++; - auto const Hit{m_receivers.equal_range(std::string{N->relname})}; + std::string const name{N->relname}; + + auto const Hit{m_receivers.equal_range(name)}; if (Hit.second != Hit.first) { std::string const payload{N->extra}; @@ -649,8 +653,16 @@ int pqxx::connection::get_notifs() } } + auto const handler{m_notification_handlers.find(N->relname)}; + if (handler != std::end(m_notification_handlers)) + { + ++notifs; + (handler->second)(name, N->be_pid, N->extra); + } + N.reset(); } + return notifs; }