execute.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_EXECUTE_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_EXECUTE_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/impl/internal/coroutine.hpp>
  15. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  16. #include <boost/mysql/impl/internal/sansio/read_resultset_head.hpp>
  17. #include <boost/mysql/impl/internal/sansio/read_some_rows.hpp>
  18. #include <boost/mysql/impl/internal/sansio/start_execution.hpp>
  19. namespace boost {
  20. namespace mysql {
  21. namespace detail {
  22. class read_execute_response_algo
  23. {
  24. int resume_point_{0};
  25. read_resultset_head_algo read_head_st_;
  26. read_some_rows_algo read_some_rows_st_;
  27. public:
  28. read_execute_response_algo(diagnostics* diag, execution_processor* proc) noexcept
  29. : read_head_st_(read_resultset_head_algo_params{diag, proc}),
  30. read_some_rows_st_(read_some_rows_algo_params{diag, proc, output_ref()})
  31. {
  32. }
  33. diagnostics& diag() { return read_head_st_.diag(); }
  34. execution_processor& processor() { return read_head_st_.processor(); }
  35. next_action resume(connection_state_data& st, error_code ec)
  36. {
  37. next_action act;
  38. switch (resume_point_)
  39. {
  40. case 0:
  41. while (!processor().is_complete())
  42. {
  43. if (processor().is_reading_head())
  44. {
  45. read_head_st_.reset();
  46. while (!(act = read_head_st_.resume(st, ec)).is_done())
  47. BOOST_MYSQL_YIELD(resume_point_, 1, act)
  48. if (act.error())
  49. return act;
  50. }
  51. else if (processor().is_reading_rows())
  52. {
  53. read_some_rows_st_.reset();
  54. while (!(act = read_some_rows_st_.resume(st, ec)).is_done())
  55. BOOST_MYSQL_YIELD(resume_point_, 2, act)
  56. if (act.error())
  57. return act;
  58. }
  59. }
  60. }
  61. return next_action();
  62. }
  63. };
  64. class execute_algo
  65. {
  66. int resume_point_{0};
  67. start_execution_algo start_execution_st_;
  68. read_execute_response_algo read_response_st_;
  69. diagnostics& diag() { return read_response_st_.diag(); }
  70. execution_processor& processor() { return read_response_st_.processor(); }
  71. public:
  72. execute_algo(execute_algo_params params) noexcept
  73. : start_execution_st_(start_execution_algo_params{params.diag, params.req, params.proc}),
  74. read_response_st_(params.diag, params.proc)
  75. {
  76. }
  77. next_action resume(connection_state_data& st, error_code ec)
  78. {
  79. next_action act;
  80. switch (resume_point_)
  81. {
  82. case 0:
  83. // Send request and read the first response
  84. while (!(act = start_execution_st_.resume(st, ec)).is_done())
  85. BOOST_MYSQL_YIELD(resume_point_, 1, act)
  86. if (act.error())
  87. return act;
  88. // Read anything else
  89. while (!(act = read_response_st_.resume(st, ec)).is_done())
  90. BOOST_MYSQL_YIELD(resume_point_, 2, act)
  91. return act;
  92. }
  93. return next_action();
  94. }
  95. };
  96. } // namespace detail
  97. } // namespace mysql
  98. } // namespace boost
  99. #endif