statement binding progress
This commit is contained in:
@@ -17,7 +17,7 @@ class sql_error;
|
||||
class statement_impl
|
||||
{
|
||||
protected:
|
||||
explicit statement_impl(query_context query);
|
||||
explicit statement_impl(query_context query, size_t start_bind_pos);
|
||||
|
||||
public:
|
||||
virtual ~statement_impl() = default;
|
||||
@@ -27,7 +27,7 @@ public:
|
||||
|
||||
template < class Type >
|
||||
size_t bind_object(Type &obj, parameter_binder& bindings) {
|
||||
object_parameter_binder object_binder_(query_.command != sql_command::SQL_UPDATE);
|
||||
object_parameter_binder object_binder_;
|
||||
object_binder_.reset(start_index());
|
||||
object_binder_.bind(obj, bindings);
|
||||
|
||||
@@ -36,13 +36,21 @@ public:
|
||||
|
||||
template < class Type >
|
||||
void bind(const size_t pos, Type &val, parameter_binder& bindings) {
|
||||
utils::data_type_traits<Type>::bind_value(bindings, adjust_index(pos), val);
|
||||
current_bind_pos_ = pos;
|
||||
bind(val, bindings);
|
||||
}
|
||||
template < class Type >
|
||||
void bind(Type &val, parameter_binder& bindings) {
|
||||
utils::data_type_traits<Type>::bind_value(bindings, adjust_index(current_bind_pos_++), val);
|
||||
}
|
||||
void bind(size_t pos, const char *value, size_t size, parameter_binder& bindings);
|
||||
void bind(const char *value, size_t size, parameter_binder& bindings);
|
||||
void bind(size_t pos, std::string &val, size_t size, parameter_binder& bindings);
|
||||
void bind(std::string &value, size_t size, parameter_binder& bindings);
|
||||
|
||||
void bind(size_t pos, const char *value, size_t size, parameter_binder& bindings) const;
|
||||
void bind(size_t pos, std::string &val, size_t size, parameter_binder& bindings) const;
|
||||
virtual void reset();
|
||||
|
||||
virtual void reset() = 0;
|
||||
[[nodiscard]] size_t bind_pos() const;
|
||||
|
||||
[[nodiscard]] const std::vector<std::string>& bind_vars() const;
|
||||
[[nodiscard]] bool is_valid_host_var(const std::string &host_var, size_t pos) const;
|
||||
@@ -57,6 +65,8 @@ protected:
|
||||
friend class statement_proxy;
|
||||
|
||||
query_context query_;
|
||||
size_t start_bind_pos_{0};
|
||||
size_t current_bind_pos_{0};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -23,10 +23,17 @@ public:
|
||||
void bind(size_t pos, Type &value, parameter_binder& bindings) {
|
||||
statement_->bind(pos, value, bindings);
|
||||
}
|
||||
template<typename Type>
|
||||
void bind(Type &value, parameter_binder& bindings) {
|
||||
statement_->bind(value, bindings);
|
||||
}
|
||||
void bind(size_t pos, const char *value, size_t size, parameter_binder& bindings) const;
|
||||
void bind(const char *value, size_t size, parameter_binder& bindings) const;
|
||||
void bind(size_t pos, std::string &val, size_t size, parameter_binder& bindings) const;
|
||||
void bind(std::string &val, size_t size, parameter_binder& bindings) const;
|
||||
|
||||
void reset() const;
|
||||
[[nodiscard]] size_t bind_pos() const;
|
||||
|
||||
[[nodiscard]] std::string sql() const;
|
||||
|
||||
|
||||
@@ -1,68 +1,12 @@
|
||||
#ifndef QUERY_OBJECT_PARAMETER_BINDER_HPP
|
||||
#define QUERY_OBJECT_PARAMETER_BINDER_HPP
|
||||
|
||||
#include "matador/utils/attribute_writer.hpp"
|
||||
#include "matador/utils/default_type_traits.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
#include "matador/sql/object_pk_binder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
namespace detail {
|
||||
|
||||
class fk_binder
|
||||
{
|
||||
public:
|
||||
template<class Type>
|
||||
void bind(Type &obj, const size_t column_index, utils::attribute_writer &binder)
|
||||
{
|
||||
binder_ = &binder;
|
||||
index_ = column_index;
|
||||
access::process(*this, obj);
|
||||
binder_ = nullptr;
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
|
||||
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
||||
|
||||
template < class Type >
|
||||
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
||||
template < class Pointer >
|
||||
static void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
|
||||
template < class Pointer >
|
||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
|
||||
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
const char * /*join_column*/,
|
||||
const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many_to_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
const char * /*join_column*/,
|
||||
const char * /*inverse_join_column*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many_to_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
utils::attribute_writer *binder_{};
|
||||
size_t index_{0};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
class object_parameter_binder {
|
||||
public:
|
||||
explicit object_parameter_binder(bool include_primary_key = true);
|
||||
|
||||
template<class Type>
|
||||
void bind(Type &obj, utils::attribute_writer &binder) {
|
||||
binder_ = &binder;
|
||||
@@ -75,9 +19,7 @@ public:
|
||||
|
||||
template < class Type >
|
||||
void on_primary_key(const char * /*id*/, Type &val, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
||||
if (include_primary_key_) {
|
||||
utils::data_type_traits<Type>::bind_value(*binder_, index_++, val, attr.size());
|
||||
}
|
||||
utils::data_type_traits<Type>::bind_value(*binder_, index_++, val, attr.size());
|
||||
}
|
||||
void on_revision(const char *id, uint64_t &/*rev*/);
|
||||
|
||||
@@ -88,11 +30,11 @@ public:
|
||||
|
||||
template<class Type, template < class ... > class Pointer>
|
||||
void on_belongs_to(const char * /*id*/, Pointer<Type> &x, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {
|
||||
fk_binder_.bind(*x, index_++, *binder_);
|
||||
pk_binder_.bind(*x, index_++, *binder_);
|
||||
}
|
||||
template<class Type, template < class ... > class Pointer>
|
||||
void on_has_one(const char * /*id*/, Pointer<Type> &x, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {
|
||||
fk_binder_.bind(*x, index_++, *binder_);
|
||||
pk_binder_.bind(*x, index_++, *binder_);
|
||||
}
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/,
|
||||
@@ -113,17 +55,7 @@ public:
|
||||
private:
|
||||
utils::attribute_writer *binder_{};
|
||||
size_t index_{0};
|
||||
detail::fk_binder fk_binder_;
|
||||
bool include_primary_key_{true};
|
||||
object_pk_binder pk_binder_;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename ValueType>
|
||||
void fk_binder::on_primary_key(const char * /*id*/, ValueType &value, const utils::primary_key_attribute& attr) {
|
||||
utils::data_type_traits<ValueType>::bind_value(*binder_, index_++, value, attr.size());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif //QUERY_OBJECT_PARAMETER_BINDER_HPP
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef MATADOR_OBJECT_PK_BINDER_HPP
|
||||
#define MATADOR_OBJECT_PK_BINDER_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/attribute_writer.hpp"
|
||||
#include "matador/utils/default_type_traits.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class object_pk_binder {
|
||||
public:
|
||||
template<class Type>
|
||||
void bind(Type &obj, const size_t column_index, utils::attribute_writer &binder) {
|
||||
binder_ = &binder;
|
||||
index_ = column_index;
|
||||
access::process(*this, obj);
|
||||
binder_ = nullptr;
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
|
||||
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
||||
|
||||
template < class Type >
|
||||
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
||||
template < class Pointer >
|
||||
static void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
|
||||
template < class Pointer >
|
||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
|
||||
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
const char * /*join_column*/,
|
||||
const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many_to_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
const char * /*join_column*/,
|
||||
const char * /*inverse_join_column*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many_to_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
utils::attribute_writer *binder_{};
|
||||
size_t index_{0};
|
||||
};
|
||||
|
||||
template<typename ValueType>
|
||||
void object_pk_binder::on_primary_key(const char * /*id*/, ValueType &value, const utils::primary_key_attribute& attr) {
|
||||
utils::data_type_traits<ValueType>::bind_value(*binder_, index_++, value, attr.size());
|
||||
}
|
||||
|
||||
}
|
||||
#endif //MATADOR_OBJECT_PK_BINDER_HPP
|
||||
@@ -121,6 +121,21 @@ public:
|
||||
*/
|
||||
void reset() const;
|
||||
|
||||
/**
|
||||
* Returns the current binding position.
|
||||
* Is set to the initial position after
|
||||
* a call to reset().
|
||||
*
|
||||
* @return The current binding position
|
||||
*/
|
||||
[[nodiscard]] size_t bind_pos() const;
|
||||
|
||||
/**
|
||||
* Returns the statements underlying SQL
|
||||
* query string.
|
||||
*
|
||||
* @return The underlying SQL string
|
||||
*/
|
||||
[[nodiscard]] std::string sql() const;
|
||||
|
||||
[[nodiscard]] utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch_internal() const;
|
||||
|
||||
Reference in New Issue
Block a user