mirror of
https://github.com/boostorg/mysql.git
synced 2026-02-14 00:42:53 +00:00
New execute_params
This commit is contained in:
@@ -10,13 +10,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <boost/mysql/detail/network_algorithms/execute_statement.hpp>
|
||||
#include <boost/mysql/detail/network_algorithms/execute_generic.hpp>
|
||||
#include <boost/mysql/detail/protocol/prepared_statement_messages.hpp>
|
||||
#include <boost/mysql/detail/protocol/resultset_encoding.hpp>
|
||||
#include <boost/mysql/detail/auxiliar/stringize.hpp>
|
||||
#include <boost/mysql/statement_base.hpp>
|
||||
#include <boost/mysql/resultset_base.hpp>
|
||||
#include <boost/mysql/execute_params.hpp>
|
||||
#include <boost/mysql/detail/network_algorithms/execute_statement.hpp>
|
||||
#include <boost/mysql/detail/protocol/prepared_statement_messages.hpp>
|
||||
#include <boost/mysql/detail/protocol/resultset_encoding.hpp>
|
||||
#include <boost/mysql/detail/network_algorithms/execute_generic.hpp>
|
||||
#include <boost/mysql/error.hpp>
|
||||
#include <boost/asio/post.hpp>
|
||||
#include <boost/asio/compose.hpp>
|
||||
|
||||
|
||||
namespace boost {
|
||||
namespace mysql {
|
||||
@@ -38,6 +43,44 @@ com_stmt_execute_packet<FieldViewFwdIterator> make_stmt_execute_packet(
|
||||
};
|
||||
}
|
||||
|
||||
template <class FieldViewFwdIterator>
|
||||
error_code check_num_params(
|
||||
const statement_base& stmt,
|
||||
const execute_params<FieldViewFwdIterator>& 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<class Self>
|
||||
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 <class Stream, class FieldViewFwdIterator, class CompletionToken>
|
||||
@@ -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<CompletionToken, void(error_code)>(
|
||||
fast_fail_op(err),
|
||||
token,
|
||||
chan
|
||||
);
|
||||
}
|
||||
return async_execute_generic(
|
||||
resultset_encoding::binary,
|
||||
chan,
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef BOOST_MYSQL_EXECUTE_PARAMS_HPP
|
||||
#define BOOST_MYSQL_EXECUTE_PARAMS_HPP
|
||||
|
||||
#include <boost/mysql/statement_base.hpp>
|
||||
#include <boost/mysql/detail/auxiliar/field_type_traits.hpp>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
@@ -32,20 +31,14 @@ namespace mysql {
|
||||
template <class FieldViewFwdIterator>
|
||||
class execute_params
|
||||
{
|
||||
std::uint32_t statement_id_;
|
||||
FieldViewFwdIterator first_;
|
||||
FieldViewFwdIterator last_;
|
||||
static_assert(detail::is_field_view_forward_iterator<FieldViewFwdIterator>::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<FieldViewFwdIterator>
|
||||
make_execute_params(
|
||||
const statement_base& stmt,
|
||||
FieldViewFwdIterator first,
|
||||
FieldViewFwdIterator last
|
||||
)
|
||||
{
|
||||
return execute_params<FieldViewFwdIterator>(stmt, first, last);
|
||||
return execute_params<FieldViewFwdIterator>(first, last);
|
||||
}
|
||||
|
||||
template <
|
||||
@@ -73,18 +65,14 @@ template <
|
||||
class EnableIf = detail::enable_if_field_view_collection<FieldViewCollection>
|
||||
>
|
||||
constexpr auto make_execute_params(
|
||||
const statement_base& stmt,
|
||||
const FieldViewCollection& col
|
||||
) -> execute_params<decltype(std::begin(col))>
|
||||
{
|
||||
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 <boost/mysql/impl/execute_params.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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 <boost/mysql/execute_params.hpp>
|
||||
#include <boost/mysql/detail/auxiliar/stringize.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace boost {
|
||||
namespace mysql {
|
||||
namespace detail {
|
||||
|
||||
template <class FieldViewFwdIterator>
|
||||
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 <class FieldViewFwdIterator>
|
||||
boost::mysql::execute_params<FieldViewFwdIterator>::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
|
||||
Reference in New Issue
Block a user