| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- //
- // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_MYSQL_DETAIL_CONNECTION_IMPL_HPP
- #define BOOST_MYSQL_DETAIL_CONNECTION_IMPL_HPP
- #include <boost/mysql/any_address.hpp>
- #include <boost/mysql/connect_params.hpp>
- #include <boost/mysql/diagnostics.hpp>
- #include <boost/mysql/error_code.hpp>
- #include <boost/mysql/execution_state.hpp>
- #include <boost/mysql/field_view.hpp>
- #include <boost/mysql/handshake_params.hpp>
- #include <boost/mysql/metadata_mode.hpp>
- #include <boost/mysql/rows_view.hpp>
- #include <boost/mysql/statement.hpp>
- #include <boost/mysql/string_view.hpp>
- #include <boost/mysql/detail/access.hpp>
- #include <boost/mysql/detail/algo_params.hpp>
- #include <boost/mysql/detail/config.hpp>
- #include <boost/mysql/detail/connect_params_helpers.hpp>
- #include <boost/mysql/detail/engine.hpp>
- #include <boost/mysql/detail/execution_processor/execution_processor.hpp>
- #include <boost/mysql/detail/initiation_base.hpp>
- #include <boost/mysql/detail/intermediate_handler.hpp>
- #include <boost/asio/any_io_executor.hpp>
- #include <boost/optional/optional.hpp>
- #include <boost/system/result.hpp>
- #include <cstddef>
- #include <cstdint>
- #include <cstring>
- #include <memory>
- #include <type_traits>
- #include <utility>
- #include <vector>
- namespace boost {
- namespace mysql {
- // Forward decl
- template <class... StaticRow>
- class static_execution_state;
- struct character_set;
- class pipeline_request;
- namespace detail {
- //
- // Helpers to interact with connection_state, without including its definition
- //
- class connection_state;
- struct connection_state_deleter
- {
- BOOST_MYSQL_DECL void operator()(connection_state*) const;
- };
- BOOST_MYSQL_DECL std::vector<field_view>& get_shared_fields(connection_state&);
- template <class AlgoParams>
- any_resumable_ref setup(connection_state&, diagnostics&, const AlgoParams&);
- // Note: AlgoParams should have !is_void_result
- template <class AlgoParams>
- typename AlgoParams::result_type get_result(const connection_state&);
- //
- // helpers to run algos
- //
- template <class AlgoParams>
- using has_void_result = std::is_same<typename AlgoParams::result_type, void>;
- template <class AlgoParams, bool is_void>
- struct completion_signature_impl;
- template <class AlgoParams>
- struct completion_signature_impl<AlgoParams, true>
- {
- // Using typedef to workaround a msvc 14.1 bug
- typedef void(type)(error_code);
- };
- template <class AlgoParams>
- struct completion_signature_impl<AlgoParams, false>
- {
- // Using typedef to workaround a msvc 14.1 bug
- typedef void(type)(error_code, typename AlgoParams::result_type);
- };
- template <class AlgoParams>
- using completion_signature_t = typename completion_signature_impl<
- AlgoParams,
- has_void_result<AlgoParams>::value>::type;
- // Intermediate handler
- template <class AlgoParams>
- struct generic_algo_fn
- {
- static_assert(!has_void_result<AlgoParams>::value, "Internal error: result_type should be non-void");
- using result_t = typename AlgoParams::result_type;
- template <class Handler>
- void operator()(Handler&& handler, error_code ec)
- {
- std::move(handler)(ec, ec ? result_t{} : get_result<AlgoParams>(*st));
- }
- connection_state* st;
- };
- // Note: the 1st async_initiate arg should be a diagnostics*,
- // so completion tokens knowing how Boost.MySQL work can operate
- class connection_impl
- {
- std::unique_ptr<engine> engine_;
- std::unique_ptr<connection_state, connection_state_deleter> st_;
- asio::any_io_executor get_executor() const { return engine_->get_executor(); }
- // Helper for execution requests
- template <class T>
- static auto make_request(T&& input, connection_state& st)
- -> decltype(execution_request_traits<typename std::decay<T>::type>::make_request(
- std::forward<T>(input),
- get_shared_fields(st)
- ))
- {
- return execution_request_traits<typename std::decay<T>::type>::make_request(
- std::forward<T>(input),
- get_shared_fields(st)
- );
- }
- // Generic algorithm
- template <class AlgoParams>
- typename AlgoParams::result_type run_impl(
- AlgoParams params,
- error_code& ec,
- diagnostics& diag,
- std::true_type /* has_void_result */
- )
- {
- engine_->run(setup(*st_, diag, params), ec);
- }
- template <class AlgoParams>
- typename AlgoParams::result_type run_impl(
- AlgoParams params,
- error_code& ec,
- diagnostics& diag,
- std::false_type /* has_void_result */
- )
- {
- engine_->run(setup(*st_, diag, params), ec);
- return get_result<AlgoParams>(*st_);
- }
- template <class AlgoParams, class Handler>
- static void async_run_impl(
- engine& eng,
- connection_state& st,
- AlgoParams params,
- diagnostics& diag,
- Handler&& handler,
- std::true_type /* has_void_result */
- )
- {
- eng.async_run(setup(st, diag, params), std::forward<Handler>(handler));
- }
- template <class AlgoParams, class Handler>
- static void async_run_impl(
- engine& eng,
- connection_state& st,
- AlgoParams params,
- diagnostics& diag,
- Handler&& handler,
- std::false_type /* has_void_result */
- )
- {
- eng.async_run(
- setup(st, diag, params),
- make_intermediate_handler(generic_algo_fn<AlgoParams>{&st}, std::forward<Handler>(handler))
- );
- }
- template <class AlgoParams, class Handler>
- static void async_run_impl(
- engine& eng,
- connection_state& st,
- AlgoParams params,
- diagnostics& diag,
- Handler&& handler
- )
- {
- async_run_impl(eng, st, params, diag, std::forward<Handler>(handler), has_void_result<AlgoParams>{});
- }
- struct run_algo_initiation : initiation_base
- {
- using initiation_base::initiation_base;
- template <class Handler, class AlgoParams>
- void operator()(
- Handler&& handler,
- diagnostics* diag,
- engine* eng,
- connection_state* st,
- AlgoParams params
- )
- {
- async_run_impl(*eng, *st, params, *diag, std::forward<Handler>(handler));
- }
- };
- // Connect
- static connect_algo_params make_params_connect(const void* server_address, const handshake_params& params)
- {
- return connect_algo_params{server_address, params, false};
- }
- static connect_algo_params make_params_connect_v2(const connect_params& params)
- {
- return connect_algo_params{
- ¶ms.server_address,
- make_hparams(params),
- params.server_address.type() == address_type::unix_path
- };
- }
- template <class EndpointType>
- struct initiate_connect : initiation_base
- {
- using initiation_base::initiation_base;
- template <class Handler>
- void operator()(
- Handler&& handler,
- diagnostics* diag,
- engine* eng,
- connection_state* st,
- const EndpointType& endpoint,
- handshake_params params
- )
- {
- async_run_impl(
- *eng,
- *st,
- make_params_connect(&endpoint, params),
- *diag,
- std::forward<Handler>(handler)
- );
- }
- };
- struct initiate_connect_v2 : initiation_base
- {
- using initiation_base::initiation_base;
- template <class Handler>
- void operator()(
- Handler&& handler,
- diagnostics* diag,
- engine* eng,
- connection_state* st,
- const connect_params* params
- )
- {
- async_run_impl(*eng, *st, make_params_connect_v2(*params), *diag, std::forward<Handler>(handler));
- }
- };
- // execute
- struct initiate_execute : initiation_base
- {
- using initiation_base::initiation_base;
- template <class Handler, class ExecutionRequest>
- void operator()(
- Handler&& handler,
- diagnostics* diag,
- engine* eng,
- connection_state* st,
- ExecutionRequest&& req,
- execution_processor* proc
- )
- {
- async_run_impl(
- *eng,
- *st,
- execute_algo_params{make_request(std::forward<ExecutionRequest>(req), *st), proc},
- *diag,
- std::forward<Handler>(handler)
- );
- }
- };
- // start execution
- struct initiate_start_execution : initiation_base
- {
- using initiation_base::initiation_base;
- template <class Handler, class ExecutionRequest>
- void operator()(
- Handler&& handler,
- diagnostics* diag,
- engine* eng,
- connection_state* st,
- ExecutionRequest&& req,
- execution_processor* proc
- )
- {
- async_run_impl(
- *eng,
- *st,
- start_execution_algo_params{make_request(std::forward<ExecutionRequest>(req), *st), proc},
- *diag,
- std::forward<Handler>(handler)
- );
- }
- };
- public:
- BOOST_MYSQL_DECL connection_impl(
- std::size_t read_buff_size,
- std::size_t max_buffer_size,
- std::unique_ptr<engine> eng
- );
- BOOST_MYSQL_DECL metadata_mode meta_mode() const;
- BOOST_MYSQL_DECL void set_meta_mode(metadata_mode m);
- BOOST_MYSQL_DECL bool ssl_active() const;
- BOOST_MYSQL_DECL bool backslash_escapes() const;
- BOOST_MYSQL_DECL system::result<character_set> current_character_set() const;
- BOOST_MYSQL_DECL boost::optional<std::uint32_t> connection_id() const;
- BOOST_MYSQL_DECL diagnostics& shared_diag();
- engine& get_engine()
- {
- BOOST_ASSERT(engine_);
- return *engine_;
- }
- const engine& get_engine() const
- {
- BOOST_ASSERT(engine_);
- return *engine_;
- }
- // Generic algorithm
- template <class AlgoParams>
- typename AlgoParams::result_type run(AlgoParams params, error_code& ec, diagnostics& diag)
- {
- return run_impl(params, ec, diag, has_void_result<AlgoParams>{});
- }
- template <class AlgoParams, class CompletionToken>
- auto async_run(AlgoParams params, diagnostics& diag, CompletionToken&& token)
- -> decltype(asio::async_initiate<CompletionToken, completion_signature_t<AlgoParams>>(
- run_algo_initiation(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- params
- ))
- {
- return asio::async_initiate<CompletionToken, completion_signature_t<AlgoParams>>(
- run_algo_initiation(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- params
- );
- }
- // Connect
- template <class EndpointType>
- void connect(
- const EndpointType& endpoint,
- const handshake_params& params,
- error_code& err,
- diagnostics& diag
- )
- {
- run(make_params_connect(&endpoint, params), err, diag);
- }
- void connect_v2(const connect_params& params, error_code& err, diagnostics& diag)
- {
- run(make_params_connect_v2(params), err, diag);
- }
- template <class EndpointType, class CompletionToken>
- auto async_connect(
- const EndpointType& endpoint,
- const handshake_params& params,
- diagnostics& diag,
- CompletionToken&& token
- )
- -> decltype(asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_connect<EndpointType>(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- endpoint,
- params
- ))
- {
- return asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_connect<EndpointType>(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- endpoint,
- params
- );
- }
- template <class CompletionToken>
- auto async_connect_v2(const connect_params& params, diagnostics& diag, CompletionToken&& token)
- -> decltype(asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_connect_v2(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- ¶ms
- ))
- {
- return asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_connect_v2(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- ¶ms
- );
- }
- // Handshake
- handshake_algo_params make_params_handshake(const handshake_params& params) const
- {
- return {params, false};
- }
- // Execute
- template <class ExecutionRequest, class ResultsType>
- void execute(ExecutionRequest&& req, ResultsType& result, error_code& err, diagnostics& diag)
- {
- run(
- execute_algo_params{
- make_request(std::forward<ExecutionRequest>(req), *st_),
- &access::get_impl(result).get_interface()
- },
- err,
- diag
- );
- }
- template <class ExecutionRequest, class ResultsType, class CompletionToken>
- auto async_execute(
- ExecutionRequest&& req,
- ResultsType& result,
- diagnostics& diag,
- CompletionToken&& token
- )
- -> decltype(asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_execute(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- std::forward<ExecutionRequest>(req),
- &access::get_impl(result).get_interface()
- ))
- {
- return asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_execute(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- std::forward<ExecutionRequest>(req),
- &access::get_impl(result).get_interface()
- );
- }
- // Start execution
- template <class ExecutionRequest, class ExecutionStateType>
- void start_execution(
- ExecutionRequest&& req,
- ExecutionStateType& exec_st,
- error_code& err,
- diagnostics& diag
- )
- {
- run(
- start_execution_algo_params{
- make_request(std::forward<ExecutionRequest>(req), *st_),
- &access::get_impl(exec_st).get_interface()
- },
- err,
- diag
- );
- }
- template <class ExecutionRequest, class ExecutionStateType, class CompletionToken>
- auto async_start_execution(
- ExecutionRequest&& req,
- ExecutionStateType& exec_st,
- diagnostics& diag,
- CompletionToken&& token
- )
- -> decltype(asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_start_execution(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- std::forward<ExecutionRequest>(req),
- &access::get_impl(exec_st).get_interface()
- ))
- {
- return asio::async_initiate<CompletionToken, void(error_code)>(
- initiate_start_execution(get_executor()),
- token,
- &diag,
- engine_.get(),
- st_.get(),
- std::forward<ExecutionRequest>(req),
- &access::get_impl(exec_st).get_interface()
- );
- }
- // Read some rows (dynamic)
- read_some_rows_dynamic_algo_params make_params_read_some_rows(execution_state& st) const
- {
- return {&access::get_impl(st).get_interface()};
- }
- // Read some rows (static)
- template <class SpanElementType, class ExecutionState>
- read_some_rows_algo_params make_params_read_some_rows_static(
- ExecutionState& exec_st,
- span<SpanElementType> output
- ) const
- {
- return {
- &access::get_impl(exec_st).get_interface(),
- access::get_impl(exec_st).make_output_ref(output)
- };
- }
- // Read resultset head
- template <class ExecutionStateType>
- read_resultset_head_algo_params make_params_read_resultset_head(ExecutionStateType& st) const
- {
- return {&detail::access::get_impl(st).get_interface()};
- }
- // Close statement
- close_statement_algo_params make_params_close_statement(statement stmt) const { return {stmt.id()}; }
- // Run pipeline. Separately compiled to avoid including the pipeline header here
- BOOST_MYSQL_DECL
- static run_pipeline_algo_params make_params_pipeline(
- const pipeline_request& req,
- std::vector<stage_response>& response
- );
- // Exposed for testing
- connection_state& get_state() { return *st_; }
- };
- // To use some completion tokens, like deferred, in C++11, the old macros
- // BOOST_ASIO_INITFN_AUTO_RESULT_TYPE are no longer enough.
- // Helper typedefs to reduce duplication
- template <class AlgoParams, class CompletionToken>
- using async_run_t = decltype(std::declval<connection_impl&>().async_run(
- std::declval<AlgoParams>(),
- std::declval<diagnostics&>(),
- std::declval<CompletionToken>()
- ));
- template <class EndpointType, class CompletionToken>
- using async_connect_t = decltype(std::declval<connection_impl&>().async_connect(
- std::declval<const EndpointType&>(),
- std::declval<const handshake_params&>(),
- std::declval<diagnostics&>(),
- std::declval<CompletionToken>()
- ));
- template <class CompletionToken>
- using async_connect_v2_t = decltype(std::declval<connection_impl&>().async_connect_v2(
- std::declval<const connect_params&>(),
- std::declval<diagnostics&>(),
- std::declval<CompletionToken>()
- ));
- template <class ExecutionRequest, class ResultsType, class CompletionToken>
- using async_execute_t = decltype(std::declval<connection_impl&>().async_execute(
- std::declval<ExecutionRequest>(),
- std::declval<ResultsType&>(),
- std::declval<diagnostics&>(),
- std::declval<CompletionToken>()
- ));
- template <class ExecutionRequest, class ExecutionStateType, class CompletionToken>
- using async_start_execution_t = decltype(std::declval<connection_impl&>().async_start_execution(
- std::declval<ExecutionRequest>(),
- std::declval<ExecutionStateType&>(),
- std::declval<diagnostics&>(),
- std::declval<CompletionToken>()
- ));
- template <class CompletionToken>
- using async_handshake_t = async_run_t<handshake_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_read_resultset_head_t = async_run_t<read_resultset_head_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_read_some_rows_dynamic_t = async_run_t<read_some_rows_dynamic_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_prepare_statement_t = async_run_t<prepare_statement_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_close_statement_t = async_run_t<close_statement_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_set_character_set_t = async_run_t<set_character_set_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_ping_t = async_run_t<ping_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_reset_connection_t = async_run_t<reset_connection_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_quit_connection_t = async_run_t<quit_connection_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_close_connection_t = async_run_t<close_connection_algo_params, CompletionToken>;
- template <class CompletionToken>
- using async_run_pipeline_t = async_run_t<run_pipeline_algo_params, CompletionToken>;
- } // namespace detail
- } // namespace mysql
- } // namespace boost
- #ifdef BOOST_MYSQL_HEADER_ONLY
- #include <boost/mysql/impl/connection_impl.ipp>
- #endif
- #endif
|