sql select join progress

This commit is contained in:
2024-02-02 07:54:19 +01:00
parent 27353e011a
commit e8b0f0e802
5 changed files with 81 additions and 2 deletions
+47
View File
@@ -179,4 +179,51 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]"
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
}
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[session][join]") {
db.create()
.table<airplane>("airplane")
.execute();
db.create()
.table<flight>("flight")
.execute();
std::vector<entity<airplane>> planes {
make_entity<airplane>(1, "Airbus", "A380"),
make_entity<airplane>(2, "Boeing", "707"),
make_entity<airplane>(3, "Boeing", "747")
};
for (const auto &plane : planes) {
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
auto count = db.select({count_all()}).from("airplane").fetch_value<int>();
REQUIRE(count == 3);
std::vector<entity<flight>> flights {
make_entity<flight>(4, planes.at(0), "hans"),
make_entity<flight>(5, planes.at(0), "otto"),
make_entity<flight>(6, planes.at(1), "george"),
make_entity<flight>(7, planes.at(2), "paul")
};
for (const auto &f : flights) {
auto res = db.insert().into<flight>("flight").values(*f).execute();
REQUIRE(res == 1);
}
auto f = *db.select<flight>().from("flight").fetch_all<flight>().begin();
REQUIRE(f.id == 4);
REQUIRE(f.pilot_name == "hans");
REQUIRE(f.airplane.get() != nullptr);
REQUIRE(f.airplane->id == 1);
db.select({"f.id", "ap.brand", "f.pilot_name"}).from("flight", "f").join("airplane", join_type_t::INNER, "ap").on("f.airplane_id", "ap.id");
db.drop().table("flight").execute();
db.drop().table("airplane").execute();
}