diff --git a/include/matador/query/schema.hpp b/include/matador/query/schema.hpp index c17c038..d1fc108 100644 --- a/include/matador/query/schema.hpp +++ b/include/matador/query/schema.hpp @@ -102,12 +102,25 @@ public: const_iterator begin() const; const_iterator end() const; - iterator find(const std::type_index& ti) { return schema_nodes_.find(ti); } - const_iterator find(const std::type_index& ti) const { return schema_nodes_.find(ti); } + [[nodiscard]] size_t size() const; + [[nodiscard]] bool empty() const; + template + iterator find() { return find(typeid(Type)); } + iterator find(const std::type_index& ti); iterator find(const std::string& name); + + template + const_iterator find() const { return find(typeid(Type)); } + const_iterator find(const std::type_index& ti) const; const_iterator find(const std::string& name) const; + [[nodiscard]] bool contains(const std::type_index &index) const; + template < typename Type > + [[nodiscard]] bool contains() const { + return contains(std::type_index(typeid(Type))); + } + const object::repository &repo() const { return repo_; } object::repository &repo() { return repo_; } diff --git a/source/orm/query/schema.cpp b/source/orm/query/schema.cpp index 3336e37..410f3f5 100644 --- a/source/orm/query/schema.cpp +++ b/source/orm/query/schema.cpp @@ -200,6 +200,22 @@ schema::const_iterator schema::end() const { return schema_nodes_.end(); } +size_t schema::size() const { + return schema_nodes_.size(); +} + +bool schema::empty() const { + return schema_nodes_.empty(); +} + +schema::iterator schema::find(const std::type_index &ti) { + return schema_nodes_.find(ti); +} + +schema::const_iterator schema::find(const std::type_index &ti) const { + return schema_nodes_.find(ti); +} + schema::iterator schema::find(const std::string &name) { const auto result = repo_.basic_info(name); if (!result) { @@ -218,6 +234,10 @@ schema::const_iterator schema::find(const std::string &name) const { return schema_nodes_.find(result->get().type_index()); } +bool schema::contains(const std::type_index &index) const { + return schema_nodes_.count(index) == 1; +} + void schema::dump(std::ostream &os) const { repo_.dump(os); } diff --git a/test/backends/QueryTest.cpp b/test/backends/QueryTest.cpp index 4e4cea5..798f6b6 100644 --- a/test/backends/QueryTest.cpp +++ b/test/backends/QueryTest.cpp @@ -27,7 +27,7 @@ META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id); META_TABLE(airplanes, AIRPLANE, id, brand, model); META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name); META_TABLE(shipments, SHIPMENT, id, tracking_number) -META_TABLE(packages, PACKAGE, id, weight, shipment) +META_TABLE(packages, PACKAGE, id, weight, shipment_id) TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") { auto result = repo.attach("airplane") @@ -593,10 +593,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship" select_result = query::select({r.id, r.name, ri.ingredient_id, i.name}) .from(r) - .join_left(ri) - .on(r.id == ri.recipe_id) - .join_left(i) - .on(ri.ingredient_id == i.id) + .join_left(ri).on(r.id == ri.recipe_id) + .join_left(i).on(ri.ingredient_id == i.id) .where(r.id == 8) .fetch_all(db); REQUIRE(result.is_ok()); @@ -621,8 +619,6 @@ void print(std::ostream& out, const record& row) { TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") { auto result = repo.attach("packages") .and_then( [this] { return repo.attach("shipments"); } ); - // auto result = repo.attach("shipments") - // .and_then( [this] { return repo.attach("packages"); } ); REQUIRE(result.is_ok()); tables_to_drop.emplace("shipments"); tables_to_drop.emplace("packages"); @@ -675,10 +671,10 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", REQUIRE(count.is_ok()); REQUIRE(*count == 5); - auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment, SHIPMENT.tracking_number}) + auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id, SHIPMENT.tracking_number}) .from(PACKAGE) .join_left(SHIPMENT) - .on( PACKAGE.shipment == SHIPMENT.id ) + .on( PACKAGE.shipment_id == SHIPMENT.id ) .where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" ) .group_by({PACKAGE.id, SHIPMENT.tracking_number}) .order_by(PACKAGE.weight).asc() @@ -694,23 +690,28 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight}) .from(SHIPMENT) .join_left(PACKAGE) - .on( SHIPMENT.id == PACKAGE.shipment ) + .on( SHIPMENT.id == PACKAGE.shipment_id ) .fetch_all(db); REQUIRE(shipment_records.is_ok()); printer.print(*shipment_records); - auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment}) + auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id}) .from(SHIPMENT) .join_left(PACKAGE) - .on( SHIPMENT.id == PACKAGE.shipment ) + .on( SHIPMENT.id == PACKAGE.shipment_id ) + .order_by(SHIPMENT.id).asc() .fetch_all(db); REQUIRE(shipment_result.is_ok()); + size_t index{0}; + std::vector packages_sizes{2, 3}; std::cout << "\n"; for (const auto &s : *shipment_result) { - std::cout << s.id << " " << s.tracking_number << " packages: " << s.packages.size() << std::endl; + REQUIRE(s.id == shipments.at(index)->id); + REQUIRE(s.tracking_number == shipments.at(index)->tracking_number); + REQUIRE(s.packages.size() == packages_sizes.at(index++)); } } \ No newline at end of file diff --git a/test/core/object/RepositoryTest.cpp b/test/core/object/RepositoryTest.cpp index 55e9c37..5d38897 100644 --- a/test/core/object/RepositoryTest.cpp +++ b/test/core/object/RepositoryTest.cpp @@ -48,15 +48,15 @@ TEST_CASE("Test add type to prototype tree", "[schema_node][add]") { REQUIRE(repo.empty()); - // auto res = repo.attach("person"); - // REQUIRE(res.is_ok()); - // res = repo.attach("student"); - // REQUIRE(res.is_ok()); - // res = repo.attach("teacher"); - // REQUIRE(res.is_ok()); - // - // REQUIRE(!repo.empty()); - // REQUIRE(repo.size() == 3); + auto res = repo.attach("person"); + REQUIRE(res.is_ok()); + res = repo.attach("student"); + REQUIRE(res.is_ok()); + res = repo.attach("teacher"); + REQUIRE(res.is_ok()); + + REQUIRE(!repo.empty()); + REQUIRE(repo.size() == 3); } TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") { diff --git a/test/models/shipment.hpp b/test/models/shipment.hpp index 11f0f59..01fa646 100644 --- a/test/models/shipment.hpp +++ b/test/models/shipment.hpp @@ -35,7 +35,7 @@ struct package { namespace field = matador::access; field::primary_key(op, "id", id); field::attribute(op, "weight", weight); - field::belongs_to(op, "shipment", delivery, utils::CascadeAllFetchLazy); + field::belongs_to(op, "shipment_id", delivery, utils::CascadeAllFetchLazy); } }; } diff --git a/test/orm/CMakeLists.txt b/test/orm/CMakeLists.txt index 9d6090f..a03bee7 100644 --- a/test/orm/CMakeLists.txt +++ b/test/orm/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(OrmTests utils/auto_reset_event.cpp utils/auto_reset_event.hpp query/GeneratorTests.cpp + query/SchemaTest.cpp ) target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain) diff --git a/test/orm/query/SchemaTest.cpp b/test/orm/query/SchemaTest.cpp new file mode 100644 index 0000000..8816c1f --- /dev/null +++ b/test/orm/query/SchemaTest.cpp @@ -0,0 +1,42 @@ +#include + +#include "matador/query/schema.hpp" + +#include "../../models/shipment.hpp" + +using namespace matador::query; +using namespace matador::test; + +TEST_CASE("Test schema with belongs-to to has-many relation" "[schema][relation][belongs_to]") { + schema repo; + auto result = repo.attach("packages") + .and_then( [&repo] { return repo.attach("shipments"); } ); + REQUIRE(result.is_ok()); + REQUIRE(repo.size() == 2); + REQUIRE(repo.contains()); + REQUIRE(repo.contains()); + + auto it = repo.find(); + REQUIRE(it != repo.end()); + REQUIRE(it->second.name() == "shipments"); + it = repo.find(); + REQUIRE(it != repo.end()); + REQUIRE(it->second.name() == "packages"); +} + +TEST_CASE("Test schema with has-many to belongs-to relation" "[schema][relation][has_many]") { + schema repo; + auto result = repo.attach("shipments") + .and_then( [&repo] { return repo.attach("packages"); } ); + REQUIRE(result.is_ok()); + REQUIRE(repo.size() == 2); + REQUIRE(repo.contains()); + REQUIRE(repo.contains()); + + auto it = repo.find(); + REQUIRE(it != repo.end()); + REQUIRE(it->second.name() == "shipments"); + it = repo.find(); + REQUIRE(it != repo.end()); + REQUIRE(it->second.name() == "packages"); +} \ No newline at end of file