#include "catch2/catch_test_macros.hpp" #include "matador/sql/column_definition.hpp" #include "matador/sql/condition.hpp" #include "matador/sql/query_builder.hpp" #include "matador/sql/session.hpp" #include "connection.hpp" #include "models/airplane.hpp" #include "models/flight.hpp" #include "models/person.hpp" using namespace matador::sql; using namespace matador::test; class QueryFixture { public: QueryFixture() : db(matador::test::connection::dns) , schema(db.dialect().default_schema_name()) { db.open(); } ~QueryFixture() { drop_table_if_exists("flight"); drop_table_if_exists("airplane"); drop_table_if_exists("person"); } protected: matador::sql::connection db; matador::sql::schema schema; private: void drop_table_if_exists(const std::string &table_name) { if (db.exists(table_name)) { db.query(schema).drop().table(table_name).execute(); } } }; TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[session]") { schema.attach("airplane"); schema.attach("flight"); db.query(schema).create() .table("airplane") .execute(); REQUIRE(db.exists("airplane")); db.query(schema).create() .table("flight") .execute(); REQUIRE(db.exists("flight")); db.query(schema).drop().table("flight").execute(); db.query(schema).drop().table("airplane").execute(); REQUIRE(!db.exists("flight")); REQUIRE(!db.exists("airplane")); } TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[session]") { schema.attach("person"); db.query(schema).create() .table("person") .execute(); person george{7, "george", 45}; george.image.push_back(37); auto res = db.query(schema) .insert() .into("person") .values(george) .execute(); REQUIRE(res == 1); // fetch person as record auto result_record = db.query(schema).select() .from("person") .where("id"_col == 7) .fetch_all(); for (const auto &i: result_record) { REQUIRE(i.size() == 4); REQUIRE(i.at(0).name() == "id"); REQUIRE(i.at(0).type() == data_type_t::type_long_long); REQUIRE(i.at(0).as() == george.id); REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).type() == data_type_t::type_varchar); REQUIRE(i.at(1).as() == george.name); REQUIRE(i.at(2).name() == "age"); REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long); REQUIRE(i.at(2).as() == george.age); } // fetch person as person auto result_person = db.query(schema).select() .from("person") .where("id"_col == 7) .fetch_all(); for (const auto &i: result_person) { REQUIRE(i.id == 7); REQUIRE(i.name == "george"); REQUIRE(i.age == 45); } db.query(schema).drop().table("person").execute(); } TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[session]") { db.query(schema).create() .table("person", { make_pk_column("id"), make_column("name", 255), make_column("color", 63) }) .execute(); auto res = db.query(schema).insert() .into("person", {{"", "id", ""}, {"", "name", ""}, {"", "color", ""}}) .values({7, "george", "green"}) .execute(); REQUIRE(res == 1); // fetch person as record auto result_record = db.query(schema).select({"id", "name", "color"}) .from("person") .where("id"_col == 7) .fetch_all(); for (const auto &i: result_record) { REQUIRE(i.size() == 3); REQUIRE(i.at(0).name() == "id"); REQUIRE(i.at(0).type() == data_type_t::type_long_long); REQUIRE(i.at(0).as() == 7); REQUIRE(i.at(1).name() == "name"); REQUIRE(i.at(1).type() == data_type_t::type_varchar); REQUIRE(i.at(1).as() == "george"); REQUIRE(i.at(2).name() == "color"); REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_varchar); REQUIRE(i.at(2).as() == "green"); } db.query(schema).drop().table("person").execute(); } TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[session]") { schema.attach("airplane"); schema.attach("flight"); db.query(schema).create() .table("airplane") .execute(); db.query(schema).create() .table("flight") .execute(); std::vector> planes{ make_entity(1, "Airbus", "A380"), make_entity(2, "Boeing", "707"), make_entity(3, "Boeing", "747") }; for (const auto &plane: planes) { auto res = db.query(schema).insert().into("airplane").values(*plane).execute(); REQUIRE(res == 1); } auto count = db.query(schema).select({count_all()}).from("airplane").fetch_value(); REQUIRE(count == 3); flight f4711{4, planes.at(1), "hans"}; auto res = db.query(schema).insert().into("flight").values(f4711).execute(); REQUIRE(res == 1); auto f = *db.query(schema).select().from("flight").fetch_all().begin(); REQUIRE(f.id == 4); REQUIRE(f.pilot_name == "hans"); REQUIRE(f.airplane.get() != nullptr); REQUIRE(f.airplane->id == 2); db.query(schema).drop().table("flight").execute(); db.query(schema).drop().table("airplane").execute(); } TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left", "[session][join_left]") { schema.attach("airplane"); schema.attach("flight"); db.query(schema).create() .table("airplane") .execute(); db.query(schema).create() .table("flight") .execute(); std::vector> planes{ make_entity(1, "Airbus", "A380"), make_entity(2, "Boeing", "707"), make_entity(3, "Boeing", "747") }; for (const auto &plane: planes) { auto res = db.query(schema).insert().into("airplane").values(*plane).execute(); REQUIRE(res == 1); } auto count = db.query(schema).select({count_all()}).from("airplane").fetch_value(); REQUIRE(count == 3); std::vector> flights{ make_entity(4, planes.at(0), "hans"), make_entity(5, planes.at(0), "otto"), make_entity(6, planes.at(1), "george"), make_entity(7, planes.at(2), "paul") }; for (const auto &f: flights) { auto res = db.query(schema).insert().into("flight").values(*f).execute(); REQUIRE(res == 1); } auto f = *db.query(schema).select().from("flight").fetch_all().begin(); REQUIRE(f.id == 4); REQUIRE(f.pilot_name == "hans"); REQUIRE(f.airplane.get() != nullptr); REQUIRE(f.airplane->id == 1); auto result = db.query(schema).select({"f.id", "ap.brand", "ap.model", "f.pilot_name"}) .from({"flight", "f"}) .join_left({"airplane", "ap"}) .on("f.airplane_id"_col == "ap.id"_col) .order_by("f.id").asc() .fetch_all(); std::vector> expected_result { {4, "hans"}, {5, "otto"}, {6, "george"}, {7, "paul"} }; size_t index{0}; for (const auto &r: result) { REQUIRE(r.size() == 4); REQUIRE(r.at(0).as() == expected_result[index].first); REQUIRE(r.at(3).as() == expected_result[index++].second); } db.query(schema).drop().table("flight").execute(); db.query(schema).drop().table("airplane").execute(); }