implemented lazy loading for object_ptr and belongs_to

This commit is contained in:
2026-01-15 12:40:54 +01:00
parent db8e137c11
commit 7626331866
81 changed files with 1790 additions and 927 deletions
@@ -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_{};