collection resolver producer progress

This commit is contained in:
Sascha Kühl 2026-01-25 22:00:11 +01:00
parent 64f2d6899a
commit 046c1d8ed2
4 changed files with 110 additions and 8 deletions

View File

@ -12,6 +12,14 @@ public:
using iterator = typename std::vector<value_type>::iterator; using iterator = typename std::vector<value_type>::iterator;
using const_iterator = typename std::vector<value_type>::const_iterator; using const_iterator = typename std::vector<value_type>::const_iterator;
collection()
: proxy_(std::make_shared<collection_proxy<Type>>()) {}
explicit collection(std::shared_ptr<collection_proxy<Type>> proxy)
: proxy_(std::move(proxy)) {}
explicit collection(std::vector<Type> items)
: proxy_(std::make_shared<collection_proxy<Type>>(std::move(items))) {}
collection(const collection& other) = default;
void push_back(const value_type& value) { void push_back(const value_type& value) {
proxy_->items().push_back(value); proxy_->items().push_back(value);
} }

View File

@ -36,12 +36,17 @@ public:
template<typename Type> template<typename Type>
class collection_proxy final { class collection_proxy final {
public: public:
collection_proxy() = default;
// Lazy
collection_proxy(std::weak_ptr<object_resolver<typename Type::value_type>> resolver, utils::identifier owner_id) collection_proxy(std::weak_ptr<object_resolver<typename Type::value_type>> resolver, utils::identifier owner_id)
: owner_id_(std::move(owner_id)), resolver_(std::move(resolver)) {} : owner_id_(std::move(owner_id)), resolver_(std::move(resolver)) {}
// Eager
collection_proxy(std::weak_ptr<object_resolver<typename Type::value_type>> resolver, std::vector<Type> items) collection_proxy(std::weak_ptr<object_resolver<typename Type::value_type>> resolver, std::vector<Type> items)
: items_(std::move(items)), resolver_(std::move(resolver)) {} : items_(std::move(items)), resolver_(std::move(resolver)) {}
// Transient
explicit collection_proxy(std::vector<Type> items) explicit collection_proxy(std::vector<Type> items)
: items_(std::move(items)) {} : items_(std::move(items)) {}

View File

@ -3,6 +3,7 @@
#include "matador/object/abstract_type_resolver_factory.hpp" #include "matador/object/abstract_type_resolver_factory.hpp"
#include "matador/object/object_resolver.hpp" #include "matador/object/object_resolver.hpp"
#include "matador/object/collection_resolver_factory.hpp"
namespace matador::object { namespace matador::object {
class object_resolver_factory : public abstract_type_resolver_factory { class object_resolver_factory : public abstract_type_resolver_factory {
@ -16,6 +17,16 @@ public:
return std::dynamic_pointer_cast<object_resolver<Type>>(res); return std::dynamic_pointer_cast<object_resolver<Type>>(res);
} }
//
// template<class Type>
// std::shared_ptr<collection_resolver<Type>> collection_resolver(const std::string &name) {
// const auto res = acquire_collection_resolver(std::type_index(typeid(Type)), name);
// if (!res) {
// return std::dynamic_pointer_cast<collection_resolver<Type>>(res);
// }
//
// return std::dynamic_pointer_cast<collection_resolver<Type>>(res);
// }
}; };
} }
#endif //MATADOR_OBJECT_RESOLVER_FACTORY_HPP #endif //MATADOR_OBJECT_RESOLVER_FACTORY_HPP

View File

@ -497,6 +497,8 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
.and_then([this] {return repo.create(db); }); .and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
repo.initialize_executor(db);
const std::vector shipments { const std::vector shipments {
object_ptr{std::make_shared<shipment>(1, "4711")}, object_ptr{std::make_shared<shipment>(1, "4711")},
object_ptr{std::make_shared<shipment>(2, "0815")} object_ptr{std::make_shared<shipment>(2, "0815")}
@ -575,14 +577,90 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(shipment_result.is_ok()); REQUIRE(shipment_result.is_ok());
// size_t index{0}; size_t index{0};
// std::vector<size_t> packages_sizes{2, 3}; std::vector<size_t> packages_sizes{2, 3};
// std::cout << "\n"; std::cout << "\n";
// for (const auto &s : *shipment_result) { for (const auto &s : *shipment_result) {
// REQUIRE(s->id == shipments.at(index)->id); REQUIRE(s->id == shipments.at(index)->id);
// REQUIRE(s->tracking_number == shipments.at(index)->tracking_number); REQUIRE(s->tracking_number == shipments.at(index)->tracking_number);
// REQUIRE(s->packages.size() == packages_sizes.at(index++)); REQUIRE(s->packages.size() == packages_sizes.at(index++));
// } }
}
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "[query][has_many][lazy]") {
auto result = repo.attach<author>("authors")
.and_then( [this] { return repo.attach<book>("books"); } )
.and_then([this] {
return repo.create(db);
} );
repo.initialize_executor(db);
const std::vector authors {
object_ptr{std::make_shared<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true)},
object_ptr{std::make_shared<author>( 2, "Steven", "King", "21.9.1947", 1956, false)}
};
const std::vector books {
object_ptr{std::make_shared<book>(3, "Jurassic Park", authors[0], 1990)},
object_ptr{std::make_shared<book>(4, "Timeline", authors[0], 1999)},
object_ptr{std::make_shared<book>(5, "The Andromeda Strain", authors[0], 1969)},
object_ptr{std::make_shared<book>(6, "Congo", authors[0], 1980)},
object_ptr{std::make_shared<book>(7, "Prey", authors[0], 2002)},
object_ptr{std::make_shared<book>(8, "Carrie", authors[1], 1974)},
object_ptr{std::make_shared<book>(9, "The Shining", authors[1], 1977)},
object_ptr{std::make_shared<book>(10, "It", authors[1], 1986)},
object_ptr{std::make_shared<book>(11, "Misery", authors[1], 1987)},
object_ptr{std::make_shared<book>(12, "The Dark Tower: The Gunslinger", authors[1], 1982)},
};
for (const auto &a: authors) {
auto res = query::insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(*a)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(AUTHOR)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &b: books) {
auto res = query::insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(*b)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::select({count_all()})
.from(BOOK)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 10);
auto author_result = query::select({AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.from(AUTHOR)
.order_by(AUTHOR.id).asc()
.fetch_all<author>(db);
REQUIRE(author_result.is_ok());
size_t index{0};
for (const auto &a : *author_result) {
REQUIRE(a->id == authors.at(index)->id);
REQUIRE(a->first_name == authors.at(index)->first_name);
REQUIRE(a->last_name == authors.at(index)->last_name);
REQUIRE(a->date_of_birth == authors.at(index)->date_of_birth);
REQUIRE(a->year_of_birth == authors.at(index)->year_of_birth);
REQUIRE(a->distinguished == authors.at(index)->distinguished);
REQUIRE(!a->books.empty());
++index;
}
} }
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation", "[query][belongs_to][lazy]") { TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation", "[query][belongs_to][lazy]") {