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
+3 -3
View File
@@ -79,7 +79,7 @@ TEST_CASE_METHOD(QueryFixture, "Insert and select basic datatypes", "[query][dat
.from("types")
.fetch_one<types>(db);
REQUIRE(result.is_ok());
REQUIRE(*result != nullptr);
REQUIRE(result->get() != nullptr);
REQUIRE((*result)->id_ == 1);
REQUIRE((*result)->char_ == cval);
@@ -356,7 +356,7 @@ TEST_CASE_METHOD(QueryFixture, "Test primary key", "[query][primary key]") {
.from("pk")
.fetch_one<pk>(db);
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(row->get() != nullptr);
REQUIRE(row.value()->id > 0);
}
@@ -386,7 +386,7 @@ TEST_CASE_METHOD(QueryFixture, "Test primary key prepared", "[query][primary key
auto row = stmt->fetch_one<pk>();
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(*row);
REQUIRE(row.value()->id > 0);
REQUIRE(row.value()->name == "george");
}
+18 -22
View File
@@ -70,7 +70,7 @@ TEST_CASE_METHOD(QueryFixture, "Test insert statement", "[query][statement][inse
.fetch_one<person>(db);
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(*row);
REQUIRE((*row)->id == 1);
REQUIRE((*row)->name == "george");
REQUIRE((*row)->age == 45);
@@ -101,7 +101,7 @@ TEST_CASE_METHOD(QueryFixture, "Test update statement", "[query][statement][upda
.fetch_one<person>(db);
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(*row);
REQUIRE((*row)->id == 1);
REQUIRE((*row)->name == "george");
REQUIRE((*row)->age == 45);
@@ -126,7 +126,7 @@ TEST_CASE_METHOD(QueryFixture, "Test update statement", "[query][statement][upda
.fetch_one<person>(db);
REQUIRE(row.is_ok());
REQUIRE(*row != nullptr);
REQUIRE(*row);
REQUIRE((*row)->id == 1);
REQUIRE((*row)->name == "george");
REQUIRE((*row)->age == 36);
@@ -170,12 +170,12 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
.fetch<person>();
REQUIRE(rows.is_ok());
for (const auto &r : *rows) {
for (const auto r : *rows) {
constexpr size_t index = 1;
REQUIRE(r.id == peoples[index].id);
REQUIRE(r.name == peoples[index].name);
REQUIRE(r.age == peoples[index].age);
REQUIRE(r.image == peoples[index].image);
REQUIRE(r->id == peoples[index].id);
REQUIRE(r->name == peoples[index].name);
REQUIRE(r->age == peoples[index].age);
REQUIRE(r->image == peoples[index].image);
}
stmt = query::remove()
@@ -192,9 +192,7 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
select_stmt->reset();
auto row = select_stmt->bind(0, "jane")
.fetch_one<person>();
REQUIRE(row.is_ok());
REQUIRE(*row == nullptr);
REQUIRE(!row);
stmt->reset();
res = stmt->bind(0, "merlin")
@@ -205,9 +203,7 @@ TEST_CASE_METHOD(QueryFixture, "Test delete statement", "[query][statement][dele
select_stmt->reset();
row = select_stmt->bind(0, "merlin")
.fetch_one<person>();
REQUIRE(row.is_ok());
REQUIRE(*row == nullptr);
REQUIRE(!row);
}
TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][statement][reuse]") {
@@ -247,10 +243,10 @@ TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][stateme
size_t index = 0;
for (const auto &r : *rows) {
REQUIRE(r.id == peoples[index].id);
REQUIRE(r.name == peoples[index].name);
REQUIRE(r.age == peoples[index].age);
REQUIRE(r.image == peoples[index].image);
REQUIRE(r->id == peoples[index].id);
REQUIRE(r->name == peoples[index].name);
REQUIRE(r->age == peoples[index].age);
REQUIRE(r->image == peoples[index].image);
++index;
}
@@ -261,10 +257,10 @@ TEST_CASE_METHOD(QueryFixture, "Test reuse prepared statement", "[query][stateme
index = 0;
for (const auto &r : *rows) {
REQUIRE(r.id == peoples[index].id);
REQUIRE(r.name == peoples[index].name);
REQUIRE(r.age == peoples[index].age);
REQUIRE(r.image == peoples[index].image);
REQUIRE(r->id == peoples[index].id);
REQUIRE(r->name == peoples[index].name);
REQUIRE(r->age == peoples[index].age);
REQUIRE(r->image == peoples[index].image);
++index;
}
}
+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;
}
}
+6 -4
View File
@@ -7,13 +7,15 @@
namespace matador::test {
SessionFixture::SessionFixture()
: pool(connection::dns, 4)
, ses({bus, pool}, schema) {}
: ses({bus, connection::dns, 4}, schema)
, db(connection::dns) {
REQUIRE(db.open());
}
SessionFixture::~SessionFixture() {
const auto conn = pool.acquire();
const auto result = schema.drop(*conn);
const auto result = schema.drop(db);
REQUIRE(result.is_ok());
REQUIRE(db.close());
}
void SessionFixture::drop_table_if_exists(const std::string &table_name) const {
+1 -1
View File
@@ -15,9 +15,9 @@ public:
~SessionFixture();
protected:
sql::connection_pool pool;
orm::session ses;
utils::message_bus bus;
sql::connection db;
query::schema schema;
+25 -38
View File
@@ -18,8 +18,7 @@ using namespace matador::test;
TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
return schema.create(db);
} );
REQUIRE(result.is_ok());
@@ -37,8 +36,7 @@ TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
const auto res = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
return schema.create(db);
} );
REQUIRE(res.is_ok());
@@ -69,12 +67,11 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
const auto res = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
.and_then([this] { return schema.create(db); } );
REQUIRE(res.is_ok());
schema.initialize_executor(ses);
auto result = ses.insert<airplane>(1, "Boeing", "747");
REQUIRE(result.is_ok());
@@ -94,10 +91,7 @@ TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] { return schema.attach<flight>("flights"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
auto plane = ses.insert<airplane>(1, "Boeing", "A380");
@@ -118,10 +112,7 @@ TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]")
TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session][find]") {
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
@@ -141,8 +132,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
const auto result = schema.attach<airplane>("airplanes")
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
return schema.create(db);
} );
REQUIRE(result.is_ok());
@@ -167,9 +157,9 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
auto all_planes = find_result.release();
size_t index {0};
for (const auto &i: all_planes) {
REQUIRE(i.id == std::get<0>(expected_result[index]));
REQUIRE(i.brand == std::get<1>(expected_result[index]));
REQUIRE(i.model == std::get<2>(expected_result[index]));
REQUIRE(i->id == std::get<0>(expected_result[index]));
REQUIRE(i->brand == std::get<1>(expected_result[index]));
REQUIRE(i->model == std::get<2>(expected_result[index]));
++index;
}
}
@@ -177,17 +167,16 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many lazy relation", "[session][find][one-to-many][eager]") {
auto result = schema.attach<author>("authors")
.and_then( [this] { return schema.attach<book>("books"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
} );
.and_then([this] { return schema.create(db); } );
std::vector<std::unique_ptr<author>> authors;
authors.emplace_back(new author{1, "Michael", "Crichton", "23.10.1942", 1975, true, {}});
authors.emplace_back(new author{ 2, "Steven", "King", "21.9.1947", 1956, false, {}});
schema.initialize_executor(ses);
std::vector authors {
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)}
};
for (auto &&a: authors) {
auto res = ses.insert(a.release());
for (const auto &a: authors) {
auto res = ses.insert(a);
REQUIRE(res.is_ok());
}
@@ -197,7 +186,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
std::vector<object_ptr<author>> author_repo;
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
std::cout << "author: " << it->first_name << " (books: " << it->books.size() << ")\n";
author_repo.emplace_back(it.release());
author_repo.emplace_back(it.optr());
}
REQUIRE(author_repo.size() == 2);
@@ -231,13 +220,12 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
auto result = schema.attach<department>("departments")
.and_then( [this] { return schema.attach<employee>("employees"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
return schema.create(db);
} );
std::vector<std::unique_ptr<department>> departments;
departments.emplace_back(new department{1, "Insurance", {}});
departments.emplace_back(new department{2, "Invoice", {}});
departments.emplace_back(new department{1, "Insurance"});
departments.emplace_back(new department{2, "Invoice"});
for (auto &&a: departments) {
auto res = ses.insert(a.release());
@@ -250,7 +238,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
std::vector<object_ptr<department>> departments_repo;
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
std::cout << "department: " << it->name << " (employees: " << it->employees.size() << ")\n";
departments_repo.emplace_back(it.release());
departments_repo.emplace_back(it.optr());
}
REQUIRE(departments_repo.size() == 2);
@@ -287,8 +275,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m
auto result = schema.attach<recipe>("recipes")
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } )
.and_then([this] {
const auto conn = pool.acquire();
return schema.create(*conn);
return schema.create(db);
} );
std::vector<std::unique_ptr<ingredient>> ingredients;
+17 -23
View File
@@ -21,24 +21,18 @@ using namespace matador::query;
using namespace matador::test;
using namespace matador::query::meta;
namespace matador::test::detail {
template<class Type, typename... Args>
[[maybe_unused]] object_ptr<Type> make_object_ptr(Args&&... args) {
return object_ptr(new Type(std::forward<Args>(args)...));
}
}
class StatementTestFixture : public QueryFixture {
public:
StatementTestFixture() {
REQUIRE(repo.attach<airplane>("airplanes"));
repo.initialize_executor(db);
}
protected:
std::vector<object_ptr<airplane>> planes{
matador::test::detail::make_object_ptr<airplane>(1, "Airbus", "A380"),
matador::test::detail::make_object_ptr<airplane>(2, "Boeing", "707"),
matador::test::detail::make_object_ptr<airplane>(3, "Boeing", "747")
std::vector<airplane> planes {
{1, "Airbus", "A380"},
{2, "Boeing", "707"},
{3, "Boeing", "747"}
};
};
@@ -53,7 +47,7 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
REQUIRE(stmt.is_ok());
for (const auto &plane: planes) {
auto res = stmt->bind(*plane).execute();
auto res = stmt->bind(plane).execute();
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
stmt->reset();
@@ -66,9 +60,9 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
REQUIRE(result.is_ok());
size_t index{0};
for (const auto &i: *result) {
REQUIRE(i.id == planes[index]->id);
REQUIRE(i.brand == planes[index]->brand);
REQUIRE(i.model == planes[index++]->model);
REQUIRE(i->id == planes[index].id);
REQUIRE(i->brand == planes[index].brand);
REQUIRE(i->model == planes[index++].model);
}
}
@@ -76,13 +70,13 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
for (const auto &plane: planes) {
auto res = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.values(*plane)
.values(plane)
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(*res == 1);
}
auto stmt = query::select(generator::columns<airplane>(repo))
auto stmt = query::select({AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})
.from(AIRPLANE)
.where(AIRPLANE.brand == _)
.prepare(db);
@@ -93,9 +87,9 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
REQUIRE(result.is_ok());
for (const auto &i: *result) {
REQUIRE(i.id == planes[0]->id);
REQUIRE(i.brand == planes[0]->brand);
REQUIRE(i.model == planes[0]->model);
REQUIRE(i->id == planes[0].id);
REQUIRE(i->brand == planes[0].brand);
REQUIRE(i->model == planes[0].model);
}
stmt->reset();
@@ -106,9 +100,9 @@ TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]
size_t index{1};
REQUIRE(result.is_ok());
for (const auto &i: *result) {
REQUIRE(i.id == planes[index]->id);
REQUIRE(i.brand == planes[index]->brand);
REQUIRE(i.model == planes[index++]->model);
REQUIRE(i->id == planes[index].id);
REQUIRE(i->brand == planes[index].brand);
REQUIRE(i->model == planes[index++].model);
}
}
}
+7
View File
@@ -12,6 +12,13 @@ namespace matador::test {
struct book;
struct author {
author() = default;
author(const unsigned int id, std::string first_name, std::string last_name, std::string date_of_birth, const unsigned short year_of_birth, const bool distinguished)
: id(id), first_name(std::move(first_name))
, last_name(std::move(last_name))
, date_of_birth(std::move(date_of_birth))
, year_of_birth(year_of_birth)
, distinguished(distinguished) {}
unsigned int id{};
std::string first_name;
std::string last_name;
+3
View File
@@ -12,6 +12,9 @@ namespace matador::test {
struct author;
struct book {
book() = default;
book(const unsigned int id, std::string title, object::object_ptr<author> author, unsigned short published_in)
: id(id), title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
unsigned int id{};
std::string title;
object::object_ptr<author> book_author;
+6
View File
@@ -11,6 +11,9 @@ namespace matador::test {
struct employee;
struct department {
department() = default;
department(const unsigned int id, std::string name)
: id(id), name(std::move(name)) {}
unsigned int id{};
std::string name;
std::vector<object::object_ptr<employee>> employees{};
@@ -27,6 +30,9 @@ struct department {
};
struct employee {
employee() = default;
employee(const unsigned int id, std::string first, std::string last, object::object_ptr<department> dep)
: id(id), first_name(std::move(first)), last_name(std::move(last)), dep(std::move(dep)) {}
unsigned int id{};
std::string first_name;
std::string last_name;
+4 -5
View File
@@ -5,7 +5,6 @@
#include "supplier.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/cascade_type.hpp"
#include "matador/object/object_ptr.hpp"
@@ -14,8 +13,8 @@
namespace matador::test {
struct product {
std::string product_name;
object::object_ptr<supplier> supplier;
object::object_ptr<category> category;
object::object_ptr<supplier> seller;
object::object_ptr<category> type;
std::string quantity_per_unit;
unsigned int unit_price;
unsigned int units_in_stock;
@@ -28,8 +27,8 @@ struct product {
namespace field = matador::access;
using namespace matador::utils;
field::primary_key(op, "product_name", product_name, 255);
field::belongs_to(op, "supplier_id", supplier, CascadeAllFetchLazy);
field::belongs_to(op, "category_id", category, CascadeAllFetchLazy);
field::belongs_to(op, "supplier_id", seller, CascadeAllFetchLazy);
field::belongs_to(op, "category_id", type, CascadeAllFetchLazy);
field::attribute(op, "quantity_per_unit", quantity_per_unit, 255);
field::attribute(op, "unit_price", unit_price);
field::attribute(op, "units_in_stock", units_in_stock);
+8
View File
@@ -13,6 +13,10 @@ namespace matador::test {
struct package;
struct shipment {
shipment() = default;
shipment(const long id, std::string tracking_number)
: id(id), tracking_number(std::move(tracking_number)) {}
long id{};
std::string tracking_number;
object::collection<object::object_ptr<package> > packages{};
@@ -27,6 +31,10 @@ struct shipment {
};
struct package {
package() = default;
package(const long id, double weight, object::object_ptr<shipment> del)
: id(id), weight(weight), delivery(std::move(del)){}
long id{};
double weight{};
object::object_ptr<shipment> delivery;
+8 -5
View File
@@ -49,7 +49,10 @@ utils::result<size_t, utils::error> test_connection::execute(const std::string &
}
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> test_connection::fetch(const sql::query_context &context) {
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(), context.prototype, context.prototype.size()));
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(),
context.prototype,
context.resolver_factory,
context.prototype.size()));
}
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> test_connection::prepare(const sql::query_context &context) {
@@ -61,12 +64,12 @@ utils::result<std::vector<object::attribute>, utils::error> test_connection::des
return utils::ok(std::vector<object::attribute>{});
}
utils::result<bool, utils::error> test_connection::exists(const std::string &/*schema_name*/, const std::string &/*table_name*/) {
utils::result<bool, utils::error> test_connection::exists(const std::string &/*schema_name*/,
const std::string &/*table_name*/) {
return utils::ok(false);
}
std::string test_connection::to_escaped_string(const utils::blob_type_t& value) const {
std::string test_connection::to_escaped_string(const utils::blob_type_t &value) const {
return utils::to_string(value);
}
}
}
+4 -1
View File
@@ -19,7 +19,10 @@ utils::result<size_t, utils::error> test_statement::execute(const sql::parameter
}
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> test_statement::fetch(const sql::parameter_binder &/*bindings*/) {
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(), query_.prototype, query_.prototype.size()));
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<test_result_reader>(),
query_.prototype,
query_.resolver_factory,
query_.prototype.size()));
}
void test_statement::reset() {}
+7 -11
View File
@@ -12,11 +12,8 @@
#include "matador/query/table.hpp"
#include "matador/query/schema.hpp"
#include "matador/utils/macro_map.hpp"
#include "matador/query/query_builder.hpp"
#include "matador/orm/session_query_builder.hpp"
#include "../backend/test_connection.hpp"
#include "../backend/test_backend_service.hpp"
#include "../../models/airplane.hpp"
@@ -29,7 +26,6 @@
#include "../../models/student.hpp"
using namespace matador::object;
using namespace matador::orm;
using namespace matador::query;
using namespace matador::utils;
using namespace matador::sql;
@@ -46,7 +42,7 @@ TEST_CASE("Create sql query data for entity with eager has one", "[query][entity
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
REQUIRE(result);
session_query_builder eqb(scm, db);
query_builder eqb(scm, db);
const auto it = scm.find(typeid(flight));
REQUIRE(it != scm.end());
@@ -100,7 +96,7 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
.and_then( [&scm] { return scm.attach<book>("books"); } );
REQUIRE(result);
session_query_builder eqb(scm, db);
query_builder eqb(scm, db);
const auto it = scm.find(typeid(book));
REQUIRE(it != scm.end());
@@ -172,7 +168,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
.and_then( [&scm] { return scm.attach<order>("orders"); } );
REQUIRE(result);
session_query_builder eqb(scm, db);
query_builder eqb(scm, db);
const auto it = scm.find(typeid(order));
REQUIRE(it != scm.end());
@@ -236,7 +232,7 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
.and_then( [&scm] { return scm.attach<ingredient>("ingredients"); } );
REQUIRE(result);
session_query_builder eqb(scm, db);
query_builder eqb(scm, db);
const auto it = scm.find(typeid(ingredient));
REQUIRE(it != scm.end());
@@ -290,7 +286,7 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par
.and_then( [&scm] { return scm.attach<course>("courses"); } );
REQUIRE(result);
session_query_builder eqb(scm, db);
query_builder eqb(scm, db);
const auto it = scm.find(typeid(course));
REQUIRE(it != scm.end());
@@ -344,7 +340,7 @@ TEST_CASE("Test eager relationship", "[session][eager]") {
.and_then( [&scm] { return scm.attach<employee>("employees"); } );
REQUIRE(result);
session_query_builder eqb(scm, db);
query_builder eqb(scm, db);
auto data = eqb.build<department>();
REQUIRE(data.is_ok());