many to many query progress (multiple joins)
This commit is contained in:
parent
50a0eb580a
commit
4cda0f2664
|
|
@ -10,6 +10,9 @@
|
||||||
#include "models/airplane.hpp"
|
#include "models/airplane.hpp"
|
||||||
#include "models/flight.hpp"
|
#include "models/flight.hpp"
|
||||||
#include "models/person.hpp"
|
#include "models/person.hpp"
|
||||||
|
#include "models/recipe.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
@ -29,6 +32,9 @@ public:
|
||||||
drop_table_if_exists("flight");
|
drop_table_if_exists("flight");
|
||||||
drop_table_if_exists("airplane");
|
drop_table_if_exists("airplane");
|
||||||
drop_table_if_exists("person");
|
drop_table_if_exists("person");
|
||||||
|
drop_table_if_exists("recipe_ingredients");
|
||||||
|
drop_table_if_exists("recipes");
|
||||||
|
drop_table_if_exists("ingredients");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
@ -79,16 +85,17 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[s
|
||||||
|
|
||||||
auto res = db.query(schema)
|
auto res = db.query(schema)
|
||||||
.insert()
|
.insert()
|
||||||
.into<person>("person")
|
.into("person", column_generator::generate<person>(schema, true))
|
||||||
.values(george)
|
.values(george)
|
||||||
.execute();
|
.execute();
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
|
|
||||||
// fetch person as record
|
// fetch person as record
|
||||||
auto result_record = db.query(schema).select<person>()
|
auto result_record = db.query(schema)
|
||||||
.from("person")
|
.select(column_generator::generate<person>(schema, true))
|
||||||
.where("id"_col == 7)
|
.from("person")
|
||||||
.fetch_all();
|
.where("id"_col == 7)
|
||||||
|
.fetch_all();
|
||||||
|
|
||||||
for (const auto &i: result_record) {
|
for (const auto &i: result_record) {
|
||||||
REQUIRE(i.size() == 4);
|
REQUIRE(i.size() == 4);
|
||||||
|
|
@ -104,10 +111,11 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[s
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch person as person
|
// fetch person as person
|
||||||
auto result_person = db.query(schema).select<person>()
|
auto result_person = db.query(schema)
|
||||||
.from("person")
|
.select(column_generator::generate<person>(schema, true))
|
||||||
.where("id"_col == 7)
|
.from("person")
|
||||||
.fetch_all<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.id == 7);
|
||||||
|
|
@ -176,20 +184,31 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[session]")
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const auto &plane: planes) {
|
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);
|
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);
|
REQUIRE(count == 3);
|
||||||
|
|
||||||
flight f4711{4, planes.at(1), "hans"};
|
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);
|
REQUIRE(res == 1);
|
||||||
|
|
||||||
auto f = *db.query(schema)
|
auto f = *db.query(schema)
|
||||||
.select<flight>()
|
.select(column_generator::generate<flight>(schema, true))
|
||||||
.from("flight")
|
.from("flight")
|
||||||
.fetch_all().begin();
|
.fetch_all().begin();
|
||||||
REQUIRE(f.at(0).as<unsigned long>() == 4);
|
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) {
|
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);
|
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);
|
REQUIRE(count == 3);
|
||||||
|
|
||||||
std::vector<entity<flight>> flights{
|
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) {
|
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);
|
REQUIRE(res == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto f = *db.query(schema)
|
auto f = *db.query(schema)
|
||||||
.select<flight>()
|
.select(column_generator::generate<flight>(schema, true))
|
||||||
.from("flight")
|
.from("flight")
|
||||||
.fetch_all().begin();
|
.fetch_all().begin();
|
||||||
REQUIRE(f.at(0).as<unsigned long>() == 4);
|
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
|
auto res = db
|
||||||
.query(schema)
|
.query(schema)
|
||||||
.insert()
|
.insert()
|
||||||
.into<airplane>("airplane")
|
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||||
.values(*plane)
|
.values(*plane)
|
||||||
.execute();
|
.execute();
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
|
|
@ -301,7 +331,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||||
.query(schema)
|
.query(schema)
|
||||||
.select({count_all()})
|
.select({count_all()})
|
||||||
.from("airplane")
|
.from("airplane")
|
||||||
.fetch_value<int>();
|
.fetch_value<int>().value();
|
||||||
REQUIRE(count == 3);
|
REQUIRE(count == 3);
|
||||||
|
|
||||||
std::vector<entity<flight>> flights{
|
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) {
|
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);
|
REQUIRE(res == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto f = *db.query(schema)
|
auto f = db.query(schema)
|
||||||
.select<flight>()
|
.select(column_generator::generate<flight>(schema, true))
|
||||||
.from("flight")
|
.from("flight")
|
||||||
.fetch_all().begin();
|
.fetch_one();
|
||||||
REQUIRE(f.at(0).as<unsigned long>() == 4);
|
REQUIRE(f.has_value());
|
||||||
REQUIRE(f.at(1).as<unsigned long>() == 1);
|
REQUIRE(f->at(0).as<unsigned long>() == 4);
|
||||||
REQUIRE(f.at(2).as<std::string>() == "hans");
|
REQUIRE(f->at(1).as<unsigned long>() == 1);
|
||||||
|
REQUIRE(f->at(2).as<std::string>() == "hans");
|
||||||
|
|
||||||
auto result = db
|
auto result = db
|
||||||
.query(schema)
|
.query(schema)
|
||||||
|
|
@ -346,3 +381,121 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||||
db.query(schema).drop().table("flight").execute();
|
db.query(schema).drop().table("flight").execute();
|
||||||
db.query(schema).drop().table("airplane").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"};
|
table ap{"airplane"};
|
||||||
SECTION("Insert with prepared statement and placeholder") {
|
SECTION("Insert with prepared statement and placeholder") {
|
||||||
auto stmt = db.query(schema).insert()
|
auto stmt = db.query(schema).insert()
|
||||||
.into<airplane>("airplane")
|
.into("airplane", column_generator::generate<airplane>(schema, true))
|
||||||
.values<airplane>()
|
.values<airplane>()
|
||||||
.prepare();
|
.prepare();
|
||||||
|
|
||||||
|
|
@ -62,7 +62,10 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
|
||||||
stmt.reset();
|
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};
|
size_t index{0};
|
||||||
for (const auto &i: result) {
|
for (const auto &i: result) {
|
||||||
|
|
@ -74,11 +77,19 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
|
||||||
|
|
||||||
SECTION("Select with prepared statement") {
|
SECTION("Select with prepared statement") {
|
||||||
for (const auto &plane: planes) {
|
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);
|
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");
|
stmt.bind(0, "Airbus");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
#include "matador/sql/connection.hpp"
|
#include "matador/sql/connection.hpp"
|
||||||
|
#include "matador/sql/column_generator.hpp"
|
||||||
|
|
||||||
#include "matador/utils/enum_mapper.hpp"
|
#include "matador/utils/enum_mapper.hpp"
|
||||||
|
|
||||||
|
|
@ -8,6 +9,7 @@
|
||||||
|
|
||||||
#include "models/location.hpp"
|
#include "models/location.hpp"
|
||||||
|
|
||||||
|
using namespace matador::sql;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
||||||
class TypeTraitsTestFixture
|
class TypeTraitsTestFixture
|
||||||
|
|
@ -91,14 +93,14 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||||
auto res = db
|
auto res = db
|
||||||
.query(schema)
|
.query(schema)
|
||||||
.insert()
|
.insert()
|
||||||
.into<location>("location")
|
.into("location", column_generator::generate<location>(schema, true))
|
||||||
.values(loc)
|
.values(loc)
|
||||||
.execute();
|
.execute();
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
|
|
||||||
auto result = db
|
auto result = db
|
||||||
.query(schema)
|
.query(schema)
|
||||||
.select<location>()
|
.select(column_generator::generate<location>(schema, true))
|
||||||
.from("location")
|
.from("location")
|
||||||
.fetch_all<location>();
|
.fetch_all<location>();
|
||||||
|
|
||||||
|
|
@ -113,7 +115,7 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||||
auto stmt = db
|
auto stmt = db
|
||||||
.query(schema)
|
.query(schema)
|
||||||
.insert()
|
.insert()
|
||||||
.into<location>("location")
|
.into("location", column_generator::generate<location>(schema, true))
|
||||||
.values<location>()
|
.values<location>()
|
||||||
.prepare();
|
.prepare();
|
||||||
|
|
||||||
|
|
@ -124,7 +126,7 @@ TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with typ
|
||||||
|
|
||||||
auto result = db
|
auto result = db
|
||||||
.query(schema)
|
.query(schema)
|
||||||
.select<location>()
|
.select(column_generator::generate<location>(schema, true))
|
||||||
.from("location")
|
.from("location")
|
||||||
.fetch_all<location>();
|
.fetch_all<location>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,14 +90,14 @@ int main()
|
||||||
mc.distinguished = true;
|
mc.distinguished = true;
|
||||||
auto insert_authors_sql = c.query(s)
|
auto insert_authors_sql = c.query(s)
|
||||||
.insert()
|
.insert()
|
||||||
.into<author>(qh::authors)
|
.into(qh::authors)
|
||||||
.values(mc)
|
.values(mc)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
std::cout << "SQL: " << insert_authors_sql << "\n";
|
std::cout << "SQL: " << insert_authors_sql << "\n";
|
||||||
|
|
||||||
auto result = c.query(s)
|
auto result = c.query(s)
|
||||||
.select<author>()
|
.select(qh::authors.columns)
|
||||||
.from(qh::authors)
|
.from(qh::authors)
|
||||||
.fetch_all();
|
.fetch_all();
|
||||||
|
|
||||||
|
|
@ -115,7 +115,7 @@ int main()
|
||||||
std::cout << "SQL: " << update_authors_sql << "\n";
|
std::cout << "SQL: " << update_authors_sql << "\n";
|
||||||
|
|
||||||
auto authors = c.query(s)
|
auto authors = c.query(s)
|
||||||
.select<author>()
|
.select(qh::authors.columns)
|
||||||
.from(qh::authors)
|
.from(qh::authors)
|
||||||
.fetch_all<author>();
|
.fetch_all<author>();
|
||||||
|
|
||||||
|
|
@ -125,18 +125,18 @@ int main()
|
||||||
|
|
||||||
c.query(s)
|
c.query(s)
|
||||||
.insert()
|
.insert()
|
||||||
.into<book>(qh::books)
|
.into(qh::books)
|
||||||
.values({2, "It", mc.id, 1980})
|
.values({2, "It", mc.id, 1980})
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
c.query(s)
|
c.query(s)
|
||||||
.insert()
|
.insert()
|
||||||
.into<book>(qh::books)
|
.into(qh::books)
|
||||||
.values({3, "Misery", mc.id, 1984})
|
.values({3, "Misery", mc.id, 1984})
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
auto select_books_sql = c.query(s)
|
auto select_books_sql = c.query(s)
|
||||||
.select<book>({qh::authors.last_name})
|
.select(qh::books.columns, {qh::authors.last_name})
|
||||||
.from(qh::books)
|
.from(qh::books)
|
||||||
.join_left(qh::authors)
|
.join_left(qh::authors)
|
||||||
.on(qh::books.author_id == qh::authors.id)
|
.on(qh::books.author_id == qh::authors.id)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
|
struct table;
|
||||||
|
|
||||||
enum class sql_function_t {
|
enum class sql_function_t {
|
||||||
NONE,
|
NONE,
|
||||||
COUNT,
|
COUNT,
|
||||||
|
|
@ -21,6 +23,7 @@ struct column
|
||||||
column(sql_function_t func, std::string name); // NOLINT(*-explicit-constructor)
|
column(sql_function_t func, std::string name); // NOLINT(*-explicit-constructor)
|
||||||
column(std::string table_name, std::string name, std::string as);
|
column(std::string table_name, std::string name, std::string as);
|
||||||
column(std::string table_name, const char* name, std::string as);
|
column(std::string table_name, const char* name, std::string as);
|
||||||
|
column(table &t, const char* name, std::string as);
|
||||||
|
|
||||||
bool equals(const column &x) const;
|
bool equals(const column &x) const;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,20 +17,23 @@ namespace matador::sql {
|
||||||
class column_generator
|
class column_generator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
column_generator(std::vector<column> &column_infos, const sql::schema &ts, const std::string &table_name);
|
column_generator(std::vector<column> &column_infos,
|
||||||
|
const sql::schema &ts,
|
||||||
|
const std::string &table_name,
|
||||||
|
bool force_lazy);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~column_generator() = default;
|
~column_generator() = default;
|
||||||
|
|
||||||
template < class Type >
|
template < class Type >
|
||||||
static std::vector<column> generate(const sql::schema &ts)
|
static std::vector<column> generate(const sql::schema &ts, bool force_lazy = false)
|
||||||
{
|
{
|
||||||
const auto info = ts.info<Type>();
|
const auto info = ts.info<Type>();
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
std::vector<column> columns;
|
std::vector<column> columns;
|
||||||
column_generator gen(columns, ts, info.value().name);
|
column_generator gen(columns, ts, info.value().name, force_lazy);
|
||||||
Type obj;
|
Type obj;
|
||||||
matador::utils::access::process(gen, obj);
|
matador::utils::access::process(gen, obj);
|
||||||
return std::move(columns);
|
return std::move(columns);
|
||||||
|
|
@ -53,7 +56,7 @@ public:
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||||
{
|
{
|
||||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||||
push(id);
|
push(id);
|
||||||
} else {
|
} else {
|
||||||
const auto info = table_schema_.info<typename Pointer::value_type>();
|
const auto info = table_schema_.info<typename Pointer::value_type>();
|
||||||
|
|
@ -69,7 +72,7 @@ public:
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||||
{
|
{
|
||||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||||
push(id);
|
push(id);
|
||||||
} else {
|
} else {
|
||||||
const auto info = table_schema_.info<typename Pointer::value_type>();
|
const auto info = table_schema_.info<typename Pointer::value_type>();
|
||||||
|
|
@ -85,7 +88,7 @@ public:
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
|
void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
|
||||||
{
|
{
|
||||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
|
const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
|
||||||
|
|
@ -112,6 +115,7 @@ private:
|
||||||
std::vector<column> &column_infos_;
|
std::vector<column> &column_infos_;
|
||||||
const sql::schema &table_schema_;
|
const sql::schema &table_schema_;
|
||||||
int column_index{0};
|
int column_index{0};
|
||||||
|
bool force_lazy_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ public:
|
||||||
columns_.clear();
|
columns_.clear();
|
||||||
table_name_ = info.value().name;
|
table_name_ = info.value().name;
|
||||||
query q(db, schema_);
|
query q(db, schema_);
|
||||||
return q.select<EntityType>().from({table_name_}).build();
|
return q.select(column_generator::generate<EntityType>(schema_)).from({table_name_}).build();
|
||||||
// auto from_intermediate = q.select<EntityType>().from({"t"});
|
// auto from_intermediate = q.select<EntityType>().from({"t"});
|
||||||
// return {};
|
// return {};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,9 @@ public:
|
||||||
|
|
||||||
query_create_intermediate create();
|
query_create_intermediate create();
|
||||||
query_drop_intermediate drop();
|
query_drop_intermediate drop();
|
||||||
template < class Type >
|
|
||||||
query_select_intermediate select();
|
|
||||||
template < class Type >
|
|
||||||
query_select_intermediate select(std::initializer_list<column> columns);
|
|
||||||
query_select_intermediate select(std::initializer_list<column> columns);
|
query_select_intermediate select(std::initializer_list<column> columns);
|
||||||
query_select_intermediate select(const std::vector<column>& columns);
|
query_select_intermediate select(const std::vector<column>& columns);
|
||||||
|
query_select_intermediate select(std::vector<column> columns, std::initializer_list<column> additional_columns);
|
||||||
query_insert_intermediate insert();
|
query_insert_intermediate insert();
|
||||||
query_update_intermediate update(const sql::table &table);
|
query_update_intermediate update(const sql::table &table);
|
||||||
query_delete_intermediate remove();
|
query_delete_intermediate remove();
|
||||||
|
|
@ -31,19 +28,5 @@ private:
|
||||||
const sql::schema &schema_;
|
const sql::schema &schema_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Type>
|
|
||||||
query_select_intermediate query::select()
|
|
||||||
{
|
|
||||||
return select(column_generator::generate<Type>(schema_));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Type>
|
|
||||||
query_select_intermediate query::select(std::initializer_list<column> columns)
|
|
||||||
{
|
|
||||||
auto cols = column_generator::generate<Type>(schema_);
|
|
||||||
cols.insert(cols.end(), columns);
|
|
||||||
return select(cols);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //QUERY_QUERY_HPP
|
#endif //QUERY_QUERY_HPP
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
#define FIELD(x) const sql::column x{this->name, #x, ""};
|
#define FIELD(x) const sql::column x{*this, #x, ""};
|
||||||
|
|
||||||
#define QUERY_HELPER(C, ...) \
|
#define QUERY_HELPER(C, ...) \
|
||||||
namespace matador::qh { \
|
namespace matador::qh { \
|
||||||
|
|
|
||||||
|
|
@ -257,9 +257,6 @@ public:
|
||||||
template<class Type>
|
template<class Type>
|
||||||
query_execute_finish table(const sql::table &table)
|
query_execute_finish table(const sql::table &table)
|
||||||
{
|
{
|
||||||
// if (!schema_.exists<Type>()) {
|
|
||||||
// schema_.attach<Type>(table.name);
|
|
||||||
// }
|
|
||||||
return this->table(table, column_definition_generator::generate<Type>(schema_));
|
return this->table(table, column_definition_generator::generate<Type>(schema_));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -279,11 +276,7 @@ public:
|
||||||
|
|
||||||
query_into_intermediate into(const sql::table &table, std::initializer_list<column> column_names);
|
query_into_intermediate into(const sql::table &table, std::initializer_list<column> column_names);
|
||||||
query_into_intermediate into(const sql::table &table, std::vector<column> &&column_names);
|
query_into_intermediate into(const sql::table &table, std::vector<column> &&column_names);
|
||||||
template<class Type>
|
query_into_intermediate into(const sql::table &table);
|
||||||
query_into_intermediate into(const sql::table &table)
|
|
||||||
{
|
|
||||||
return into(table, column_generator::generate<Type>(schema_));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_execute_where_intermediate : public query_execute_finish
|
class query_execute_where_intermediate : public query_execute_finish
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,11 @@ entity<Type> session::insert(Type *obj)
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
c->query(*schema_).insert().into<Type>(info->name).values(*obj).execute();
|
c->query(*schema_)
|
||||||
|
.insert()
|
||||||
|
.into(info->name, column_generator::generate<Type>(*schema_))
|
||||||
|
.values(*obj)
|
||||||
|
.execute();
|
||||||
|
|
||||||
return entity{obj};
|
return entity{obj};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
#ifndef QUERY_TABLE_HPP
|
#ifndef QUERY_TABLE_HPP
|
||||||
#define QUERY_TABLE_HPP
|
#define QUERY_TABLE_HPP
|
||||||
|
|
||||||
|
#include "matador/sql/column.hpp"
|
||||||
|
|
||||||
#include <typeindex>
|
#include <typeindex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
struct table
|
struct table
|
||||||
{
|
{
|
||||||
std::string name;
|
|
||||||
std::string alias;
|
|
||||||
|
|
||||||
table(const char *name, std::string as = "") // NOLINT(*-explicit-constructor)
|
table(const char *name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||||
: name(name), alias(std::move(as)) {}
|
: name(name), alias(std::move(as)) {}
|
||||||
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
|
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||||
|
|
@ -21,6 +21,10 @@ struct table
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string name;
|
||||||
|
std::string alias;
|
||||||
|
|
||||||
|
std::vector<column> columns;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "matador/sql/column.hpp"
|
#include "matador/sql/column.hpp"
|
||||||
|
#include "matador/sql/table.hpp"
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
|
|
@ -23,6 +24,14 @@ column::column(std::string table_name, const char *name, std::string as)
|
||||||
, name(name)
|
, name(name)
|
||||||
, alias(std::move(as)) {}
|
, alias(std::move(as)) {}
|
||||||
|
|
||||||
|
column::column(struct table &t, const char *name, std::string as)
|
||||||
|
: table(t.name)
|
||||||
|
, name(name)
|
||||||
|
, alias(std::move(as))
|
||||||
|
{
|
||||||
|
t.columns.push_back(*this);
|
||||||
|
}
|
||||||
|
|
||||||
bool column::equals(const column &x) const
|
bool column::equals(const column &x) const
|
||||||
{
|
{
|
||||||
return table == x.table &&
|
return table == x.table &&
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,11 @@ namespace matador::sql {
|
||||||
|
|
||||||
column_generator::column_generator(std::vector<column> &column_infos,
|
column_generator::column_generator(std::vector<column> &column_infos,
|
||||||
const sql::schema &ts,
|
const sql::schema &ts,
|
||||||
const std::string &table_name)
|
const std::string &table_name,
|
||||||
|
bool force_lazy)
|
||||||
: column_infos_(column_infos)
|
: column_infos_(column_infos)
|
||||||
, table_schema_(ts)
|
, table_schema_(ts)
|
||||||
|
, force_lazy_(force_lazy)
|
||||||
{
|
{
|
||||||
table_name_stack_.push(table_name);
|
table_name_stack_.push(table_name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,14 @@ query_select_intermediate query::select(const std::vector<column>& columns)
|
||||||
return {connection_, schema_, columns};
|
return {connection_, schema_, columns};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query_select_intermediate query::select(std::vector<column> columns, std::initializer_list<column> additional_columns)
|
||||||
|
{
|
||||||
|
for (const auto &col : additional_columns) {
|
||||||
|
columns.push_back(col);
|
||||||
|
}
|
||||||
|
return {connection_, schema_, columns};
|
||||||
|
}
|
||||||
|
|
||||||
query_insert_intermediate query::insert()
|
query_insert_intermediate query::insert()
|
||||||
{
|
{
|
||||||
return query_insert_intermediate{connection_, schema_};
|
return query_insert_intermediate{connection_, schema_};
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,17 @@ void query_compiler::visit(query_from_part &from_part)
|
||||||
|
|
||||||
void query_compiler::visit(query_join_part &join_part)
|
void query_compiler::visit(query_join_part &join_part)
|
||||||
{
|
{
|
||||||
query_.sql += " " + dialect_.token_at(dialect::token_t::JOIN) +
|
if (dialect_.default_schema_name().empty()) {
|
||||||
" " + dialect_.prepare_identifier(join_part.table().name) +
|
query_.sql += " " + dialect_.token_at(dialect::token_t::JOIN) +
|
||||||
(join_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(join_part.table().alias));
|
" " + dialect_.prepare_identifier(join_part.table().name) +
|
||||||
|
(join_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(join_part.table().alias));
|
||||||
|
} else {
|
||||||
|
query_.sql += " " + dialect_.token_at(dialect::token_t::JOIN) +
|
||||||
|
" " + dialect_.prepare_identifier(dialect_.default_schema_name()) +
|
||||||
|
"." + dialect_.prepare_identifier(join_part.table().name) +
|
||||||
|
(join_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(join_part.table().alias));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(query_on_part &on_part)
|
void query_compiler::visit(query_on_part &on_part)
|
||||||
|
|
|
||||||
|
|
@ -178,6 +178,12 @@ query_into_intermediate query_insert_intermediate::into(const table &table, std:
|
||||||
return {connection_, schema_, data_};
|
return {connection_, schema_, data_};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query_into_intermediate query_insert_intermediate::into(const table &table)
|
||||||
|
{
|
||||||
|
data_.parts.push_back(std::make_unique<query_into_part>(table, table.columns));
|
||||||
|
return {connection_, schema_, data_};
|
||||||
|
}
|
||||||
|
|
||||||
size_t query_execute_finish::execute()
|
size_t query_execute_finish::execute()
|
||||||
{
|
{
|
||||||
query_compiler compiler(connection_.dialect());
|
query_compiler compiler(connection_.dialect());
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,8 @@ add_executable(tests
|
||||||
EntityQueryBuilderTest.cpp
|
EntityQueryBuilderTest.cpp
|
||||||
models/author.hpp
|
models/author.hpp
|
||||||
models/book.hpp
|
models/book.hpp
|
||||||
FieldTest.cpp)
|
FieldTest.cpp
|
||||||
|
models/recipe.hpp)
|
||||||
|
|
||||||
target_link_libraries(tests PRIVATE
|
target_link_libraries(tests PRIVATE
|
||||||
Catch2::Catch2WithMain
|
Catch2::Catch2WithMain
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
#ifndef QUERY_RECIPE_HPP
|
||||||
|
#define QUERY_RECIPE_HPP
|
||||||
|
|
||||||
|
#include "matador/utils/access.hpp"
|
||||||
|
#include "matador/utils/field_attributes.hpp"
|
||||||
|
#include "matador/utils/foreign_attributes.hpp"
|
||||||
|
|
||||||
|
#include "matador/sql/entity.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace matador::test {
|
||||||
|
|
||||||
|
struct ingredient
|
||||||
|
{
|
||||||
|
unsigned long id{};
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
template<class Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::utils::access;
|
||||||
|
field::primary_key(op, "id", id);
|
||||||
|
field::attribute(op, "name", name, 255);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct recipe
|
||||||
|
{
|
||||||
|
unsigned long id{};
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
template<class Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::utils::access;
|
||||||
|
field::primary_key(op, "id", id);
|
||||||
|
field::attribute(op, "name", name, 255);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct recipe_ingredient
|
||||||
|
{
|
||||||
|
sql::entity<recipe> recipe_;
|
||||||
|
sql::entity<ingredient> ingredient_;
|
||||||
|
|
||||||
|
template<class Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::utils::access;
|
||||||
|
field::belongs_to(op, "recipe_id", recipe_, utils::default_foreign_attributes);
|
||||||
|
field::belongs_to(op, "ingredient_id", ingredient_, utils::default_foreign_attributes);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //QUERY_RECIPE_HPP
|
||||||
Loading…
Reference in New Issue