implemented lazy loading for object_ptr and belongs_to
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#ifndef MATADOR_IDENTIFIER_READER_HPP
|
||||
#define MATADOR_IDENTIFIER_READER_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/identifier.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class query_result_reader;
|
||||
}
|
||||
|
||||
namespace matador::sql::internal {
|
||||
class identifier_reader final {
|
||||
public:
|
||||
explicit identifier_reader(query_result_reader &reader);
|
||||
|
||||
template<class Type>
|
||||
utils::identifier read(const Type &obj, const size_t column_index) {
|
||||
identifier_.clear();
|
||||
column_index_ = column_index;
|
||||
access::process(*this, obj);
|
||||
return identifier_;
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
||||
utils::data_type_traits<ValueType>::read_value(reader_, id, column_index_, value, attr.size());
|
||||
identifier_ = value;
|
||||
}
|
||||
|
||||
static void on_revision(const char * /*id*/, uint64_t &/*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*/) {}
|
||||
template < class Pointer >
|
||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||
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:
|
||||
query_result_reader &reader_;
|
||||
size_t column_index_{};
|
||||
utils::identifier identifier_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_IDENTIFIER_READER_HPP
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef MATADOR_IDENTIFIER_STATEMENT_BINDER_HPP
|
||||
#define MATADOR_IDENTIFIER_STATEMENT_BINDER_HPP
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class statement;
|
||||
|
||||
class identifier_statement_binder : public utils::identifier_serializer {
|
||||
public:
|
||||
explicit identifier_statement_binder(statement &stmt, size_t index = 0);
|
||||
|
||||
void bind(const utils::identifier &id);
|
||||
|
||||
void serialize(int8_t &, const utils::field_attributes &) override;
|
||||
void serialize(int16_t &, const utils::field_attributes &) override;
|
||||
void serialize(int32_t &, const utils::field_attributes &) override;
|
||||
void serialize(int64_t &, const utils::field_attributes &) override;
|
||||
void serialize(uint8_t &, const utils::field_attributes &) override;
|
||||
void serialize(uint16_t &, const utils::field_attributes &) override;
|
||||
void serialize(uint32_t &, const utils::field_attributes &) override;
|
||||
void serialize(uint64_t &, const utils::field_attributes &) override;
|
||||
void serialize(const char *, const utils::field_attributes &) override;
|
||||
void serialize(std::string &, const utils::field_attributes &) override;
|
||||
void serialize(utils::null_type_t &, const utils::field_attributes &) override;
|
||||
|
||||
private:
|
||||
statement &stmt_;
|
||||
size_t index_{};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //MATADOR_IDENTIFIER_STATEMENT_BINDER_HPP
|
||||
@@ -10,16 +10,17 @@
|
||||
|
||||
#include "matador/sql/interface/query_result_reader.hpp"
|
||||
#include "matador/sql/internal/query_result_pk_resolver.hpp"
|
||||
#include "matador/sql/internal/identifier_reader.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/object_proxy.hpp"
|
||||
#include "matador/object/object_resolver_factory.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace matador::utils {
|
||||
class value;
|
||||
@@ -73,9 +74,9 @@ private:
|
||||
class query_result_impl {
|
||||
public:
|
||||
query_result_impl(std::unique_ptr<query_result_reader> &&reader,
|
||||
std::vector<object::attribute> &&prototype, size_t column_index = 0);
|
||||
query_result_impl(std::unique_ptr<query_result_reader> &&reader,
|
||||
const std::vector<object::attribute> &prototype, size_t column_index = 0);
|
||||
std::vector<object::attribute> prototype,
|
||||
const std::shared_ptr<object::object_resolver_factory>& resolver_factory,
|
||||
size_t column_index = 0);
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
||||
@@ -122,17 +123,19 @@ public:
|
||||
}
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &cont, const char * /*join_column*/,
|
||||
const utils::foreign_attributes &attr) {
|
||||
void on_has_many(const char * /*id*/, ContainerType &cont, const char * /*join_column*/, const utils::foreign_attributes &attr) {
|
||||
using value_type = typename ContainerType::value_type::value_type;
|
||||
auto resolver = resolver_factory_->resolver<value_type>();
|
||||
|
||||
if (attr.fetch() == utils::fetch_type::Lazy) {
|
||||
// pk_reader_.read(*id, column_index_++);
|
||||
} else {
|
||||
const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type));
|
||||
auto obj = std::make_unique<typename ContainerType::value_type::value_type>();
|
||||
const auto ti = std::type_index(typeid(value_type));
|
||||
auto obj = std::make_shared<typename ContainerType::value_type::value_type>();
|
||||
type_stack_.push(ti);
|
||||
access::process(*this, *obj);
|
||||
type_stack_.pop();
|
||||
auto ptr = typename ContainerType::value_type(obj.release());
|
||||
auto ptr = typename ContainerType::value_type(std::make_shared<object::object_proxy<value_type>>(resolver, obj));
|
||||
const auto pk = ptr.primary_key();
|
||||
if (ptr.primary_key().is_valid()) {
|
||||
cont.push_back(ptr);
|
||||
@@ -194,16 +197,18 @@ private:
|
||||
|
||||
template<class Pointer>
|
||||
void on_foreign_key(Pointer &x, const utils::foreign_attributes &attr) {
|
||||
if (x.empty()) {
|
||||
x.reset(new typename Pointer::value_type);
|
||||
}
|
||||
const auto resolver = resolver_factory_->resolver<typename Pointer::value_type>();
|
||||
if (attr.fetch() == utils::fetch_type::Lazy) {
|
||||
pk_reader_.read(*x, column_index_++);
|
||||
typename Pointer::value_type obj;
|
||||
auto pk = id_reader_.read(obj, column_index_++);
|
||||
x = Pointer(std::make_shared<object::object_proxy<typename Pointer::value_type>>(resolver, pk));
|
||||
} else {
|
||||
auto obj = std::make_shared<typename Pointer::value_type>();
|
||||
const auto ti = std::type_index(typeid(*x));
|
||||
type_stack_.push(ti);
|
||||
access::process(*this, *x);
|
||||
access::process(*this, *obj);
|
||||
type_stack_.pop();
|
||||
x = Pointer(std::make_shared<object::object_proxy<typename Pointer::value_type>>(resolver, obj));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,6 +216,8 @@ protected:
|
||||
size_t column_index_ = 0;
|
||||
std::vector<object::attribute> prototype_;
|
||||
std::unique_ptr<query_result_reader> reader_;
|
||||
std::shared_ptr<object::object_resolver_factory> resolver_factory_;
|
||||
internal::identifier_reader id_reader_;
|
||||
detail::pk_reader pk_reader_;
|
||||
std::stack<std::type_index> type_stack_;
|
||||
utils::identifier current_pk_{};
|
||||
|
||||
@@ -50,9 +50,9 @@ public:
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||
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*/) {}
|
||||
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*/) {}
|
||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*c*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
@@ -60,8 +60,8 @@ private:
|
||||
if (fetch == utils::fetch_type::Lazy) {
|
||||
++column_index_;
|
||||
} else {
|
||||
const Type obj;
|
||||
type_stack_.push(typeid(Type));
|
||||
const Type obj{};
|
||||
type_stack_.emplace(typeid(Type));
|
||||
access::process(*this, obj);
|
||||
type_stack_.pop();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef MATADOR_RESOLVER_PRODUCER_HPP
|
||||
#define MATADOR_RESOLVER_PRODUCER_HPP
|
||||
|
||||
#include "matador/object/object_resolver.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
class resolver_producer {
|
||||
public:
|
||||
virtual ~resolver_producer() = default;
|
||||
virtual std::shared_ptr<object::abstract_object_resolver> produce(executor &exec) = 0;
|
||||
|
||||
[[nodiscard]] const std::type_index& type() const;
|
||||
|
||||
protected:
|
||||
explicit resolver_producer(const std::type_index &type);
|
||||
|
||||
private:
|
||||
std::type_index type_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_RESOLVER_PRODUCER_HPP
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef MATADOR_STATEMENT_OBJECT_RESOLVER_HPP
|
||||
#define MATADOR_STATEMENT_OBJECT_RESOLVER_HPP
|
||||
|
||||
#include "matador/object/object_resolver.hpp"
|
||||
|
||||
#include "matador/sql/statement.hpp"
|
||||
#include "matador/sql/internal/identifier_statement_binder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
template<typename Type>
|
||||
class statement_object_resolver : public object::object_resolver<Type> {
|
||||
public:
|
||||
explicit statement_object_resolver(statement stmt)
|
||||
: statement_(std::move(stmt)) {}
|
||||
|
||||
std::shared_ptr<Type> resolve(const utils::identifier &id) override;
|
||||
protected:
|
||||
statement statement_;
|
||||
std::type_index index{typeid(Type)};
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
std::shared_ptr<Type> statement_object_resolver<Type>::resolve(const utils::identifier &id) {
|
||||
identifier_statement_binder binder(statement_);
|
||||
binder.bind(id);
|
||||
|
||||
auto result = statement_.template fetch_one<Type>();
|
||||
if (!result) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_shared<Type>(*result);
|
||||
}
|
||||
|
||||
}
|
||||
#endif //MATADOR_STATEMENT_OBJECT_RESOLVER_HPP
|
||||
Reference in New Issue
Block a user