pipeline.ipp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_IMPL_PIPELINE_IPP
  8. #define BOOST_MYSQL_IMPL_PIPELINE_IPP
  9. #pragma once
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/mysql/field_view.hpp>
  12. #include <boost/mysql/pipeline.hpp>
  13. #include <boost/mysql/detail/access.hpp>
  14. #include <boost/mysql/detail/pipeline.hpp>
  15. #include <boost/mysql/detail/resultset_encoding.hpp>
  16. #include <boost/mysql/impl/internal/protocol/serialization.hpp>
  17. #include <boost/mysql/impl/internal/sansio/set_character_set.hpp>
  18. #include <boost/core/span.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <stdexcept>
  21. boost::mysql::pipeline_request& boost::mysql::pipeline_request::add_execute(string_view query)
  22. {
  23. impl_.stages_.reserve(impl_.stages_.size() + 1); // strong guarantee
  24. impl_.stages_.push_back({
  25. detail::pipeline_stage_kind::execute,
  26. detail::serialize_top_level(detail::query_command{query}, impl_.buffer_),
  27. detail::resultset_encoding::text,
  28. });
  29. return *this;
  30. }
  31. boost::mysql::pipeline_request& boost::mysql::pipeline_request::add_execute_range(
  32. statement stmt,
  33. span<const field_view> params
  34. )
  35. {
  36. if (params.size() != stmt.num_params())
  37. {
  38. BOOST_THROW_EXCEPTION(
  39. std::invalid_argument("Wrong number of actual parameters supplied to a prepared statement")
  40. );
  41. }
  42. impl_.stages_.reserve(impl_.stages_.size() + 1); // strong guarantee
  43. impl_.stages_.push_back({
  44. detail::pipeline_stage_kind::execute,
  45. detail::serialize_top_level(detail::execute_stmt_command{stmt.id(), params}, impl_.buffer_),
  46. detail::resultset_encoding::binary,
  47. });
  48. return *this;
  49. }
  50. boost::mysql::pipeline_request& boost::mysql::pipeline_request::add_prepare_statement(string_view stmt_sql)
  51. {
  52. impl_.stages_.reserve(impl_.stages_.size() + 1); // strong guarantee
  53. impl_.stages_.push_back({
  54. detail::pipeline_stage_kind::prepare_statement,
  55. detail::serialize_top_level(detail::prepare_stmt_command{stmt_sql}, impl_.buffer_),
  56. {},
  57. });
  58. return *this;
  59. }
  60. boost::mysql::pipeline_request& boost::mysql::pipeline_request::add_close_statement(statement stmt)
  61. {
  62. impl_.stages_.reserve(impl_.stages_.size() + 1); // strong guarantee
  63. impl_.stages_.push_back({
  64. detail::pipeline_stage_kind::close_statement,
  65. detail::serialize_top_level(detail::close_stmt_command{stmt.id()}, impl_.buffer_),
  66. {},
  67. });
  68. return *this;
  69. }
  70. boost::mysql::pipeline_request& boost::mysql::pipeline_request::add_reset_connection()
  71. {
  72. impl_.stages_.reserve(impl_.stages_.size() + 1); // strong guarantee
  73. impl_.stages_.push_back({
  74. detail::pipeline_stage_kind::reset_connection,
  75. detail::serialize_top_level(detail::reset_connection_command{}, impl_.buffer_),
  76. {},
  77. });
  78. return *this;
  79. }
  80. boost::mysql::pipeline_request& boost::mysql::pipeline_request::add_set_character_set(character_set charset)
  81. {
  82. auto q = detail::compose_set_names(charset);
  83. if (q.has_error())
  84. {
  85. BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid character set name"));
  86. }
  87. impl_.stages_.reserve(impl_.stages_.size() + 1); // strong guarantee
  88. impl_.stages_.push_back({
  89. detail::pipeline_stage_kind::set_character_set,
  90. detail::serialize_top_level(detail::query_command{*q}, impl_.buffer_),
  91. charset,
  92. });
  93. return *this;
  94. }
  95. void boost::mysql::stage_response::check_has_results() const
  96. {
  97. if (!has_results())
  98. {
  99. BOOST_THROW_EXCEPTION(
  100. std::invalid_argument("stage_response::as_results: object doesn't contain results")
  101. );
  102. }
  103. }
  104. boost::mysql::statement boost::mysql::stage_response::as_statement() const
  105. {
  106. if (!has_statement())
  107. {
  108. BOOST_THROW_EXCEPTION(
  109. std::invalid_argument("stage_response::as_statement: object doesn't contain a statement")
  110. );
  111. }
  112. return variant2::unsafe_get<1>(impl_.value);
  113. }
  114. #endif