sql select join progress

This commit is contained in:
Sascha Kuehl 2024-02-02 07:54:19 +01:00
parent 27353e011a
commit e8b0f0e802
5 changed files with 81 additions and 2 deletions

View File

@ -12,7 +12,7 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
set(POSTGRES_CONNECTION_STRING "postgres://test:test123@127.0.0.1:15432/test")
set(POSTGRES_CONNECTION_STRING "postgres://test:test123@127.0.0.1:5432/matador_test")
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)

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();
}

View File

@ -117,11 +117,33 @@ public:
query_order_by_intermediate order_by(const std::string &name);
};
class query_join_intermediate;
class query_on_intermediate : public query_select_finish
{
public:
using query_select_finish::query_select_finish;
query_join_intermediate join(const std::string &join_table_name, const std::string &as);
query_where_intermediate where(const basic_condition &cond);
query_group_by_intermediate group_by(const std::string &name);
query_order_by_intermediate order_by(const std::string &name);
};
class query_join_intermediate : public query_intermediate
{
public:
using query_intermediate::query_intermediate;
query_on_intermediate on(const std::string &column, const std::string &join_column);
};
class query_from_intermediate : public query_select_finish
{
public:
using query_select_finish::query_select_finish;
query_join_intermediate join(const std::string &join_table_name, const std::string &as);
query_where_intermediate where(const basic_condition &cond);
query_group_by_intermediate group_by(const std::string &name);
query_order_by_intermediate order_by(const std::string &name);

View File

@ -76,6 +76,11 @@ query_order_by_intermediate query_where_intermediate::order_by(const std::string
return {connection_, builder_.order_by(name)};
}
query_join_intermediate query_from_intermediate::join(const std::string &join_table_name, const std::string &as)
{
return query_join_intermediate();
}
query_where_intermediate query_from_intermediate::where(const basic_condition &cond)
{
return query_where_intermediate{connection_, builder_.where(cond)};

View File

@ -13,7 +13,12 @@
namespace matador::test {
struct flight {
struct flight
{
flight() = default;
flight(unsigned long id, const sql::entity<airplane> &plane, const std::string &name)
: id(id), airplane(plane), pilot_name(name) {}
unsigned long id{};
sql::entity<test::airplane> airplane;
std::string pilot_name;