added belongs-to-eager test
This commit is contained in:
parent
b9f5819be5
commit
db8e137c11
|
|
@ -9,6 +9,9 @@
|
|||
#include "QueryFixture.hpp"
|
||||
|
||||
#include "models/airplane.hpp"
|
||||
#include "models/author.hpp"
|
||||
#include "models/book.hpp"
|
||||
#include "models/department.hpp"
|
||||
#include "models/flight.hpp"
|
||||
#include "models/person.hpp"
|
||||
#include "models/recipe.hpp"
|
||||
|
|
@ -578,3 +581,149 @@ TEST_CASE_METHOD(QueryFixture, "Test load entity with eager has many relation",
|
|||
REQUIRE(s.packages.size() == packages_sizes.at(index++));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test load entity with lazy belongs to relation", "[query][belongs_to][lazy]") {
|
||||
auto result = repo.attach<department>("departments")
|
||||
.and_then([this] { return repo.attach<employee>("employees"); })
|
||||
.and_then([this] {return repo.create(db); });
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
const std::vector deps {
|
||||
object_ptr{new department{1, "Human Resources"}},
|
||||
object_ptr{new 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]}},
|
||||
};
|
||||
|
||||
for (const auto &dep: deps) {
|
||||
auto res = query::insert()
|
||||
.into(DEPARTMENT, {DEPARTMENT.id, DEPARTMENT.name})
|
||||
.values(*dep)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
auto count = query::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()
|
||||
.into(EMPLOYEE, {EMPLOYEE.id, EMPLOYEE.first_name, EMPLOYEE.last_name, EMPLOYEE.dep_id})
|
||||
.values(*emp)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
count = query::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, DEPARTMENT.id, DEPARTMENT.name})
|
||||
.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);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(QueryFixture, "Test load entity with eager belongs to relation", "[query][belongs_to][eager]") {
|
||||
auto result = repo.attach<author>("authors")
|
||||
.and_then( [this] { return repo.attach<book>("books"); } )
|
||||
.and_then([this] {
|
||||
return repo.create(db);
|
||||
} );
|
||||
|
||||
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, {}}}
|
||||
};
|
||||
|
||||
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}},
|
||||
};
|
||||
|
||||
for (const auto &a: authors) {
|
||||
auto res = query::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);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
auto count = query::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()
|
||||
.into(BOOK, {BOOK.id, BOOK.title, BOOK.author_id, BOOK.published_in})
|
||||
.values(*b)
|
||||
.execute(db);
|
||||
REQUIRE(res.is_ok());
|
||||
REQUIRE(*res == 1);
|
||||
}
|
||||
|
||||
count = query::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})
|
||||
.from(BOOK)
|
||||
.join_left(AUTHOR)
|
||||
.on(BOOK.author_id == AUTHOR.id)
|
||||
.order_by(BOOK.id).asc()
|
||||
.fetch_all<book>(db);
|
||||
|
||||
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);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,27 +6,28 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct airplane {
|
||||
airplane() = default;
|
||||
|
||||
airplane(const unsigned int id, std::string b, std::string m)
|
||||
: id(id)
|
||||
, brand(std::move(b))
|
||||
, model(std::move(m)) {}
|
||||
, brand(std::move(b))
|
||||
, model(std::move(m)) {
|
||||
}
|
||||
|
||||
unsigned int id{};
|
||||
std::string brand;
|
||||
std::string model;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "brand", brand, 255);
|
||||
field::attribute(op, "model", model, 255);
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "brand", brand, 255);
|
||||
field::attribute(op, "model", model, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// namespace matador::access {
|
||||
|
|
|
|||
|
|
@ -9,31 +9,29 @@
|
|||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct book;
|
||||
|
||||
struct author {
|
||||
unsigned int id{};
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::string date_of_birth;
|
||||
unsigned short year_of_birth{};
|
||||
bool distinguished{false};
|
||||
std::vector<object::object_ptr<book>> books;
|
||||
unsigned int id{};
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::string date_of_birth;
|
||||
unsigned short year_of_birth{};
|
||||
bool distinguished{false};
|
||||
std::vector<object::object_ptr<book> > books;
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "first_name", first_name, 63);
|
||||
field::attribute(op, "last_name", last_name, 63);
|
||||
field::attribute(op, "date_of_birth", date_of_birth, 31);
|
||||
field::attribute(op, "year_of_birth", year_of_birth);
|
||||
field::attribute(op, "distinguished", distinguished);
|
||||
field::has_many(op, "books", books, "author_id", utils::fetch_type::Lazy);
|
||||
}
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "first_name", first_name, 63);
|
||||
field::attribute(op, "last_name", last_name, 63);
|
||||
field::attribute(op, "date_of_birth", date_of_birth, 31);
|
||||
field::attribute(op, "year_of_birth", year_of_birth);
|
||||
field::attribute(op, "distinguished", distinguished);
|
||||
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_AUTHOR_HPP
|
||||
|
|
|
|||
|
|
@ -9,24 +9,22 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct author;
|
||||
|
||||
struct book {
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
object::object_ptr<author> book_author;
|
||||
unsigned short published_in{};
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
object::object_ptr<author> book_author;
|
||||
unsigned short published_in{};
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 511);
|
||||
field::belongs_to(op, "author_id", book_author, utils::fetch_type::Eager);
|
||||
field::attribute(op, "published_in", published_in);
|
||||
}
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 511);
|
||||
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
|
||||
field::attribute(op, "published_in", published_in);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_BOOK_HPP
|
||||
|
|
|
|||
|
|
@ -6,19 +6,17 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct category {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_CATEGORY_HPP
|
||||
|
|
|
|||
|
|
@ -5,24 +5,20 @@
|
|||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
struct coordinate
|
||||
{
|
||||
struct coordinate {
|
||||
int x{};
|
||||
int y{};
|
||||
int z{};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace matador::access {
|
||||
|
||||
template<class Operator>
|
||||
void attribute(Operator &op, const char *id, test::coordinate &value, const utils::field_attributes &attr = utils::null_attributes) {
|
||||
attribute(op, (std::string(id) + "_x").c_str(), value.x, attr);
|
||||
attribute(op, (std::string(id) + "_y").c_str(), value.y, attr);
|
||||
attribute(op, (std::string(id) + "_z").c_str(), value.z, attr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_COORDINATE_HPP
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ struct employee;
|
|||
struct department {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<object::object_ptr<employee>> employees;
|
||||
std::vector<object::object_ptr<employee>> employees{};
|
||||
// object::object_ptr<employee> manager;
|
||||
|
||||
template<typename Operator>
|
||||
|
|
|
|||
|
|
@ -13,11 +13,12 @@
|
|||
#include <utility>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct flight {
|
||||
flight() = default;
|
||||
|
||||
flight(const unsigned int id, const object::object_ptr<airplane> &plane, std::string name)
|
||||
: id(id), plane(plane), pilot_name(std::move(name)) {}
|
||||
: id(id), plane(plane), pilot_name(std::move(name)) {
|
||||
}
|
||||
|
||||
unsigned int id{};
|
||||
object::object_ptr<airplane> plane;
|
||||
|
|
@ -32,7 +33,6 @@ struct flight {
|
|||
field::attribute(op, "pilot_name", pilot_name, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_FLIGHT_HPP
|
||||
|
|
|
|||
|
|
@ -8,21 +8,18 @@
|
|||
#include <cstdint>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
enum class Color : uint8_t {
|
||||
Green, Red, Blue, Yellow, Black, White, Brown
|
||||
};
|
||||
|
||||
struct location
|
||||
{
|
||||
struct location {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
coordinate coord;
|
||||
Color color{Color::Green};
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op)
|
||||
{
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
|
|
@ -30,7 +27,6 @@ struct location
|
|||
field::attribute(op, "color", color);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_LOCATION_HPP
|
||||
|
|
|
|||
|
|
@ -11,5 +11,9 @@ META_TABLE(flights, FLIGHT, id, airplane_id, pilot_name);
|
|||
META_TABLE(shipments, SHIPMENT, id, tracking_number)
|
||||
META_TABLE(packages, PACKAGE, id, weight, shipment_id)
|
||||
META_TABLE(persons, PERSON, id, name, age, image)
|
||||
META_TABLE(departments, DEPARTMENT, id, name)
|
||||
META_TABLE(employees, EMPLOYEE, id, first_name, last_name, dep_id)
|
||||
META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
||||
META_TABLE(books, BOOK, id, title, author_id, published_in)
|
||||
|
||||
#endif //MATADOR_MODEL_METAS_HPP
|
||||
|
|
@ -8,23 +8,20 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
struct optional {
|
||||
unsigned int id{};
|
||||
std::optional<std::string> name;
|
||||
std::optional<unsigned int> age{};
|
||||
|
||||
struct optional
|
||||
{
|
||||
unsigned int id{};
|
||||
std::optional<std::string> name;
|
||||
std::optional<unsigned int> age{};
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::attribute(op, "age", age);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::attribute(op, "age", age);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_OPTIONAL_HPP
|
||||
|
|
|
|||
|
|
@ -10,41 +10,38 @@
|
|||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
struct order {
|
||||
unsigned int order_id{};
|
||||
std::string order_date;
|
||||
std::string required_date;
|
||||
std::string shipped_date;
|
||||
unsigned int ship_via{};
|
||||
unsigned int freight{};
|
||||
std::string ship_name;
|
||||
std::string ship_address;
|
||||
std::string ship_city;
|
||||
std::string ship_region;
|
||||
std::string ship_postal_code;
|
||||
std::string ship_country;
|
||||
std::vector<object::object_ptr<order_details> > order_details_;
|
||||
|
||||
struct order
|
||||
{
|
||||
unsigned int order_id{};
|
||||
std::string order_date;
|
||||
std::string required_date;
|
||||
std::string shipped_date;
|
||||
unsigned int ship_via{};
|
||||
unsigned int freight{};
|
||||
std::string ship_name;
|
||||
std::string ship_address;
|
||||
std::string ship_city;
|
||||
std::string ship_region;
|
||||
std::string ship_postal_code;
|
||||
std::string ship_country;
|
||||
std::vector<object::object_ptr<order_details>> order_details_;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "order_id", order_id);
|
||||
field::attribute(op, "order_date", order_date, 255);
|
||||
field::attribute(op, "required_date", required_date, 255);
|
||||
field::attribute(op, "shipped_date", shipped_date, 255);
|
||||
field::attribute(op, "ship_via", ship_via);
|
||||
field::attribute(op, "freight", freight);
|
||||
field::attribute(op, "ship_name", ship_name, 255);
|
||||
field::attribute(op, "ship_address", ship_address, 255);
|
||||
field::attribute(op, "ship_city", ship_city, 255);
|
||||
field::attribute(op, "ship_region", ship_region, 255);
|
||||
field::attribute(op, "ship_postal_code", ship_postal_code, 255);
|
||||
field::attribute(op, "ship_country", ship_country, 255);
|
||||
field::has_many(op, "order_details", order_details_, "order_id", utils::fetch_type::Eager);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "order_id", order_id);
|
||||
field::attribute(op, "order_date", order_date, 255);
|
||||
field::attribute(op, "required_date", required_date, 255);
|
||||
field::attribute(op, "shipped_date", shipped_date, 255);
|
||||
field::attribute(op, "ship_via", ship_via);
|
||||
field::attribute(op, "freight", freight);
|
||||
field::attribute(op, "ship_name", ship_name, 255);
|
||||
field::attribute(op, "ship_address", ship_address, 255);
|
||||
field::attribute(op, "ship_city", ship_city, 255);
|
||||
field::attribute(op, "ship_region", ship_region, 255);
|
||||
field::attribute(op, "ship_postal_code", ship_postal_code, 255);
|
||||
field::attribute(op, "ship_country", ship_country, 255);
|
||||
field::has_many(op, "order_details", order_details_, "order_id", utils::fetch_type::Eager);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_ORDER_HPP
|
||||
|
|
|
|||
|
|
@ -8,23 +8,20 @@
|
|||
#include "matador/utils/foreign_attributes.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct order;
|
||||
|
||||
struct order_details
|
||||
{
|
||||
unsigned int order_details_id;
|
||||
matador::object::object_ptr<order> order_;
|
||||
matador::object::object_ptr<product> product_;
|
||||
struct order_details {
|
||||
unsigned int order_details_id;
|
||||
object::object_ptr<order> order_;
|
||||
object::object_ptr<product> product_;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "order_details_id", order_details_id);
|
||||
field::belongs_to(op, "order_id", order_, utils::CascadeNoneFetchLazy);
|
||||
field::has_one(op, "product_id", product_, utils::CascadeNoneFetchLazy);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "order_details_id", order_details_id);
|
||||
field::belongs_to(op, "order_id", order_, utils::CascadeNoneFetchLazy);
|
||||
field::has_one(op, "product_id", product_, utils::CascadeNoneFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_ORDER_DETAILS_HPP
|
||||
|
|
|
|||
|
|
@ -8,23 +8,21 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct person {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
unsigned int age{};
|
||||
utils::blob_type_t image{};
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
unsigned int age{};
|
||||
utils::blob_type_t image{};
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::attribute(op, "age", age);
|
||||
field::attribute(op, "image", image);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::attribute(op, "age", age);
|
||||
field::attribute(op, "image", image);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_PERSON_HPP
|
||||
|
|
|
|||
|
|
@ -6,39 +6,37 @@
|
|||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/cascade_type.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct product {
|
||||
std::string product_name;
|
||||
object::object_ptr<test::supplier> supplier;
|
||||
object::object_ptr<test::category> category;
|
||||
std::string quantity_per_unit;
|
||||
unsigned int unit_price;
|
||||
unsigned int units_in_stock;
|
||||
unsigned int units_in_order;
|
||||
unsigned int reorder_level;
|
||||
bool discontinued;
|
||||
std::string product_name;
|
||||
object::object_ptr<supplier> supplier;
|
||||
object::object_ptr<category> category;
|
||||
std::string quantity_per_unit;
|
||||
unsigned int unit_price;
|
||||
unsigned int units_in_stock;
|
||||
unsigned int units_in_order;
|
||||
unsigned int reorder_level;
|
||||
bool discontinued;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "product_name", product_name, 255);
|
||||
field::belongs_to(op, "supplier_id", supplier, utils::cascade_type::ALL);
|
||||
field::belongs_to(op, "category_id", category, utils::cascade_type::ALL);
|
||||
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);
|
||||
field::attribute(op, "units_in_order", units_in_order);
|
||||
field::attribute(op, "reorder_level", reorder_level);
|
||||
field::attribute(op, "discontinued", discontinued);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
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::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);
|
||||
field::attribute(op, "units_in_order", units_in_order);
|
||||
field::attribute(op, "reorder_level", reorder_level);
|
||||
field::attribute(op, "discontinued", discontinued);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,51 +10,52 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct recipe;
|
||||
struct ingredient
|
||||
{
|
||||
|
||||
struct ingredient {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<object::object_ptr<recipe>> recipes{};
|
||||
std::vector<object::object_ptr<recipe> > recipes{};
|
||||
|
||||
ingredient() = default;
|
||||
|
||||
ingredient()= default;
|
||||
ingredient(const unsigned int id, std::string name)
|
||||
: id(id), name(std::move(name)) {}
|
||||
: id(id), name(std::move(name)) {
|
||||
}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::fetch_type::Eager);
|
||||
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::CascadeAllFetchEager);
|
||||
}
|
||||
};
|
||||
|
||||
struct recipe
|
||||
{
|
||||
struct recipe {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<object::object_ptr<ingredient>> ingredients{};
|
||||
std::vector<object::object_ptr<ingredient> > ingredients{};
|
||||
|
||||
recipe() = default;
|
||||
|
||||
recipe()= default;
|
||||
recipe(const unsigned int id, std::string name)
|
||||
: id(id), name(std::move(name)) {}
|
||||
: id(id), name(std::move(name)) {
|
||||
}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::fetch_type::Lazy);
|
||||
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
// class recipe_ingredient : public object::many_to_many_relation<recipe, ingredient> {
|
||||
// public:
|
||||
// recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
|
||||
// recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
|
||||
// };
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_RECIPE_HPP
|
||||
|
|
|
|||
|
|
@ -11,10 +11,11 @@
|
|||
|
||||
namespace matador::test {
|
||||
struct package;
|
||||
|
||||
struct shipment {
|
||||
long id{};
|
||||
std::string tracking_number;
|
||||
object::collection<object::object_ptr<package>> packages{};
|
||||
object::collection<object::object_ptr<package> > packages{};
|
||||
|
||||
template<typename Operator>
|
||||
void process(Operator &op) {
|
||||
|
|
|
|||
|
|
@ -11,52 +11,54 @@
|
|||
#include <vector>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct course;
|
||||
|
||||
struct student {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<object::object_ptr<course>> courses;
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::vector<object::object_ptr<course> > courses;
|
||||
|
||||
student() = default;
|
||||
explicit student(unsigned int id, std::string name)
|
||||
: id(id)
|
||||
, name(std::move(name)) {}
|
||||
student() = default;
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::fetch_type::Lazy);
|
||||
}
|
||||
explicit student(unsigned int id, std::string name)
|
||||
: id(id)
|
||||
, name(std::move(name)) {
|
||||
}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
field::has_many_to_many(op, "student_courses", courses, "student_id", "course_id", utils::CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
struct course {
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
std::vector<object::object_ptr<student>> students;
|
||||
unsigned int id{};
|
||||
std::string title;
|
||||
std::vector<object::object_ptr<student> > students;
|
||||
|
||||
course() = default;
|
||||
explicit course(unsigned int id, std::string title)
|
||||
: id(id)
|
||||
, title(std::move(title)) {}
|
||||
course() = default;
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 255);
|
||||
field::has_many_to_many(op, "student_courses", students, utils::fetch_type::Eager);
|
||||
}
|
||||
explicit course(unsigned int id, std::string title)
|
||||
: id(id)
|
||||
, title(std::move(title)) {
|
||||
}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "title", title, 255);
|
||||
field::has_many_to_many(op, "student_courses", students, utils::CascadeAllFetchEager);
|
||||
}
|
||||
};
|
||||
|
||||
class student_course : public object::many_to_many_relation<student, course> {
|
||||
public:
|
||||
student_course() : many_to_many_relation("student_id", "course_id") {}
|
||||
student_course() : many_to_many_relation("student_id", "course_id") {
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_STUDENT_HPP
|
||||
|
|
|
|||
|
|
@ -6,19 +6,17 @@
|
|||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct supplier {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::attribute(op, "name", name, 255);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_SUPPLIER_HPP
|
||||
|
|
|
|||
|
|
@ -5,56 +5,52 @@
|
|||
#include "matador/utils/types.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
struct types {
|
||||
// enum { CSTR_LEN=255 };
|
||||
|
||||
struct types
|
||||
{
|
||||
// enum { CSTR_LEN=255 };
|
||||
unsigned int id_ = 0;
|
||||
int8_t char_ = 'c';
|
||||
short short_ = -127;
|
||||
int int_ = -65000;
|
||||
int64_t long64_ = -1234567890;
|
||||
unsigned char unsigned_char_ = 'H';
|
||||
unsigned short unsigned_short_ = 128;
|
||||
unsigned int unsigned_int_ = 65000;
|
||||
uint64_t unsigned_long64_ = 1234567890;
|
||||
float float_ = 3.1415f;
|
||||
double double_ = 1.1414;
|
||||
bool bool_ = true;
|
||||
// char cstr_[CSTR_LEN]{};
|
||||
std::string string_ = "Welt";
|
||||
std::string varchar_ = "Erde";
|
||||
// matador::date date_;
|
||||
// matador::time time_;
|
||||
utils::blob_type_t binary_{1, 2, 3, 4};
|
||||
|
||||
unsigned int id_ = 0;
|
||||
int8_t char_ = 'c';
|
||||
short short_ = -127;
|
||||
int int_ = -65000;
|
||||
int64_t long64_ = -1234567890;
|
||||
unsigned char unsigned_char_ = 'H';
|
||||
unsigned short unsigned_short_ = 128;
|
||||
unsigned int unsigned_int_ = 65000;
|
||||
uint64_t unsigned_long64_ = 1234567890;
|
||||
float float_ = 3.1415f;
|
||||
double double_ = 1.1414;
|
||||
bool bool_ = true;
|
||||
// char cstr_[CSTR_LEN]{};
|
||||
std::string string_ = "Welt";
|
||||
std::string varchar_ = "Erde";
|
||||
// matador::date date_;
|
||||
// matador::time time_;
|
||||
utils::blob_type_t binary_{ 1, 2, 3, 4 };
|
||||
|
||||
template < class Operator >
|
||||
void process(Operator &op)
|
||||
{
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id_);
|
||||
field::attribute(op, "val_char", char_);
|
||||
field::attribute(op, "val_float", float_);
|
||||
field::attribute(op, "val_double", double_);
|
||||
field::attribute(op, "val_short", short_);
|
||||
field::attribute(op, "val_int", int_);
|
||||
field::attribute(op, "val_long_long", long64_);
|
||||
field::attribute(op, "val_unsigned_char", unsigned_char_);
|
||||
field::attribute(op, "val_unsigned_short", unsigned_short_);
|
||||
field::attribute(op, "val_unsigned_int", unsigned_int_);
|
||||
field::attribute(op, "val_unsigned_long_long", unsigned_long64_);
|
||||
field::attribute(op, "val_bool", bool_);
|
||||
// field::attribute(op, "val_cstr", cstr_, CSTR_LEN);
|
||||
field::attribute(op, "val_string", string_);
|
||||
field::attribute(op, "val_varchar", varchar_, 63);
|
||||
// field::attribute(op, "val_date", date_);
|
||||
// field::attribute(op, "val_time", time_);
|
||||
field::attribute(op, "val_binary", binary_);
|
||||
}
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id_);
|
||||
field::attribute(op, "val_char", char_);
|
||||
field::attribute(op, "val_float", float_);
|
||||
field::attribute(op, "val_double", double_);
|
||||
field::attribute(op, "val_short", short_);
|
||||
field::attribute(op, "val_int", int_);
|
||||
field::attribute(op, "val_long_long", long64_);
|
||||
field::attribute(op, "val_unsigned_char", unsigned_char_);
|
||||
field::attribute(op, "val_unsigned_short", unsigned_short_);
|
||||
field::attribute(op, "val_unsigned_int", unsigned_int_);
|
||||
field::attribute(op, "val_unsigned_long_long", unsigned_long64_);
|
||||
field::attribute(op, "val_bool", bool_);
|
||||
// field::attribute(op, "val_cstr", cstr_, CSTR_LEN);
|
||||
field::attribute(op, "val_string", string_);
|
||||
field::attribute(op, "val_varchar", varchar_, 63);
|
||||
// field::attribute(op, "val_date", date_);
|
||||
// field::attribute(op, "val_time", time_);
|
||||
field::attribute(op, "val_binary", binary_);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // QUERY_TYPES_HPP
|
||||
|
|
|
|||
Loading…
Reference in New Issue