added belongs to and has one tests

This commit is contained in:
2026-05-13 16:09:35 +02:00
parent e3618c60e3
commit 1cdd83cb31
8 changed files with 275 additions and 81 deletions
+51
View File
@@ -41,6 +41,57 @@ struct author {
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
}
};
template<const utils::primary_key_attribute &PkAttribute>
struct book_pk_generator;
template<const utils::primary_key_attribute &PkAttribute>
struct author_pk_generator {
unsigned int id{};
std::string name;
object::collection<object::object_ptr<book_pk_generator<PkAttribute>>> books;
author_pk_generator() = default;
explicit author_pk_generator(std::string name)
: name(std::move(name)) {}
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "name", name, VarChar63);
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
}
};
template<const utils::primary_key_attribute &PkAttribute>
struct book_pk_generator {
unsigned int id{};
std::string title;
object::object_ptr<author_pk_generator<PkAttribute>> book_author;
unsigned short published_in{};
book_pk_generator() = default;
book_pk_generator(std::string title, object::object_ptr<author_pk_generator<PkAttribute>> author, const unsigned short published_in)
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "title", title, VarChar511);
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
field::attribute(op, "published_in", published_in);
}
};
using author_identity = author_pk_generator<utils::Identity>;
using author_sequence = author_pk_generator<utils::Sequence>;
using author_table = author_pk_generator<utils::Table>;
using book_identity = book_pk_generator<utils::Identity>;
using book_sequence = book_pk_generator<utils::Sequence>;
using book_table = book_pk_generator<utils::Table>;
}
#endif //QUERY_AUTHOR_HPP