From e8b0f0e802e359ab4be20f2fe6808338df0374c6 Mon Sep 17 00:00:00 2001 From: Sascha Kuehl Date: Fri, 2 Feb 2024 07:54:19 +0100 Subject: [PATCH] sql select join progress --- backends/postgres/test/CMakeLists.txt | 2 +- backends/tests/QueryTest.cpp | 47 +++++++++++++++++++++ include/matador/sql/query_intermediates.hpp | 22 ++++++++++ src/sql/query_intermediates.cpp | 5 +++ test/models/flight.hpp | 7 ++- 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/backends/postgres/test/CMakeLists.txt b/backends/postgres/test/CMakeLists.txt index a07d07f..a2f4a6a 100644 --- a/backends/postgres/test/CMakeLists.txt +++ b/backends/postgres/test/CMakeLists.txt @@ -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) diff --git a/backends/tests/QueryTest.cpp b/backends/tests/QueryTest.cpp index 1c20c5e..c9bc514 100644 --- a/backends/tests/QueryTest.cpp +++ b/backends/tests/QueryTest.cpp @@ -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") + .execute(); + + db.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.insert().into("airplane").values(*plane).execute(); + REQUIRE(res == 1); + } + + auto count = db.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.insert().into("flight").values(*f).execute(); + REQUIRE(res == 1); + } + + auto f = *db.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); + + 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(); + } \ No newline at end of file diff --git a/include/matador/sql/query_intermediates.hpp b/include/matador/sql/query_intermediates.hpp index e365b1c..a8b5c95 100644 --- a/include/matador/sql/query_intermediates.hpp +++ b/include/matador/sql/query_intermediates.hpp @@ -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); diff --git a/src/sql/query_intermediates.cpp b/src/sql/query_intermediates.cpp index 6bad0d2..a8ea2e0 100644 --- a/src/sql/query_intermediates.cpp +++ b/src/sql/query_intermediates.cpp @@ -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)}; diff --git a/test/models/flight.hpp b/test/models/flight.hpp index 4187691..88ed0ea 100644 --- a/test/models/flight.hpp +++ b/test/models/flight.hpp @@ -13,7 +13,12 @@ namespace matador::test { -struct flight { +struct flight +{ + flight() = default; + flight(unsigned long id, const sql::entity &plane, const std::string &name) + : id(id), airplane(plane), pilot_name(name) {} + unsigned long id{}; sql::entity airplane; std::string pilot_name;