character_set.hpp 2.7 KB

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