diff --git a/include/matador/query/select_query_builder.hpp b/include/matador/query/select_query_builder.hpp index ef33011..3bfe85b 100644 --- a/include/matador/query/select_query_builder.hpp +++ b/include/matador/query/select_query_builder.hpp @@ -112,13 +112,67 @@ public: } template - void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) { - on_foreign_object(id, obj, attr, true); + void on_belongs_to(const char *id, Pointer &/*obj*/, const utils::foreign_attributes &attr) { + const auto it = schema_.find(typeid(typename Pointer::value_type)); + if (it == schema_.end()) { + throw query_builder_exception{error_code::UnknownType, "Unknown type"}; + } + if (!it->second.node().info().has_primary_key()) { + throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"}; + } + + const auto& info = it->second.node().info(); + auto foreign_table = it->second.table().as(build_alias('t', ++table_index)); + if (attr.fetch() == utils::fetch_type::Eager) { + + auto next = processed_tables_.find(info.name()); + if (next != processed_tables_.end()) { + return; + } + table_info_stack_.push({info, std::move(foreign_table)}); + next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first; + typename Pointer::value_type obj; + access::process(*this, obj); + table_info_stack_.pop(); + + append_join( + table_column{&table_info_stack_.top().table, id}, + table_column{&next->second, info.primary_key_attribute()->name()} + ); + } else { + push(id); + } } template - void on_has_one(const char *id, Pointer &obj, const char * /*join_column*/, const utils::foreign_attributes &attr) { - on_foreign_object(id, obj, attr, false); + void on_has_one(const char * /*id*/, Pointer &/*obj*/, const char * join_column, const utils::foreign_attributes &attr) { + const auto it = schema_.find(typeid(typename Pointer::value_type)); + if (it == schema_.end()) { + throw query_builder_exception{error_code::UnknownType, "Unknown type"}; + } + if (!it->second.node().info().has_primary_key()) { + throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"}; + } + + const auto& info = it->second.node().info(); + auto foreign_table = it->second.table().as(build_alias('t', ++table_index)); + if (attr.fetch() == utils::fetch_type::Eager) { + + auto next = processed_tables_.find(info.name()); + if (next != processed_tables_.end()) { + return; + } + table_info_stack_.push({info, std::move(foreign_table)}); + next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first; + typename Pointer::value_type obj; + access::process(*this, obj); + table_info_stack_.pop(); + + append_join( + table_column{&table_info_stack_.top().table, it->second.node().info().primary_key_attribute()->name()}, + table_column{&next->second, join_column} + ); + } } template @@ -257,8 +311,6 @@ public: [[nodiscard]] const select_query_data &query_data() const; private: - template - void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, bool add_column_on_lazy); void push(const std::string &column_name); static std::string build_alias(char prefix, unsigned int count); [[nodiscard]] bool is_root_entity() const; @@ -278,39 +330,5 @@ private: unsigned int table_index{0}; object::join_columns_collector join_columns_collector_{}; }; - -template -void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, const bool add_column_on_lazy) { - const auto it = schema_.find(typeid(typename Pointer::value_type)); - if (it == schema_.end()) { - throw query_builder_exception{error_code::UnknownType, "Unknown type"}; - } - if (!it->second.node().info().has_primary_key()) { - throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"}; - } - - const auto& info = it->second.node().info(); - auto foreign_table = it->second.table().as(build_alias('t', ++table_index)); - if (attr.fetch() == utils::fetch_type::Eager) { - - auto next = processed_tables_.find(info.name()); - if (next != processed_tables_.end()) { - return; - } - table_info_stack_.push({info, std::move(foreign_table)}); - next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first; - typename Pointer::value_type obj; - access::process(*this, obj); - table_info_stack_.pop(); - - append_join( - table_column{&table_info_stack_.top().table, id}, - table_column{&next->second, info.primary_key_attribute()->name()} - ); - } else if (add_column_on_lazy) { - push(id); - } -} - } #endif //QUERY_ENTITY_QUERY_BUILDER_HPP diff --git a/test/backends/SessionInsertHasOne.cpp b/test/backends/SessionInsertHasOne.cpp index 43e0967..9b18eac 100644 --- a/test/backends/SessionInsertHasOne.cpp +++ b/test/backends/SessionInsertHasOne.cpp @@ -7,12 +7,13 @@ #include "matador/query/session.hpp" #include "models/user.hpp" +#include "models/country.hpp" using namespace matador::test; using namespace matador::query; using namespace matador::object; -TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[session][insert][has_one]") { +TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation lazy", "[session][insert][has_one][lazy]") { const auto result = schema.attach("users") .and_then( [this] { return schema.attach("user_sessions"); } ) .and_then([this] { return schema.create(db); } ); @@ -33,4 +34,27 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[s REQUIRE(user_result.is_ok()); us = user_result.value()->session; REQUIRE(us->session_token == "session1"); +} + +TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation eager", "[session][insert][has_one][eager]") { + const auto result = schema.attach("countries") + .and_then( [this] { return schema.attach("capitals"); } ) + .and_then([this] { return schema.create(db); } ); + REQUIRE(result.is_ok()); + + session ses({bus, connection::dns, 4}, schema); + + const auto ger = make_object("Germany"); + auto ger_cap = make_object("Berlin", ger); + + REQUIRE(ger.is_transient()); + REQUIRE(ger_cap.is_transient()); + REQUIRE(ses.insert(ger_cap).is_ok()); + REQUIRE(ger_cap.is_persistent()); + REQUIRE(ger.is_persistent()); + + const auto ger_result = ses.find(ger->id); + REQUIRE(ger_result.is_ok()); + ger_cap = ger_result.value()->capital; + REQUIRE(ger_cap->name == "Berlin"); } \ No newline at end of file diff --git a/test/models/country.hpp b/test/models/country.hpp new file mode 100644 index 0000000..fa3b15e --- /dev/null +++ b/test/models/country.hpp @@ -0,0 +1,72 @@ +#ifndef MATADOR_COUNTRY_HPP +#define MATADOR_COUNTRY_HPP + +#include "matador/utils/access.hpp" + +#include "matador/object/object_ptr.hpp" + +#include + +namespace matador::test { +template +struct capital_pk_generator; + +template +struct country_pk_generator { + unsigned int id{}; + std::string name; + object::object_ptr> capital; + + country_pk_generator() = default; + explicit country_pk_generator(std::string name) + : name(std::move(name)) {} + country_pk_generator(const unsigned int id, std::string name) + : id(id) + , name(std::move(name)) {} + + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id, PkAttribute); + field::attribute(op, "name", name, UniqueVarChar255); + field::has_one(op, "capital", capital, "country_id", CascadeAllFetchEager); + } +}; + +template +struct capital_pk_generator { + unsigned int id{}; + std::string name; + object::object_ptr> country; + + capital_pk_generator() = default; + capital_pk_generator(std::string name, object::object_ptr> country) + : name(std::move(name)) + , country(std::move(country)) {} + capital_pk_generator(const unsigned int id, std::string name, object::object_ptr> country) + : id(id) + , name(std::move(name)) + , country(std::move(country)) {} + + template + void process(Operator &op) { + namespace field = matador::access; + using namespace matador::utils; + field::primary_key(op, "id", id, PkAttribute); + field::attribute(op, "name", name, VarChar255); + field::belongs_to(op, "country_id", country, CascadeAllFetchLazy); + } +}; + +using country = country_pk_generator; +using country_identity = country_pk_generator; +using country_sequence = country_pk_generator; +using country_table = country_pk_generator; +using capital = capital_pk_generator; +using capital_identity = capital_pk_generator; +using capital_sequence = capital_pk_generator; +using capital_table = capital_pk_generator; +} + +#endif //MATADOR_COUNTRY_HPP diff --git a/test/orm/query/SessionQueryBuilderTest.cpp b/test/orm/query/SessionQueryBuilderTest.cpp index a42f764..61c0baf 100644 --- a/test/orm/query/SessionQueryBuilderTest.cpp +++ b/test/orm/query/SessionQueryBuilderTest.cpp @@ -24,6 +24,7 @@ #include "../../models/order.hpp" #include "../../models/student.hpp" #include "../../models/user.hpp" +#include "../../models/country.hpp" #include "../../models/model_metas.hpp" using namespace matador::object; @@ -386,7 +387,7 @@ TEST_CASE("Test eager relationship", "[query][entity][builder]") { // std::cout << ctx << std::endl; } -TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") { +TEST_CASE("Test has one lazy relationship", "[query][entity][builder][has_one][lazy]") { using namespace matador::test; backend_provider::instance().register_backend("noop", std::make_unique()); connection db("noop://noop.db"); @@ -401,5 +402,27 @@ TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") { auto q = eqb.build(); REQUIRE(q.is_ok()); const auto sql = q->str(db); + const std::string expected_sql = R"(SELECT "t01"."id", "t01"."name", "t01"."password" FROM "users" "t01" ORDER BY "t01"."id" ASC)"; + REQUIRE(expected_sql == sql); +} + +TEST_CASE("Test has one eager relationship", "[query][entity][builder][has_one][eager]") { + using namespace matador::test; + backend_provider::instance().register_backend("noop", std::make_unique()); + connection db("noop://noop.db"); + + schema scm; + auto result = scm.attach("countries") + .and_then( [&scm] { return scm.attach("capitals"); } ); + REQUIRE(result); + + select_query_builder eqb(scm); + + auto q = eqb.build(); + REQUIRE(q.is_ok()); + const auto sql = q->str(db); + const std::string expected_sql = R"(SELECT "t01"."id", "t01"."name", "t02"."id", "t02"."name", "t02"."country_id" FROM "countries" "t01" LEFT JOIN "capitals" "t02" ON "t01"."id" = "t02"."country_id" ORDER BY "t01"."id" ASC)"; + + REQUIRE(expected_sql == sql); } \ No newline at end of file