notification.hxx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /** Definition of the pqxx::notification_receiver functor interface.
  2. *
  3. * pqxx::notification_receiver handles incoming notifications.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/notification instead.
  6. *
  7. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  8. *
  9. * See COPYING for copyright license. If you did not receive a file called
  10. * COPYING with this source code, please notify the distributor of this mistake,
  11. * or contact the author.
  12. */
  13. #ifndef PQXX_H_NOTIFICATION
  14. #define PQXX_H_NOTIFICATION
  15. #include "pqxx/compiler-public.hxx"
  16. #include "pqxx/compiler-internal-pre.hxx"
  17. #include <string>
  18. #include "pqxx/types.hxx"
  19. namespace pqxx
  20. {
  21. /// "Observer" base class for notifications.
  22. /** @addtogroup notification Notifications and Receivers
  23. *
  24. * To listen on a notification issued using the NOTIFY command, derive your own
  25. * class from notification_receiver and define its function-call operator to
  26. * perform whatever action you wish to take when the given notification arrives.
  27. * Then create an object of that class and pass it to your connection. DO NOT
  28. * use raw SQL to listen for notifications, or your attempts to listen won't be
  29. * resumed when a connection fails--and you'll have no way to notice.
  30. *
  31. * Notifications never arrive inside a transaction, not even in a
  32. * nontransaction. Therefore, you are free to open a transaction of your own
  33. * inside your receiver's function invocation operator.
  34. *
  35. * Notifications you are listening for may arrive anywhere within libpqxx code,
  36. * but be aware that @b PostgreSQL @b defers @b notifications @b occurring
  37. * @b inside @b transactions. (This was done for excellent reasons; just think
  38. * about what happens if the transaction where you happen to handle an incoming
  39. * notification is later rolled back for other reasons). So if you're keeping a
  40. * transaction open, don't expect any of your receivers on the same connection
  41. * to be notified.
  42. *
  43. * (For very similar reasons, outgoing notifications are also not sent until the
  44. * transaction that sends them commits.)
  45. *
  46. * Multiple receivers on the same connection may listen on a notification of the
  47. * same name. An incoming notification is processed by invoking all receivers
  48. * (zero or more) of the same name.
  49. */
  50. class PQXX_LIBEXPORT PQXX_NOVTABLE notification_receiver
  51. {
  52. public:
  53. /// Register the receiver with a connection.
  54. /**
  55. * @param c Connnection to operate on.
  56. * @param channel Name of the notification to listen for.
  57. */
  58. notification_receiver(connection_base &c, const std::string &channel);
  59. notification_receiver(const notification_receiver &) =delete;
  60. notification_receiver &operator=(const notification_receiver &) =delete;
  61. virtual ~notification_receiver();
  62. /// The channel that this receiver listens on.
  63. const std::string &channel() const { return m_channel; }
  64. /// Overridable: action to invoke when notification arrives.
  65. /**
  66. * @param payload On PostgreSQL 9.0 or later, an optional string that may have
  67. * been passed to the NOTIFY command.
  68. * @param backend_pid Process ID of the database backend process that served
  69. * our connection when the notification arrived. The actual process ID behind
  70. * the connection may have changed by the time this method is called.
  71. */
  72. virtual void operator()(const std::string &payload, int backend_pid) =0;
  73. protected:
  74. connection_base &conn() const noexcept { return m_conn; }
  75. private:
  76. connection_base &m_conn;
  77. std::string m_channel;
  78. };
  79. }
  80. #include "pqxx/compiler-internal-post.hxx"
  81. #endif