handle many-to-many relations separately when process entities
This commit is contained in:
+108
-18
@@ -1,33 +1,123 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <matador/sql/connection.hpp>
|
||||
#include <matador/sql/dialect.hpp>
|
||||
#include <matador/sql/entity_query_builder.hpp>
|
||||
|
||||
#include "models/author.hpp"
|
||||
#include "models/book.hpp"
|
||||
|
||||
struct schiff {
|
||||
std::string id;
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op)
|
||||
{
|
||||
namespace field = matador::utils::access;
|
||||
field::primary_key(op, "id", id, 255);
|
||||
}
|
||||
};
|
||||
#include "models/recipe.hpp"
|
||||
#include "models/order.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create sql query for entity", "[query][entity][builder]") {
|
||||
connection noop("noop://noop.db");
|
||||
TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") {
|
||||
using namespace matador::test;
|
||||
connection db("noop://noop.db");
|
||||
schema scm("noop");
|
||||
scm.attach<matador::test::author>("authors");
|
||||
scm.attach<matador::test::book>("books");
|
||||
scm.attach<author>("authors");
|
||||
scm.attach<book>("books");
|
||||
|
||||
entity_query_builder eqb(17, scm);
|
||||
|
||||
auto context = eqb.build<matador::test::book>(noop);
|
||||
std::cout << "SQL: " << context.value().sql << "\n";
|
||||
context = eqb.build<schiff>(noop);
|
||||
auto data = eqb.build<book>();
|
||||
|
||||
REQUIRE(data.has_value());
|
||||
REQUIRE(data->root_table_name == "books");
|
||||
REQUIRE(data->joins.size() == 1);
|
||||
REQUIRE(data->columns.size() == 9);
|
||||
REQUIRE(data->where_clause);
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||
{ "books", "books.author_id = authors.id"}
|
||||
};
|
||||
|
||||
query_context qc;
|
||||
size_t index{0};
|
||||
for (const auto &jd : data->joins) {
|
||||
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
||||
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
||||
++index;
|
||||
}
|
||||
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == "books.id = 17");
|
||||
|
||||
auto &jd = data->joins.front();
|
||||
|
||||
auto context = db.query(scm)
|
||||
.select(data->columns)
|
||||
.from(data->root_table_name)
|
||||
.join_left(jd.join_table).on(std::move(jd.condition))
|
||||
.where(std::move(data->where_clause))
|
||||
.build();
|
||||
}
|
||||
|
||||
TEST_CASE("Create sql query data for entity with eager has many belongs to", "[query][entity][builder]") {
|
||||
using namespace matador::test;
|
||||
connection db("noop://noop.db");
|
||||
schema scm("noop");
|
||||
scm.attach<product>("products");
|
||||
scm.attach<order_details>("order_details");
|
||||
scm.attach<order>("orders");
|
||||
|
||||
entity_query_builder eqb(17, scm);
|
||||
|
||||
auto data = eqb.build<order>();
|
||||
|
||||
REQUIRE(data.has_value());
|
||||
REQUIRE(data->root_table_name == "orders");
|
||||
REQUIRE(data->joins.size() == 1);
|
||||
REQUIRE(data->columns.size() == 15);
|
||||
REQUIRE(data->where_clause);
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||
{ "orders", "orders.order_id = order_details.order_id"}
|
||||
};
|
||||
|
||||
query_context qc;
|
||||
size_t index{0};
|
||||
for (const auto &jd : data->joins) {
|
||||
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
||||
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
||||
++index;
|
||||
}
|
||||
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == "orders.order_id = 17");
|
||||
}
|
||||
|
||||
TEST_CASE("Create sql query data for entity with eager many to many", "[query][entity][builder]") {
|
||||
using namespace matador::test;
|
||||
connection db("noop://noop.db");
|
||||
schema scm("noop");
|
||||
scm.attach<recipe>("recipes");
|
||||
scm.attach<ingredient>("ingredients");
|
||||
scm.attach<recipe_ingredient>("recipe_ingredients");
|
||||
|
||||
entity_query_builder eqb(17, scm);
|
||||
|
||||
auto data = eqb.build<ingredient>();
|
||||
|
||||
REQUIRE(data.has_value());
|
||||
REQUIRE(data->root_table_name == "ingredients");
|
||||
REQUIRE(data->joins.size() == 2);
|
||||
REQUIRE(data->columns.size() == 4);
|
||||
REQUIRE(data->where_clause);
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||
{ "ingredients", "ingredients.id = recipe_ingredients.ingredient_id"},
|
||||
{ "recipes", "recipes.id = recipe_ingredients.recipe_id"}
|
||||
};
|
||||
|
||||
query_context qc;
|
||||
size_t index{0};
|
||||
for (const auto &jd : data->joins) {
|
||||
REQUIRE(jd.join_table.name == expected_join_data[index].first);
|
||||
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
|
||||
++index;
|
||||
}
|
||||
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == "ingredients.id = 17");
|
||||
}
|
||||
@@ -3,10 +3,14 @@
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/sql/entity.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct book;
|
||||
|
||||
struct author
|
||||
{
|
||||
unsigned long id{};
|
||||
@@ -27,7 +31,7 @@ struct author
|
||||
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", "id", utils::fetch_type::LAZY);
|
||||
field::has_many(op, books, "author_id", utils::fetch_type::LAZY);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ struct order
|
||||
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_, utils::fetch_type::EAGER);
|
||||
field::has_many(op, order_details_, "order_id", utils::fetch_type::EAGER);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+7
-12
@@ -6,6 +6,7 @@
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
#include "matador/sql/entity.hpp"
|
||||
#include "matador/sql/has_many_to_many_relation.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
@@ -23,7 +24,7 @@ struct ingredient
|
||||
namespace field = matador::utils::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::has_many(op, "recipes", recipes, "ingredient_id", "recipe_id", utils::fetch_type::EAGER);
|
||||
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::fetch_type::EAGER);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -38,21 +39,15 @@ struct recipe
|
||||
namespace field = matador::utils::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::has_many(op, "ingredients", ingredients, "recipe_id", "ingredient_id", utils::fetch_type::EAGER);
|
||||
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::fetch_type::LAZY);
|
||||
}
|
||||
};
|
||||
|
||||
struct recipe_ingredient
|
||||
class recipe_ingredient : public sql::has_many_to_many_relation<recipe, ingredient>
|
||||
{
|
||||
sql::entity<recipe> recipe_;
|
||||
sql::entity<ingredient> ingredient_;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::utils::access;
|
||||
field::belongs_to(op, "recipe_id", recipe_, utils::default_foreign_attributes);
|
||||
field::belongs_to(op, "ingredient_id", ingredient_, utils::default_foreign_attributes);
|
||||
}
|
||||
public:
|
||||
recipe_ingredient()
|
||||
: has_many_to_many_relation("recipe_id", "ingredient_id") {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user