query/test/models/recipe.hpp

121 lines
3.7 KiB
C++

#ifndef QUERY_RECIPE_HPP
#define QUERY_RECIPE_HPP
#include "matador/utils/access.hpp"
#include "matador/utils/foreign_attributes.hpp"
#include "matador/object/object_ptr.hpp"
#include "matador/object/collection.hpp"
#include "matador/object/many_to_many_relation.hpp"
#include <string>
#include <utility>
namespace matador::test {
struct recipe;
struct ingredient {
unsigned int id{};
std::string name;
object::collection<object::object_ptr<recipe> > recipes{};
ingredient() = default;
ingredient(const 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, UniqueVarChar255);
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::CascadeAllFetchEager);
}
};
struct recipe {
unsigned int id{};
std::string name;
object::collection<object::object_ptr<ingredient> > ingredients{};
recipe() = default;
recipe(const unsigned int id, std::string name)
: id(id), name(std::move(name)) {
}
recipe(const unsigned int id, std::string name, std::vector<object::object_ptr<ingredient>> ings)
: id(id)
, name(std::move(name))
, ingredients(std::move(ings)){
}
template<class Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, UniqueVarChar255);
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::CascadeAllFetchLazy);
}
};
template<const utils::primary_key_attribute &PkAttribute>
struct recipe_pk_generator;
template<const utils::primary_key_attribute &PkAttribute>
struct ingredient_pk_generator {
unsigned int id{};
std::string name;
object::collection<object::object_ptr<recipe_pk_generator<PkAttribute>>> recipes{};
ingredient_pk_generator() = default;
explicit ingredient_pk_generator(std::string name)
: name(std::move(name)) {
}
ingredient_pk_generator(std::string name, std::vector<object::object_ptr<recipe_pk_generator<PkAttribute>>> recps)
: name(std::move(name))
, recipes(std::move(recps)){}
template<class Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "name", name, UniqueVarChar255);
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::CascadeAllFetchEager);
}
};
template<const utils::primary_key_attribute &PkAttribute>
struct recipe_pk_generator {
unsigned int id{};
std::string name;
object::collection<object::object_ptr<ingredient_pk_generator<PkAttribute>>> ingredients{};
recipe_pk_generator() = default;
explicit recipe_pk_generator(std::string name)
: name(std::move(name)) {
}
recipe_pk_generator(std::string name, std::vector<object::object_ptr<ingredient_pk_generator<PkAttribute>>> ings)
: name(std::move(name))
, ingredients(std::move(ings)){}
template<class Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "name", name, UniqueVarChar255);
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::CascadeAllFetchLazy);
}
};
using ingredient_identity = ingredient_pk_generator<utils::Identity>;
using ingredient_table = ingredient_pk_generator<utils::Table>;
using ingredient_sequence = ingredient_pk_generator<utils::Sequence>;
using recipe_identity = recipe_pk_generator<utils::Identity>;
using recipe_table = recipe_pk_generator<utils::Table>;
using recipe_sequence = recipe_pk_generator<utils::Sequence>;
}
#endif //QUERY_RECIPE_HPP