Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:58:23

0001 //
0002 // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_HPP
0009 #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_HPP
0010 
0011 #include <boost/mysql/client_errc.hpp>
0012 #include <boost/mysql/diagnostics.hpp>
0013 #include <boost/mysql/error_code.hpp>
0014 #include <boost/mysql/handshake_params.hpp>
0015 #include <boost/mysql/statement.hpp>
0016 
0017 #include <boost/mysql/detail/algo_params.hpp>
0018 #include <boost/mysql/detail/any_resumable_ref.hpp>
0019 #include <boost/mysql/detail/next_action.hpp>
0020 
0021 #include <boost/mysql/impl/internal/sansio/close_connection.hpp>
0022 #include <boost/mysql/impl/internal/sansio/close_statement.hpp>
0023 #include <boost/mysql/impl/internal/sansio/connect.hpp>
0024 #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
0025 #include <boost/mysql/impl/internal/sansio/execute.hpp>
0026 #include <boost/mysql/impl/internal/sansio/handshake.hpp>
0027 #include <boost/mysql/impl/internal/sansio/ping.hpp>
0028 #include <boost/mysql/impl/internal/sansio/prepare_statement.hpp>
0029 #include <boost/mysql/impl/internal/sansio/quit_connection.hpp>
0030 #include <boost/mysql/impl/internal/sansio/read_resultset_head.hpp>
0031 #include <boost/mysql/impl/internal/sansio/read_some_rows.hpp>
0032 #include <boost/mysql/impl/internal/sansio/read_some_rows_dynamic.hpp>
0033 #include <boost/mysql/impl/internal/sansio/reset_connection.hpp>
0034 #include <boost/mysql/impl/internal/sansio/run_pipeline.hpp>
0035 #include <boost/mysql/impl/internal/sansio/set_character_set.hpp>
0036 #include <boost/mysql/impl/internal/sansio/start_execution.hpp>
0037 #include <boost/mysql/impl/internal/sansio/top_level_algo.hpp>
0038 
0039 #include <boost/variant2/variant.hpp>
0040 
0041 #include <cstddef>
0042 
0043 namespace boost {
0044 namespace mysql {
0045 namespace detail {
0046 
0047 // clang-format off
0048 template <class AlgoParams> struct get_algo;
0049 template <> struct get_algo<connect_algo_params> { using type = connect_algo; };
0050 template <> struct get_algo<handshake_algo_params> { using type = handshake_algo; };
0051 template <> struct get_algo<execute_algo_params> { using type = execute_algo; };
0052 template <> struct get_algo<start_execution_algo_params> { using type = start_execution_algo; };
0053 template <> struct get_algo<read_resultset_head_algo_params> { using type = read_resultset_head_algo; };
0054 template <> struct get_algo<read_some_rows_algo_params> { using type = read_some_rows_algo; };
0055 template <> struct get_algo<read_some_rows_dynamic_algo_params> { using type = read_some_rows_dynamic_algo; };
0056 template <> struct get_algo<prepare_statement_algo_params> { using type = prepare_statement_algo; };
0057 template <> struct get_algo<set_character_set_algo_params> { using type = set_character_set_algo; };
0058 template <> struct get_algo<quit_connection_algo_params> { using type = quit_connection_algo; };
0059 template <> struct get_algo<close_connection_algo_params> { using type = close_connection_algo; };
0060 template <> struct get_algo<run_pipeline_algo_params> { using type = run_pipeline_algo; };
0061 template <class AlgoParams> using get_algo_t = typename get_algo<AlgoParams>::type;
0062 // clang-format on
0063 
0064 class connection_state
0065 {
0066     // Helper
0067     template <class... Algos>
0068     using make_any_algo_type = variant2::variant<top_level_algo<Algos>...>;
0069 
0070     using any_algo = make_any_algo_type<
0071         connect_algo,
0072         handshake_algo,
0073         execute_algo,
0074         start_execution_algo,
0075         read_resultset_head_algo,
0076         read_some_rows_algo,
0077         read_some_rows_dynamic_algo,
0078         prepare_statement_algo,
0079         set_character_set_algo,
0080         quit_connection_algo,
0081         close_connection_algo,
0082         run_pipeline_algo>;
0083 
0084     connection_state_data st_data_;
0085     any_algo algo_;
0086 
0087     // A function compatible with any_resumable_ref that always fails immediately
0088     // with operation_in_progress. We can't change algo_ while an algorithm is running,
0089     // so we need an any_resumable_ref that doesn't point into algo_
0090     static next_action fail_op_in_progress(void*, error_code, std::size_t)
0091     {
0092         return error_code(client_errc::operation_in_progress);
0093     }
0094 
0095 public:
0096     // We initialize the algo state with a dummy value. This will be overwritten
0097     // by setup() before the first algorithm starts running. Doing this avoids
0098     // the need for a special null algo
0099     connection_state(std::size_t read_buffer_size, std::size_t max_buffer_size, bool transport_supports_ssl)
0100         : st_data_(read_buffer_size, max_buffer_size, transport_supports_ssl),
0101           algo_(top_level_algo<quit_connection_algo>(
0102               st_data_,
0103               st_data_.shared_diag,
0104               quit_connection_algo_params{}
0105           ))
0106     {
0107     }
0108 
0109     const connection_state_data& data() const { return st_data_; }
0110     connection_state_data& data() { return st_data_; }
0111 
0112     template <class AlgoParams>
0113     any_resumable_ref setup(diagnostics& diag, AlgoParams params)
0114     {
0115         // Clear diagnostics
0116         diag.clear();
0117 
0118         // If there is an operation in progress, don't change anything, just fail
0119         if (st_data_.op_in_progress)
0120             return any_resumable_ref(nullptr, &fail_op_in_progress);
0121 
0122         // Emplace the algorithm
0123         using algo_type = top_level_algo<get_algo_t<AlgoParams>>;
0124         return any_resumable_ref(algo_.emplace<algo_type>(st_data_, diag, params));
0125     }
0126 
0127     any_resumable_ref setup(diagnostics& diag, close_statement_algo_params params)
0128     {
0129         return setup(diag, setup_close_statement_pipeline(st_data_, params));
0130     }
0131 
0132     any_resumable_ref setup(diagnostics& diag, reset_connection_algo_params)
0133     {
0134         return setup(diag, setup_reset_connection_pipeline(st_data_));
0135     }
0136 
0137     any_resumable_ref setup(diagnostics& diag, ping_algo_params)
0138     {
0139         return setup(diag, setup_ping_pipeline(st_data_));
0140     }
0141 
0142     template <typename AlgoParams>
0143     typename AlgoParams::result_type result() const
0144     {
0145         return variant2::get<top_level_algo<get_algo_t<AlgoParams>>>(algo_).inner_algo().result(st_data_);
0146     }
0147 };
0148 
0149 }  // namespace detail
0150 }  // namespace mysql
0151 }  // namespace boost
0152 
0153 #endif