sql select join progress

This commit is contained in:
2024-02-02 19:30:16 +01:00
parent e8b0f0e802
commit aa221aa571
8 changed files with 131 additions and 68 deletions
+81 -54
View File
@@ -21,7 +21,9 @@ public:
{
db.open();
}
~QueryFixture() {
~QueryFixture()
{
drop_table_if_exists("flight");
drop_table_if_exists("airplane");
drop_table_if_exists("person");
@@ -31,14 +33,16 @@ protected:
matador::sql::connection db;
private:
void drop_table_if_exists(const std::string &table_name) {
void drop_table_if_exists(const std::string &table_name)
{
if (db.exists(table_name)) {
db.drop().table(table_name).execute();
}
}
};
TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]") {
TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[session]")
{
db.create()
.table<airplane>("airplane")
.execute();
@@ -58,26 +62,27 @@ TEST_CASE_METHOD(QueryFixture, " Create table with foreign key relation", "[sess
REQUIRE(!db.exists("airplane"));
}
TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[session]") {
TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[session]")
{
db.create()
.table<person>("person")
.execute();
.table<person>("person")
.execute();
person george{7, "george", 45};
george.image.push_back(37);
auto res = db.insert()
.into("person", george)
.execute();
REQUIRE(res == 1);
.into("person", george)
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = db.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all();
.from("person")
.where("id"_col == 7)
.fetch_all();
for (const auto& i : result_record) {
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_unsigned_long);
@@ -92,11 +97,11 @@ TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[
// fetch person as person
auto result_person = db.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all<person>();
.from("person")
.where("id"_col == 7)
.fetch_all<person>();
for (const auto& i : result_person) {
for (const auto &i: result_person) {
REQUIRE(i.id == 7);
REQUIRE(i.name == "george");
REQUIRE(i.age == 45);
@@ -105,29 +110,30 @@ TEST_CASE_METHOD(QueryFixture, " Execute select statement with where clause", "[
db.drop().table("person").execute();
}
TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]") {
TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]")
{
db.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
auto res = db.insert()
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
.execute();
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
.execute();
REQUIRE(res == 1);
// fetch person as record
auto result_record = db.select({"id", "name", "color"})
.from("person")
.where("id"_col == 7)
.fetch_all();
.from("person")
.where("id"_col == 7)
.fetch_all();
for (const auto& i : result_record) {
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);
@@ -143,7 +149,8 @@ TEST_CASE_METHOD(QueryFixture, " Execute insert statement", "[session]") {
db.drop().table("person").execute();
}
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]") {
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]")
{
db.create()
.table<airplane>("airplane")
.execute();
@@ -152,13 +159,13 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]"
.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")
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) {
for (const auto &plane: planes) {
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
@@ -181,22 +188,23 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key", "[session]"
db.drop().table("airplane").execute();
}
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[session][join]") {
TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[session][join]")
{
db.create()
.table<airplane>("airplane")
.execute();
.table<airplane>("airplane")
.execute();
db.create()
.table<flight>("flight")
.execute();
.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")
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) {
for (const auto &plane: planes) {
auto res = db.insert().into<airplane>("airplane").values(*plane).execute();
REQUIRE(res == 1);
}
@@ -204,14 +212,14 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[
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")
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) {
for (const auto &f: flights) {
auto res = db.insert().into<flight>("flight").values(*f).execute();
REQUIRE(res == 1);
}
@@ -222,7 +230,26 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join", "[
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");
auto result = db.select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
.from("flight", "f")
.join("airplane", "ap")
.on("f.airplane_id", "ap.id")
.order_by("f.id").asc()
.fetch_all();
std::vector<std::pair<unsigned long, std::string>> 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<unsigned long>() == expected_result[index].first);
REQUIRE(r.at(3).as<std::string>() == expected_result[index++].second);
}
db.drop().table("flight").execute();
db.drop().table("airplane").execute();