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_PREPARE_STATEMENT_HPP
0009 #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_PREPARE_STATEMENT_HPP
0010 
0011 #include <boost/mysql/diagnostics.hpp>
0012 #include <boost/mysql/error_code.hpp>
0013 #include <boost/mysql/statement.hpp>
0014 
0015 #include <boost/mysql/detail/algo_params.hpp>
0016 #include <boost/mysql/detail/next_action.hpp>
0017 
0018 #include <boost/mysql/impl/internal/coroutine.hpp>
0019 #include <boost/mysql/impl/internal/protocol/deserialization.hpp>
0020 #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
0021 
0022 namespace boost {
0023 namespace mysql {
0024 namespace detail {
0025 
0026 class read_prepare_statement_response_algo
0027 {
0028     int resume_point_{0};
0029     diagnostics* diag_;
0030     std::uint8_t sequence_number_{0};
0031     unsigned remaining_meta_{0};
0032     statement res_;
0033 
0034     error_code process_response(connection_state_data& st)
0035     {
0036         prepare_stmt_response response{};
0037         auto err = deserialize_prepare_stmt_response(st.reader.message(), st.flavor, response, *diag_);
0038         if (err)
0039             return err;
0040         res_ = access::construct<statement>(response.id, response.num_params);
0041         remaining_meta_ = response.num_columns + response.num_params;
0042         return error_code();
0043     }
0044 
0045 public:
0046     read_prepare_statement_response_algo(diagnostics* diag, std::uint8_t seqnum) noexcept
0047         : diag_(diag), sequence_number_(seqnum)
0048     {
0049     }
0050 
0051     std::uint8_t& sequence_number() { return sequence_number_; }
0052     diagnostics& diag() { return *diag_; }
0053 
0054     next_action resume(connection_state_data& st, error_code ec)
0055     {
0056         if (ec)
0057             return ec;
0058 
0059         switch (resume_point_)
0060         {
0061         case 0:
0062 
0063             // Note: diagnostics should have been cleaned by other algos
0064 
0065             // Read response
0066             BOOST_MYSQL_YIELD(resume_point_, 1, st.read(sequence_number_))
0067 
0068             // Process response
0069             ec = process_response(st);
0070             if (ec)
0071                 return ec;
0072 
0073             // Server sends now one packet per parameter and field.
0074             // We ignore these for now.
0075             for (; remaining_meta_ > 0u; --remaining_meta_)
0076                 BOOST_MYSQL_YIELD(resume_point_, 2, st.read(sequence_number_))
0077         }
0078 
0079         return next_action();
0080     }
0081 
0082     statement result(const connection_state_data&) const { return res_; }
0083 };
0084 
0085 class prepare_statement_algo
0086 {
0087     int resume_point_{0};
0088     read_prepare_statement_response_algo read_response_st_;
0089     string_view stmt_sql_;
0090 
0091 public:
0092     prepare_statement_algo(prepare_statement_algo_params params) noexcept
0093         : read_response_st_(params.diag, 0u), stmt_sql_(params.stmt_sql)
0094     {
0095     }
0096 
0097     next_action resume(connection_state_data& st, error_code ec)
0098     {
0099         next_action act;
0100 
0101         switch (resume_point_)
0102         {
0103         case 0:
0104 
0105             // Clear diagnostics
0106             read_response_st_.diag().clear();
0107 
0108             // Send request
0109             BOOST_MYSQL_YIELD(
0110                 resume_point_,
0111                 1,
0112                 st.write(prepare_stmt_command{stmt_sql_}, read_response_st_.sequence_number())
0113             )
0114             if (ec)
0115                 return ec;
0116 
0117             // Read response
0118             while (!(act = read_response_st_.resume(st, ec)).is_done())
0119                 BOOST_MYSQL_YIELD(resume_point_, 2, act)
0120             return act;
0121         }
0122 
0123         return next_action();
0124     }
0125 
0126     statement result(const connection_state_data& st) const { return read_response_st_.result(st); }
0127 };
0128 
0129 }  // namespace detail
0130 }  // namespace mysql
0131 }  // namespace boost
0132 
0133 #endif