From 046c1d8ed28242565f1d18fd114d860a490064ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Sun, 25 Jan 2026 22:00:11 +0100 Subject: [PATCH] collection resolver producer progress --- include/matador/object/collection.hpp | 8 ++ include/matador/object/collection_proxy.hpp | 5 + .../object/object_resolver_factory.hpp | 11 +++ test/backends/QueryTest.cpp | 94 +++++++++++++++++-- 4 files changed, 110 insertions(+), 8 deletions(-) diff --git a/include/matador/object/collection.hpp b/include/matador/object/collection.hpp index 564f321..49a6c10 100644 --- a/include/matador/object/collection.hpp +++ b/include/matador/object/collection.hpp @@ -12,6 +12,14 @@ public: using iterator = typename std::vector::iterator; using const_iterator = typename std::vector::const_iterator; + collection() + : proxy_(std::make_shared>()) {} + explicit collection(std::shared_ptr> proxy) + : proxy_(std::move(proxy)) {} + explicit collection(std::vector items) + : proxy_(std::make_shared>(std::move(items))) {} + collection(const collection& other) = default; + void push_back(const value_type& value) { proxy_->items().push_back(value); } diff --git a/include/matador/object/collection_proxy.hpp b/include/matador/object/collection_proxy.hpp index ae3026d..3929549 100644 --- a/include/matador/object/collection_proxy.hpp +++ b/include/matador/object/collection_proxy.hpp @@ -36,12 +36,17 @@ public: template class collection_proxy final { public: + collection_proxy() = default; + + // Lazy collection_proxy(std::weak_ptr> resolver, utils::identifier owner_id) : owner_id_(std::move(owner_id)), resolver_(std::move(resolver)) {} + // Eager collection_proxy(std::weak_ptr> resolver, std::vector items) : items_(std::move(items)), resolver_(std::move(resolver)) {} + // Transient explicit collection_proxy(std::vector items) : items_(std::move(items)) {} diff --git a/include/matador/object/object_resolver_factory.hpp b/include/matador/object/object_resolver_factory.hpp index a1c060b..be0f8e3 100644 --- a/include/matador/object/object_resolver_factory.hpp +++ b/include/matador/object/object_resolver_factory.hpp @@ -3,6 +3,7 @@ #include "matador/object/abstract_type_resolver_factory.hpp" #include "matador/object/object_resolver.hpp" +#include "matador/object/collection_resolver_factory.hpp" namespace matador::object { class object_resolver_factory : public abstract_type_resolver_factory { @@ -16,6 +17,16 @@ public: return std::dynamic_pointer_cast>(res); } + // + // template + // std::shared_ptr> 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>(res); + // } + // + // return std::dynamic_pointer_cast>(res); + // } }; } #endif //MATADOR_OBJECT_RESOLVER_FACTORY_HPP \ No newline at end of file diff --git a/test/backends/QueryTest.cpp b/test/backends/QueryTest.cpp index 1d66848..faa151c 100644 --- a/test/backends/QueryTest.cpp +++ b/test/backends/QueryTest.cpp @@ -497,6 +497,8 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", .and_then([this] {return repo.create(db); }); REQUIRE(result.is_ok()); + repo.initialize_executor(db); + const std::vector shipments { object_ptr{std::make_shared(1, "4711")}, object_ptr{std::make_shared(2, "0815")} @@ -575,14 +577,90 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", REQUIRE(shipment_result.is_ok()); - // size_t index{0}; - // std::vector packages_sizes{2, 3}; - // std::cout << "\n"; - // for (const auto &s : *shipment_result) { - // REQUIRE(s->id == shipments.at(index)->id); - // REQUIRE(s->tracking_number == shipments.at(index)->tracking_number); - // REQUIRE(s->packages.size() == packages_sizes.at(index++)); - // } + size_t index{0}; + std::vector packages_sizes{2, 3}; + std::cout << "\n"; + for (const auto &s : *shipment_result) { + REQUIRE(s->id == shipments.at(index)->id); + REQUIRE(s->tracking_number == shipments.at(index)->tracking_number); + 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("authors") + .and_then( [this] { return repo.attach("books"); } ) + .and_then([this] { + return repo.create(db); + } ); + + repo.initialize_executor(db); + + const std::vector authors { + object_ptr{std::make_shared(1, "Michael", "Crichton", "23.10.1942", 1975, true)}, + object_ptr{std::make_shared( 2, "Steven", "King", "21.9.1947", 1956, false)} + }; + + const std::vector books { + object_ptr{std::make_shared(3, "Jurassic Park", authors[0], 1990)}, + object_ptr{std::make_shared(4, "Timeline", authors[0], 1999)}, + object_ptr{std::make_shared(5, "The Andromeda Strain", authors[0], 1969)}, + object_ptr{std::make_shared(6, "Congo", authors[0], 1980)}, + object_ptr{std::make_shared(7, "Prey", authors[0], 2002)}, + object_ptr{std::make_shared(8, "Carrie", authors[1], 1974)}, + object_ptr{std::make_shared(9, "The Shining", authors[1], 1977)}, + object_ptr{std::make_shared(10, "It", authors[1], 1986)}, + object_ptr{std::make_shared(11, "Misery", authors[1], 1987)}, + object_ptr{std::make_shared(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(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(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(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]") {