From c4a00f19fdefa5e0a3785d9785ac64b9b3c059e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Wed, 2 Jul 2025 16:13:56 +0200 Subject: [PATCH] relation_completer progress --- include/matador/object/relation_endpoint.hpp | 5 ++- include/matador/object/schema.hpp | 40 +++++++------------- include/matador/object/schema_node.hpp | 6 +-- source/core/object/relation_endpoint.cpp | 8 ++++ 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/include/matador/object/relation_endpoint.hpp b/include/matador/object/relation_endpoint.hpp index ed06d95..bb07ddf 100644 --- a/include/matador/object/relation_endpoint.hpp +++ b/include/matador/object/relation_endpoint.hpp @@ -33,11 +33,14 @@ public: [[nodiscard]] bool is_has_many() const; [[nodiscard]] bool is_belongs_to() const; + [[nodiscard]] std::shared_ptr foreign_endpoint() const; + void link_foreign_endpoint(const std::shared_ptr& endpoint); + private: std::string field_name_; relation_type type_; std::shared_ptr node_; - std::weak_ptr foreign_endpoint; + std::shared_ptr foreign_endpoint_; }; } diff --git a/include/matador/object/schema.hpp b/include/matador/object/schema.hpp index c5adb8a..371136c 100644 --- a/include/matador/object/schema.hpp +++ b/include/matador/object/schema.hpp @@ -81,9 +81,7 @@ public: on_foreign_key(); } template - void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) { - on_foreign_key(); - } + void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/); template void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t::value>* = nullptr ); @@ -248,18 +246,11 @@ template void relation_completer::on_has_many( const char *id, CollectionType&, const char *join_column, const utils::foreign_attributes&, std::enable_if_t::value>* /*unused*/ ) { using value_type = typename CollectionType::value_type; - // Process foreign key has many relation - // Check if foreign key type was already registered - auto result = schema_.find_node(typeid(value_type)); - if (result) { - } else { - // - using relation_type = many_to_many_relation; - auto creator = [] { - return new many_to_many_relation(); - }; - } + const auto node = schema_node::make_relation_node>(schema_, id, [join_column] { + return new many_to_many_relation(join_column, "id"); + }); + schema_.attach_node(node, typeid(many_to_many_relation)); } template @@ -267,20 +258,14 @@ template void relation_completer::on_has_many( const char *id, CollectionType&, const char *join_column, const utils::foreign_attributes&, std::enable_if_t::value>* /*unused*/ ) { using value_type = typename CollectionType::value_type; - // Process values has many relation - // Register relation table + const auto node = schema_node::make_relation_node>(schema_, id, [join_column] { + return new many_to_many_relation(join_column, "value"); + }); - // many_to_relation *relation = new many_to_relation(join_column, "value"); - auto result = schema_.find_node(typeid(value_type)); - if (result) { - } else { - // - using relation_type = many_to_many_relation; - auto creator = [] { - return new many_to_many_relation(); - }; + const auto result = schema_.attach>(id); + if (!result) { + // Todo: throw internal exception } - } template @@ -308,7 +293,8 @@ void relation_completer::on_has_many_to_many( const char *id, ContainerTyp template template -void relation_completer::on_foreign_key() { +void relation_completer::on_has_one(const char * id, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) { + auto endpoint = std::make_shared(std::string(id), relation_type::HAS_ONE, std::shared_ptr()); auto ti = std::type_index(typeid(typename ForeignPointerType::value_type)); if (const auto result = schema_.find_node(ti); !result.is_ok() && schema_.expected_node_map_.count(ti) == 0) { schema_.expected_node_map_.insert({ti, schema_node::make_node(schema_, ti.name())}); diff --git a/include/matador/object/schema_node.hpp b/include/matador/object/schema_node.hpp index 4eddca3..670f83a 100644 --- a/include/matador/object/schema_node.hpp +++ b/include/matador/object/schema_node.hpp @@ -33,8 +33,8 @@ public: return node; } - template < typename Type > - static std::shared_ptr make_relation_node(object::schema& tree, const std::string& name) { + template < typename Type, typename CreatorFunc > + static std::shared_ptr make_relation_node(object::schema& tree, const std::string& name, CreatorFunc &&creator) { auto node = std::shared_ptr(new schema_node(tree, name)); auto info = std::make_unique>( @@ -69,7 +69,7 @@ public: return std::ref(static_cast&>(*info_)); } - const object::schema& schema() const; + [[nodiscard]] const object::schema& schema() const; private: explicit schema_node(object::schema& tree); diff --git a/source/core/object/relation_endpoint.cpp b/source/core/object/relation_endpoint.cpp index 06e67e2..bf137b6 100644 --- a/source/core/object/relation_endpoint.cpp +++ b/source/core/object/relation_endpoint.cpp @@ -32,4 +32,12 @@ bool relation_endpoint::is_has_many() const { bool relation_endpoint::is_belongs_to() const { return type_ == relation_type::BELONGS_TO; } + +std::shared_ptr relation_endpoint::foreign_endpoint() const { + return foreign_endpoint_; +} + +void relation_endpoint::link_foreign_endpoint( const std::shared_ptr& endpoint ) { + foreign_endpoint_ = endpoint; +} }