moved recipe and ingredient pk generators into recipe.hpp

This commit is contained in:
sascha 2026-05-26 15:49:49 +02:00
parent 6701031007
commit 2e1b583741
2 changed files with 58 additions and 60 deletions

View File

@ -14,66 +14,6 @@ using namespace matador::object;
using namespace matador::test; using namespace matador::test;
using namespace matador::query::meta; using namespace matador::query::meta;
namespace matador::test {
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;
collection<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_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;
collection<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_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>;
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relation", "[session][insert][has_many_to_many]") { TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relation", "[session][insert][has_many_to_many]") {
auto result = schema.attach<recipe>("recipes") auto result = schema.attach<recipe>("recipes")
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } ) .and_then( [this] { return schema.attach<ingredient>("ingredients"); } )

View File

@ -57,6 +57,64 @@ struct recipe {
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::CascadeAllFetchLazy); 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 #endif //QUERY_RECIPE_HPP