errorhandler.hxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /** Definition of the pqxx::errorhandler class.
  2. *
  3. * pqxx::errorhandler handlers errors and warnings in a database session.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/connection_base 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_ERRORHANDLER
  14. #define PQXX_H_ERRORHANDLER
  15. #include "pqxx/compiler-public.hxx"
  16. #include "pqxx/compiler-internal-pre.hxx"
  17. #include "pqxx/types.hxx"
  18. namespace pqxx
  19. {
  20. namespace internal
  21. {
  22. namespace gate
  23. {
  24. class errorhandler_connection_base;
  25. }
  26. }
  27. /**
  28. * @addtogroup errorhandler
  29. * @{
  30. */
  31. /// Base class for error-handler callbacks.
  32. /** To receive errors and warnings from a connection, subclass this with your
  33. * own error-handler functor, and instantiate it for the connection. Destroying
  34. * the handler un-registers it.
  35. *
  36. * A connection can have multiple error handlers at the same time. When the
  37. * database connection emits an error or warning message, it passes the message
  38. * to each error handler, starting with the most recently registered one and
  39. * progressing towards the oldest one. However an error handler may also
  40. * instruct the connection not to pass the message to further handlers by
  41. * returning "false."
  42. *
  43. * @warning Strange things happen when a result object outlives its parent
  44. * connection. If you register an error handler on a connection, then you must
  45. * not access the result after destroying the connection. This applies even if
  46. * you destroy the error handler first!
  47. */
  48. class PQXX_LIBEXPORT errorhandler
  49. {
  50. public:
  51. explicit errorhandler(connection_base &);
  52. virtual ~errorhandler();
  53. /// Define in subclass: receive an error or warning message from the database.
  54. /**
  55. * @return Whether the same error message should also be passed to the
  56. * remaining, older errorhandlers.
  57. */
  58. virtual bool operator()(const char msg[]) noexcept =0;
  59. private:
  60. connection_base *m_home;
  61. friend class internal::gate::errorhandler_connection_base;
  62. void unregister() noexcept;
  63. errorhandler() =delete;
  64. errorhandler(const errorhandler &) =delete;
  65. errorhandler &operator=(const errorhandler &) =delete;
  66. };
  67. /// An error handler that suppresses any previously registered error handlers.
  68. class quiet_errorhandler : public errorhandler
  69. {
  70. public:
  71. quiet_errorhandler(connection_base &conn) : errorhandler{conn} {}
  72. virtual bool operator()(const char[]) noexcept override { return false; }
  73. };
  74. /**
  75. * @}
  76. */
  77. } // namespace pqxx
  78. #include "pqxx/compiler-internal-post.hxx"
  79. #endif