removed class query and moved their static methods to simple functions

This commit is contained in:
2026-03-26 09:34:32 +01:00
parent 3fc08fb7ff
commit fedce38270
23 changed files with 283 additions and 286 deletions
+57 -57
View File
@@ -46,7 +46,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
person george{7, "george", 45};
george.image.emplace_back(37);
auto res = query::insert()
auto res = insert()
.into(PERSON, {PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.values(george)
.execute(db);
@@ -54,7 +54,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
REQUIRE(res->affected_rows == 1);
// fetch person as record
auto result_record = query::select({PERSON.id, PERSON.name, PERSON.age, PERSON.image})
auto result_record = select({PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.from(PERSON)
.where(PERSON.id == 7)
.fetch_all(db);
@@ -74,7 +74,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
}
// fetch person as person
auto result_person = query::select({PERSON.id, PERSON.name, PERSON.age, PERSON.image})
auto result_person = select({PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.from(PERSON)
.where(PERSON.id == 7)
.fetch_all<person>(db);
@@ -97,7 +97,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert with returning", "[query][insert]
person george{7, "george", 45};
george.image.emplace_back(37);
auto res = query::insert()
auto res = insert()
.into(PERSON, {PERSON.id, PERSON.name, PERSON.age, PERSON.image})
.values(george)
.returning(PERSON.id)
@@ -112,26 +112,26 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert with returning", "[query][insert]
}
TEST_CASE_METHOD(QueryFixture, "Test sequence", "[query][sequence]") {
auto result = query::create()
auto result = create()
.sequence("person_seq")
.execute(db);
REQUIRE(result.is_ok());
auto next_id = query::select()
auto next_id = select()
.nextval("person_seq")
.fetch_value<uint32_t>(db);
REQUIRE(next_id.is_ok());
REQUIRE(*next_id == 1);
result = query::drop()
result = drop()
.sequence("person_seq")
.execute(db);
REQUIRE(result.is_ok());
}
TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") {
auto res = query::create()
auto res = create()
.table("person")
.columns({
column("id", matador::utils::basic_type::UInt32),
@@ -148,7 +148,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") {
REQUIRE(db.exists("person"));
tables_to_drop.emplace("person");
res = query::insert()
res = insert()
.into("person", {{"id", ""}, {"name", ""}, {"color", ""}})
.values({7, "george", "green"})
.execute(db);
@@ -156,7 +156,7 @@ TEST_CASE_METHOD(QueryFixture, "Execute insert statement", "[query][insert]") {
REQUIRE(res->affected_rows == 1);
// fetch person as record
auto result_record = query::select({"id", "name", "color"})
auto result_record = select({"id", "name", "color"})
.from("person")
.where("id"_col == 7)
.fetch_all(db);
@@ -192,7 +192,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
};
for (const auto &plane: planes) {
auto res = query::insert()
auto res = insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(plane)
.execute(db);
@@ -200,7 +200,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(AIRPLANE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
@@ -209,14 +209,14 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
flight f4711{4, planes.at(1), "hans"};
auto res = query::insert()
auto res = insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(f4711)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 1);
auto f = query::select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
auto f = select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.from(FLIGHT)
.fetch_one(db);
REQUIRE(f.is_ok());
@@ -242,7 +242,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
};
for (const auto &plane: planes) {
auto res = query::insert()
auto res = insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(plane)
.execute(db);
@@ -250,7 +250,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(AIRPLANE)
.fetch_value<int>(db).value();
REQUIRE(count == 3);
@@ -263,7 +263,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
};
for (const auto &f: flights) {
auto res = query::insert()
auto res = insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(f)
.execute(db);
@@ -271,7 +271,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(res->affected_rows == 1);
}
auto flight_result = query::select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
auto flight_result = select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.from(FLIGHT)
.fetch_one(db);
REQUIRE(flight_result.is_ok());
@@ -283,7 +283,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
const auto f = FLIGHT.as("f");
const auto ap = AIRPLANE.as("ap");
auto select_result = query::select({f.id, ap.brand, ap.model, f.pilot_name})
auto select_result = select({f.id, ap.brand, ap.model, f.pilot_name})
.from(f)
.join_left(ap)
.on(f.airplane_id == ap.id)
@@ -323,7 +323,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
};
for (const auto &plane: planes) {
auto res = query::insert()
auto res = insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(plane)
.execute(db);
@@ -331,7 +331,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(AIRPLANE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
@@ -346,7 +346,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
};
for (const auto &f: flights) {
auto res = query::insert()
auto res = insert()
.into(FLIGHT, {FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.values(f)
.execute(db);
@@ -354,7 +354,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(res->affected_rows == 1);
}
auto flight_result = query::select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
auto flight_result = select({FLIGHT.id, FLIGHT.airplane_id, FLIGHT.pilot_name})
.from(FLIGHT)
.fetch_one(db);
REQUIRE(flight_result.is_ok());
@@ -366,7 +366,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
const auto f = FLIGHT.as("f");
const auto ap = AIRPLANE.as("ap");
auto select_result = query::select({f.id, f.airplane_id, ap.brand, ap.model, f.pilot_name})
auto select_result = select({f.id, f.airplane_id, ap.brand, ap.model, f.pilot_name})
.from(f)
.join_left(ap)
.on(f.airplane_id == ap.id)
@@ -406,7 +406,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
};
for (const auto &i: ingredients) {
auto res = query::insert()
auto res = insert()
.into(INGREDIENT, {INGREDIENT.id, INGREDIENT.name})
.values(i)
.execute(db);
@@ -421,7 +421,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
};
for (const auto &r: recipes) {
auto res = query::insert()
auto res = insert()
.into(RECIPE, {RECIPE.id, RECIPE.name})
.values(r)
.execute(db);
@@ -441,7 +441,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
};
for (const auto & [recipe_id, ingredient_id]: recipe_ingredients) {
auto res = query::insert()
auto res = insert()
.into(RECIPE_INGREDIENT, {RECIPE_INGREDIENT.recipe_id, RECIPE_INGREDIENT.ingredient_id})
.values({recipe_id, ingredient_id})
.execute(db);
@@ -454,7 +454,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
const auto ri= RECIPE_INGREDIENT.as("ri");
const auto i = INGREDIENT.as("i");
auto select_result = query::select({ r.id, r.name, ri.ingredient_id})
auto select_result = select({ r.id, r.name, ri.ingredient_id})
.from(r)
.join_left(ri)
.on(r.id == ri.recipe_id)
@@ -480,7 +480,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
++index;
}
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
select_result = select({r.id, r.name, ri.ingredient_id, i.name})
.from(r)
.join_left(ri).on(r.id == ri.recipe_id)
.join_left(i).on(ri.ingredient_id == i.id)
@@ -507,7 +507,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
++index;
}
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
select_result = select({r.id, r.name, ri.ingredient_id, i.name})
.from(r)
.join_left(ri).on(r.id == ri.recipe_id)
.join_left(i).on(ri.ingredient_id == i.id)
@@ -546,7 +546,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
};
for (const auto &sh: shipments) {
auto res = query::insert()
auto res = insert()
.into(SHIPMENT, SHIPMENT)
.values(sh)
.execute(db);
@@ -554,7 +554,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(SHIPMENT)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
@@ -569,7 +569,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
};
for (const auto &pkg: packages) {
auto res = query::insert()
auto res = insert()
.into(PACKAGE, PACKAGE)
.values(pkg)
.execute(db);
@@ -577,13 +577,13 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(res->affected_rows == 1);
}
count = query::select({count_all()})
count = select({count_all()})
.from(PACKAGE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto pkgs = query::select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id, SHIPMENT.tracking_number})
auto pkgs = select({PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id, SHIPMENT.tracking_number})
.from(PACKAGE)
.join_left(SHIPMENT)
.on( PACKAGE.shipment_id == SHIPMENT.id )
@@ -599,7 +599,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
record_printer printer(std::cout);
printer.print(*pkgs);
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
auto shipment_records = select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment_id )
@@ -609,7 +609,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
printer.print(*shipment_records);
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id})
auto shipment_result = select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment_id})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment_id )
@@ -657,7 +657,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
};
for (const auto &a: authors) {
auto res = query::insert()
auto res = insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(a)
.execute(db);
@@ -665,14 +665,14 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(AUTHOR)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &b: books) {
auto res = query::insert()
auto res = insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(b)
.execute(db);
@@ -680,13 +680,13 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy has many relation", "
REQUIRE(res->affected_rows == 1);
}
count = query::select({count_all()})
count = select({count_all()})
.from(BOOK)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 10);
auto author_result = query::select({AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
auto author_result = select({AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.from(AUTHOR)
.order_by(AUTHOR.id).asc()
.fetch_all<author>(db);
@@ -734,7 +734,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
};
for (const auto &dep: deps) {
auto res = query::insert()
auto res = insert()
.into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name})
.values(dep)
.execute(db);
@@ -742,14 +742,14 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(DEPARTMENT)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &emp: emps) {
auto res = query::insert()
auto res = insert()
.into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
.values(emp)
.execute(db);
@@ -757,13 +757,13 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
REQUIRE(res->affected_rows == 1);
}
count = query::select({count_all()})
count = select({count_all()})
.from(EMPLOYEE)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
auto emps_result = select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
.from(EMPLOYEE)
.order_by(EMPLOYEE.id).asc()
.fetch_all<employee>(db);
@@ -805,7 +805,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
};
for (const auto &a: authors) {
auto res = query::insert()
auto res = insert()
.into(AUTHOR, {AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished})
.values(a)
.execute(db);
@@ -813,14 +813,14 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
REQUIRE(res->affected_rows == 1);
}
auto count = query::select({count_all()})
auto count = select({count_all()})
.from(AUTHOR)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
for (const auto &b: books) {
auto res = query::insert()
auto res = insert()
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
.values(b)
.execute(db);
@@ -828,13 +828,13 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
REQUIRE(res->affected_rows == 1);
}
count = query::select({count_all()})
count = select({count_all()})
.from(BOOK)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 10);
auto books_result = query::select({BOOK.id, BOOK.title, AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished, BOOK.published_in})
auto books_result = select({BOOK.id, BOOK.title, AUTHOR.id, AUTHOR.first_name, AUTHOR.last_name, AUTHOR.date_of_birth, AUTHOR.year_of_birth, AUTHOR.distinguished, BOOK.published_in})
.from(BOOK)
.join_left(AUTHOR)
.on(BOOK.author_id == AUTHOR.id)
@@ -881,7 +881,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many to many rel
};
for (const auto &i: ingredients) {
auto res = query::insert()
auto res = insert()
.into(INGREDIENT, {INGREDIENT.id, INGREDIENT.name})
.values(i)
.execute(db);
@@ -896,7 +896,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many to many rel
};
for (const auto &r: recipes) {
auto res = query::insert()
auto res = insert()
.into(RECIPE, {RECIPE.id, RECIPE.name})
.values(r)
.execute(db);
@@ -916,7 +916,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many to many rel
};
for (const auto & [recipe_id, ingredient_id]: recipe_ingredients) {
auto res = query::insert()
auto res = insert()
.into(RECIPE_INGREDIENT, {RECIPE_INGREDIENT.recipe_id, RECIPE_INGREDIENT.ingredient_id})
.values({recipe_id, ingredient_id})
.execute(db);
@@ -928,7 +928,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many to many rel
const auto ri= RECIPE_INGREDIENT.as("ri");
const auto i = INGREDIENT.as("i");
auto recipes_result = query::select({r.id, r.name })
auto recipes_result = select({r.id, r.name })
.from(r)
.order_by({r.id}).asc()
.fetch_all<recipe>(db);
@@ -941,7 +941,7 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many to many rel
}
}
auto ingredients_result = query::select({i.id, i.name, ri.recipe_id, r.name})
auto ingredients_result = select({i.id, i.name, ri.recipe_id, r.name})
.from(i)
.join_left(ri).on(i.id == ri.ingredient_id)
.join_left(r).on(ri.recipe_id == r.id)