connection_state.hpp 5.2 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_INTERNAL_SANSIO_CONNECTION_STATE_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_HPP
  9. #include <boost/mysql/diagnostics.hpp>
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/mysql/handshake_params.hpp>
  12. #include <boost/mysql/statement.hpp>
  13. #include <boost/mysql/detail/algo_params.hpp>
  14. #include <boost/mysql/detail/any_resumable_ref.hpp>
  15. #include <boost/mysql/impl/internal/sansio/close_connection.hpp>
  16. #include <boost/mysql/impl/internal/sansio/close_statement.hpp>
  17. #include <boost/mysql/impl/internal/sansio/connect.hpp>
  18. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  19. #include <boost/mysql/impl/internal/sansio/execute.hpp>
  20. #include <boost/mysql/impl/internal/sansio/handshake.hpp>
  21. #include <boost/mysql/impl/internal/sansio/ping.hpp>
  22. #include <boost/mysql/impl/internal/sansio/prepare_statement.hpp>
  23. #include <boost/mysql/impl/internal/sansio/quit_connection.hpp>
  24. #include <boost/mysql/impl/internal/sansio/read_resultset_head.hpp>
  25. #include <boost/mysql/impl/internal/sansio/read_some_rows.hpp>
  26. #include <boost/mysql/impl/internal/sansio/read_some_rows_dynamic.hpp>
  27. #include <boost/mysql/impl/internal/sansio/reset_connection.hpp>
  28. #include <boost/mysql/impl/internal/sansio/run_pipeline.hpp>
  29. #include <boost/mysql/impl/internal/sansio/set_character_set.hpp>
  30. #include <boost/mysql/impl/internal/sansio/start_execution.hpp>
  31. #include <boost/mysql/impl/internal/sansio/top_level_algo.hpp>
  32. #include <boost/variant2/variant.hpp>
  33. #include <cstddef>
  34. namespace boost {
  35. namespace mysql {
  36. namespace detail {
  37. // clang-format off
  38. template <class AlgoParams> struct get_algo;
  39. template <> struct get_algo<connect_algo_params> { using type = connect_algo; };
  40. template <> struct get_algo<handshake_algo_params> { using type = handshake_algo; };
  41. template <> struct get_algo<execute_algo_params> { using type = execute_algo; };
  42. template <> struct get_algo<start_execution_algo_params> { using type = start_execution_algo; };
  43. template <> struct get_algo<read_resultset_head_algo_params> { using type = read_resultset_head_algo; };
  44. template <> struct get_algo<read_some_rows_algo_params> { using type = read_some_rows_algo; };
  45. template <> struct get_algo<read_some_rows_dynamic_algo_params> { using type = read_some_rows_dynamic_algo; };
  46. template <> struct get_algo<prepare_statement_algo_params> { using type = prepare_statement_algo; };
  47. template <> struct get_algo<set_character_set_algo_params> { using type = set_character_set_algo; };
  48. template <> struct get_algo<quit_connection_algo_params> { using type = quit_connection_algo; };
  49. template <> struct get_algo<close_connection_algo_params> { using type = close_connection_algo; };
  50. template <> struct get_algo<run_pipeline_algo_params> { using type = run_pipeline_algo; };
  51. template <class AlgoParams> using get_algo_t = typename get_algo<AlgoParams>::type;
  52. // clang-format on
  53. class connection_state
  54. {
  55. // Helper
  56. template <class... Algos>
  57. using make_any_algo_type = variant2::variant<top_level_algo<Algos>...>;
  58. using any_algo = make_any_algo_type<
  59. connect_algo,
  60. handshake_algo,
  61. execute_algo,
  62. start_execution_algo,
  63. read_resultset_head_algo,
  64. read_some_rows_algo,
  65. read_some_rows_dynamic_algo,
  66. prepare_statement_algo,
  67. set_character_set_algo,
  68. quit_connection_algo,
  69. close_connection_algo,
  70. run_pipeline_algo>;
  71. connection_state_data st_data_;
  72. any_algo algo_;
  73. public:
  74. // We initialize the algo state with a dummy value. This will be overwritten
  75. // by setup() before the first algorithm starts running. Doing this avoids
  76. // the need for a special null algo
  77. connection_state(std::size_t read_buffer_size, std::size_t max_buffer_size, bool transport_supports_ssl)
  78. : st_data_(read_buffer_size, max_buffer_size, transport_supports_ssl),
  79. algo_(top_level_algo<quit_connection_algo>(
  80. st_data_,
  81. quit_connection_algo_params{&st_data_.shared_diag}
  82. ))
  83. {
  84. }
  85. const connection_state_data& data() const { return st_data_; }
  86. connection_state_data& data() { return st_data_; }
  87. template <class AlgoParams>
  88. any_resumable_ref setup(AlgoParams params)
  89. {
  90. return any_resumable_ref(algo_.emplace<top_level_algo<get_algo_t<AlgoParams>>>(st_data_, params));
  91. }
  92. any_resumable_ref setup(close_statement_algo_params params)
  93. {
  94. return setup(setup_close_statement_pipeline(st_data_, params));
  95. }
  96. any_resumable_ref setup(reset_connection_algo_params params)
  97. {
  98. return setup(setup_reset_connection_pipeline(st_data_, params));
  99. }
  100. any_resumable_ref setup(ping_algo_params params) { return setup(setup_ping_pipeline(st_data_, params)); }
  101. template <typename AlgoParams>
  102. typename AlgoParams::result_type result() const
  103. {
  104. return variant2::get<top_level_algo<get_algo_t<AlgoParams>>>(algo_).inner_algo().result(st_data_);
  105. }
  106. };
  107. } // namespace detail
  108. } // namespace mysql
  109. } // namespace boost
  110. #endif