Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 08:41:59

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_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     std::uint8_t sequence_number_{0};
0030     unsigned remaining_meta_{0};
0031     statement res_;
0032 
0033     error_code process_response(connection_state_data& st, diagnostics& diag)
0034     {
0035         prepare_stmt_response response{};
0036         auto err = deserialize_prepare_stmt_response(st.reader.message(), st.flavor, response, diag);
0037         if (err)
0038             return err;
0039         res_ = access::construct<statement>(response.id, response.num_params);
0040         remaining_meta_ = response.num_columns + response.num_params;
0041         return error_code();
0042     }
0043 
0044 public:
0045     read_prepare_statement_response_algo(std::uint8_t seqnum) noexcept : sequence_number_(seqnum) {}
0046 
0047     std::uint8_t& sequence_number() { return sequence_number_; }
0048 
0049     next_action resume(connection_state_data& st, diagnostics& diag, error_code ec)
0050     {
0051         if (ec)
0052             return ec;
0053 
0054         switch (resume_point_)
0055         {
0056         case 0:
0057 
0058             // Read response
0059             BOOST_MYSQL_YIELD(resume_point_, 1, st.read(sequence_number_))
0060 
0061             // Process response
0062             ec = process_response(st, diag);
0063             if (ec)
0064                 return ec;
0065 
0066             // Server sends now one packet per parameter and field.
0067             // We ignore these for now.
0068             for (; remaining_meta_ > 0u; --remaining_meta_)
0069                 BOOST_MYSQL_YIELD(resume_point_, 2, st.read(sequence_number_))
0070         }
0071 
0072         return next_action();
0073     }
0074 
0075     statement result(const connection_state_data&) const { return res_; }
0076 };
0077 
0078 class prepare_statement_algo
0079 {
0080     int resume_point_{0};
0081     read_prepare_statement_response_algo read_response_st_;
0082     string_view stmt_sql_;
0083 
0084 public:
0085     prepare_statement_algo(prepare_statement_algo_params params) noexcept
0086         : read_response_st_(0u), stmt_sql_(params.stmt_sql)
0087     {
0088     }
0089 
0090     next_action resume(connection_state_data& st, diagnostics& diag, error_code ec)
0091     {
0092         next_action act;
0093 
0094         switch (resume_point_)
0095         {
0096         case 0:
0097             // Check status
0098             ec = st.check_status_ready();
0099             if (ec)
0100                 return ec;
0101 
0102             // Send request
0103             BOOST_MYSQL_YIELD(
0104                 resume_point_,
0105                 1,
0106                 st.write(prepare_stmt_command{stmt_sql_}, read_response_st_.sequence_number())
0107             )
0108             if (ec)
0109                 return ec;
0110 
0111             // Read response
0112             while (!(act = read_response_st_.resume(st, diag, ec)).is_done())
0113                 BOOST_MYSQL_YIELD(resume_point_, 2, act)
0114             return act;
0115         }
0116 
0117         return next_action();
0118     }
0119 
0120     statement result(const connection_state_data& st) const { return read_response_st_.result(st); }
0121 };
0122 
0123 }  // namespace detail
0124 }  // namespace mysql
0125 }  // namespace boost
0126 
0127 #endif