with_params.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_IMPL_WITH_PARAMS_HPP
  8. #define BOOST_MYSQL_IMPL_WITH_PARAMS_HPP
  9. #pragma once
  10. #include <boost/mysql/constant_string_view.hpp>
  11. #include <boost/mysql/field_view.hpp>
  12. #include <boost/mysql/format_sql.hpp>
  13. #include <boost/mysql/with_params.hpp>
  14. #include <boost/mysql/detail/any_execution_request.hpp>
  15. #include <boost/core/ignore_unused.hpp>
  16. #include <boost/core/span.hpp>
  17. #include <boost/mp11/integer_sequence.hpp>
  18. // Execution request traits
  19. namespace boost {
  20. namespace mysql {
  21. namespace detail {
  22. template <std::size_t N>
  23. struct with_params_proxy
  24. {
  25. constant_string_view query;
  26. std::array<format_arg, N> args;
  27. operator detail::any_execution_request() const { return any_execution_request({query, args}); }
  28. };
  29. template <class... T>
  30. struct execution_request_traits<with_params_t<T...>>
  31. {
  32. template <class WithParamsType, std::size_t... I>
  33. static with_params_proxy<sizeof...(T)> make_request_impl(WithParamsType&& input, mp11::index_sequence<I...>)
  34. {
  35. boost::ignore_unused(input); // MSVC gets confused for tuples of size 0
  36. // clang-format off
  37. return {
  38. input.query,
  39. {{
  40. {
  41. string_view(),
  42. formattable_ref(std::get<I>(std::forward<WithParamsType>(input).args))
  43. }...
  44. }}
  45. };
  46. // clang-format on
  47. }
  48. // Allow the value category of the object to be deduced
  49. template <class WithParamsType>
  50. static with_params_proxy<sizeof...(T)> make_request(WithParamsType&& input, std::vector<field_view>&)
  51. {
  52. return make_request_impl(
  53. std::forward<WithParamsType>(input),
  54. mp11::make_index_sequence<sizeof...(T)>()
  55. );
  56. }
  57. };
  58. // Old MSVCs fail to process the above when sizeof...(T) is zero
  59. template <>
  60. struct execution_request_traits<with_params_t<>>
  61. {
  62. static any_execution_request make_request(with_params_t<> input, std::vector<field_view>&)
  63. {
  64. return any_execution_request({input.query, span<format_arg>{}});
  65. }
  66. };
  67. } // namespace detail
  68. } // namespace mysql
  69. } // namespace boost
  70. #endif