finished relation completer

This commit is contained in:
Sascha Kühl
2025-07-07 15:50:09 +02:00
parent c974628bee
commit e85543719c
17 changed files with 396 additions and 142 deletions
+92
View File
@@ -4,6 +4,7 @@
#include "author.hpp"
#include "book.hpp"
#include "recipe.hpp"
#include <iostream>
@@ -54,6 +55,51 @@
* - set foreign endpoint of (2) to endpoint (1)
* - check relation endpoints...
*/
namespace demo {
struct names {
unsigned int id{};
std::vector<std::string> names_list;
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key( op, "id", id );
field::has_many(op, "name_list", names_list, "names_id", matador::utils::fetch_type::EAGER);
}
};
struct user;
struct profile {
unsigned int id{};
std::string first_name;
std::string last_name;
matador::object::object_ptr<user> user;
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key( op, "id", id );
field::attribute( op, "first_name", first_name, 255 );
field::attribute( op, "last_name", last_name, 255 );
field::belongs_to( op, "user_id", user, matador::utils::default_foreign_attributes );
}
};
struct user {
unsigned int id{};
std::string username;
matador::object::object_ptr<profile> profile;
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key( op, "id", id );
field::attribute( op, "username", username, 255 );
field::has_one(op, "profile_id", profile, matador::utils::default_foreign_attributes );
}
};
}
using namespace demo;
using namespace matador;
@@ -62,6 +108,15 @@ int main() {
logger::add_log_sink(logger::create_stdout_sink());
{
// has_many with builtin-type
object::schema schema;
auto result = schema.attach<names>("names");
schema.dump(std::cout);
}
{
// has_many to belongs_to
object::schema schema;
auto result = schema.attach<author>("authors")
@@ -70,6 +125,7 @@ int main() {
schema.dump(std::cout);
}
{
// belongs_to to has_many
object::schema schema;
auto result = schema.attach<book>("books")
@@ -77,4 +133,40 @@ int main() {
schema.dump(std::cout);
}
{
// has_many_to_many (with join columns first)
object::schema schema;
auto result = schema.attach<ingredient>("ingredients")
.and_then([&schema] { return schema.attach<recipe>("recipes"); });
schema.dump(std::cout);
}
{
// has_many_to_many (with join columns last)
object::schema schema;
auto result = schema.attach<recipe>("recipes")
.and_then([&schema] { return schema.attach<ingredient>("ingredients"); });
schema.dump(std::cout);
}
{
// belongs_to to has_one
object::schema schema;
auto result = schema.attach<profile>("profiles")
.and_then([&schema] { return schema.attach<user>("users"); });
schema.dump(std::cout);
}
{
// has_one to belongs_to
object::schema schema;
auto result = schema.attach<user>("users")
.and_then([&schema] { return schema.attach<profile>("profiles"); });
schema.dump(std::cout);
}
}