added has_one eager functionality and tests
This commit is contained in:
parent
8c8423bf64
commit
caacdfa34a
|
|
@ -112,13 +112,67 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
void on_belongs_to(const char *id, Pointer &/*obj*/, const utils::foreign_attributes &attr) {
|
||||||
on_foreign_object(id, obj, attr, true);
|
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<class Pointer>
|
template<class Pointer>
|
||||||
void on_has_one(const char *id, Pointer &obj, const char * /*join_column*/, const utils::foreign_attributes &attr) {
|
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);
|
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<typename T>
|
template<typename T>
|
||||||
|
|
@ -257,8 +311,6 @@ public:
|
||||||
[[nodiscard]] const select_query_data &query_data() const;
|
[[nodiscard]] const select_query_data &query_data() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<class Pointer>
|
|
||||||
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);
|
void push(const std::string &column_name);
|
||||||
static std::string build_alias(char prefix, unsigned int count);
|
static std::string build_alias(char prefix, unsigned int count);
|
||||||
[[nodiscard]] bool is_root_entity() const;
|
[[nodiscard]] bool is_root_entity() const;
|
||||||
|
|
@ -278,39 +330,5 @@ private:
|
||||||
unsigned int table_index{0};
|
unsigned int table_index{0};
|
||||||
object::join_columns_collector join_columns_collector_{};
|
object::join_columns_collector join_columns_collector_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Pointer>
|
|
||||||
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
|
#endif //QUERY_ENTITY_QUERY_BUILDER_HPP
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,13 @@
|
||||||
#include "matador/query/session.hpp"
|
#include "matador/query/session.hpp"
|
||||||
|
|
||||||
#include "models/user.hpp"
|
#include "models/user.hpp"
|
||||||
|
#include "models/country.hpp"
|
||||||
|
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
using namespace matador::query;
|
using namespace matador::query;
|
||||||
using namespace matador::object;
|
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<user_identity>("users")
|
const auto result = schema.attach<user_identity>("users")
|
||||||
.and_then( [this] { return schema.attach<user_session_identity>("user_sessions"); } )
|
.and_then( [this] { return schema.attach<user_session_identity>("user_sessions"); } )
|
||||||
.and_then([this] { return schema.create(db); } );
|
.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());
|
REQUIRE(user_result.is_ok());
|
||||||
us = user_result.value()->session;
|
us = user_result.value()->session;
|
||||||
REQUIRE(us->session_token == "session1");
|
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<country_identity>("countries")
|
||||||
|
.and_then( [this] { return schema.attach<capital_identity>("capitals"); } )
|
||||||
|
.and_then([this] { return schema.create(db); } );
|
||||||
|
REQUIRE(result.is_ok());
|
||||||
|
|
||||||
|
session ses({bus, connection::dns, 4}, schema);
|
||||||
|
|
||||||
|
const auto ger = make_object<country_identity>("Germany");
|
||||||
|
auto ger_cap = make_object<capital_identity>("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<country_identity>(ger->id);
|
||||||
|
REQUIRE(ger_result.is_ok());
|
||||||
|
ger_cap = ger_result.value()->capital;
|
||||||
|
REQUIRE(ger_cap->name == "Berlin");
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
#ifndef MATADOR_COUNTRY_HPP
|
||||||
|
#define MATADOR_COUNTRY_HPP
|
||||||
|
|
||||||
|
#include "matador/utils/access.hpp"
|
||||||
|
|
||||||
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace matador::test {
|
||||||
|
template<const utils::primary_key_attribute &PkAttribute>
|
||||||
|
struct capital_pk_generator;
|
||||||
|
|
||||||
|
template<const utils::primary_key_attribute &PkAttribute>
|
||||||
|
struct country_pk_generator {
|
||||||
|
unsigned int id{};
|
||||||
|
std::string name;
|
||||||
|
object::object_ptr<capital_pk_generator<PkAttribute>> 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<class Operator>
|
||||||
|
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<const utils::primary_key_attribute &PkAttribute>
|
||||||
|
struct capital_pk_generator {
|
||||||
|
unsigned int id{};
|
||||||
|
std::string name;
|
||||||
|
object::object_ptr<country_pk_generator<PkAttribute>> country;
|
||||||
|
|
||||||
|
capital_pk_generator() = default;
|
||||||
|
capital_pk_generator(std::string name, object::object_ptr<country_pk_generator<PkAttribute>> country)
|
||||||
|
: name(std::move(name))
|
||||||
|
, country(std::move(country)) {}
|
||||||
|
capital_pk_generator(const unsigned int id, std::string name, object::object_ptr<country_pk_generator<PkAttribute>> country)
|
||||||
|
: id(id)
|
||||||
|
, name(std::move(name))
|
||||||
|
, country(std::move(country)) {}
|
||||||
|
|
||||||
|
template<class Operator>
|
||||||
|
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<utils::Manual>;
|
||||||
|
using country_identity = country_pk_generator<utils::Identity>;
|
||||||
|
using country_sequence = country_pk_generator<utils::Sequence>;
|
||||||
|
using country_table = country_pk_generator<utils::Table>;
|
||||||
|
using capital = capital_pk_generator<utils::Manual>;
|
||||||
|
using capital_identity = capital_pk_generator<utils::Identity>;
|
||||||
|
using capital_sequence = capital_pk_generator<utils::Sequence>;
|
||||||
|
using capital_table = capital_pk_generator<utils::Table>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //MATADOR_COUNTRY_HPP
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
#include "../../models/order.hpp"
|
#include "../../models/order.hpp"
|
||||||
#include "../../models/student.hpp"
|
#include "../../models/student.hpp"
|
||||||
#include "../../models/user.hpp"
|
#include "../../models/user.hpp"
|
||||||
|
#include "../../models/country.hpp"
|
||||||
#include "../../models/model_metas.hpp"
|
#include "../../models/model_metas.hpp"
|
||||||
|
|
||||||
using namespace matador::object;
|
using namespace matador::object;
|
||||||
|
|
@ -386,7 +387,7 @@ TEST_CASE("Test eager relationship", "[query][entity][builder]") {
|
||||||
// std::cout << ctx << std::endl;
|
// 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;
|
using namespace matador::test;
|
||||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||||
connection db("noop://noop.db");
|
connection db("noop://noop.db");
|
||||||
|
|
@ -401,5 +402,27 @@ TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") {
|
||||||
auto q = eqb.build<user>();
|
auto q = eqb.build<user>();
|
||||||
REQUIRE(q.is_ok());
|
REQUIRE(q.is_ok());
|
||||||
const auto sql = q->str(db);
|
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<orm::test_backend_service>());
|
||||||
|
connection db("noop://noop.db");
|
||||||
|
|
||||||
|
schema scm;
|
||||||
|
auto result = scm.attach<country>("countries")
|
||||||
|
.and_then( [&scm] { return scm.attach<capital>("capitals"); } );
|
||||||
|
REQUIRE(result);
|
||||||
|
|
||||||
|
select_query_builder eqb(scm);
|
||||||
|
|
||||||
|
auto q = eqb.build<country>();
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue