start_execution.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_INTERNAL_SANSIO_START_EXECUTION_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_START_EXECUTION_HPP
  9. #include <boost/mysql/diagnostics.hpp>
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/mysql/detail/algo_params.hpp>
  12. #include <boost/mysql/detail/any_execution_request.hpp>
  13. #include <boost/mysql/detail/execution_processor/execution_processor.hpp>
  14. #include <boost/mysql/detail/next_action.hpp>
  15. #include <boost/mysql/impl/internal/coroutine.hpp>
  16. #include <boost/mysql/impl/internal/protocol/serialization.hpp>
  17. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  18. #include <boost/mysql/impl/internal/sansio/read_resultset_head.hpp>
  19. namespace boost {
  20. namespace mysql {
  21. namespace detail {
  22. inline error_code check_client_errors(const any_execution_request& req)
  23. {
  24. if (req.is_query)
  25. return error_code();
  26. return req.data.stmt.stmt.num_params() == req.data.stmt.params.size() ? error_code()
  27. : client_errc::wrong_num_params;
  28. }
  29. inline resultset_encoding get_encoding(const any_execution_request& req)
  30. {
  31. return req.is_query ? resultset_encoding::text : resultset_encoding::binary;
  32. }
  33. class start_execution_algo
  34. {
  35. int resume_point_{0};
  36. read_resultset_head_algo read_head_st_;
  37. any_execution_request req_;
  38. std::uint8_t& seqnum() { return processor().sequence_number(); }
  39. execution_processor& processor() { return read_head_st_.processor(); }
  40. diagnostics& diag() { return read_head_st_.diag(); }
  41. next_action compose_request(connection_state_data& st)
  42. {
  43. if (req_.is_query)
  44. {
  45. return st.write(query_command{req_.data.query}, seqnum());
  46. }
  47. else
  48. {
  49. return st.write(execute_stmt_command{req_.data.stmt.stmt.id(), req_.data.stmt.params}, seqnum());
  50. }
  51. }
  52. public:
  53. start_execution_algo(start_execution_algo_params params) noexcept
  54. : read_head_st_(read_resultset_head_algo_params{params.diag, params.proc}), req_(params.req)
  55. {
  56. }
  57. next_action resume(connection_state_data& st, error_code ec)
  58. {
  59. next_action act;
  60. switch (resume_point_)
  61. {
  62. case 0:
  63. // Clear diagnostics
  64. diag().clear();
  65. // Check for errors
  66. ec = check_client_errors(req_);
  67. if (ec)
  68. return ec;
  69. // Reset the processor
  70. processor().reset(get_encoding(req_), st.meta_mode);
  71. // Send the execution request
  72. BOOST_MYSQL_YIELD(resume_point_, 1, compose_request(st))
  73. if (ec)
  74. return ec;
  75. // Read the first resultset's head and return its result
  76. while (!(act = read_head_st_.resume(st, ec)).is_done())
  77. BOOST_MYSQL_YIELD(resume_point_, 2, act)
  78. return act;
  79. }
  80. return next_action();
  81. }
  82. };
  83. } // namespace detail
  84. } // namespace mysql
  85. } // namespace boost
  86. #endif /* INCLUDE_MYSQL_IMPL_NETWORK_ALGORITHMS_READ_RESULTSET_HEAD_HPP_ */