moved parameter_binder from member to parameter to bind, execute and fetch methods

This commit is contained in:
2025-07-30 21:00:40 +02:00
parent ebd8bccfb3
commit 6e2baad3ef
18 changed files with 169 additions and 168 deletions
@@ -1,37 +1,11 @@
#ifndef QUERY_PARAMETER_BINDER_HPP
#define QUERY_PARAMETER_BINDER_HPP
#include "matador/utils/types.hpp"
#include <string>
#include <cstring>
#include "matador/utils/attribute_writer.hpp"
namespace matador::sql::interface {
class parameter_binder
{
public:
virtual ~parameter_binder() = default;
virtual void bind(size_t pos, int8_t) = 0;
virtual void bind(size_t pos, int16_t) = 0;
virtual void bind(size_t pos, int32_t) = 0;
virtual void bind(size_t pos, int64_t) = 0;
virtual void bind(size_t pos, uint8_t) = 0;
virtual void bind(size_t pos, uint16_t) = 0;
virtual void bind(size_t pos, uint32_t) = 0;
virtual void bind(size_t pos, uint64_t) = 0;
virtual void bind(size_t pos, bool) = 0;
virtual void bind(size_t pos, float) = 0;
virtual void bind(size_t pos, double) = 0;
virtual void bind(size_t pos, const char *) = 0;
virtual void bind(size_t pos, const char *, size_t size) = 0;
virtual void bind(size_t pos, const std::string&) = 0;
virtual void bind(size_t pos, const std::string &x, size_t size) = 0;
virtual void bind(size_t pos, const utils::blob &) = 0;
// virtual void bind(size_t pos, const matador::time&) = 0;
// virtual void bind(size_t pos, const matador::date&) = 0;
};
using parameter_binder = utils::attribute_writer;
}
@@ -3,6 +3,7 @@
#include "matador/sql/query_context.hpp"
#include "matador/sql/internal/query_result_impl.hpp"
#include "matador/sql/interface/parameter_binder.hpp"
#include "matador/sql/object_parameter_binder.hpp"
#include "matador/utils/data_type_traits.hpp"
@@ -21,24 +22,23 @@ protected:
public:
virtual ~statement_impl() = default;
virtual utils::result<size_t, utils::error> execute() = 0;
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch() = 0;
virtual utils::result<size_t, utils::error> execute(const interface::parameter_binder& bindings) = 0;
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const interface::parameter_binder& bindings) = 0;
template < class Type >
void bind_object(Type &obj)
{
void bind_object(Type &obj, const interface::parameter_binder& bindings) {
object_parameter_binder object_binder_;
object_binder_.reset(start_index());
object_binder_.bind(obj, binder());
object_binder_.bind(obj, bindings);
}
template < class Type >
void bind(const size_t pos, Type &val) {
utils::data_type_traits<Type>::bind_value(binder(), adjust_index(pos), val);
void bind(const size_t pos, Type &val, const interface::parameter_binder& bindings) {
utils::data_type_traits<Type>::bind_value(bindings, adjust_index(pos), val);
}
void bind(size_t pos, const char *value, size_t size);
void bind(size_t pos, std::string &val, size_t size);
void bind(size_t pos, const char *value, size_t size, interface::parameter_binder& bindings) const;
void bind(size_t pos, std::string &val, size_t size, interface::parameter_binder& bindings) const;
virtual void reset() = 0;
@@ -46,12 +46,13 @@ public:
[[nodiscard]] bool is_valid_host_var(const std::string &host_var, size_t pos) const;
protected:
virtual utils::attribute_writer& binder() = 0;
[[nodiscard]] virtual size_t start_index() const;
[[nodiscard]] virtual size_t adjust_index(size_t index) const;
[[nodiscard]] virtual std::unique_ptr<utils::attribute_writer> create_binder() const = 0;
protected:
friend class statement;
friend class statement_proxy;
query_context query_;
};
@@ -2,6 +2,7 @@
#define STATEMENT_PROXY_HPP
#include "matador/sql/interface/statement_impl.hpp"
#include "matador/sql/interface/parameter_binder.hpp"
namespace matador::sql {
class statement_proxy {
@@ -11,22 +12,24 @@ protected:
public:
virtual ~statement_proxy() = default;
virtual utils::result<size_t, utils::error> execute() = 0;
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch() = 0;
virtual utils::result<size_t, utils::error> execute(interface::parameter_binder& bindings) = 0;
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(interface::parameter_binder& bindings) = 0;
template<class Type>
void bind(const Type &obj) {
statement_->bind_object(obj);
void bind(const Type &obj, const interface::parameter_binder& bindings) {
statement_->bind_object(obj, bindings);
}
template<typename Type>
void bind(size_t pos, Type &value) {
statement_->bind(pos, value);
void bind(size_t pos, Type &value, const interface::parameter_binder& bindings) {
statement_->bind(pos, value, bindings);
}
void bind(size_t pos, const char *value, size_t size) const;
void bind(size_t pos, std::string &val, size_t size) const;
void bind(size_t pos, const char *value, size_t size, interface::parameter_binder& bindings) const;
void bind(size_t pos, std::string &val, size_t size, interface::parameter_binder& bindings) const;
void reset() const;
[[nodiscard]] std::unique_ptr<utils::attribute_writer> create_binder() const;
protected:
std::unique_ptr<statement_impl> statement_;
};
@@ -9,8 +9,6 @@
#include "matador/utils/foreign_attributes.hpp"
#include "matador/utils/primary_key_attribute.hpp"
#include <string>
namespace matador::sql {
namespace detail {
@@ -61,8 +59,7 @@ private:
}
class object_parameter_binder
{
class object_parameter_binder {
public:
template<class Type>
void bind(Type &obj, utils::attribute_writer &binder) {
+10 -15
View File
@@ -5,6 +5,7 @@
#include "matador/sql/query_result.hpp"
#include "matador/sql/interface/statement_proxy.hpp"
#include "matador/sql/interface/parameter_binder.hpp"
#include "matador/utils/error.hpp"
#include "matador/utils/result.hpp"
@@ -38,10 +39,7 @@ public:
*
* @param x The statement to move from
*/
statement(statement &&x) noexcept
: statement_proxy_(std::move(x.statement_proxy_))
, logger_(std::move(x.logger_)) {
}
statement(statement &&x) noexcept;
/**
* Assignment move constructor for statement
@@ -49,14 +47,10 @@ public:
* @param x The statement to move from
* @return Reference to this
*/
statement &operator=(statement &&x) noexcept {
statement_proxy_ = std::move(x.statement_proxy_);
logger_ = std::move(x.logger_);
return *this;
}
statement &operator=(statement &&x) noexcept;
statement(const statement &x) = default;
statement &operator=(const statement &x) = default;
statement(const statement &x);
statement &operator=(const statement &x);
statement &bind(size_t pos, const char *value);
statement &bind(size_t pos, std::string &val, size_t size);
@@ -134,24 +128,25 @@ private:
private:
std::shared_ptr<statement_proxy> statement_proxy_;
std::unique_ptr<utils::attribute_writer> bindings_;
std::shared_ptr<abstract_sql_logger> logger_;
};
template<typename Type>
statement &statement::bind(size_t pos, Type &value) {
statement_proxy_->bind(pos, value);
statement_proxy_->bind(pos, value, *bindings_);
return *this;
}
template<class Type>
statement &statement::bind(const Type &obj) {
statement_proxy_->bind(obj);
statement_proxy_->bind(obj, bindings_);
return *this;
}
template<class Type>
utils::result<query_result<Type>, utils::error> statement::fetch() {
return statement_proxy_->fetch().and_then([](std::unique_ptr<query_result_impl> &&value) {
return statement_proxy_->fetch(*bindings_).and_then([](std::unique_ptr<query_result_impl> &&value) {
return utils::ok(query_result<Type>(std::forward<decltype(value)>(value)));
});
// if (!result.is_ok()) {
@@ -162,7 +157,7 @@ utils::result<query_result<Type>, utils::error> statement::fetch() {
template<class Type>
utils::result<std::unique_ptr<Type>, utils::error> statement::fetch_one() {
auto result = statement_proxy_->fetch();
auto result = statement_proxy_->fetch(*bindings_);
if (!result.is_ok()) {
return utils::failure(result.err());
}