query/include/matador/sql/interface/statement_impl.hpp

63 lines
1.9 KiB
C++

#ifndef QUERY_STATEMENT_IMPL_HPP
#define QUERY_STATEMENT_IMPL_HPP
#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"
#include <memory>
namespace matador::sql {
class sql_error;
class statement_impl
{
protected:
explicit statement_impl(query_context query);
public:
virtual ~statement_impl() = default;
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, interface::parameter_binder& bindings) {
object_parameter_binder object_binder_;
object_binder_.reset(start_index());
object_binder_.bind(obj, bindings);
}
template < class Type >
void bind(const size_t pos, Type &val, 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, 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;
[[nodiscard]] const std::vector<std::string>& bind_vars() const;
[[nodiscard]] bool is_valid_host_var(const std::string &host_var, size_t pos) const;
protected:
[[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_;
};
}
#endif //QUERY_STATEMENT_IMPL_HPP