added has_one eager functionality and tests
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#ifndef MATADOR_COUNTRY_HPP
|
||||
#define MATADOR_COUNTRY_HPP
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/object/object_ptr.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct capital_pk_generator;
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct country_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
object::object_ptr<capital_pk_generator<PkAttribute>> capital;
|
||||
|
||||
country_pk_generator() = default;
|
||||
explicit country_pk_generator(std::string name)
|
||||
: name(std::move(name)) {}
|
||||
country_pk_generator(const unsigned int id, std::string name)
|
||||
: id(id)
|
||||
, name(std::move(name)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, UniqueVarChar255);
|
||||
field::has_one(op, "capital", capital, "country_id", CascadeAllFetchEager);
|
||||
}
|
||||
};
|
||||
|
||||
template<const utils::primary_key_attribute &PkAttribute>
|
||||
struct capital_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
object::object_ptr<country_pk_generator<PkAttribute>> country;
|
||||
|
||||
capital_pk_generator() = default;
|
||||
capital_pk_generator(std::string name, object::object_ptr<country_pk_generator<PkAttribute>> country)
|
||||
: name(std::move(name))
|
||||
, country(std::move(country)) {}
|
||||
capital_pk_generator(const unsigned int id, std::string name, object::object_ptr<country_pk_generator<PkAttribute>> country)
|
||||
: id(id)
|
||||
, name(std::move(name))
|
||||
, country(std::move(country)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, VarChar255);
|
||||
field::belongs_to(op, "country_id", country, CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
||||
using country = country_pk_generator<utils::Manual>;
|
||||
using country_identity = country_pk_generator<utils::Identity>;
|
||||
using country_sequence = country_pk_generator<utils::Sequence>;
|
||||
using country_table = country_pk_generator<utils::Table>;
|
||||
using capital = capital_pk_generator<utils::Manual>;
|
||||
using capital_identity = capital_pk_generator<utils::Identity>;
|
||||
using capital_sequence = capital_pk_generator<utils::Sequence>;
|
||||
using capital_table = capital_pk_generator<utils::Table>;
|
||||
}
|
||||
|
||||
#endif //MATADOR_COUNTRY_HPP
|
||||
Reference in New Issue
Block a user