many to many query progress (multiple joins)
This commit is contained in:
+179
-26
@@ -10,6 +10,9 @@
|
||||
#include "models/airplane.hpp"
|
||||
#include "models/flight.hpp"
|
||||
#include "models/person.hpp"
|
||||
#include "models/recipe.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
@@ -29,6 +32,9 @@ public:
|
||||
drop_table_if_exists("flight");
|
||||
drop_table_if_exists("airplane");
|
||||
drop_table_if_exists("person");
|
||||
drop_table_if_exists("recipe_ingredients");
|
||||
drop_table_if_exists("recipes");
|
||||
drop_table_if_exists("ingredients");
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -79,16 +85,17 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[s
|
||||
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into<person>("person")
|
||||
.into("person", column_generator::generate<person>(schema, true))
|
||||
.values(george)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
// fetch person as record
|
||||
auto result_record = db.query(schema).select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
auto result_record = db.query(schema)
|
||||
.select(column_generator::generate<person>(schema, true))
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &i: result_record) {
|
||||
REQUIRE(i.size() == 4);
|
||||
@@ -104,10 +111,11 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[s
|
||||
}
|
||||
|
||||
// fetch person as person
|
||||
auto result_person = db.query(schema).select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all<person>();
|
||||
auto result_person = db.query(schema)
|
||||
.select(column_generator::generate<person>(schema, true))
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all<person>();
|
||||
|
||||
for (const auto &i: result_person) {
|
||||
REQUIRE(i.id == 7);
|
||||
@@ -176,20 +184,31 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[session]")
|
||||
};
|
||||
|
||||
for (const auto &plane: planes) {
|
||||
auto res = db.query(schema).insert().into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||
.values(*plane)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto count = db.query(schema).select({count_all()}).from("airplane").fetch_value<int>();
|
||||
auto count = db.query(schema)
|
||||
.select({count_all()})
|
||||
.from("airplane")
|
||||
.fetch_value<int>().value();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
flight f4711{4, planes.at(1), "hans"};
|
||||
|
||||
auto res = db.query(schema).insert().into<flight>("flight").values(f4711).execute();
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into("flight", column_generator::generate<flight>(schema, true))
|
||||
.values(f4711)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto f = *db.query(schema)
|
||||
.select<flight>()
|
||||
.select(column_generator::generate<flight>(schema, true))
|
||||
.from("flight")
|
||||
.fetch_all().begin();
|
||||
REQUIRE(f.at(0).as<unsigned long>() == 4);
|
||||
@@ -219,11 +238,18 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
|
||||
};
|
||||
|
||||
for (const auto &plane: planes) {
|
||||
auto res = db.query(schema).insert().into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||
.values(*plane)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto count = db.query(schema).select({count_all()}).from("airplane").fetch_value<int>();
|
||||
auto count = db.query(schema)
|
||||
.select({count_all()})
|
||||
.from("airplane")
|
||||
.fetch_value<int>().value();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
std::vector<entity<flight>> flights{
|
||||
@@ -234,12 +260,16 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
|
||||
};
|
||||
|
||||
for (const auto &f: flights) {
|
||||
auto res = db.query(schema).insert().into<flight>("flight").values(*f).execute();
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into("flight", {"id", "airplane_id", "pilot_name"})
|
||||
.values(*f)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto f = *db.query(schema)
|
||||
.select<flight>()
|
||||
.select(column_generator::generate<flight>(schema, true))
|
||||
.from("flight")
|
||||
.fetch_all().begin();
|
||||
REQUIRE(f.at(0).as<unsigned long>() == 4);
|
||||
@@ -291,7 +321,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||
auto res = db
|
||||
.query(schema)
|
||||
.insert()
|
||||
.into<airplane>("airplane")
|
||||
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||
.values(*plane)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
@@ -301,7 +331,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||
.query(schema)
|
||||
.select({count_all()})
|
||||
.from("airplane")
|
||||
.fetch_value<int>();
|
||||
.fetch_value<int>().value();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
std::vector<entity<flight>> flights{
|
||||
@@ -312,17 +342,22 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||
};
|
||||
|
||||
for (const auto &f: flights) {
|
||||
auto res = db.query(schema).insert().into<flight>("flight").values(*f).execute();
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into("flight", column_generator::generate<flight>(schema, true))
|
||||
.values(*f)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto f = *db.query(schema)
|
||||
.select<flight>()
|
||||
auto f = db.query(schema)
|
||||
.select(column_generator::generate<flight>(schema, true))
|
||||
.from("flight")
|
||||
.fetch_all().begin();
|
||||
REQUIRE(f.at(0).as<unsigned long>() == 4);
|
||||
REQUIRE(f.at(1).as<unsigned long>() == 1);
|
||||
REQUIRE(f.at(2).as<std::string>() == "hans");
|
||||
.fetch_one();
|
||||
REQUIRE(f.has_value());
|
||||
REQUIRE(f->at(0).as<unsigned long>() == 4);
|
||||
REQUIRE(f->at(1).as<unsigned long>() == 1);
|
||||
REQUIRE(f->at(2).as<std::string>() == "hans");
|
||||
|
||||
auto result = db
|
||||
.query(schema)
|
||||
@@ -345,4 +380,122 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||
|
||||
db.query(schema).drop().table("flight").execute();
|
||||
db.query(schema).drop().table("airplane").execute();
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[session][join][many_to_many]") {
|
||||
schema.attach<recipe>("recipes");
|
||||
schema.attach<ingredient>("ingredients");
|
||||
schema.attach<recipe_ingredient>("recipe_ingredients");
|
||||
db.query(schema).create()
|
||||
.table<recipe>("recipes")
|
||||
.execute();
|
||||
|
||||
db.query(schema).create()
|
||||
.table<ingredient>("ingredients")
|
||||
.execute();
|
||||
|
||||
db.query(schema).create()
|
||||
.table<recipe_ingredient>("recipe_ingredients")
|
||||
.execute();
|
||||
|
||||
std::vector<ingredient> ingredients {
|
||||
{1, "Apple"},
|
||||
{2, "Strawberry"},
|
||||
{3, "Pineapple"},
|
||||
{4, "Sugar"},
|
||||
{5, "Flour"},
|
||||
{6, "Butter"},
|
||||
{7, "Beans"}
|
||||
};
|
||||
|
||||
for (const auto &i: ingredients) {
|
||||
auto res = db
|
||||
.query(schema)
|
||||
.insert()
|
||||
.into("ingredients", column_generator::generate<ingredient>(schema, true))
|
||||
.values(i)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
std::vector<recipe> recipes{
|
||||
{7, "Apple Crumble"},
|
||||
{8, "Beans Chili"},
|
||||
{9, "Fruit Salad"}
|
||||
};
|
||||
|
||||
for (const auto &r: recipes) {
|
||||
auto res = db
|
||||
.query(schema)
|
||||
.insert()
|
||||
.into("recipes", column_generator::generate<recipe>(schema, true))
|
||||
.values(r)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
std::vector<std::pair<int, int>> recipe_ingredients {
|
||||
{ 7, 1 },
|
||||
{ 7, 4 },
|
||||
{ 7, 5 },
|
||||
{ 8, 6 },
|
||||
{ 8, 7 },
|
||||
{ 9, 1 },
|
||||
{ 9, 2 },
|
||||
{ 9, 3 }
|
||||
};
|
||||
|
||||
for (const auto &ri: recipe_ingredients) {
|
||||
auto res = db
|
||||
.query(schema)
|
||||
.insert()
|
||||
.into("recipe_ingredients", column_generator::generate<recipe_ingredient>(schema, true))
|
||||
.values({ri.first, ri.second})
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto result = db
|
||||
.query(schema)
|
||||
.select({"r.id", "r.name", "ri.recipe_id"})
|
||||
.from({"recipes", "r"})
|
||||
.join_left({"recipe_ingredients", "ri"})
|
||||
.on("r.id"_col == "ri.recipe_id"_col)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &r: result) {
|
||||
REQUIRE(r.size() == 3);
|
||||
std::cout << "record r.id " << r.at(0).as<unsigned long>().value() << " r.name " << r.at(1).as<std::string>().value() << " ri.id " << r.at(2).as<unsigned long>().value() << "\n";
|
||||
}
|
||||
|
||||
result = db
|
||||
.query(schema)
|
||||
.select({"r.id", "r.name", "ri.recipe_id", "i.name"})
|
||||
.from({"recipes", "r"})
|
||||
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
|
||||
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &r: result) {
|
||||
REQUIRE(r.size() == 4);
|
||||
std::cout << "record r.id " << r.at(0).as<unsigned long>().value() << " r.name " << r.at(1).as<std::string>().value() << " ri.id " << r.at(2).as<unsigned long>().value() << " i.name " << r.at(3).as<std::string>().value() << "\n";
|
||||
}
|
||||
|
||||
result = db
|
||||
.query(schema)
|
||||
.select({"r.id", "r.name", "ri.recipe_id", "i.name"})
|
||||
.from({"recipes", "r"})
|
||||
.join_left({"recipe_ingredients", "ri"}).on("r.id"_col == "ri.recipe_id"_col)
|
||||
.join_left({"ingredients", "i"}).on("ri.ingredient_id"_col == "i.id"_col)
|
||||
.where("r.id"_col == 8)
|
||||
.fetch_all();
|
||||
|
||||
for (const auto &r: result) {
|
||||
REQUIRE(r.size() == 4);
|
||||
std::cout << "record r.id " << r.at(0).as<unsigned long>().value() << " r.name " << r.at(1).as<std::string>().value() << " ri.id " << r.at(2).as<unsigned long>().value() << " i.name " << r.at(3).as<std::string>().value() << "\n";
|
||||
}
|
||||
|
||||
db.query(schema).drop().table("recipe_ingredients").execute();
|
||||
db.query(schema).drop().table("recipes").execute();
|
||||
db.query(schema).drop().table("ingredients").execute();
|
||||
}
|
||||
@@ -52,7 +52,7 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
|
||||
table ap{"airplane"};
|
||||
SECTION("Insert with prepared statement and placeholder") {
|
||||
auto stmt = db.query(schema).insert()
|
||||
.into<airplane>("airplane")
|
||||
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||
.values<airplane>()
|
||||
.prepare();
|
||||
|
||||
@@ -62,7 +62,10 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
|
||||
stmt.reset();
|
||||
}
|
||||
|
||||
auto result = db.query(schema).select<airplane>().from(ap).fetch_all<airplane>();
|
||||
auto result = db.query(schema)
|
||||
.select(column_generator::generate<airplane>(schema, true))
|
||||
.from(ap)
|
||||
.fetch_all<airplane>();
|
||||
|
||||
size_t index{0};
|
||||
for (const auto &i: result) {
|
||||
@@ -74,11 +77,19 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
|
||||
|
||||
SECTION("Select with prepared statement") {
|
||||
for (const auto &plane: planes) {
|
||||
auto res = db.query(schema).insert().into<airplane>("airplane").values(*plane).execute();
|
||||
auto res = db.query(schema)
|
||||
.insert()
|
||||
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||
.values(*plane)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
}
|
||||
|
||||
auto stmt = db.query(schema).select<airplane>().from(ap).where("brand"_col == _).prepare();
|
||||
auto stmt = db.query(schema)
|
||||
.select(column_generator::generate<airplane>(schema, true))
|
||||
.from(ap)
|
||||
.where("brand"_col == _)
|
||||
.prepare();
|
||||
|
||||
stmt.bind(0, "Airbus");
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/column_generator.hpp"
|
||||
|
||||
#include "matador/utils/enum_mapper.hpp"
|
||||
|
||||
@@ -8,6 +9,7 @@
|
||||
|
||||
#include "models/location.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::test;
|
||||
|
||||
class TypeTraitsTestFixture
|
||||
@@ -91,14 +93,14 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||
auto res = db
|
||||
.query(schema)
|
||||
.insert()
|
||||
.into<location>("location")
|
||||
.into("location", column_generator::generate<location>(schema, true))
|
||||
.values(loc)
|
||||
.execute();
|
||||
REQUIRE(res == 1);
|
||||
|
||||
auto result = db
|
||||
.query(schema)
|
||||
.select<location>()
|
||||
.select(column_generator::generate<location>(schema, true))
|
||||
.from("location")
|
||||
.fetch_all<location>();
|
||||
|
||||
@@ -113,7 +115,7 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||
auto stmt = db
|
||||
.query(schema)
|
||||
.insert()
|
||||
.into<location>("location")
|
||||
.into("location", column_generator::generate<location>(schema, true))
|
||||
.values<location>()
|
||||
.prepare();
|
||||
|
||||
@@ -124,7 +126,7 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||
|
||||
auto result = db
|
||||
.query(schema)
|
||||
.select<location>()
|
||||
.select(column_generator::generate<location>(schema, true))
|
||||
.from("location")
|
||||
.fetch_all<location>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user