added join_column_collector and implemented inverse has_many_to_many
This commit is contained in:
+3
-2
@@ -19,7 +19,7 @@ add_executable(tests
|
||||
BackendProviderTest.cpp
|
||||
models/product.hpp
|
||||
models/order.hpp
|
||||
models/order_details.h
|
||||
models/order_details.hpp
|
||||
ColumnDefinitionGeneratorTest.cpp
|
||||
ColumnGeneratorTest.cpp
|
||||
ValueGeneratorTest.cpp
|
||||
@@ -42,7 +42,8 @@ add_executable(tests
|
||||
ValueTest.cpp
|
||||
ResultTest.cpp
|
||||
utils/auto_reset_event.hpp
|
||||
utils/auto_reset_event.cpp)
|
||||
utils/auto_reset_event.cpp
|
||||
models/student.hpp)
|
||||
|
||||
target_link_libraries(tests PRIVATE
|
||||
Catch2::Catch2WithMain
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "models/book.hpp"
|
||||
#include "models/recipe.hpp"
|
||||
#include "models/order.hpp"
|
||||
#include "models/student.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
@@ -165,4 +166,48 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
|
||||
REQUIRE(data->where_clause);
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == R"("ingredients"."id" = 17)");
|
||||
}
|
||||
|
||||
TEST_CASE("Create sql query data for entity with eager many to many (inverse part)", "[query][entity][builder]") {
|
||||
using namespace matador::test;
|
||||
connection db("noop://noop.db");
|
||||
schema scm("noop");
|
||||
scm.attach<student>("students");
|
||||
scm.attach<course>("courses");
|
||||
scm.attach<student_course>("student_courses");
|
||||
|
||||
entity_query_builder eqb(scm);
|
||||
|
||||
auto data = eqb.build<course>(17);
|
||||
|
||||
REQUIRE(data.is_ok());
|
||||
REQUIRE(data->root_table_name == "courses");
|
||||
REQUIRE(data->joins.size() == 2);
|
||||
const std::vector<column> expected_columns {
|
||||
{ "courses", "id", "c01" },
|
||||
{ "courses", "title", "c02" },
|
||||
{ "students", "id", "c03" },
|
||||
{ "students", "name", "c04" }
|
||||
};
|
||||
REQUIRE(data->columns.size() == expected_columns.size());
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i].equals(data->columns[i]));
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> expected_join_data {
|
||||
{ "courses", R"("courses"."id" = "student_courses"."course_id")"},
|
||||
{ "students", R"("students"."id" = "student_courses"."student_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;
|
||||
}
|
||||
|
||||
REQUIRE(data->where_clause);
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == R"("courses"."id" = 17)");
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef QUERY_ORDER_HPP
|
||||
#define QUERY_ORDER_HPP
|
||||
|
||||
#include "order_details.h"
|
||||
#include "order_details.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef QUERY_ORDER_DETAILS_H
|
||||
#define QUERY_ORDER_DETAILS_H
|
||||
#ifndef QUERY_ORDER_DETAILS_HPP
|
||||
#define QUERY_ORDER_DETAILS_HPP
|
||||
|
||||
#include "product.hpp"
|
||||
|
||||
@@ -25,4 +25,4 @@ struct order_details
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_ORDER_DETAILS_H
|
||||
#endif //QUERY_ORDER_DETAILS_HPP
|
||||
@@ -0,0 +1,68 @@
|
||||
#ifndef QUERY_STUDENT_HPP
|
||||
#define QUERY_STUDENT_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
#include "matador/sql/entity.hpp"
|
||||
#include "matador/sql/has_many_to_many_relation.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
class course;
|
||||
|
||||
struct student
|
||||
{
|
||||
unsigned long id{};
|
||||
std::string name;
|
||||
std::vector<matador::sql::entity<course>> courses;
|
||||
|
||||
student() = default;
|
||||
explicit student(unsigned long id, std::string name)
|
||||
: id(id)
|
||||
, name(std::move(name)) {}
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op)
|
||||
{
|
||||
namespace field = matador::utils::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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct course
|
||||
{
|
||||
unsigned long id{};
|
||||
std::string title;
|
||||
std::vector<matador::sql::entity<student>> students;
|
||||
|
||||
course() = default;
|
||||
explicit course(unsigned long id, std::string title)
|
||||
: id(id)
|
||||
, title(std::move(title)) {}
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op)
|
||||
{
|
||||
namespace field = matador::utils::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);
|
||||
}
|
||||
};
|
||||
|
||||
class student_course : public sql::has_many_to_many_relation<student, course>
|
||||
{
|
||||
public:
|
||||
student_course()
|
||||
: has_many_to_many_relation("student_id", "course_id") {}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_STUDENT_HPP
|
||||
Reference in New Issue
Block a user