359 lines
12 KiB
C++
359 lines
12 KiB
C++
#include <iostream>
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "matador/sql/backend_provider.hpp"
|
|
#include "matador/sql/connection.hpp"
|
|
#include "matador/sql/connection_pool.hpp"
|
|
#include "matador/sql/interface/connection_impl.hpp"
|
|
|
|
#include "matador/query/criteria_evaluator.hpp"
|
|
#include "matador/query/query.hpp"
|
|
#include "matador/query/table_column.hpp"
|
|
#include "matador/query/table.hpp"
|
|
#include "matador/query/schema.hpp"
|
|
|
|
#include "matador/query/select_query_builder.hpp"
|
|
|
|
#include "../backend/test_backend_service.hpp"
|
|
|
|
#include "../../models/airplane.hpp"
|
|
#include "../../models/author.hpp"
|
|
#include "../../models/department.hpp"
|
|
#include "../../models/book.hpp"
|
|
#include "../../models/flight.hpp"
|
|
#include "../../models/recipe.hpp"
|
|
#include "../../models/order.hpp"
|
|
#include "../../models/student.hpp"
|
|
#include "../../models/model_metas.hpp"
|
|
|
|
using namespace matador::object;
|
|
using namespace matador::query;
|
|
using namespace matador::utils;
|
|
using namespace matador::sql;
|
|
using namespace matador::test;
|
|
using namespace matador::query::meta;
|
|
|
|
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
connection db("noop://noop.db");
|
|
connection_pool pool("noop://noop.db", 4);
|
|
|
|
schema scm;
|
|
auto result = scm.attach<airplane>("airplanes")
|
|
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
|
|
REQUIRE(result);
|
|
|
|
select_query_builder eqb(scm, db);
|
|
|
|
const auto it = scm.find(typeid(flight));
|
|
REQUIRE(it != scm.end());
|
|
const auto* col = it->second.table()["id"];
|
|
REQUIRE(col);
|
|
auto data = eqb.build<flight>(*col == _);
|
|
|
|
REQUIRE(data.is_ok());
|
|
REQUIRE(data->root_table->table_name() == "flights");
|
|
REQUIRE(data->joins.size() == 1);
|
|
const auto flights = table("flights").as("t01");
|
|
const auto airplanes = table("airplanes").as("t02");
|
|
const std::vector<table_column> expected_columns {
|
|
{ &flights, "id", "c01" },
|
|
{ &airplanes, "id", "c02" },
|
|
{ &airplanes, "brand", "c03" },
|
|
{ &airplanes, "model", "c04" },
|
|
{ &flights, "pilot_name", "c05" },
|
|
};
|
|
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 {
|
|
{ "airplanes", R"("t01"."airplane_id" = "t02"."id")"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
criteria_evaluator evaluator(db.dialect(), qc);
|
|
for (const auto &[join_table, condition] : data->joins) {
|
|
REQUIRE(join_table->table_name() == expected_join_data[index].first);
|
|
REQUIRE(evaluator.evaluate(*condition) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
REQUIRE(data->where_clause.get());
|
|
auto cond = evaluator.evaluate(*data->where_clause);
|
|
REQUIRE(cond == R"("t01"."id" = ?)");
|
|
}
|
|
|
|
TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
connection db("noop://noop.db");
|
|
connection_pool pool("noop://noop.db", 4);
|
|
|
|
schema scm;
|
|
auto result = scm.attach<author>("authors")
|
|
.and_then( [&scm] { return scm.attach<book>("books"); } );
|
|
REQUIRE(result);
|
|
|
|
select_query_builder eqb(scm, db);
|
|
|
|
const auto it = scm.find(typeid(book));
|
|
REQUIRE(it != scm.end());
|
|
const auto* col = it->second.table()["id"];
|
|
REQUIRE(col);
|
|
auto data = eqb.build<book>(*col == _);
|
|
|
|
REQUIRE(data.is_ok());
|
|
REQUIRE(data->root_table->table_name() == "books");
|
|
REQUIRE(data->joins.size() == 1);
|
|
const auto books = table("books").as("t01");
|
|
const auto authors = table("authors").as("t02");
|
|
const std::vector<table_column> expected_columns {
|
|
{ &books, "id", "c01" },
|
|
{ &books, "title", "c02" },
|
|
{ &authors, "id", "c03" },
|
|
{ &authors, "first_name", "c04" },
|
|
{ &authors, "last_name", "c05" },
|
|
{ &authors, "date_of_birth", "c06" },
|
|
{ &authors, "year_of_birth", "c07" },
|
|
{ &authors, "distinguished", "c08" },
|
|
{ &books, "published_in", "c09" }
|
|
};
|
|
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 {
|
|
{ "authors", R"("t01"."author_id" = "t02"."id")"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
criteria_evaluator evaluator(db.dialect(), qc);
|
|
for (const auto & [join_table, clause] : data->joins) {
|
|
REQUIRE(join_table->table_name() == expected_join_data[index].first);
|
|
REQUIRE(evaluator.evaluate(*clause) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
REQUIRE(data->where_clause.get());
|
|
auto cond = evaluator.evaluate(*data->where_clause);
|
|
REQUIRE(cond == R"("t01"."id" = ?)");
|
|
|
|
auto q = matador::query::query::select(data->columns)
|
|
.from(data->root_table->name());
|
|
|
|
for (auto &jd : data->joins) {
|
|
q.join_left(*jd.join_table)
|
|
.on(std::move(jd.condition));
|
|
}
|
|
auto context = q
|
|
.where(std::move(data->where_clause))
|
|
.str(db);
|
|
}
|
|
|
|
TEST_CASE("Create sql query data for entity with eager has many belongs to", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
connection db("noop://noop.db");
|
|
connection_pool pool("noop://noop.db", 4);
|
|
|
|
schema scm;
|
|
auto result = scm.attach<product>("products")
|
|
.and_then( [&scm] { return scm.attach<order_details>("order_details"); } )
|
|
.and_then( [&scm] { return scm.attach<supplier>("suppliers"); } )
|
|
.and_then( [&scm] { return scm.attach<category>("categories"); } )
|
|
.and_then( [&scm] { return scm.attach<order>("orders"); } );
|
|
REQUIRE(result);
|
|
|
|
select_query_builder eqb(scm, db);
|
|
|
|
const auto it = scm.find(typeid(order));
|
|
REQUIRE(it != scm.end());
|
|
const auto* col = it->second.table()["order_id"];
|
|
REQUIRE(col);
|
|
auto data = eqb.build<order>(*col == _);
|
|
|
|
REQUIRE(data.is_ok());
|
|
REQUIRE(data->root_table->table_name() == "orders");
|
|
REQUIRE(data->joins.size() == 1);
|
|
const auto orders = table("orders").as("t01");
|
|
const auto order_details = table("order_details").as("t02");
|
|
const std::vector<table_column> expected_columns = {
|
|
{ &orders, "order_id", "c01" },
|
|
{ &orders, "order_date", "c02" },
|
|
{ &orders, "required_date", "c03" },
|
|
{ &orders, "shipped_date", "c04" },
|
|
{ &orders, "ship_via", "c05" },
|
|
{ &orders, "freight", "c06" },
|
|
{ &orders, "ship_name", "c07" },
|
|
{ &orders, "ship_address", "c08" },
|
|
{ &orders, "ship_city", "c09" },
|
|
{ &orders, "ship_region", "c10" },
|
|
{ &orders, "ship_postal_code", "c11" },
|
|
{ &orders, "ship_country", "c12" },
|
|
{ &order_details, "order_details_id", "c13" },
|
|
{ &order_details, "order_id", "c14" },
|
|
{ &order_details, "product_id", "c15" }
|
|
};
|
|
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 {
|
|
{ "order_details", R"("t01"."order_id" = "t02"."order_id")"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
criteria_evaluator evaluator(db.dialect(), qc);
|
|
for (const auto &jd : data->joins) {
|
|
REQUIRE(jd.join_table->table_name() == expected_join_data[index].first);
|
|
REQUIRE(evaluator.evaluate(*jd.condition) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
REQUIRE(data->where_clause.get());
|
|
auto cond = evaluator.evaluate(*data->where_clause);
|
|
REQUIRE(cond == R"("t01"."order_id" = ?)");
|
|
}
|
|
|
|
TEST_CASE("Create sql query data for entity with eager many to many", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
connection db("noop://noop.db");
|
|
connection_pool pool("noop://noop.db", 4);
|
|
|
|
schema scm;
|
|
auto result = scm.attach<recipe>("recipes")
|
|
.and_then( [&scm] { return scm.attach<ingredient>("ingredients"); } );
|
|
REQUIRE(result);
|
|
|
|
select_query_builder eqb(scm, db);
|
|
|
|
const auto it = scm.find(typeid(ingredient));
|
|
REQUIRE(it != scm.end());
|
|
const auto* col = it->second.table()["id"];
|
|
REQUIRE(col);
|
|
auto data = eqb.build<ingredient>(*col == _);
|
|
|
|
REQUIRE(data.is_ok());
|
|
REQUIRE(data->root_table->table_name() == "ingredients");
|
|
REQUIRE(data->joins.size() == 2);
|
|
const auto ingredients = table("ingredients").as("t01");
|
|
const auto recipes = table("recipes").as("t03");
|
|
const std::vector<table_column> expected_columns {
|
|
{ &ingredients, "id", "c01" },
|
|
{ &ingredients, "name", "c02" },
|
|
{ &recipes, "id", "c03" },
|
|
{ &recipes, "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 {
|
|
{ "recipe_ingredients", R"("t01"."id" = "t02"."ingredient_id")"},
|
|
{ "recipes", R"("t02"."recipe_id" = "t03"."id")"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
criteria_evaluator evaluator(db.dialect(), qc);
|
|
for (const auto &jd : data->joins) {
|
|
REQUIRE(jd.join_table->table_name() == expected_join_data[index].first);
|
|
REQUIRE(evaluator.evaluate(*jd.condition) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
REQUIRE(data->where_clause.get());
|
|
auto cond = evaluator.evaluate(*data->where_clause);
|
|
REQUIRE(cond == R"("t01"."id" = ?)");
|
|
}
|
|
|
|
TEST_CASE("Create sql query data for entity with eager many to many (inverse part)", "[query][entity][builder]") {
|
|
using namespace matador::test;
|
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
connection db("noop://noop.db");
|
|
connection_pool pool("noop://noop.db", 4);
|
|
|
|
schema scm;
|
|
auto result = scm.attach<student>("students")
|
|
.and_then( [&scm] { return scm.attach<course>("courses"); } );
|
|
REQUIRE(result);
|
|
|
|
select_query_builder eqb(scm, db);
|
|
|
|
const auto it = scm.find(typeid(course));
|
|
REQUIRE(it != scm.end());
|
|
const auto* col = it->second.table()["id"];
|
|
REQUIRE(col);
|
|
auto data = eqb.build<course>(*col == _);
|
|
|
|
REQUIRE(data.is_ok());
|
|
REQUIRE(data->root_table->table_name() == "courses");
|
|
REQUIRE(data->joins.size() == 2);
|
|
const auto courses = table("courses").as("t01");
|
|
const auto students = table("students").as("t03");
|
|
const std::vector<table_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 {
|
|
{ "student_courses", R"("t01"."id" = "t02"."course_id")"},
|
|
{ "students", R"("t02"."student_id" = "t03"."id")"}
|
|
};
|
|
|
|
query_context qc;
|
|
size_t index{0};
|
|
criteria_evaluator evaluator(db.dialect(), qc);
|
|
for (const auto &jd : data->joins) {
|
|
REQUIRE(jd.join_table->table_name() == expected_join_data[index].first);
|
|
REQUIRE(evaluator.evaluate(*jd.condition) == expected_join_data[index].second);
|
|
++index;
|
|
}
|
|
|
|
REQUIRE(data->where_clause.get());
|
|
auto cond = evaluator.evaluate(*data->where_clause);
|
|
REQUIRE(cond == R"("t01"."id" = ?)");
|
|
}
|
|
|
|
TEST_CASE("Test eager relationship", "[session][eager]") {
|
|
using namespace matador::test;
|
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
connection db("noop://noop.db");
|
|
connection_pool pool("noop://noop.db", 4);
|
|
|
|
schema scm;
|
|
auto result = scm.attach<department>("departments")
|
|
.and_then( [&scm] { return scm.attach<employee>("employees"); } );
|
|
REQUIRE(result);
|
|
|
|
select_query_builder eqb(scm, db);
|
|
|
|
auto data = eqb.build<department>();
|
|
REQUIRE(data.is_ok());
|
|
|
|
auto ctx = query::select(data->columns)
|
|
.from(*data->root_table)
|
|
.join_left(data->joins)
|
|
.where(std::move(data->where_clause))
|
|
.order_by(table_column{data->root_table, data->pk_column_name})
|
|
.asc()
|
|
.str(db);
|
|
|
|
std::cout << ctx << std::endl;
|
|
} |