collection resolver progress

This commit is contained in:
2026-02-01 13:17:40 +01:00
parent c838e9dd7b
commit f9333158e2
21 changed files with 247 additions and 192 deletions
@@ -23,6 +23,7 @@
#include <stack>
#include <string>
#include <typeindex>
#include <unordered_set>
namespace matador::utils {
class value;
@@ -63,9 +64,9 @@ public:
template<class ContainerType>
static void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, 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:
size_t column_index_{};
@@ -126,32 +127,10 @@ public:
}
template<class CollectionType>
void on_has_many(const char * /*id*/, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
using value_type = typename CollectionType::value_type::value_type;
auto object_resolver = resolver_->object_resolver<value_type>();
auto resolver = resolver_->collection_resolver<typename CollectionType::value_type>(result_type_, join_column);
std::vector<typename CollectionType::value_type> objects;
if (attr.fetch() == utils::fetch_type::Lazy) {
cont.reset(std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, current_pk_));
} else {
const auto ti = std::type_index(typeid(value_type));
auto obj = std::make_shared<typename CollectionType::value_type::value_type>();
type_stack_.push(ti);
access::process(*this, *obj);
type_stack_.pop();
auto ptr = typename CollectionType::value_type(std::make_shared<object::object_proxy<value_type>>(object_resolver, obj));
const auto pk = ptr.primary_key();
if (ptr.primary_key().is_valid()) {
cont.push_back(ptr);
}
}
// auto cp = std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, objects);
// cont.reset();
}
void on_has_many(const char * /*id*/, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr);
template<class CollectionType>
void on_has_many(const char * /*id*/, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<!object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
void on_has_many(const char * /*id*/, CollectionType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/, std::enable_if_t<!object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
}
template<class Type>
@@ -164,38 +143,8 @@ public:
}
template<class Type>
bool fetch(Type &obj) {
bool first = true;
do {
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
return !first;
}
last_pk_ = current_pk_;
current_pk_ = discover_current_primary_key(obj);
if (pk_has_changed()) {
reader_->unshift();
last_pk_.clear();
current_pk_.clear();
break;
}
first = false;
type_stack_.emplace(typeid(Type));
column_index_ = reader_->start_column_index();
access::process(*this, obj);
type_stack_.pop();
} while (last_pk_ == current_pk_);
return true;
}
bool fetch(record &rec) {
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
return false;
}
column_index_ = reader_->start_column_index();
access::process(*this, rec);
return true;
}
bool fetch(Type &obj);
bool fetch(record &rec);
[[nodiscard]] const std::vector<object::attribute> &prototype() const;
@@ -234,8 +183,59 @@ protected:
std::stack<std::type_index> type_stack_;
utils::identifier current_pk_{};
utils::identifier last_pk_{};
std::unordered_set<object::collection_composite_key, object::collection_composite_key_hash> initialized_collections_;
};
template<class CollectionType>
void query_result_impl::on_has_many(const char *, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> *) {
using value_type = typename CollectionType::value_type::value_type;
auto object_resolver = resolver_->object_resolver<value_type>();
auto resolver = resolver_->collection_resolver<typename CollectionType::value_type>(result_type_, join_column);
if (attr.fetch() == utils::fetch_type::Lazy) {
cont.reset(std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, current_pk_));
} else {
if (initialized_collections_.insert({result_type_, typeid(typename CollectionType::value_type), std::string{join_column}}).second) {
cont.reset(std::make_shared<object::collection_proxy<typename CollectionType::value_type>>(resolver, std::vector<typename CollectionType::value_type>()));
}
const auto ti = std::type_index(typeid(value_type));
type_stack_.push(ti);
auto obj = std::make_shared<typename CollectionType::value_type::value_type>();
access::process(*this, *obj);
type_stack_.pop();
auto ptr = typename CollectionType::value_type(std::make_shared<object::object_proxy<value_type>>(object_resolver, obj));
const auto pk = ptr.primary_key();
if (ptr.primary_key().is_valid()) {
cont.push_back(ptr);
}
}
}
template<class Type>
bool query_result_impl::fetch(Type &obj) {
bool first = true;
do {
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
return !first;
}
last_pk_ = current_pk_;
current_pk_ = discover_current_primary_key(obj);
if (pk_has_changed()) {
reader_->unshift();
last_pk_.clear();
current_pk_.clear();
break;
}
first = false;
type_stack_.emplace(typeid(Type));
column_index_ = reader_->start_column_index();
access::process(*this, obj);
type_stack_.pop();
} while (last_pk_ == current_pk_);
return true;
}
namespace detail {
template<typename ValueType>
void pk_reader::on_primary_key(const char *id, ValueType &value, const utils::primary_key_attribute& attr) {
@@ -6,6 +6,7 @@
#include "matador/object/abstract_collection_resolver.hpp"
#include "matador/object/object_resolver_factory.hpp"
#include "matador/object/collection_resolver_factory.hpp"
#include "matador/object/collection_utils.hpp"
namespace matador::sql {
class executor;
@@ -19,33 +20,6 @@ private:
std::unordered_map<std::type_index, std::shared_ptr<object::abstract_type_resolver>> resolvers_;
};
struct composite_key {
std::type_index root_type;
std::type_index type;
std::string name;
bool operator==(const composite_key& other) const {
return root_type == other.root_type &&
type == other.type &&
name == other.name;
}
};
struct composite_key_hash {
std::size_t operator()(const composite_key& k) const noexcept {
const std::size_t h1 = std::hash<std::type_index>{}(k.root_type);
const std::size_t h2 = std::hash<std::type_index>{}(k.type);
const std::size_t h3 = std::hash<std::string>{}(k.name);
// Klassische Hash-Kombination (Boost-Style)
std::size_t seed = h1;
seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= h3 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
class producer_collection_resolver_factory : public object::collection_resolver_factory {
public:
[[nodiscard]] std::shared_ptr<object::abstract_collection_resolver> acquire_collection_resolver(const std::type_index& root_type,
@@ -54,7 +28,7 @@ public:
void register_collection_resolver(std::shared_ptr<object::abstract_collection_resolver>&& resolver) override;
private:
std::unordered_map<composite_key, std::shared_ptr<object::abstract_collection_resolver>, composite_key_hash> resolvers_;
std::unordered_map<object::collection_composite_key, std::shared_ptr<object::abstract_collection_resolver>, object::collection_composite_key_hash> resolvers_;
};
}
#endif //MATADOR_RESOLVER_FACTORY_HPP