#include "catch2/catch_test_macros.hpp" #include "matador/query/builder.hpp" #include "matador/query/criteria.hpp" #include "matador/query/generator.hpp" #include "matador/query/query.hpp" #include "matador/query/meta_table_macro.hpp" #include "QueryFixture.hpp" #include "models/airplane.hpp" #include "models/flight.hpp" #include "models/person.hpp" #include "models/recipe.hpp" #include "models/shipment.hpp" #include "../test/utils/record_printer.hpp" using namespace matador::object; using namespace matador::query; using namespace matador::sql; using namespace matador::test; META_TABLE(recipes, RECIPE, id, name); META_TABLE(ingredients, INGREDIENT, id, name); META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id); META_TABLE(airplanes, AIRPLANE, id, brand, model); META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name); META_TABLE(shipments, SHIPMENT, id, tracking_number) META_TABLE(packages, PACKAGE, id, weight, shipment) TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") { auto result = repo.attach("airplane") .and_then( [this] { return repo.attach("flight");} ); REQUIRE(result.is_ok()); auto obj = object_generator::generate(repo.repo(), "airplane"); auto res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); check_table_exists("airplane"); tables_to_drop.emplace("airplane"); obj = object_generator::generate(repo.repo(), "flight"); res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); check_table_exists("flight"); tables_to_drop.emplace("flight"); } TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[query][where]") { REQUIRE(repo.attach("person")); const auto obj = object_generator::generate(repo.repo(), "person"); auto res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); check_table_exists("person"); tables_to_drop.emplace("person"); person george{7, "george", 45}; george.image.emplace_back(37); res = query::insert() .into("person", generator::columns(repo)) .values(george) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); // fetch person as record auto result_record = query::select(generator::columns(repo)) .from("person") .where("id"_col == 7) .fetch_all(db); REQUIRE(result_record.is_ok()); for (const auto &i: *result_record) { REQUIRE(i.size() == 4); REQUIRE(i.at(0).name() == "person.id"); REQUIRE(i.at(0).is_integer()); REQUIRE(i.at(0).as() == george.id); REQUIRE(i.at(1).name() == "person.name"); REQUIRE(i.at(1).is_varchar()); REQUIRE(i.at(1).as() == george.name); REQUIRE(i.at(2).name() == "person.age"); REQUIRE(i.at(2).is_integer()); REQUIRE(i.at(2).as() == george.age); } // fetch person as person auto result_person = query::select(generator::columns(repo)) .from("person") .where("id"_col == 7) .fetch_all(db); REQUIRE(result_person.is_ok()); for (const auto &i: *result_person) { REQUIRE(i.id == 7); REQUIRE(i.name == "george"); REQUIRE(i.age == 45); } } TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") { auto res = query::create() .table("person") .columns({ column("id", matador::utils::basic_type::UInt32), column("name", matador::utils::basic_type::Varchar, 255), column("color", matador::utils::basic_type::Varchar, 63) }) .constraints({ constraint("PK_person").primary_key("id") }) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("person")); tables_to_drop.emplace("person"); res = query::insert() .into("person", {{"id", ""}, {"name", ""}, {"color", ""}}) .values({7, "george", "green"}) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); // fetch person as record auto result_record = query::select({"id", "name", "color"}) .from("person") .where("id"_col == 7) .fetch_all(db); REQUIRE(result_record.is_ok()); for (const auto &i: *result_record) { REQUIRE(i.size() == 3); REQUIRE(i.at(0).name() == "id"); REQUIRE(i.at(0).is_integer()); REQUIRE(i.at(0).as() == 7); REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).is_varchar()); REQUIRE(i.at(1).as() == "george"); REQUIRE(i.at(2).name() == "color"); REQUIRE(i.at(2).is_varchar()); REQUIRE(i.at(2).as() == "green"); } } TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][foreign]") { auto result = repo.attach("airplane") .and_then( [this] { return repo.attach("flight");} ); REQUIRE(result.is_ok()); auto obj = object_generator::generate(repo.repo(), "airplane"); auto res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("airplane")); tables_to_drop.emplace("airplane"); obj = object_generator::generate(repo.repo(), "flight"); res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("flight")); tables_to_drop.emplace("flight"); std::vector planes{ object_ptr(new airplane{1, "Airbus", "A380"}), object_ptr(new airplane{2, "Boeing", "707"}), object_ptr(new airplane{3, "Boeing", "747"}) }; for (const auto &plane: planes) { res = query::insert() .into("airplane", generator::columns(repo)) .values(*plane) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } auto count = query::select({count_all()}) .from("airplane") .fetch_value(db); REQUIRE(count.is_ok()); REQUIRE(*count == 3); flight f4711{4, planes.at(1), "hans"}; res = query::insert() .into("flight", generator::columns(repo)) .values(f4711) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); auto f = query::select(generator::columns(repo)) .from("flight") .fetch_one(db); REQUIRE(f.is_ok()); REQUIRE(f.value()->at(0).as() == 4); REQUIRE(f.value()->at(1).as() == 2); REQUIRE(f.value()->at(2).as() == "hans"); } TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[query][foreign][join_left]") { auto result = repo.attach("airplanes") .and_then( [this] { return repo.attach("flights");} ); REQUIRE(result.is_ok()); auto obj = object_generator::generate(repo.repo(), "airplanes"); auto res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("airplanes")); tables_to_drop.emplace("airplanes"); obj = object_generator::generate(repo.repo(), "flights"); res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("flights")); tables_to_drop.emplace("flights"); std::vector planes{ object_ptr(new airplane{1, "Airbus", "A380"}), object_ptr(new airplane{2, "Boeing", "707"}), object_ptr(new airplane{3, "Boeing", "747"}) }; for (const auto &plane: planes) { res = query::insert() .into("airplanes", generator::columns(repo)) .values(*plane) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } auto count = query::select({count_all()}) .from("airplanes") .fetch_value(db).value(); REQUIRE(count == 3); std::vector flights{ object_ptr(new flight{4, planes.at(0), "hans"}), object_ptr(new flight{5, planes.at(0), "otto"}), object_ptr(new flight{6, planes.at(1), "george"}), object_ptr(new flight{7, planes.at(2), "paul"}) }; for (const auto &f: flights) { res = query::insert() .into("flights", {"id", "airplane_id", "pilot_name"}) .values(*f) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } auto flight_result = query::select(generator::columns(repo)) .from("flights") .fetch_one(db); REQUIRE(flight_result.is_ok()); REQUIRE(flight_result->has_value()); REQUIRE(flight_result.value()->at(0).as() == 4); REQUIRE(flight_result.value()->at(1).as() == 1); REQUIRE(flight_result.value()->at(2).as() == "hans"); using namespace matador::query::meta; const auto f = FLIGHT.as("f"); const auto ap = AIRPLANE.as("ap"); auto select_result = query::select({f.id, ap.brand, ap.model, f.pilot_name}) .from(f) .join_left(ap) .on(f.airplane_id == ap.id) .order_by(f.id).asc() .fetch_all(db); REQUIRE(select_result.is_ok()); std::vector> expected_result { {4, "hans"}, {5, "otto"}, {6, "george"}, {7, "paul"} }; size_t index{0}; for (const auto &r: *select_result) { REQUIRE(r.size() == 4); REQUIRE(r.at(0).as() == expected_result[index].first); REQUIRE(r.at(3).as() == expected_result[index++].second); } } TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single entity", "[query][join_left][find]") { auto result = repo.attach("airplanes") .and_then( [this] { return repo.attach("flights");} ); REQUIRE(result.is_ok()); auto obj = object_generator::generate(repo.repo(), "airplanes"); auto res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("airplanes")); tables_to_drop.emplace("airplanes"); obj = object_generator::generate(repo.repo(), "flights"); res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("flights")); tables_to_drop.emplace("flights"); std::vector planes{ object_ptr(new airplane{1, "Airbus", "A380"}), object_ptr(new airplane{2, "Boeing", "707"}), object_ptr(new airplane{3, "Boeing", "747"}) }; for (const auto &plane: planes) { res = query::insert() .into("airplanes", generator::columns(repo)) .values(*plane) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } auto count = query::select({count_all()}) .from("airplanes") .fetch_value(db); REQUIRE(count.is_ok()); REQUIRE(count->has_value()); REQUIRE(count->value() == 3); std::vector flights{ object_ptr(new flight{4, planes.at(0), "hans"}), object_ptr(new flight{5, planes.at(0), "otto"}), object_ptr(new flight{6, planes.at(1), "george"}), object_ptr(new flight{7, planes.at(2), "paul"}) }; for (const auto &f: flights) { res = query::insert() .into("flights", generator::columns(repo)) .values(*f) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } auto flight_result = query::select(generator::columns(repo)) .from("flights") .fetch_one(db); REQUIRE(flight_result.is_ok()); REQUIRE(flight_result->has_value()); REQUIRE(flight_result.value()->at(0).as() == 4); REQUIRE(flight_result.value()->at(1).as() == 1); REQUIRE(flight_result.value()->at(2).as() == "hans"); using namespace matador::query::meta; const auto f = FLIGHT.as("f"); const auto ap = AIRPLANE.as("ap"); auto select_result = query::select({f.id, f.airplane_id, ap.brand, ap.model, f.pilot_name}) .from(f) .join_left(ap) .on(f.airplane_id == ap.id) .where(f.id == 4) .fetch_one(db); auto expected_flight = flights[0]; REQUIRE(select_result.is_ok()); REQUIRE(*select_result); auto f1 = select_result.release(); REQUIRE(f1->id == expected_flight->id); REQUIRE(f1->pilot_name == expected_flight->pilot_name); REQUIRE(f1->plane.get()); REQUIRE(f1->plane->id == 1); REQUIRE(f1->plane->model == "A380"); REQUIRE(f1->plane->brand == "Airbus"); } TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[query][join][many_to_many]") { auto result = repo.attach("ingredients") .and_then( [this] { return repo.attach("recipes"); } ); auto obj = object_generator::generate(repo.repo(), "recipes"); auto res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("recipes")); tables_to_drop.emplace("recipes"); obj = object_generator::generate(repo.repo(), "ingredients"); res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("ingredients")); tables_to_drop.emplace("ingredients"); const auto it = repo.find("recipe_ingredients"); REQUIRE(it != repo.end()); obj = it->second.node().info().object(); // obj = object_generator::generate(repo.repo(), "recipe_ingredients"); res = query::create() .table(obj->name()) .columns(obj->attributes()) .constraints(obj->constraints()) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 0); REQUIRE(db.exists("recipe_ingredients")); tables_to_drop.emplace("recipe_ingredients"); std::vector ingredients { {1, "Apple"}, {2, "Strawberry"}, {3, "Pineapple"}, {4, "Sugar"}, {5, "Flour"}, {6, "Butter"}, {7, "Beans"} }; for (const auto &i: ingredients) { res = query::insert() .into("ingredients", generator::columns(repo)) .values(i) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } std::vector recipes{ {7, "Apple Crumble"}, {8, "Beans Chili"}, {9, "Fruit Salad"} }; for (const auto &r: recipes) { res = query::insert() .into("recipes", generator::columns(repo)) .values(r) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } std::vector> recipe_ingredients { { 7, 1 }, { 7, 4 }, { 7, 5 }, { 8, 6 }, { 8, 7 }, { 9, 1 }, { 9, 2 }, { 9, 3 } }; using namespace matador::query::meta; for (const auto & [recipe_id, ingredient_id]: recipe_ingredients) { res = query::insert() .into("recipe_ingredients", RECIPE_INGREDIENT) .values({recipe_id, ingredient_id}) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } const auto r = RECIPE.as("r"); const auto ri= RECIPE_INGREDIENT.as("ri"); const auto i = INGREDIENT.as("i"); auto select_result = query::select({ r.id, r.name, ri.ingredient_id}) .from(r) .join_left(ri) .on(r.id == ri.recipe_id) .fetch_all(db); REQUIRE(select_result.is_ok()); std::vector> expected_result_one_join { {7, "Apple Crumble", 1}, {7, "Apple Crumble", 4}, {7, "Apple Crumble", 5}, {8, "Beans Chili", 6}, {8, "Beans Chili", 7}, {9, "Fruit Salad", 1}, {9, "Fruit Salad", 2}, {9, "Fruit Salad", 3} }; size_t index{0}; for (const auto &row: *select_result) { REQUIRE(row.size() == 3); REQUIRE(row.at(0).as().value() == std::get<0>(expected_result_one_join[index])); REQUIRE(row.at(1).as().value() == std::get<1>(expected_result_one_join[index])); REQUIRE(row.at(2).as().value() == std::get<2>(expected_result_one_join[index])); ++index; } select_result = query::select({r.id, r.name, ri.ingredient_id, i.name}) .from(r) .join_left(ri) .on(r.id == ri.recipe_id) .join_left(i) .on(ri.ingredient_id == i.id) .fetch_all(db); REQUIRE(result.is_ok()); std::vector> expected_result_two_joins { {7, "Apple Crumble", 1, "Apple"}, {7, "Apple Crumble", 4, "Sugar"}, {7, "Apple Crumble", 5, "Flour"}, {8, "Beans Chili", 6, "Butter"}, {8, "Beans Chili", 7, "Beans"}, {9, "Fruit Salad", 1, "Apple"}, {9, "Fruit Salad", 2, "Strawberry"}, {9, "Fruit Salad", 3, "Pineapple"} }; index = 0; for (const auto &row: *select_result) { REQUIRE(row.size() == 4); REQUIRE(row.at(0).as().value() == std::get<0>(expected_result_two_joins[index])); REQUIRE(row.at(1).as().value() == std::get<1>(expected_result_two_joins[index])); REQUIRE(row.at(2).as().value() == std::get<2>(expected_result_two_joins[index])); REQUIRE(row.at(3).as().value() == std::get<3>(expected_result_two_joins[index])); ++index; } select_result = query::select({r.id, r.name, ri.ingredient_id, i.name}) .from(r) .join_left(ri) .on(r.id == ri.recipe_id) .join_left(i) .on(ri.ingredient_id == i.id) .where(r.id == 8) .fetch_all(db); REQUIRE(result.is_ok()); index = 3; for (const auto &row: *select_result) { REQUIRE(row.size() == 4); REQUIRE(row.at(0).as().value() == std::get<0>(expected_result_two_joins[index])); REQUIRE(row.at(1).as().value() == std::get<1>(expected_result_two_joins[index])); REQUIRE(row.at(2).as().value() == std::get<2>(expected_result_two_joins[index])); REQUIRE(row.at(3).as().value() == std::get<3>(expected_result_two_joins[index])); ++index; } } void print(std::ostream& out, const record& row) { for (const auto& f : row) { out << f << " "; } } TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") { auto result = repo.attach("packages") .and_then( [this] { return repo.attach("shipments"); } ); // auto result = repo.attach("shipments") // .and_then( [this] { return repo.attach("packages"); } ); REQUIRE(result.is_ok()); tables_to_drop.emplace("shipments"); tables_to_drop.emplace("packages"); result = repo.create(db); REQUIRE(result.is_ok()); const std::vector shipments { object_ptr{new shipment{1, "4711"}}, object_ptr{new shipment{2, "0815"}} }; using namespace matador::query::meta; for (const auto &sh: shipments) { auto res = query::insert() .into(SHIPMENT, SHIPMENT) .values(*sh) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } auto count = query::select({count_all()}) .from(SHIPMENT) .fetch_value(db); REQUIRE(count.is_ok()); REQUIRE(*count == 2); std::vector packages { object_ptr{new package{3, 15.4, shipments.at(0)}}, object_ptr{new package{4, 1.3, shipments.at(0)}}, object_ptr{new package{5, 30.9, shipments.at(1)}}, object_ptr{new package{6, 22.8, shipments.at(1)}}, object_ptr{new package{7, 17.2, shipments.at(1)}} }; for (const auto &pkg: packages) { auto res = query::insert() .into(PACKAGE, PACKAGE) .values(*pkg) .execute(db); REQUIRE(res.is_ok()); REQUIRE(*res == 1); } count = query::select({count_all()}) .from(PACKAGE) .fetch_value(db); REQUIRE(count.is_ok()); REQUIRE(*count == 5); auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment, SHIPMENT.tracking_number}) .from(PACKAGE) .join_left(SHIPMENT) .on( PACKAGE.shipment == SHIPMENT.id ) .where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" ) .group_by({PACKAGE.id, SHIPMENT.tracking_number}) .order_by(PACKAGE.weight).asc() .limit( 5 ) .offset( 0 ) .fetch_all(db); REQUIRE(pkgs.is_ok()); record_printer printer(std::cout); printer.print(*pkgs); auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight}) .from(SHIPMENT) .join_left(PACKAGE) .on( SHIPMENT.id == PACKAGE.shipment ) .fetch_all(db); REQUIRE(shipment_records.is_ok()); printer.print(*shipment_records); auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment}) .from(SHIPMENT) .join_left(PACKAGE) .on( SHIPMENT.id == PACKAGE.shipment ) .fetch_all(db); REQUIRE(shipment_result.is_ok()); std::cout << "\n"; for (const auto &s : *shipment_result) { std::cout << s.id << " " << s.tracking_number << " packages: " << s.packages.size() << std::endl; } }