implemented lazy loading for object_ptr and belongs_to

This commit is contained in:
2026-01-15 12:40:54 +01:00
parent db8e137c11
commit 7626331866
81 changed files with 1790 additions and 927 deletions
+77 -67
View File
@@ -27,7 +27,7 @@ using namespace matador::test;
using namespace matador::query::meta;
TEST_CASE_METHOD(QueryFixture, "Create table with foreign key relation", "[query][foreign][relation]") {
auto result = repo.attach<airplane>("airplane")
const auto result = repo.attach<airplane>("airplane")
.and_then([this] { return repo.attach<flight>("flight");})
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
@@ -81,9 +81,9 @@ TEST_CASE_METHOD(QueryFixture, "Execute select statement with where clause", "[q
REQUIRE(result_person.is_ok());
for (const auto &i: *result_person) {
REQUIRE(i.id == 7);
REQUIRE(i.name == "george");
REQUIRE(i.age == 45);
REQUIRE(i->id == 7);
REQUIRE(i->name == "george");
REQUIRE(i->age == 45);
}
}
@@ -143,9 +143,9 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
object_ptr(std::make_shared<airplane>(1, "Airbus", "A380")),
object_ptr(std::make_shared<airplane>(2, "Boeing", "707")),
object_ptr(std::make_shared<airplane>(3, "Boeing", "747"))
};
for (const auto &plane: planes) {
@@ -163,6 +163,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key", "[query][for
REQUIRE(count.is_ok());
REQUIRE(*count == 3);
flight f4711{4, planes.at(1), "hans"};
auto res = query::insert()
@@ -192,9 +193,9 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
object_ptr(std::make_shared<airplane>(1, "Airbus", "A380")),
object_ptr(std::make_shared<airplane>(2, "Boeing", "707")),
object_ptr(std::make_shared<airplane>(3, "Boeing", "747"))
};
for (const auto &plane: planes) {
@@ -212,10 +213,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and join_left"
REQUIRE(count == 3);
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
object_ptr(std::make_shared<flight>(4, planes.at(0), "hans")),
object_ptr(std::make_shared<flight>(5, planes.at(0), "otto")),
object_ptr(std::make_shared<flight>(6, planes.at(1), "george")),
object_ptr(std::make_shared<flight>(7, planes.at(2), "paul"))
};
for (const auto &f: flights) {
@@ -267,13 +268,15 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
repo.initialize_executor(db);
REQUIRE(db.exists(AIRPLANE.table_name()));
REQUIRE(db.exists(FLIGHT.table_name()));
std::vector planes{
object_ptr(new airplane{1, "Airbus", "A380"}),
object_ptr(new airplane{2, "Boeing", "707"}),
object_ptr(new airplane{3, "Boeing", "747"})
object_ptr(std::make_shared<airplane>(1, "Airbus", "A380")),
object_ptr(std::make_shared<airplane>(2, "Boeing", "707")),
object_ptr(std::make_shared<airplane>(3, "Boeing", "747"))
};
for (const auto &plane: planes) {
@@ -293,10 +296,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
REQUIRE(count->value() == 3);
std::vector flights{
object_ptr(new flight{4, planes.at(0), "hans"}),
object_ptr(new flight{5, planes.at(0), "otto"}),
object_ptr(new flight{6, planes.at(1), "george"}),
object_ptr(new flight{7, planes.at(2), "paul"})
object_ptr(std::make_shared<flight>(4, planes.at(0), "hans")),
object_ptr(std::make_shared<flight>(5, planes.at(0), "otto")),
object_ptr(std::make_shared<flight>(6, planes.at(1), "george")),
object_ptr(std::make_shared<flight>(7, planes.at(2), "paul"))
};
for (const auto &f: flights) {
@@ -495,8 +498,8 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
REQUIRE(result.is_ok());
const std::vector shipments {
object_ptr{new shipment{1, "4711"}},
object_ptr{new shipment{2, "0815"}}
object_ptr{std::make_shared<shipment>(1, "4711")},
object_ptr{std::make_shared<shipment>(2, "0815")}
};
for (const auto &sh: shipments) {
@@ -515,11 +518,11 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
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)}}
object_ptr{std::make_shared<package>(3, 15.4, shipments.at(0))},
object_ptr{std::make_shared<package>(4, 1.3, shipments.at(0))},
object_ptr{std::make_shared<package>(5, 30.9, shipments.at(1))},
object_ptr{std::make_shared<package>(6, 22.8, shipments.at(1))},
object_ptr{std::make_shared<package>(7, 17.2, shipments.at(1))}
};
for (const auto &pkg: packages) {
@@ -576,9 +579,9 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
std::vector<size_t> packages_sizes{2, 3};
std::cout << "\n";
for (const auto &s : *shipment_result) {
REQUIRE(s.id == shipments.at(index)->id);
REQUIRE(s.tracking_number == shipments.at(index)->tracking_number);
REQUIRE(s.packages.size() == packages_sizes.at(index++));
REQUIRE(s->id == shipments.at(index)->id);
REQUIRE(s->tracking_number == shipments.at(index)->tracking_number);
REQUIRE(s->packages.size() == packages_sizes.at(index++));
}
}
@@ -588,17 +591,19 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
.and_then([this] {return repo.create(db); });
REQUIRE(result.is_ok());
repo.initialize_executor(db);
const std::vector deps {
object_ptr{new department{1, "Human Resources"}},
object_ptr{new department{2, "Invoices"}}
object_ptr{std::make_shared<department>(1, "Human Resources")},
object_ptr{std::make_shared<department>(2, "Invoices")}
};
const std::vector emps {
object_ptr{new employee{3, "Hans", "Wurst", deps[0]}},
object_ptr{new employee{4, "Steven", "Spielberg", deps[0]}},
object_ptr{new employee{5, "Julia", "Roberts", deps[0]}},
object_ptr{new employee{6, "Otto", "Walkes", deps[1]}},
object_ptr{new employee{7, "Miss", "Marple", deps[1]}},
object_ptr{std::make_shared<employee>(3, "Hans", "Wurst", deps[0])},
object_ptr{std::make_shared<employee>(4, "Steven", "Spielberg", deps[0])},
object_ptr{std::make_shared<employee>(5, "Julia", "Roberts", deps[0])},
object_ptr{std::make_shared<employee>(6, "Otto", "Walkes", deps[1])},
object_ptr{std::make_shared<employee>(7, "Miss", "Marple", deps[1])},
};
for (const auto &dep: deps) {
@@ -631,21 +636,26 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation",
REQUIRE(count.is_ok());
REQUIRE(*count == 5);
auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, DEPARTMENT.id, DEPARTMENT.name})
// auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, DEPARTMENT.id, DEPARTMENT.name})
// .from(EMPLOYEE)
// .join_left(DEPARTMENT)
// .on(EMPLOYEE.dep_id == DEPARTMENT.id)
// .order_by(EMPLOYEE.id).asc()
// .fetch_all<employee>(db);
auto emps_result = query::select({EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
.from(EMPLOYEE)
.join_left(DEPARTMENT)
.on(EMPLOYEE.dep_id == DEPARTMENT.id)
.order_by(EMPLOYEE.id).asc()
.fetch_all<employee>(db);
REQUIRE(emps_result.is_ok());
size_t index{0};
for (const auto &e : *emps_result) {
REQUIRE(e.id == emps.at(index)->id);
REQUIRE(e.first_name == emps.at(index)->first_name);
REQUIRE(e.last_name == emps.at(index)->last_name);
REQUIRE(e.dep->id == emps.at(index)->dep->id);
REQUIRE(e.dep->name == emps.at(index)->dep->name);
REQUIRE(e->id == emps.at(index)->id);
REQUIRE(e->first_name == emps.at(index)->first_name);
REQUIRE(e->last_name == emps.at(index)->last_name);
REQUIRE(e->dep->id == emps.at(index)->dep->id);
REQUIRE(e->dep->name == emps.at(index)->dep->name);
++index;
}
}
@@ -658,21 +668,21 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
} );
const std::vector authors {
object_ptr{new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}}},
object_ptr{new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}}}
object_ptr{std::make_shared<author>(1, "Michael", "Crichton", "23.10.1942", 1975, true)},
object_ptr{std::make_shared<author>( 2, "Steven", "King", "21.9.1947", 1956, false)}
};
const std::vector books {
object_ptr{new book{3, "Jurassic Park", authors[0], 1990}},
object_ptr{new book{4, "Timeline", authors[0], 1999}},
object_ptr{new book{5, "The Andromeda Strain", authors[0], 1969}},
object_ptr{new book{6, "Congo", authors[0], 1980}},
object_ptr{new book{7, "Prey", authors[0], 2002}},
object_ptr{new book{8, "Carrie", authors[1], 1974}},
object_ptr{new book{9, "The Shining", authors[1], 1977}},
object_ptr{new book{10, "It", authors[1], 1986}},
object_ptr{new book{11, "Misery", authors[1], 1987}},
object_ptr{new book{12, "The Dark Tower: The Gunslinger", authors[1], 1982}},
object_ptr{std::make_shared<book>(3, "Jurassic Park", authors[0], 1990)},
object_ptr{std::make_shared<book>(4, "Timeline", authors[0], 1999)},
object_ptr{std::make_shared<book>(5, "The Andromeda Strain", authors[0], 1969)},
object_ptr{std::make_shared<book>(6, "Congo", authors[0], 1980)},
object_ptr{std::make_shared<book>(7, "Prey", authors[0], 2002)},
object_ptr{std::make_shared<book>(8, "Carrie", authors[1], 1974)},
object_ptr{std::make_shared<book>(9, "The Shining", authors[1], 1977)},
object_ptr{std::make_shared<book>(10, "It", authors[1], 1986)},
object_ptr{std::make_shared<book>(11, "Misery", authors[1], 1987)},
object_ptr{std::make_shared<book>(12, "The Dark Tower: The Gunslinger", authors[1], 1982)},
};
for (const auto &a: authors) {
@@ -715,15 +725,15 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation"
REQUIRE(books_result.is_ok());
size_t index{0};
for (const auto &b : *books_result) {
REQUIRE(b.id == books.at(index)->id);
REQUIRE(b.title == books.at(index)->title);
REQUIRE(b.book_author->id == books.at(index)->book_author->id);
REQUIRE(b.book_author->first_name == books.at(index)->book_author->first_name);
REQUIRE(b.book_author->last_name == books.at(index)->book_author->last_name);
REQUIRE(b.book_author->date_of_birth == books.at(index)->book_author->date_of_birth);
REQUIRE(b.book_author->year_of_birth == books.at(index)->book_author->year_of_birth);
REQUIRE(b.book_author->distinguished == books.at(index)->book_author->distinguished);
REQUIRE(b.published_in == books.at(index)->published_in);
REQUIRE(b->id == books.at(index)->id);
REQUIRE(b->title == books.at(index)->title);
REQUIRE(b->book_author->id == books.at(index)->book_author->id);
REQUIRE(b->book_author->first_name == books.at(index)->book_author->first_name);
REQUIRE(b->book_author->last_name == books.at(index)->book_author->last_name);
REQUIRE(b->book_author->date_of_birth == books.at(index)->book_author->date_of_birth);
REQUIRE(b->book_author->year_of_birth == books.at(index)->book_author->year_of_birth);
REQUIRE(b->book_author->distinguished == books.at(index)->book_author->distinguished);
REQUIRE(b->published_in == books.at(index)->published_in);
++index;
}
}