character_set.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/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 (EXPERIMENTAL) 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. * \n
  46. * \par Function signature
  47. * The function signature should be:
  48. * `std::size_t (*next_char)(boost::span<const unsigned char> r)`.
  49. */
  50. std::size_t (*next_char)(span<const unsigned char>);
  51. };
  52. /// (EXPERIMENTAL) The utf8mb4 character set (the one you should use by default).
  53. BOOST_INLINE_CONSTEXPR character_set utf8mb4_charset
  54. #ifndef BOOST_MYSQL_DOXYGEN
  55. {"utf8mb4", detail::next_char_utf8mb4}
  56. #endif
  57. ;
  58. /// (EXPERIMENTAL) The ascii character set.
  59. BOOST_INLINE_CONSTEXPR character_set ascii_charset
  60. #ifndef BOOST_MYSQL_DOXYGEN
  61. {"ascii", detail::next_char_ascii};
  62. #endif
  63. ;
  64. /**
  65. * \brief (EXPERIMENTAL) Settings required to format SQL queries client-side.
  66. * \details
  67. * The recommended way to obtain a value of this type is using \ref any_connection::format_opts.
  68. */
  69. struct format_options
  70. {
  71. /// The connection's current character set.
  72. character_set charset;
  73. /// Whether backslashes represent escape sequences.
  74. bool backslash_escapes;
  75. };
  76. } // namespace mysql
  77. } // namespace boost
  78. #ifdef BOOST_MYSQL_HEADER_ONLY
  79. #include <boost/mysql/impl/character_set.ipp>
  80. #endif
  81. #endif