handshake_params.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2019-2023 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. public:
  29. /// The default collation to use with the connection (`utf8mb4_general_ci` on both MySQL and MariaDB).
  30. static constexpr std::uint16_t default_collation = 45;
  31. /**
  32. * \brief Initializing constructor.
  33. * \par Exception safety
  34. * No-throw guarantee.
  35. *
  36. * \param username User name to authenticate as.
  37. * \param password Password for that username, possibly empty.
  38. * \param db Database name to use, or empty string for no database (this is the default).
  39. * \param connection_col The ID of the collation to use for the connection.
  40. * Impacts how text queries and prepared statements are interpreted. Defaults to
  41. * `utf8mb4_general_ci` (see \ref default_collation), which is compatible with MySQL 5.x, 8.x and MariaDB.
  42. * \param mode The \ref ssl_mode to use with this connection; ignored if
  43. * the connection's `Stream` does not support SSL.
  44. */
  45. handshake_params(
  46. string_view username,
  47. string_view password,
  48. string_view db = "",
  49. std::uint16_t connection_col = default_collation,
  50. ssl_mode mode = ssl_mode::require
  51. )
  52. : username_(username),
  53. password_(password),
  54. database_(db),
  55. connection_collation_(connection_col),
  56. ssl_(mode)
  57. {
  58. }
  59. /**
  60. * \brief Retrieves the username.
  61. * \par Exception safety
  62. * No-throw guarantee.
  63. */
  64. string_view username() const noexcept { return username_; }
  65. /**
  66. * \brief Sets the username.
  67. * \par Exception safety
  68. * No-throw guarantee.
  69. */
  70. void set_username(string_view value) noexcept { username_ = value; }
  71. /**
  72. * \brief Retrieves the password.
  73. * \par Exception safety
  74. * No-throw guarantee.
  75. */
  76. string_view password() const noexcept { return password_; }
  77. /**
  78. * \brief Sets the password.
  79. * \par Exception safety
  80. * No-throw guarantee.
  81. */
  82. void set_password(string_view value) noexcept { password_ = value; }
  83. /**
  84. * \brief Retrieves the database name to use when connecting.
  85. * \par Exception safety
  86. * No-throw guarantee.
  87. */
  88. string_view database() const noexcept { return database_; }
  89. /**
  90. * \brief Sets the database name to use when connecting.
  91. * \par Exception safety
  92. * No-throw guarantee.
  93. */
  94. void set_database(string_view value) noexcept { database_ = value; }
  95. /**
  96. * \brief Retrieves the connection collation.
  97. * \par Exception safety
  98. * No-throw guarantee.
  99. */
  100. std::uint16_t connection_collation() const noexcept { return connection_collation_; }
  101. /**
  102. * \brief Sets the connection collation.
  103. * \par Exception safety
  104. * No-throw guarantee.
  105. */
  106. void set_connection_collation(std::uint16_t value) noexcept { connection_collation_ = value; }
  107. /**
  108. * \brief Retrieves the SSL mode.
  109. * \par Exception safety
  110. * No-throw guarantee.
  111. */
  112. ssl_mode ssl() const noexcept { return ssl_; }
  113. /**
  114. * \brief Sets the SSL mode.
  115. * \par Exception safety
  116. * No-throw guarantee.
  117. */
  118. void set_ssl(ssl_mode value) noexcept { ssl_ = value; }
  119. };
  120. } // namespace mysql
  121. } // namespace boost
  122. #endif