added timestamp code, support columns for group by and order by and tested load has-many eager code

This commit is contained in:
2026-01-08 15:14:31 +01:00
parent 0bf945cd13
commit 930b9d1aa4
34 changed files with 853 additions and 846 deletions
+108
View File
@@ -12,6 +12,9 @@
#include "models/flight.hpp"
#include "models/person.hpp"
#include "models/recipe.hpp"
#include "models/shipment.hpp"
#include "../test/utils/record_printer.hpp"
using namespace matador::object;
using namespace matador::query;
@@ -23,6 +26,8 @@ META_TABLE(ingredients, INGREDIENT, id, name);
META_TABLE(recipe_ingredients, RECIPE_INGREDIENT, recipe_id, ingredient_id);
META_TABLE(airplanes, AIRPLANE, id, brand, model);
META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
META_TABLE(shipments, SHIPMENT, id, tracking_number)
META_TABLE(packages, PACKAGE, id, weight, shipment)
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
auto result = repo.attach<airplane>("airplane")
@@ -605,4 +610,107 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
REQUIRE(row.at(3).as<std::string>().value() == std::get<3>(expected_result_two_joins[index]));
++index;
}
}
void print(std::ostream& out, const record& row) {
for (const auto& f : row) {
out << f << " ";
}
}
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation", "[query][has_many][eager]") {
auto result = repo.attach<package>("packages")
.and_then( [this] { return repo.attach<shipment>("shipments"); } );
// auto result = repo.attach<shipment>("shipments")
// .and_then( [this] { return repo.attach<package>("packages"); } );
REQUIRE(result.is_ok());
tables_to_drop.emplace("shipments");
tables_to_drop.emplace("packages");
result = repo.create(db);
REQUIRE(result.is_ok());
const std::vector shipments {
object_ptr{new shipment{1, "4711"}},
object_ptr{new shipment{2, "0815"}}
};
using namespace matador::query::meta;
for (const auto &sh: shipments) {
auto res = query::insert()
.into(SHIPMENT, SHIPMENT)
.values(*sh)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto count = query::select({count_all()})
.from(SHIPMENT)
.fetch_value<int>(db);
REQUIRE(count.is_ok());
REQUIRE(*count == 2);
std::vector packages {
object_ptr{new package{3, 15.4, shipments.at(0)}},
object_ptr{new package{4, 1.3, shipments.at(0)}},
object_ptr{new package{5, 30.9, shipments.at(1)}},
object_ptr{new package{6, 22.8, shipments.at(1)}},
object_ptr{new package{7, 17.2, shipments.at(1)}}
};
for (const auto &pkg: packages) {
auto res = query::insert()
.into(PACKAGE, PACKAGE)
.values(*pkg)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
count = query::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, SHIPMENT.tracking_number})
.from(PACKAGE)
.join_left(SHIPMENT)
.on( PACKAGE.shipment == SHIPMENT.id )
.where( PACKAGE.weight > 20.0 && SHIPMENT.tracking_number == "0815" )
.group_by({PACKAGE.id, SHIPMENT.tracking_number})
.order_by(PACKAGE.weight).asc()
.limit( 5 )
.offset( 0 )
.fetch_all(db);
REQUIRE(pkgs.is_ok());
record_printer printer(std::cout);
printer.print(*pkgs);
auto shipment_records = query::select({SHIPMENT.tracking_number, SHIPMENT.id, PACKAGE.weight, PACKAGE.weight})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment )
.fetch_all(db);
REQUIRE(shipment_records.is_ok());
printer.print(*shipment_records);
auto shipment_result = query::select({SHIPMENT.id, SHIPMENT.tracking_number, PACKAGE.id, PACKAGE.weight, PACKAGE.shipment})
.from(SHIPMENT)
.join_left(PACKAGE)
.on( SHIPMENT.id == PACKAGE.shipment )
.fetch_all<shipment>(db);
REQUIRE(shipment_result.is_ok());
std::cout << "\n";
for (const auto &s : *shipment_result) {
std::cout << s.id << " " << s.tracking_number << " packages: " << s.packages.size() << std::endl;
}
}