diff --git a/include/matador/object/basic_object_info.hpp b/include/matador/object/basic_object_info.hpp index 5884981..138d807 100644 --- a/include/matador/object/basic_object_info.hpp +++ b/include/matador/object/basic_object_info.hpp @@ -26,11 +26,11 @@ public: [[nodiscard]] const object_definition& definition() const; [[nodiscard]] std::shared_ptr reference_column() const; - void register_relation_endpoint(const std::type_index &tindex, const relation_endpoint &endpoint); - void unregister_relation_endpoint(const std::type_index &tindex); + void register_relation_endpoint(const std::type_index &type, const relation_endpoint &endpoint); + void unregister_relation_endpoint(const std::type_index &type); - const_endpoint_iterator find_relation_endpoint(const std::type_index &tindex) const; - endpoint_iterator find_relation_endpoint(const std::type_index &tindex); + const_endpoint_iterator find_relation_endpoint(const std::type_index &type) const; + endpoint_iterator find_relation_endpoint(const std::type_index &type); const_endpoint_iterator find_relation_endpoint(const std::string &field) const; endpoint_iterator find_relation_endpoint(const std::string &field); diff --git a/include/matador/object/relation_endpoint.hpp b/include/matador/object/relation_endpoint.hpp index c12e455..cd3b951 100644 --- a/include/matador/object/relation_endpoint.hpp +++ b/include/matador/object/relation_endpoint.hpp @@ -27,6 +27,10 @@ public: [[nodiscard]] relation_type type() const; [[nodiscard]] const schema_node& node() const; + [[nodiscard]] bool is_has_one() const; + [[nodiscard]] bool is_has_many() const; + [[nodiscard]] bool is_belongs_to() const; + private: std::string field_name_; relation_type type_; diff --git a/include/matador/object/schema.hpp b/include/matador/object/schema.hpp index c2827c4..75ab7c1 100644 --- a/include/matador/object/schema.hpp +++ b/include/matador/object/schema.hpp @@ -21,12 +21,12 @@ utils::error make_error(error_code ec, const std::string& msg); class schema; template -class type_analyzer final { +class relation_completer final { public: using value_type = Type; static void prepare_expected_nodes(schema &scm) { - type_analyzer analyzer(scm); + relation_completer analyzer(scm); Type obj; access::process(analyzer, obj); @@ -64,12 +64,14 @@ private: void on_foreign_key(); private: - explicit type_analyzer(schema& schema) - : schema_(schema) {}; + explicit relation_completer(schema& schema, const std::shared_ptr &node) + : schema_(schema) + , node_(node){} private: schema &schema_; + std::shared_ptr node_; }; @@ -97,7 +99,7 @@ public: type_index_node_map_.insert({node->type_index(), node}); } else { // analyze node (collect unknown types by type index) - type_analyzer::prepare_expected_nodes(*this); + relation_completer::prepare_expected_nodes(*this); const auto node = schema_node::make_node(*this, name); if (auto result = attach_node(node, parent); !result) { @@ -196,7 +198,7 @@ private: private: template - friend class type_analyzer; + friend class relation_completer; std::string name_; std::shared_ptr root_; @@ -208,13 +210,24 @@ private: template template -void type_analyzer::on_foreign_key() { +void relation_completer::on_foreign_key() { auto ti = std::type_index(typeid(typename ForeignPointerType::value_type)); - if (schema_.has_node(ti) || schema_.expected_node_map_.count(ti) > 0) { - return; + 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())}); + } else { + const auto& foreign_node = result.value(); + if (const auto rit = foreign_node->basic_info().find_relation_endpoint(ti); rit != foreign_node->basic_info().endpoint_end()) { + if (rit->second.is_has_many()) { + + } else if (rit->second.is_has_one()) { + + } else { + + } + } + } - 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 d22a4d3..9146fd7 100644 --- a/include/matador/object/schema_node.hpp +++ b/include/matador/object/schema_node.hpp @@ -64,7 +64,7 @@ private: friend class schema; friend class const_schema_node_iterator; - object::schema &schema_; + schema &schema_; std::unique_ptr info_; std::shared_ptr parent_; diff --git a/source/core/object/basic_object_info.cpp b/source/core/object/basic_object_info.cpp index a639724..b0aeb55 100644 --- a/source/core/object/basic_object_info.cpp +++ b/source/core/object/basic_object_info.cpp @@ -43,21 +43,21 @@ std::shared_ptr basic_object_info::reference_column() cons return pk_column_; } -void basic_object_info::register_relation_endpoint(const std::type_index &tindex, const relation_endpoint &endpoint) { - relation_endpoints_.insert(std::make_pair(tindex, endpoint)); +void basic_object_info::register_relation_endpoint(const std::type_index &type, const relation_endpoint &endpoint) { + relation_endpoints_.insert(std::make_pair(type, endpoint)); } -void basic_object_info::unregister_relation_endpoint(const std::type_index &tindex) { - relation_endpoints_.erase(tindex); +void basic_object_info::unregister_relation_endpoint(const std::type_index &type) { + relation_endpoints_.erase(type); } basic_object_info::const_endpoint_iterator -basic_object_info::find_relation_endpoint(const std::type_index &tindex) const { - return relation_endpoints_.find(tindex); +basic_object_info::find_relation_endpoint(const std::type_index &type) const { + return relation_endpoints_.find(type); } -basic_object_info::endpoint_iterator basic_object_info::find_relation_endpoint(const std::type_index &tindex) { - return relation_endpoints_.find(tindex); +basic_object_info::endpoint_iterator basic_object_info::find_relation_endpoint(const std::type_index &type) { + return relation_endpoints_.find(type); } basic_object_info::const_endpoint_iterator basic_object_info::find_relation_endpoint(const std::string &field) const { diff --git a/source/core/object/relation_endpoint.cpp b/source/core/object/relation_endpoint.cpp index bae4c59..a617245 100644 --- a/source/core/object/relation_endpoint.cpp +++ b/source/core/object/relation_endpoint.cpp @@ -18,4 +18,16 @@ relation_type relation_endpoint::type() const { const schema_node &relation_endpoint::node() const { return *node_; } -} \ No newline at end of file + +bool relation_endpoint::is_has_one() const { + return type_ == relation_type::HAS_ONE; +} + +bool relation_endpoint::is_has_many() const { + return type_ == relation_type::HAS_MANY; +} + +bool relation_endpoint::is_belongs_to() const { + return type_ == relation_type::BELONGS_TO; +} +}