From db8e137c11d289c1b2c579cf26fba2231d29dbe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Sat, 10 Jan 2026 18:54:18 +0100 Subject: [PATCH] added belongs-to-eager test --- test/backends/QueryTest.cpp | 149 ++++++++++++++++++++++++++++++++++ test/models/airplane.hpp | 19 +++-- test/models/author.hpp | 38 ++++----- test/models/book.hpp | 26 +++--- test/models/category.hpp | 20 ++--- test/models/coordinate.hpp | 6 +- test/models/department.hpp | 2 +- test/models/flight.hpp | 6 +- test/models/location.hpp | 10 +-- test/models/model_metas.hpp | 4 + test/models/optional.hpp | 27 +++--- test/models/order.hpp | 65 +++++++-------- test/models/order_details.hpp | 25 +++--- test/models/person.hpp | 28 +++---- test/models/product.hpp | 48 ++++++----- test/models/recipe.hpp | 31 +++---- test/models/shipment.hpp | 3 +- test/models/student.hpp | 64 ++++++++------- test/models/supplier.hpp | 20 ++--- test/models/types.hpp | 90 ++++++++++---------- 20 files changed, 403 insertions(+), 278 deletions(-) diff --git a/test/backends/QueryTest.cpp b/test/backends/QueryTest.cpp index 5bd0705..78cd201 100644 --- a/test/backends/QueryTest.cpp +++ b/test/backends/QueryTest.cpp @@ -9,6 +9,9 @@ #include "QueryFixture.hpp" #include "models/airplane.hpp" +#include "models/author.hpp" +#include "models/book.hpp" +#include "models/department.hpp" #include "models/flight.hpp" #include "models/person.hpp" #include "models/recipe.hpp" @@ -577,4 +580,150 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", 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 belongs to relation", "[query][belongs_to][lazy]") { + auto result = repo.attach("departments") + .and_then([this] { return repo.attach("employees"); }) + .and_then([this] {return repo.create(db); }); + REQUIRE(result.is_ok()); + + const std::vector deps { + object_ptr{new department{1, "Human Resources"}}, + object_ptr{new department{2, "Invoices"}} + }; + + const std::vector emps { + object_ptr{new employee{3, "Hans", "Wurst", deps[0]}}, + object_ptr{new employee{4, "Steven", "Spielberg", deps[0]}}, + object_ptr{new employee{5, "Julia", "Roberts", deps[0]}}, + object_ptr{new employee{6, "Otto", "Walkes", deps[1]}}, + object_ptr{new employee{7, "Miss", "Marple", deps[1]}}, + }; + + for (const auto &dep: deps) { + auto res = query::insert() + .into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name}) + .values(*dep) + .execute(db); + REQUIRE(res.is_ok()); + REQUIRE(*res == 1); + } + + auto count = query::select({count_all()}) + .from(DEPARTMENT) + .fetch_value(db); + REQUIRE(count.is_ok()); + REQUIRE(*count == 2); + + for (const auto &emp: emps) { + auto res = query::insert() + .into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id}) + .values(*emp) + .execute(db); + REQUIRE(res.is_ok()); + REQUIRE(*res == 1); + } + + count = query::select({count_all()}) + .from(EMPLOYEE) + .fetch_value(db); + REQUIRE(count.is_ok()); + REQUIRE(*count == 5); + + auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, DEPARTMENT.id, DEPARTMENT.name}) + .from(EMPLOYEE) + .join_left(DEPARTMENT) + .on(EMPLOYEE.dep_id == DEPARTMENT.id) + .order_by(EMPLOYEE.id).asc() + .fetch_all(db); + + REQUIRE(emps_result.is_ok()); + size_t index{0}; + for (const auto &e : *emps_result) { + REQUIRE(e.id == emps.at(index)->id); + REQUIRE(e.first_name == emps.at(index)->first_name); + REQUIRE(e.last_name == emps.at(index)->last_name); + REQUIRE(e.dep->id == emps.at(index)->dep->id); + REQUIRE(e.dep->name == emps.at(index)->dep->name); + ++index; + } +} + +TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") { + auto result = repo.attach("authors") + .and_then( [this] { return repo.attach("books"); } ) + .and_then([this] { + return repo.create(db); + } ); + + const std::vector authors { + object_ptr{new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}}}, + object_ptr{new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}}} + }; + + const std::vector books { + object_ptr{new book{3, "Jurassic Park", authors[0], 1990}}, + object_ptr{new book{4, "Timeline", authors[0], 1999}}, + object_ptr{new book{5, "The Andromeda Strain", authors[0], 1969}}, + object_ptr{new book{6, "Congo", authors[0], 1980}}, + object_ptr{new book{7, "Prey", authors[0], 2002}}, + object_ptr{new book{8, "Carrie", authors[1], 1974}}, + object_ptr{new book{9, "The Shining", authors[1], 1977}}, + object_ptr{new book{10, "It", authors[1], 1986}}, + object_ptr{new book{11, "Misery", authors[1], 1987}}, + object_ptr{new 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(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 books_result = query::select({BOOK.id, BOOK.title, AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished, BOOK.published_in}) + .from(BOOK) + .join_left(AUTHOR) + .on(BOOK.author_id == AUTHOR.id) + .order_by(BOOK.id).asc() + .fetch_all(db); + + REQUIRE(books_result.is_ok()); + size_t index{0}; + for (const auto &b : *books_result) { + REQUIRE(b.id == books.at(index)->id); + REQUIRE(b.title == books.at(index)->title); + REQUIRE(b.book_author->id == books.at(index)->book_author->id); + REQUIRE(b.book_author->first_name == books.at(index)->book_author->first_name); + REQUIRE(b.book_author->last_name == books.at(index)->book_author->last_name); + REQUIRE(b.book_author->date_of_birth == books.at(index)->book_author->date_of_birth); + REQUIRE(b.book_author->year_of_birth == books.at(index)->book_author->year_of_birth); + REQUIRE(b.book_author->distinguished == books.at(index)->book_author->distinguished); + REQUIRE(b.published_in == books.at(index)->published_in); + ++index; + } } \ No newline at end of file diff --git a/test/models/airplane.hpp b/test/models/airplane.hpp index c7f00a7..3691873 100644 --- a/test/models/airplane.hpp +++ b/test/models/airplane.hpp @@ -6,27 +6,28 @@ #include namespace matador::test { - struct airplane { airplane() = default; + airplane(const unsigned int id, std::string b, std::string m) : id(id) - , brand(std::move(b)) - , model(std::move(m)) {} + , brand(std::move(b)) + , model(std::move(m)) { + } + unsigned int id{}; std::string brand; std::string model; template void process(Operator &op) { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "id", id); - field::attribute(op, "brand", brand, 255); - field::attribute(op, "model", model, 255); + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id); + field::attribute(op, "brand", brand, 255); + field::attribute(op, "model", model, 255); } }; - } // namespace matador::access { diff --git a/test/models/author.hpp b/test/models/author.hpp index 845e163..cb2ab35 100644 --- a/test/models/author.hpp +++ b/test/models/author.hpp @@ -9,31 +9,29 @@ #include namespace matador::test { - struct book; struct author { - unsigned int id{}; - std::string first_name; - std::string last_name; - std::string date_of_birth; - unsigned short year_of_birth{}; - bool distinguished{false}; - std::vector> books; + unsigned int id{}; + std::string first_name; + std::string last_name; + std::string date_of_birth; + unsigned short year_of_birth{}; + bool distinguished{false}; + std::vector > books; - template - void process(Operator &op) { - namespace field = matador::access; - field::primary_key(op, "id", id); - field::attribute(op, "first_name", first_name, 63); - field::attribute(op, "last_name", last_name, 63); - field::attribute(op, "date_of_birth", date_of_birth, 31); - field::attribute(op, "year_of_birth", year_of_birth); - field::attribute(op, "distinguished", distinguished); - field::has_many(op, "books", books, "author_id", utils::fetch_type::Lazy); - } + template + void process(Operator &op) { + namespace field = matador::access; + field::primary_key(op, "id", id); + field::attribute(op, "first_name", first_name, 63); + field::attribute(op, "last_name", last_name, 63); + field::attribute(op, "date_of_birth", date_of_birth, 31); + field::attribute(op, "year_of_birth", year_of_birth); + field::attribute(op, "distinguished", distinguished); + field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy); + } }; - } #endif //QUERY_AUTHOR_HPP diff --git a/test/models/book.hpp b/test/models/book.hpp index 0079db1..da61dc1 100644 --- a/test/models/book.hpp +++ b/test/models/book.hpp @@ -9,24 +9,22 @@ #include namespace matador::test { - struct author; struct book { - unsigned int id{}; - std::string title; - object::object_ptr book_author; - unsigned short published_in{}; + unsigned int id{}; + std::string title; + object::object_ptr book_author; + unsigned short published_in{}; - template - void process(Operator &op) { - namespace field = matador::access; - field::primary_key(op, "id", id); - field::attribute(op, "title", title, 511); - field::belongs_to(op, "author_id", book_author, utils::fetch_type::Eager); - field::attribute(op, "published_in", published_in); - } + template + void process(Operator &op) { + namespace field = matador::access; + field::primary_key(op, "id", id); + field::attribute(op, "title", title, 511); + field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager); + field::attribute(op, "published_in", published_in); + } }; - } #endif //QUERY_BOOK_HPP diff --git a/test/models/category.hpp b/test/models/category.hpp index 718ade8..a9644b3 100644 --- a/test/models/category.hpp +++ b/test/models/category.hpp @@ -6,19 +6,17 @@ #include namespace matador::test { - struct category { - unsigned int id{}; - std::string name; + unsigned int id{}; + std::string name; - template - void process(Operator &op) { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "id", id); - field::attribute(op, "name", name, 255); - } + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id); + field::attribute(op, "name", name, 255); + } }; - } #endif //QUERY_CATEGORY_HPP diff --git a/test/models/coordinate.hpp b/test/models/coordinate.hpp index 6165c91..f8d2633 100644 --- a/test/models/coordinate.hpp +++ b/test/models/coordinate.hpp @@ -5,24 +5,20 @@ #include "matador/utils/field_attributes.hpp" namespace matador::test { -struct coordinate -{ +struct coordinate { int x{}; int y{}; int z{}; }; - } namespace matador::access { - template void attribute(Operator &op, const char *id, test::coordinate &value, const utils::field_attributes &attr = utils::null_attributes) { attribute(op, (std::string(id) + "_x").c_str(), value.x, attr); attribute(op, (std::string(id) + "_y").c_str(), value.y, attr); attribute(op, (std::string(id) + "_z").c_str(), value.z, attr); } - } #endif //QUERY_COORDINATE_HPP diff --git a/test/models/department.hpp b/test/models/department.hpp index afa6c8a..90a1d61 100644 --- a/test/models/department.hpp +++ b/test/models/department.hpp @@ -13,7 +13,7 @@ struct employee; struct department { unsigned int id{}; std::string name; - std::vector> employees; + std::vector> employees{}; // object::object_ptr manager; template diff --git a/test/models/flight.hpp b/test/models/flight.hpp index 26eaf07..c223a93 100644 --- a/test/models/flight.hpp +++ b/test/models/flight.hpp @@ -13,11 +13,12 @@ #include namespace matador::test { - struct flight { flight() = default; + flight(const unsigned int id, const object::object_ptr &plane, std::string name) - : id(id), plane(plane), pilot_name(std::move(name)) {} + : id(id), plane(plane), pilot_name(std::move(name)) { + } unsigned int id{}; object::object_ptr plane; @@ -32,7 +33,6 @@ struct flight { field::attribute(op, "pilot_name", pilot_name, 255); } }; - } #endif //QUERY_FLIGHT_HPP diff --git a/test/models/location.hpp b/test/models/location.hpp index f158a03..44e37ad 100644 --- a/test/models/location.hpp +++ b/test/models/location.hpp @@ -8,21 +8,18 @@ #include namespace matador::test { - enum class Color : uint8_t { Green, Red, Blue, Yellow, Black, White, Brown }; -struct location -{ +struct location { unsigned int id{}; std::string name; coordinate coord; Color color{Color::Green}; - template < class Operator > - void process(Operator &op) - { + template + void process(Operator &op) { namespace field = matador::access; field::primary_key(op, "id", id); field::attribute(op, "name", name, 255); @@ -30,7 +27,6 @@ struct location field::attribute(op, "color", color); } }; - } #endif //QUERY_LOCATION_HPP diff --git a/test/models/model_metas.hpp b/test/models/model_metas.hpp index aa5e028..bffec72 100644 --- a/test/models/model_metas.hpp +++ b/test/models/model_metas.hpp @@ -11,5 +11,9 @@ META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name); META_TABLE(shipments, SHIPMENT, id, tracking_number) META_TABLE(packages, PACKAGE, id, weight, shipment_id) META_TABLE(persons, PERSON, id, name, age, image) +META_TABLE(departments, DEPARTMENT, id, name) +META_TABLE(employees, EMPLOYEE, id, first_name, last_name, dep_id) +META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished) +META_TABLE(books, BOOK, id, title, author_id, published_in) #endif //MATADOR_MODEL_METAS_HPP \ No newline at end of file diff --git a/test/models/optional.hpp b/test/models/optional.hpp index c85c0f4..29d54d8 100644 --- a/test/models/optional.hpp +++ b/test/models/optional.hpp @@ -8,23 +8,20 @@ #include namespace matador::test { +struct optional { + unsigned int id{}; + std::optional name; + std::optional age{}; -struct optional -{ - unsigned int id{}; - std::optional name; - std::optional age{}; - - template - void process(Operator &op) { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "id", id); - field::attribute(op, "name", name, 255); - field::attribute(op, "age", age); - } + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id); + field::attribute(op, "name", name, 255); + field::attribute(op, "age", age); + } }; - } #endif //QUERY_OPTIONAL_HPP diff --git a/test/models/order.hpp b/test/models/order.hpp index 835d97e..2fbfc0d 100644 --- a/test/models/order.hpp +++ b/test/models/order.hpp @@ -10,41 +10,38 @@ #include namespace matador::test { +struct order { + unsigned int order_id{}; + std::string order_date; + std::string required_date; + std::string shipped_date; + unsigned int ship_via{}; + unsigned int freight{}; + std::string ship_name; + std::string ship_address; + std::string ship_city; + std::string ship_region; + std::string ship_postal_code; + std::string ship_country; + std::vector > order_details_; -struct order -{ - unsigned int order_id{}; - std::string order_date; - std::string required_date; - std::string shipped_date; - unsigned int ship_via{}; - unsigned int freight{}; - std::string ship_name; - std::string ship_address; - std::string ship_city; - std::string ship_region; - std::string ship_postal_code; - std::string ship_country; - std::vector> order_details_; - - template - void process(Operator &op) { - namespace field = matador::access; - field::primary_key(op, "order_id", order_id); - field::attribute(op, "order_date", order_date, 255); - field::attribute(op, "required_date", required_date, 255); - field::attribute(op, "shipped_date", shipped_date, 255); - field::attribute(op, "ship_via", ship_via); - field::attribute(op, "freight", freight); - field::attribute(op, "ship_name", ship_name, 255); - field::attribute(op, "ship_address", ship_address, 255); - field::attribute(op, "ship_city", ship_city, 255); - field::attribute(op, "ship_region", ship_region, 255); - field::attribute(op, "ship_postal_code", ship_postal_code, 255); - field::attribute(op, "ship_country", ship_country, 255); - field::has_many(op, "order_details", order_details_, "order_id", utils::fetch_type::Eager); - } + template + void process(Operator &op) { + namespace field = matador::access; + field::primary_key(op, "order_id", order_id); + field::attribute(op, "order_date", order_date, 255); + field::attribute(op, "required_date", required_date, 255); + field::attribute(op, "shipped_date", shipped_date, 255); + field::attribute(op, "ship_via", ship_via); + field::attribute(op, "freight", freight); + field::attribute(op, "ship_name", ship_name, 255); + field::attribute(op, "ship_address", ship_address, 255); + field::attribute(op, "ship_city", ship_city, 255); + field::attribute(op, "ship_region", ship_region, 255); + field::attribute(op, "ship_postal_code", ship_postal_code, 255); + field::attribute(op, "ship_country", ship_country, 255); + field::has_many(op, "order_details", order_details_, "order_id", utils::fetch_type::Eager); + } }; - } #endif //QUERY_ORDER_HPP diff --git a/test/models/order_details.hpp b/test/models/order_details.hpp index 98da120..91a7420 100644 --- a/test/models/order_details.hpp +++ b/test/models/order_details.hpp @@ -8,23 +8,20 @@ #include "matador/utils/foreign_attributes.hpp" namespace matador::test { - struct order; -struct order_details -{ - unsigned int order_details_id; - matador::object::object_ptr order_; - matador::object::object_ptr product_; +struct order_details { + unsigned int order_details_id; + object::object_ptr order_; + object::object_ptr product_; - template - void process(Operator &op) { - namespace field = matador::access; - field::primary_key(op, "order_details_id", order_details_id); - field::belongs_to(op, "order_id", order_, utils::CascadeNoneFetchLazy); - field::has_one(op, "product_id", product_, utils::CascadeNoneFetchLazy); - } + template + void process(Operator &op) { + namespace field = matador::access; + field::primary_key(op, "order_details_id", order_details_id); + field::belongs_to(op, "order_id", order_, utils::CascadeNoneFetchLazy); + field::has_one(op, "product_id", product_, utils::CascadeNoneFetchLazy); + } }; - } #endif //QUERY_ORDER_DETAILS_HPP diff --git a/test/models/person.hpp b/test/models/person.hpp index 66dc518..16b0c3c 100644 --- a/test/models/person.hpp +++ b/test/models/person.hpp @@ -8,23 +8,21 @@ #include namespace matador::test { - struct person { - unsigned int id{}; - std::string name; - unsigned int age{}; - utils::blob_type_t image{}; + unsigned int id{}; + std::string name; + unsigned int age{}; + utils::blob_type_t image{}; - template - void process(Operator &op) { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "id", id); - field::attribute(op, "name", name, 255); - field::attribute(op, "age", age); - field::attribute(op, "image", image); - } + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id); + field::attribute(op, "name", name, 255); + field::attribute(op, "age", age); + field::attribute(op, "image", image); + } }; - } #endif //QUERY_PERSON_HPP diff --git a/test/models/product.hpp b/test/models/product.hpp index 31e8218..95bf231 100644 --- a/test/models/product.hpp +++ b/test/models/product.hpp @@ -6,39 +6,37 @@ #include "matador/utils/access.hpp" #include "matador/utils/cascade_type.hpp" -#include "matador/utils/field_attributes.hpp" #include "matador/object/object_ptr.hpp" #include namespace matador::test { - struct product { - std::string product_name; - object::object_ptr supplier; - object::object_ptr category; - std::string quantity_per_unit; - unsigned int unit_price; - unsigned int units_in_stock; - unsigned int units_in_order; - unsigned int reorder_level; - bool discontinued; + std::string product_name; + object::object_ptr supplier; + object::object_ptr category; + std::string quantity_per_unit; + unsigned int unit_price; + unsigned int units_in_stock; + unsigned int units_in_order; + unsigned int reorder_level; + bool discontinued; - template - void process(Operator &op) { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "product_name", product_name, 255); - field::belongs_to(op, "supplier_id", supplier, utils::cascade_type::ALL); - field::belongs_to(op, "category_id", category, utils::cascade_type::ALL); - field::attribute(op, "quantity_per_unit", quantity_per_unit, 255); - field::attribute(op, "unit_price", unit_price); - field::attribute(op, "units_in_stock", units_in_stock); - field::attribute(op, "units_in_order", units_in_order); - field::attribute(op, "reorder_level", reorder_level); - field::attribute(op, "discontinued", discontinued); - } + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "product_name", product_name, 255); + field::belongs_to(op, "supplier_id", supplier, CascadeAllFetchLazy); + field::belongs_to(op, "category_id", category, CascadeAllFetchLazy); + field::attribute(op, "quantity_per_unit", quantity_per_unit, 255); + field::attribute(op, "unit_price", unit_price); + field::attribute(op, "units_in_stock", units_in_stock); + field::attribute(op, "units_in_order", units_in_order); + field::attribute(op, "reorder_level", reorder_level); + field::attribute(op, "discontinued", discontinued); + } }; } diff --git a/test/models/recipe.hpp b/test/models/recipe.hpp index 1a49f07..ac7d318 100644 --- a/test/models/recipe.hpp +++ b/test/models/recipe.hpp @@ -10,51 +10,52 @@ #include namespace matador::test { - struct recipe; -struct ingredient -{ + +struct ingredient { unsigned int id{}; std::string name; - std::vector> recipes{}; + std::vector > recipes{}; + + ingredient() = default; - ingredient()= default; ingredient(const unsigned int id, std::string name) - : id(id), name(std::move(name)) {} + : id(id), name(std::move(name)) { + } template void process(Operator &op) { namespace field = matador::access; field::primary_key(op, "id", id); field::attribute(op, "name", name, 255); - field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::fetch_type::Eager); + field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::CascadeAllFetchEager); } }; -struct recipe -{ +struct recipe { unsigned int id{}; std::string name; - std::vector> ingredients{}; + std::vector > ingredients{}; + + recipe() = default; - recipe()= default; recipe(const unsigned int id, std::string name) - : id(id), name(std::move(name)) {} + : id(id), name(std::move(name)) { + } template void process(Operator &op) { namespace field = matador::access; field::primary_key(op, "id", id); field::attribute(op, "name", name, 255); - field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::fetch_type::Lazy); + field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::CascadeAllFetchLazy); } }; // class recipe_ingredient : public object::many_to_many_relation { // public: - // recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {} +// recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {} // }; - } #endif //QUERY_RECIPE_HPP diff --git a/test/models/shipment.hpp b/test/models/shipment.hpp index 01fa646..5044571 100644 --- a/test/models/shipment.hpp +++ b/test/models/shipment.hpp @@ -11,10 +11,11 @@ namespace matador::test { struct package; + struct shipment { long id{}; std::string tracking_number; - object::collection> packages{}; + object::collection > packages{}; template void process(Operator &op) { diff --git a/test/models/student.hpp b/test/models/student.hpp index ab8d246..ad48917 100644 --- a/test/models/student.hpp +++ b/test/models/student.hpp @@ -11,52 +11,54 @@ #include namespace matador::test { - struct course; struct student { - unsigned int id{}; - std::string name; - std::vector> courses; + unsigned int id{}; + std::string name; + std::vector > courses; - student() = default; - explicit student(unsigned int id, std::string name) - : id(id) - , name(std::move(name)) {} + student() = default; - template < class Operator > - void process(Operator &op) { - namespace field = matador::access; - field::primary_key(op, "id", id); - field::attribute(op, "name", name, 255); - field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::fetch_type::Lazy); - } + explicit student(unsigned int id, std::string name) + : id(id) + , name(std::move(name)) { + } + template + void process(Operator &op) { + namespace field = matador::access; + field::primary_key(op, "id", id); + field::attribute(op, "name", name, 255); + field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::CascadeAllFetchLazy); + } }; struct course { - unsigned int id{}; - std::string title; - std::vector> students; + unsigned int id{}; + std::string title; + std::vector > students; - course() = default; - explicit course(unsigned int id, std::string title) - : id(id) - , title(std::move(title)) {} + course() = default; - template < class Operator > - void process(Operator &op) { - namespace field = matador::access; - field::primary_key(op, "id", id); - field::attribute(op, "title", title, 255); - field::has_many_to_many(op, "student_courses", students, utils::fetch_type::Eager); - } + explicit course(unsigned int id, std::string title) + : id(id) + , title(std::move(title)) { + } + + template + void process(Operator &op) { + namespace field = matador::access; + field::primary_key(op, "id", id); + field::attribute(op, "title", title, 255); + field::has_many_to_many(op, "student_courses", students, utils::CascadeAllFetchEager); + } }; class student_course : public object::many_to_many_relation { public: - student_course() : many_to_many_relation("student_id", "course_id") {} + student_course() : many_to_many_relation("student_id", "course_id") { + } }; - } #endif //QUERY_STUDENT_HPP diff --git a/test/models/supplier.hpp b/test/models/supplier.hpp index b785193..fe24c5a 100644 --- a/test/models/supplier.hpp +++ b/test/models/supplier.hpp @@ -6,19 +6,17 @@ #include namespace matador::test { - struct supplier { - unsigned int id{}; - std::string name; + unsigned int id{}; + std::string name; - template - void process(Operator &op) { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "id", id); - field::attribute(op, "name", name, 255); - } + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id); + field::attribute(op, "name", name, 255); + } }; - } #endif //QUERY_SUPPLIER_HPP diff --git a/test/models/types.hpp b/test/models/types.hpp index e9f35dd..9b8ee24 100644 --- a/test/models/types.hpp +++ b/test/models/types.hpp @@ -5,56 +5,52 @@ #include "matador/utils/types.hpp" namespace matador::test { +struct types { + // enum { CSTR_LEN=255 }; -struct types -{ - // enum { CSTR_LEN=255 }; + unsigned int id_ = 0; + int8_t char_ = 'c'; + short short_ = -127; + int int_ = -65000; + int64_t long64_ = -1234567890; + unsigned char unsigned_char_ = 'H'; + unsigned short unsigned_short_ = 128; + unsigned int unsigned_int_ = 65000; + uint64_t unsigned_long64_ = 1234567890; + float float_ = 3.1415f; + double double_ = 1.1414; + bool bool_ = true; + // char cstr_[CSTR_LEN]{}; + std::string string_ = "Welt"; + std::string varchar_ = "Erde"; + // matador::date date_; + // matador::time time_; + utils::blob_type_t binary_{1, 2, 3, 4}; - unsigned int id_ = 0; - int8_t char_ = 'c'; - short short_ = -127; - int int_ = -65000; - int64_t long64_ = -1234567890; - unsigned char unsigned_char_ = 'H'; - unsigned short unsigned_short_ = 128; - unsigned int unsigned_int_ = 65000; - uint64_t unsigned_long64_ = 1234567890; - float float_ = 3.1415f; - double double_ = 1.1414; - bool bool_ = true; - // char cstr_[CSTR_LEN]{}; - std::string string_ = "Welt"; - std::string varchar_ = "Erde"; -// matador::date date_; -// matador::time time_; - utils::blob_type_t binary_{ 1, 2, 3, 4 }; - - template < class Operator > - void process(Operator &op) - { - namespace field = matador::access; - using namespace matador::utils; - field::primary_key(op, "id", id_); - field::attribute(op, "val_char", char_); - field::attribute(op, "val_float", float_); - field::attribute(op, "val_double", double_); - field::attribute(op, "val_short", short_); - field::attribute(op, "val_int", int_); - field::attribute(op, "val_long_long", long64_); - field::attribute(op, "val_unsigned_char", unsigned_char_); - field::attribute(op, "val_unsigned_short", unsigned_short_); - field::attribute(op, "val_unsigned_int", unsigned_int_); - field::attribute(op, "val_unsigned_long_long", unsigned_long64_); - field::attribute(op, "val_bool", bool_); - // field::attribute(op, "val_cstr", cstr_, CSTR_LEN); - field::attribute(op, "val_string", string_); - field::attribute(op, "val_varchar", varchar_, 63); - // field::attribute(op, "val_date", date_); - // field::attribute(op, "val_time", time_); - field::attribute(op, "val_binary", binary_); - } + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id_); + field::attribute(op, "val_char", char_); + field::attribute(op, "val_float", float_); + field::attribute(op, "val_double", double_); + field::attribute(op, "val_short", short_); + field::attribute(op, "val_int", int_); + field::attribute(op, "val_long_long", long64_); + field::attribute(op, "val_unsigned_char", unsigned_char_); + field::attribute(op, "val_unsigned_short", unsigned_short_); + field::attribute(op, "val_unsigned_int", unsigned_int_); + field::attribute(op, "val_unsigned_long_long", unsigned_long64_); + field::attribute(op, "val_bool", bool_); + // field::attribute(op, "val_cstr", cstr_, CSTR_LEN); + field::attribute(op, "val_string", string_); + field::attribute(op, "val_varchar", varchar_, 63); + // field::attribute(op, "val_date", date_); + // field::attribute(op, "val_time", time_); + field::attribute(op, "val_binary", binary_); + } }; - } #endif // QUERY_TYPES_HPP