basic_connection.hxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /** Definition of the pqxx::basic_connection class template.
  2. *
  3. * Instantiations of basic_connection bring connections and policies together.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/basic_connection 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_BASIC_CONNECTION
  14. #define PQXX_H_BASIC_CONNECTION
  15. #include "pqxx/compiler-public.hxx"
  16. #include "pqxx/compiler-internal-pre.hxx"
  17. #include <cstddef>
  18. #include <memory>
  19. #include <string>
  20. #include "pqxx/connection_base.hxx"
  21. namespace pqxx
  22. {
  23. /// Base-class template for all libpqxx connection types.
  24. /** @deprecated In libpqxx 7, all built-in connection types will be implemented
  25. * as a single class. You'll specify the connection policy as an optional
  26. * constructor argument.
  27. *
  28. * Combines connection_base (the highly complex class implementing essentially
  29. * all connection-related functionality) with a connection policy (a simpler
  30. * helper class determining the rules that govern the process of setting up the
  31. * underlying connection to the backend).
  32. *
  33. * The pattern used to combine these classes is the same as for
  34. * basic_transaction. Through use of the template mechanism, the policy object
  35. * is embedded in the basic_connection object so that it does not need to be
  36. * allocated separately. This also avoids the need for virtual functions in
  37. * this class.
  38. */
  39. template<typename CONNECTPOLICY> class basic_connection_base :
  40. public connection_base
  41. {
  42. public:
  43. basic_connection_base() :
  44. connection_base(m_policy),
  45. m_options(std::string{}),
  46. m_policy(m_options)
  47. { init(); }
  48. /// The parsing of options is the same as libpq's PQconnect.
  49. /// See: https://www.postgresql.org/docs/10/static/libpq-connect.html
  50. explicit basic_connection_base(const std::string &opt) :
  51. connection_base(m_policy),
  52. m_options(opt),
  53. m_policy(m_options)
  54. {init();}
  55. /// See: @c basic_connection(const std::string &opt)
  56. explicit basic_connection_base(const char opt[]) :
  57. basic_connection_base(opt ? std::string{opt} : std::string{}) {}
  58. explicit basic_connection_base(std::nullptr_t) : basic_connection_base() {}
  59. ~basic_connection_base() noexcept
  60. { close(); }
  61. const std::string &options() const noexcept //[t01]
  62. {return m_policy.options();}
  63. private:
  64. /// Connect string. @warn Must be initialized before the connector!
  65. std::string m_options;
  66. /// Connection policy. @warn Must be initialized after the connect string!
  67. CONNECTPOLICY m_policy;
  68. };
  69. /// Concrete connection type template.
  70. /** @deprecated In libpqxx 7, all built-in connection types will be implemented
  71. * as a single class. You'll specify the connection policy as an optional
  72. * constructor argument.
  73. */
  74. template<typename CONNECTPOLICY> struct basic_connection :
  75. basic_connection_base<CONNECTPOLICY>
  76. {
  77. PQXX_DEPRECATED basic_connection() =default;
  78. PQXX_DEPRECATED explicit basic_connection(const std::string &opt) :
  79. basic_connection(opt) {}
  80. PQXX_DEPRECATED explicit basic_connection(const char opt[]) :
  81. basic_connection(opt) {}
  82. PQXX_DEPRECATED explicit basic_connection(std::nullptr_t) :
  83. basic_connection() {}
  84. using basic_connection_base<CONNECTPOLICY>::options;
  85. };
  86. } // namespace
  87. #include "pqxx/compiler-internal-post.hxx"
  88. #endif