format_sql.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_DETAIL_FORMAT_SQL_HPP
  8. #define BOOST_MYSQL_DETAIL_FORMAT_SQL_HPP
  9. #include <boost/mysql/constant_string_view.hpp>
  10. #include <boost/mysql/field_view.hpp>
  11. #include <boost/mysql/string_view.hpp>
  12. #include <boost/mysql/detail/writable_field_traits.hpp>
  13. #include <iterator>
  14. #include <type_traits>
  15. #include <utility>
  16. namespace boost {
  17. namespace mysql {
  18. // Forward decls
  19. template <class T>
  20. struct formatter;
  21. class format_context_base;
  22. class formattable_ref;
  23. class format_arg;
  24. namespace detail {
  25. class format_state;
  26. struct formatter_is_unspecialized
  27. {
  28. };
  29. template <class T>
  30. constexpr bool has_specialized_formatter()
  31. {
  32. return !std::is_base_of<formatter_is_unspecialized, formatter<typename std::decay<T>::type>>::value;
  33. }
  34. template <class T>
  35. struct is_writable_field_ref : is_writable_field<typename std::decay<T>::type>
  36. {
  37. };
  38. template <class T>
  39. struct is_formattable_ref : std::is_same<typename std::decay<T>::type, formattable_ref>
  40. {
  41. };
  42. // Is T suitable for being the element type of a formattable range?
  43. template <class T>
  44. constexpr bool is_formattable_range_elm_type()
  45. {
  46. return is_writable_field_ref<T>::value || has_specialized_formatter<T>() || is_formattable_ref<T>::value;
  47. }
  48. template <class T, class = void>
  49. struct is_formattable_range : std::false_type
  50. {
  51. };
  52. // Note: T might be a reference.
  53. // Using T& + reference collapsing gets the right semantics for non-const ranges
  54. template <class T>
  55. struct is_formattable_range<
  56. T,
  57. typename std::enable_if<
  58. // std::begin and std::end can be called on it, and we can compare values
  59. std::is_convertible<decltype(std::begin(std::declval<T&>()) != std::end(std::declval<T&>())), bool>::
  60. value &&
  61. // value_type is either a writable field or a type with a specialized formatter.
  62. // We don't support sequences of sequences out of the box (no known use case)
  63. is_formattable_range_elm_type<decltype(*std::begin(std::declval<T&>()))>()
  64. // end of conditions
  65. >::type> : std::true_type
  66. {
  67. };
  68. template <class T>
  69. constexpr bool is_formattable_type()
  70. {
  71. return is_formattable_range_elm_type<T>() || is_formattable_range<T>::value;
  72. }
  73. #ifdef BOOST_MYSQL_HAS_CONCEPTS
  74. // If you're getting an error referencing this concept,
  75. // it means that you are attempting to format a type that doesn't support it.
  76. template <class T>
  77. concept formattable =
  78. // This covers basic types and optionals
  79. is_writable_field_ref<T>::value ||
  80. // This covers custom types that specialized boost::mysql::formatter
  81. has_specialized_formatter<T>() ||
  82. // This covers ranges of formattable types
  83. is_formattable_range<T>::value ||
  84. // This covers passing formattable_ref as a format argument
  85. is_formattable_ref<T>::value;
  86. #define BOOST_MYSQL_FORMATTABLE ::boost::mysql::detail::formattable
  87. #else
  88. #define BOOST_MYSQL_FORMATTABLE class
  89. #endif
  90. // A type-erased argument passed to format. Built-in types are passed
  91. // directly in the struct (as a field_view), instead of by pointer,
  92. // to reduce the number of do_format instantiations
  93. struct formattable_ref_impl
  94. {
  95. enum class type_t
  96. {
  97. field,
  98. field_with_specs,
  99. fn_and_ptr
  100. };
  101. struct fn_and_ptr
  102. {
  103. const void* obj;
  104. bool (*format_fn)(const void*, const char*, const char*, format_context_base&);
  105. };
  106. union data_t
  107. {
  108. field_view fv;
  109. fn_and_ptr custom;
  110. data_t(field_view fv) noexcept : fv(fv) {}
  111. data_t(fn_and_ptr v) noexcept : custom(v) {}
  112. };
  113. type_t type;
  114. data_t data;
  115. };
  116. // Create a type-erased formattable_ref_impl from a formattable value
  117. template <class T>
  118. formattable_ref_impl make_formattable_ref(T&& v);
  119. BOOST_MYSQL_DECL
  120. void vformat_sql_to(format_context_base& ctx, constant_string_view format_str, span<const format_arg> args);
  121. } // namespace detail
  122. } // namespace mysql
  123. } // namespace boost
  124. #endif