character_set.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_CHARACTER_SET_HPP
  8. #define BOOST_MYSQL_CHARACTER_SET_HPP
  9. #include <boost/mysql/string_view.hpp>
  10. #include <boost/mysql/detail/character_set.hpp>
  11. #include <boost/mysql/detail/config.hpp>
  12. #include <boost/mysql/detail/make_string_view.hpp>
  13. #include <boost/core/span.hpp>
  14. #include <cstddef>
  15. namespace boost {
  16. namespace mysql {
  17. /**
  18. * \brief (EXPERIMENTAL) Represents a MySQL character set.
  19. * \details
  20. * By default, you should always use \ref utf8mb4_charset, unless there is
  21. * a strong reason not to. This struct allows you to extend this library
  22. * with character sets that are not supported out of the box.
  23. */
  24. struct character_set
  25. {
  26. /**
  27. * \brief The character set name.
  28. * \details
  29. * This should match the character set name in MySQL. This is the string
  30. * you specify when issuing `SET NAMES` statements. You can find available
  31. * character sets using the `SHOW CHARACTER SET` statement.
  32. */
  33. string_view name;
  34. /**
  35. * \brief Obtains the size of the first character of a string.
  36. * \details
  37. * Given a range of bytes, `r`, this function must interpret `r` as a
  38. * string encoded using this character set, and return the number of
  39. * bytes that the first character in the string spans, or 0 in case of error.
  40. * `r` is guaranteed to be non-empty (`r.size() > 0`).
  41. * \n
  42. * In some character sets (like UTF-8), not all byte sequences represent
  43. * valid characters. If this function finds an invalid byte sequence while
  44. * trying to interpret the first character, it should return 0 to signal the error.
  45. * \n
  46. * This function must not throw exceptions or have side effects.
  47. * \n
  48. * \par Function signature
  49. * The function signature should be:
  50. * `std::size_t (*next_char)(boost::span<const unsigned char> r) noexcept`.
  51. */
  52. std::size_t (*next_char)(span<const unsigned char>) noexcept;
  53. };
  54. /// (EXPERIMENTAL) The utf8mb4 character set (the one you should use by default).
  55. constexpr character_set utf8mb4_charset
  56. #ifndef BOOST_MYSQL_DOXYGEN
  57. {detail::make_string_view("utf8mb4"), detail::next_char_utf8mb4}
  58. #endif
  59. ;
  60. /// (EXPERIMENTAL) The ascii character set.
  61. constexpr character_set ascii_charset
  62. #ifndef BOOST_MYSQL_DOXYGEN
  63. {detail::make_string_view("ascii"), detail::next_char_ascii};
  64. #endif
  65. ;
  66. /**
  67. * \brief (EXPERIMENTAL) Settings required to format SQL queries client-side.
  68. * \details
  69. * The recommended way to obtain a value of this type is using \ref any_connection::format_opts.
  70. */
  71. struct format_options
  72. {
  73. /// The connection's current character set.
  74. character_set charset;
  75. /// Whether backslashes represent escape sequences.
  76. bool backslash_escapes;
  77. };
  78. } // namespace mysql
  79. } // namespace boost
  80. #ifdef BOOST_MYSQL_HEADER_ONLY
  81. #include <boost/mysql/impl/character_set.ipp>
  82. #endif
  83. #endif