handshake_params.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_HANDSHAKE_PARAMS_HPP
  8. #define BOOST_MYSQL_HANDSHAKE_PARAMS_HPP
  9. #include <boost/mysql/buffer_params.hpp>
  10. #include <boost/mysql/ssl_mode.hpp>
  11. #include <boost/mysql/string_view.hpp>
  12. #include <cstdint>
  13. namespace boost {
  14. namespace mysql {
  15. /**
  16. * \brief Parameters defining how to perform the handshake with a MySQL server.
  17. * \par Object lifetimes
  18. * This object stores references to strings (like username and password), performing
  19. * no copy of these values. Users are resposible for keeping them alive until required.
  20. */
  21. class handshake_params
  22. {
  23. string_view username_;
  24. string_view password_;
  25. string_view database_;
  26. std::uint16_t connection_collation_;
  27. ssl_mode ssl_;
  28. bool multi_queries_;
  29. public:
  30. /// The default collation to use with the connection (`utf8mb4_general_ci` on both MySQL and MariaDB).
  31. static BOOST_INLINE_CONSTEXPR std::uint16_t default_collation = 45;
  32. /**
  33. * \brief Initializing constructor.
  34. * \par Exception safety
  35. * No-throw guarantee.
  36. *
  37. * \param username User name to authenticate as.
  38. * \param password Password for that username, possibly empty.
  39. * \param db Database name to use, or empty string for no database (this is the default).
  40. * \param connection_col The ID of the collation to use for the connection.
  41. * Impacts how text queries and prepared statements are interpreted. Defaults to
  42. * `utf8mb4_general_ci` (see \ref default_collation), which is compatible with MySQL 5.x, 8.x and MariaDB.
  43. * \param mode The \ref ssl_mode to use with this connection; ignored if
  44. * the connection's `Stream` does not support SSL.
  45. * \param multi_queries Whether to enable support for executing semicolon-separated
  46. * queries using \ref connection::execute and \ref connection::start_execution. Disabled by default.
  47. */
  48. handshake_params(
  49. string_view username,
  50. string_view password,
  51. string_view db = "",
  52. std::uint16_t connection_col = default_collation,
  53. ssl_mode mode = ssl_mode::require,
  54. bool multi_queries = false
  55. )
  56. : username_(username),
  57. password_(password),
  58. database_(db),
  59. connection_collation_(connection_col),
  60. ssl_(mode),
  61. multi_queries_(multi_queries)
  62. {
  63. }
  64. /**
  65. * \brief Retrieves the username.
  66. * \par Exception safety
  67. * No-throw guarantee.
  68. */
  69. string_view username() const noexcept { return username_; }
  70. /**
  71. * \brief Sets the username.
  72. * \par Exception safety
  73. * No-throw guarantee.
  74. */
  75. void set_username(string_view value) noexcept { username_ = value; }
  76. /**
  77. * \brief Retrieves the password.
  78. * \par Exception safety
  79. * No-throw guarantee.
  80. */
  81. string_view password() const noexcept { return password_; }
  82. /**
  83. * \brief Sets the password.
  84. * \par Exception safety
  85. * No-throw guarantee.
  86. */
  87. void set_password(string_view value) noexcept { password_ = value; }
  88. /**
  89. * \brief Retrieves the database name to use when connecting.
  90. * \par Exception safety
  91. * No-throw guarantee.
  92. */
  93. string_view database() const noexcept { return database_; }
  94. /**
  95. * \brief Sets the database name to use when connecting.
  96. * \par Exception safety
  97. * No-throw guarantee.
  98. */
  99. void set_database(string_view value) noexcept { database_ = value; }
  100. /**
  101. * \brief Retrieves the connection collation.
  102. * \par Exception safety
  103. * No-throw guarantee.
  104. */
  105. std::uint16_t connection_collation() const noexcept { return connection_collation_; }
  106. /**
  107. * \brief Sets the connection collation.
  108. * \par Exception safety
  109. * No-throw guarantee.
  110. */
  111. void set_connection_collation(std::uint16_t value) noexcept { connection_collation_ = value; }
  112. /**
  113. * \brief Retrieves the SSL mode.
  114. * \par Exception safety
  115. * No-throw guarantee.
  116. */
  117. ssl_mode ssl() const noexcept { return ssl_; }
  118. /**
  119. * \brief Sets the SSL mode.
  120. * \par Exception safety
  121. * No-throw guarantee.
  122. */
  123. void set_ssl(ssl_mode value) noexcept { ssl_ = value; }
  124. /**
  125. * \brief Retrieves whether multi-query support is enabled.
  126. * \par Exception safety
  127. * No-throw guarantee.
  128. */
  129. bool multi_queries() const noexcept { return multi_queries_; }
  130. /**
  131. * \brief Enables or disables support for the multi-query feature.
  132. * \par Exception safety
  133. * No-throw guarantee.
  134. */
  135. void set_multi_queries(bool v) noexcept { multi_queries_ = v; }
  136. };
  137. } // namespace mysql
  138. } // namespace boost
  139. #endif