collection has-many-to-many resolve progress

This commit is contained in:
Sascha Kühl
2026-02-03 15:36:58 +01:00
parent 73abd61eba
commit 4d886f0343
7 changed files with 135 additions and 45 deletions
@@ -18,6 +18,7 @@
#include "matador/object/object_proxy.hpp"
#include "matador/object/object_ptr.hpp"
#include "matador/object/collection_proxy.hpp"
#include "matador/object/join_columns_collector.hpp"
#include <memory>
#include <stack>
@@ -112,26 +113,15 @@ public:
on_foreign_key(x, attr);
}
template<class ContainerType>
void on_has_many_to_many(const char *, ContainerType &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &attr) {
if (attr.fetch() == utils::fetch_type::Lazy) {
} else {}
}
template<class ContainerType>
void on_has_many_to_many(const char *, ContainerType &, const utils::foreign_attributes &attr) {
if (attr.fetch() == utils::fetch_type::Lazy) {
} else {}
}
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);
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) {
}
template<class CollectionType>
void on_has_many_to_many(const char *id, CollectionType &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &attr);
template<class CollectionType>
void on_has_many_to_many(const char *, CollectionType &, const utils::foreign_attributes &attr);
template<class Type>
void bind(const Type &obj) {
@@ -186,7 +176,7 @@ protected:
std::unordered_set<object::collection_composite_key, object::collection_composite_key_hash> initialized_collections_;
};
template<class CollectionType>
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>();
@@ -212,6 +202,59 @@ void query_result_impl::on_has_many(const char *, CollectionType &cont, const ch
}
}
template <class CollectionType>
void query_result_impl::on_has_many_to_many(const char *id, CollectionType &cont, const char *join_column, const char *, const utils::foreign_attributes &attr) {
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{id}}).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 CollectionType>
void query_result_impl::on_has_many_to_many(const char *id, CollectionType &cont, const utils::foreign_attributes &attr) {
using value_type = typename CollectionType::value_type::value_type;
object::join_columns_collector collector;
const auto jc = collector.collect<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_, jc.inverse_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{id}}).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;
+2 -4
View File
@@ -187,10 +187,7 @@ utils::result<object::object_ptr<Type>, utils::error> statement::fetch_one() {
});
auto first = records.begin();
if (first == records.end()) {
return utils::failure(utils::error{
error_code::FETCH_FAILED,
"Failed to find entity."
});
return utils::failure(utils::error{error_code::FETCH_FAILED,"Failed to find entity."});
}
return utils::ok(first.optr());
@@ -198,6 +195,7 @@ utils::result<object::object_ptr<Type>, utils::error> statement::fetch_one() {
template<class Type>
utils::result<std::shared_ptr<Type>, utils::error> statement::fetch_one_raw() {
std::cout << statement_proxy_->sql() << std::endl;
auto result = statement_proxy_->fetch(*bindings_);
if (!result.is_ok()) {
return utils::failure(result.err());