renamed foreign class to entity, introduced interface class query_result_reader

This commit is contained in:
2023-11-24 17:40:57 +01:00
parent 700cbc0652
commit c2a96f4cbd
18 changed files with 285 additions and 207 deletions
+47
View File
@@ -0,0 +1,47 @@
#ifndef QUERY_ENTITY_HPP
#define QUERY_ENTITY_HPP
#include <memory>
namespace matador::sql {
template < class Type >
class entity
{
public:
using value_type = Type;
using pointer = value_type*;
using reference = value_type&;
entity() = default;
explicit entity(Type *obj)
: obj_(obj) {}
entity(const entity&) = default;
entity& operator=(const entity&) = default;
entity(entity&&) noexcept = default;
entity& operator=(entity&&) noexcept = default;
~entity() = default;
void reset(Type *obj) { obj_.reset(obj); }
pointer operator->() { return obj_.get(); }
const value_type* operator->() const { return obj_.get(); }
reference operator*() { return *obj_; }
const value_type& operator*() const { return *obj_; }
pointer get() { return obj_.get(); }
const value_type* get() const { return obj_.get(); }
private:
std::shared_ptr<value_type> obj_;
};
template<class Type, typename... Args>
[[maybe_unused]] entity<Type> make_entity(Args&&... args)
{
return entity(new Type(std::forward<Args>(args)...));
}
}
#endif //QUERY_ENTITY_HPP
-42
View File
@@ -1,42 +0,0 @@
#ifndef QUERY_FOREIGN_HPP
#define QUERY_FOREIGN_HPP
#include <memory>
namespace matador::sql {
template < class Type >
class foreign
{
public:
using value_type = Type;
using pointer = value_type*;
using reference = value_type&;
foreign() = default;
explicit foreign(Type *obj)
: obj_(obj) {}
foreign(const foreign&) = default;
foreign& operator=(const foreign&) = default;
foreign(foreign&&) noexcept = default;
foreign& operator=(foreign&&) noexcept = default;
~foreign() = default;
pointer operator->() { return obj_.get(); }
const value_type* operator->() const { return obj_.get(); }
reference operator*() { return *obj_; }
const value_type& operator*() const { return *obj_; }
private:
std::shared_ptr<value_type> obj_;
};
template<class Type, typename... Args>
[[maybe_unused]] foreign<Type> make_foreign(Args&&... args)
{
return foreign(new Type(std::forward<Args>(args)...));
}
}
#endif //QUERY_FOREIGN_HPP
+70 -35
View File
@@ -5,44 +5,61 @@
#include "matador/utils/field_attributes.hpp"
#include "matador/sql/any_type.hpp"
#include "matador/sql/query_result_reader.hpp"
#include "matador/sql/record.hpp"
#include "matador/sql/types.hpp"
#include <memory>
#include <string>
namespace matador::sql {
namespace detail {
class pk_reader
{
public:
explicit pk_reader(query_result_reader &reader);
template<class Type>
void read(Type &obj, size_t column_index)
{
column_index_ = column_index;
utils::access::process(*this, obj);
}
template<typename ValueType>
void on_primary_key(const char *id, ValueType &value, typename std::enable_if<std::is_integral<ValueType>::value && !std::is_same<bool, ValueType>::value>::type* = 0);
void on_primary_key(const char *id, std::string &value, size_t size);
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
template < class Type >
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template < class Pointer >
void on_belongs_to(const char *id, Pointer &x, utils::cascade_type) {}
template < class Pointer >
void on_has_one(const char *id, Pointer &x, utils::cascade_type) {}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, utils::cascade_type) {}
private:
size_t column_index_{};
query_result_reader &reader_;
};
}
class query_result_impl
{
public:
virtual ~query_result_impl() = default;
virtual size_t column_count() const = 0;
virtual void read_value(const char *id, size_t index, char &value) = 0;
virtual void read_value(const char *id, size_t index, short &value) = 0;
virtual void read_value(const char *id, size_t index, int &value) = 0;
virtual void read_value(const char *id, size_t index, long &value) = 0;
virtual void read_value(const char *id, size_t index, long long &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned char &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned short &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned int &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned long &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned long long &value) = 0;
virtual void read_value(const char *id, size_t index, bool &value) = 0;
virtual void read_value(const char *id, size_t index, float &value) = 0;
virtual void read_value(const char *id, size_t index, double &value) = 0;
// virtual void read_value(const char *id, size_t index, matador::time &value) = 0;
// virtual void read_value(const char *id, size_t index, matador::date &value) = 0;
virtual void read_value(const char *id, size_t index, char *value, size_t s) = 0;
virtual void read_value(const char *id, size_t index, std::string &value) = 0;
virtual void read_value(const char *id, size_t index, std::string &value, size_t s) = 0;
virtual void read_value(const char *id, size_t index, any_type &value, data_type_t type, size_t size) = 0;
query_result_impl(std::unique_ptr<query_result_reader> &&reader, record prototype);
template<typename ValueType>
void on_primary_key(const char *id, ValueType &value, typename std::enable_if<std::is_integral<ValueType>::value && !std::is_same<bool, ValueType>::value>::type* = 0)
{
read_value(id, column_index_++, value);
reader_->read_value(id, column_index_++, value);
}
void on_primary_key(const char *id, std::string &value, size_t size);
void on_revision(const char *id, unsigned long long &rev);
@@ -50,16 +67,28 @@ public:
template < class Type >
void on_attribute(const char *id, Type &x, const utils::field_attributes &/*attr*/ = utils::null_attributes)
{
read_value(id, column_index_++, x);
reader_->read_value(id, column_index_++, x);
}
void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes);
void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes);
void on_attribute(const char *id, any_type &value, data_type_t type, const utils::field_attributes &attr = utils::null_attributes);
template < class Pointer >
void on_belongs_to(const char *id, Pointer &x, utils::cascade_type) {}
void on_belongs_to(const char * /*id*/, Pointer &x, utils::cascade_type)
{
if (!x.get()) {
x.reset(new typename Pointer::value_type);
}
pk_reader_.read(*x, column_index_++);
}
template < class Pointer >
void on_has_one(const char *id, Pointer &x, utils::cascade_type) {}
void on_has_one(const char * /*id*/, Pointer &x, utils::cascade_type)
{
if (!x.get()) {
x.reset(new typename Pointer::value_type);
}
pk_reader_.read(*x, column_index_++);
}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, const char *, const char *, utils::cascade_type) {}
@@ -72,26 +101,32 @@ public:
template<class Type>
bool fetch(Type &obj)
{
if (!fetch()) {
column_index_ = 0;
if (!reader_->fetch()) {
return false;
}
matador::utils::access::process(*this, obj);
return true;
}
[[nodiscard]] virtual const char* column(size_t index) const = 0;
[[nodiscard]] virtual bool fetch() = 0;
const record& prototype() const;
protected:
explicit query_result_impl(record prototype);
[[nodiscard]] const record& prototype() const;
protected:
size_t column_index_ = 0;
record prototype_;
std::unique_ptr<query_result_reader> reader_;
detail::pk_reader pk_reader_;
};
namespace detail {
template<typename ValueType>
void detail::pk_reader::on_primary_key(const char *id, ValueType &value, typename std::enable_if<std::is_integral<ValueType>::value && !std::is_same<bool, ValueType>::value>::type *)
{
reader_.read_value(id, column_index_++, value);
}
}
}
#endif //QUERY_QUERY_RESULT_IMPL_HPP
@@ -0,0 +1,40 @@
#ifndef QUERY_QUERY_RESULT_READER_HPP
#define QUERY_QUERY_RESULT_READER_HPP
#include "matador/sql/any_type.hpp"
#include "matador/sql/types.hpp"
namespace matador::sql {
class query_result_reader
{
public:
virtual ~query_result_reader() = default;
[[nodiscard]] virtual size_t column_count() const = 0;
[[nodiscard]] virtual const char* column(size_t index) const = 0;
[[nodiscard]] virtual bool fetch() = 0;
virtual void read_value(const char *id, size_t index, char &value) = 0;
virtual void read_value(const char *id, size_t index, short &value) = 0;
virtual void read_value(const char *id, size_t index, int &value) = 0;
virtual void read_value(const char *id, size_t index, long &value) = 0;
virtual void read_value(const char *id, size_t index, long long &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned char &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned short &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned int &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned long &value) = 0;
virtual void read_value(const char *id, size_t index, unsigned long long &value) = 0;
virtual void read_value(const char *id, size_t index, bool &value) = 0;
virtual void read_value(const char *id, size_t index, float &value) = 0;
virtual void read_value(const char *id, size_t index, double &value) = 0;
// virtual void read_value(const char *id, size_t index, matador::time &value) = 0;
// virtual void read_value(const char *id, size_t index, matador::date &value) = 0;
virtual void read_value(const char *id, size_t index, char *value, size_t s) = 0;
virtual void read_value(const char *id, size_t index, std::string &value) = 0;
virtual void read_value(const char *id, size_t index, std::string &value, size_t s) = 0;
virtual void read_value(const char *id, size_t index, any_type &value, data_type_t type, size_t size) = 0;
};
}
#endif //QUERY_QUERY_RESULT_READER_HPP