handshake_params.hpp 5.0 KB

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