Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:33:52

0001 //
0002 // Copyright (c) 2019-2024 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_START_EXECUTION_HPP
0009 #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_START_EXECUTION_HPP
0010 
0011 #include <boost/mysql/diagnostics.hpp>
0012 #include <boost/mysql/error_code.hpp>
0013 
0014 #include <boost/mysql/detail/algo_params.hpp>
0015 #include <boost/mysql/detail/any_execution_request.hpp>
0016 #include <boost/mysql/detail/execution_processor/execution_processor.hpp>
0017 #include <boost/mysql/detail/next_action.hpp>
0018 
0019 #include <boost/mysql/impl/internal/coroutine.hpp>
0020 #include <boost/mysql/impl/internal/protocol/serialization.hpp>
0021 #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
0022 #include <boost/mysql/impl/internal/sansio/read_resultset_head.hpp>
0023 
0024 namespace boost {
0025 namespace mysql {
0026 namespace detail {
0027 
0028 inline error_code check_client_errors(const any_execution_request& req)
0029 {
0030     if (req.is_query)
0031         return error_code();
0032     return req.data.stmt.stmt.num_params() == req.data.stmt.params.size() ? error_code()
0033                                                                           : client_errc::wrong_num_params;
0034 }
0035 
0036 inline resultset_encoding get_encoding(const any_execution_request& req)
0037 {
0038     return req.is_query ? resultset_encoding::text : resultset_encoding::binary;
0039 }
0040 
0041 class start_execution_algo
0042 {
0043     int resume_point_{0};
0044     read_resultset_head_algo read_head_st_;
0045     any_execution_request req_;
0046 
0047     std::uint8_t& seqnum() { return processor().sequence_number(); }
0048     execution_processor& processor() { return read_head_st_.processor(); }
0049     diagnostics& diag() { return read_head_st_.diag(); }
0050 
0051     next_action compose_request(connection_state_data& st)
0052     {
0053         if (req_.is_query)
0054         {
0055             return st.write(query_command{req_.data.query}, seqnum());
0056         }
0057         else
0058         {
0059             return st.write(execute_stmt_command{req_.data.stmt.stmt.id(), req_.data.stmt.params}, seqnum());
0060         }
0061     }
0062 
0063 public:
0064     start_execution_algo(start_execution_algo_params params) noexcept
0065         : read_head_st_(read_resultset_head_algo_params{params.diag, params.proc}), req_(params.req)
0066     {
0067     }
0068 
0069     next_action resume(connection_state_data& st, error_code ec)
0070     {
0071         next_action act;
0072 
0073         switch (resume_point_)
0074         {
0075         case 0:
0076 
0077             // Clear diagnostics
0078             diag().clear();
0079 
0080             // Check for errors
0081             ec = check_client_errors(req_);
0082             if (ec)
0083                 return ec;
0084 
0085             // Reset the processor
0086             processor().reset(get_encoding(req_), st.meta_mode);
0087 
0088             // Send the execution request
0089             BOOST_MYSQL_YIELD(resume_point_, 1, compose_request(st))
0090 
0091             if (ec)
0092                 return ec;
0093 
0094             // Read the first resultset's head and return its result
0095             while (!(act = read_head_st_.resume(st, ec)).is_done())
0096                 BOOST_MYSQL_YIELD(resume_point_, 2, act)
0097             return act;
0098         }
0099 
0100         return next_action();
0101     }
0102 };
0103 
0104 }  // namespace detail
0105 }  // namespace mysql
0106 }  // namespace boost
0107 
0108 #endif /* INCLUDE_MYSQL_IMPL_NETWORK_ALGORITHMS_READ_RESULTSET_HEAD_HPP_ */