From 06fb1a29b75d4657f2f40357ac98bb24c8f471de Mon Sep 17 00:00:00 2001 From: Ruben Perez Date: Wed, 5 Oct 2022 18:17:11 +0200 Subject: [PATCH] New execute_params --- .../impl/execute_statement.hpp | 80 ++++++++++++++++--- include/boost/mysql/execute_params.hpp | 20 +---- include/boost/mysql/impl/execute_params.hpp | 54 ------------- 3 files changed, 72 insertions(+), 82 deletions(-) delete mode 100644 include/boost/mysql/impl/execute_params.hpp diff --git a/include/boost/mysql/detail/network_algorithms/impl/execute_statement.hpp b/include/boost/mysql/detail/network_algorithms/impl/execute_statement.hpp index b6031057..5aad7931 100644 --- a/include/boost/mysql/detail/network_algorithms/impl/execute_statement.hpp +++ b/include/boost/mysql/detail/network_algorithms/impl/execute_statement.hpp @@ -10,13 +10,18 @@ #pragma once +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include +#include +#include +#include + namespace boost { namespace mysql { @@ -38,6 +43,44 @@ com_stmt_execute_packet make_stmt_execute_packet( }; } +template +error_code check_num_params( + const statement_base& stmt, + const execute_params& params, + error_info& info +) +{ + auto param_count = std::distance(params.first(), params.last()); + if (param_count != stmt.num_params()) + { + info.set_message(detail::stringize( + "execute statement: expected ", stmt.num_params(), " params, but got ", param_count + )); + return make_error_code(errc::wrong_num_params); + } + else + { + return error_code(); + } +} + +struct fast_fail_op : boost::asio::coroutine +{ + error_code err_; + + fast_fail_op(error_code err) noexcept : err_(err) {} + + template + void operator()(Self& self) + { + BOOST_ASIO_CORO_REENTER(*this) + { + BOOST_ASIO_CORO_YIELD boost::asio::post(std::move(self)); + self.complete(err_); + } + } +}; + } // detail } // mysql @@ -53,14 +96,18 @@ void boost::mysql::detail::execute_statement( error_info& info ) { - execute_generic( - resultset_encoding::binary, - chan, - make_stmt_execute_packet(params, stmt), - output, - err, - info - ); + err = check_num_params(stmt, params, info); + if (!err) + { + execute_generic( + resultset_encoding::binary, + chan, + make_stmt_execute_packet(params, stmt), + output, + err, + info + ); + } } template @@ -77,6 +124,15 @@ boost::mysql::detail::async_execute_statement( CompletionToken&& token ) { + error_code err = check_num_params(stmt, params, info); + if (err) + { + return boost::asio::async_compose( + fast_fail_op(err), + token, + chan + ); + } return async_execute_generic( resultset_encoding::binary, chan, diff --git a/include/boost/mysql/execute_params.hpp b/include/boost/mysql/execute_params.hpp index ab32ee2a..bbcca263 100644 --- a/include/boost/mysql/execute_params.hpp +++ b/include/boost/mysql/execute_params.hpp @@ -8,7 +8,6 @@ #ifndef BOOST_MYSQL_EXECUTE_PARAMS_HPP #define BOOST_MYSQL_EXECUTE_PARAMS_HPP -#include #include #include #include @@ -32,20 +31,14 @@ namespace mysql { template class execute_params { - std::uint32_t statement_id_; FieldViewFwdIterator first_; FieldViewFwdIterator last_; static_assert(detail::is_field_view_forward_iterator::value, "FieldViewFwdIterator requirements not met"); public: /// Constructor. - execute_params( - const statement_base& stmt, - FieldViewFwdIterator first, - FieldViewFwdIterator last - ); - - constexpr std::uint32_t statement_id() const noexcept { return statement_id_; } + constexpr execute_params(FieldViewFwdIterator first, FieldViewFwdIterator last) : + first_(first), last_(last) {} /// Retrieves the parameter value range's begin. constexpr FieldViewFwdIterator first() const { return first_; } @@ -60,12 +53,11 @@ template < > constexpr execute_params make_execute_params( - const statement_base& stmt, FieldViewFwdIterator first, FieldViewFwdIterator last ) { - return execute_params(stmt, first, last); + return execute_params(first, last); } template < @@ -73,18 +65,14 @@ template < class EnableIf = detail::enable_if_field_view_collection > constexpr auto make_execute_params( - const statement_base& stmt, const FieldViewCollection& col ) -> execute_params { - return make_execute_params(stmt, std::begin(col), std::end(col)); + return make_execute_params(std::begin(col), std::end(col)); } -// TODO: add something to create these from T... } // mysql } // boost -#include - #endif diff --git a/include/boost/mysql/impl/execute_params.hpp b/include/boost/mysql/impl/execute_params.hpp deleted file mode 100644 index 19600be9..00000000 --- a/include/boost/mysql/impl/execute_params.hpp +++ /dev/null @@ -1,54 +0,0 @@ -// -// Copyright (c) 2019-2022 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_IMPL_EXECUTE_PARAMS_HPP -#define BOOST_MYSQL_IMPL_EXECUTE_PARAMS_HPP - -#pragma once - -#include -#include -#include - -namespace boost { -namespace mysql { -namespace detail { - -template -void check_num_params( - FieldViewFwdIterator first, - FieldViewFwdIterator last, - const statement_base& stmt -) -{ - auto param_count = std::distance(first, last); - if (param_count != stmt.num_params()) - { - throw std::domain_error(detail::stringize( - "prepared_statement::execute: expected ", stmt.num_params(), " params, but got ", param_count)); - } -} - -} // detail -} // mysql -} // boost - -template -boost::mysql::execute_params::execute_params( - const statement_base& stmt, - FieldViewFwdIterator first, - FieldViewFwdIterator last -) : - statement_id_(stmt.id()), - first_(first), - last_(last) -{ - detail::check_num_params(first, last, stmt); -} - - -#endif