relation completer progress

This commit is contained in:
Sascha Kühl 2025-06-29 22:41:39 +02:00
parent 88bab8bf5e
commit e46de87355
6 changed files with 53 additions and 24 deletions

View File

@ -26,11 +26,11 @@ public:
[[nodiscard]] const object_definition& definition() const; [[nodiscard]] const object_definition& definition() const;
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const; [[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
void register_relation_endpoint(const std::type_index &tindex, const relation_endpoint &endpoint); void register_relation_endpoint(const std::type_index &type, const relation_endpoint &endpoint);
void unregister_relation_endpoint(const std::type_index &tindex); void unregister_relation_endpoint(const std::type_index &type);
const_endpoint_iterator find_relation_endpoint(const std::type_index &tindex) const; const_endpoint_iterator find_relation_endpoint(const std::type_index &type) const;
endpoint_iterator find_relation_endpoint(const std::type_index &tindex); endpoint_iterator find_relation_endpoint(const std::type_index &type);
const_endpoint_iterator find_relation_endpoint(const std::string &field) const; const_endpoint_iterator find_relation_endpoint(const std::string &field) const;
endpoint_iterator find_relation_endpoint(const std::string &field); endpoint_iterator find_relation_endpoint(const std::string &field);

View File

@ -27,6 +27,10 @@ public:
[[nodiscard]] relation_type type() const; [[nodiscard]] relation_type type() const;
[[nodiscard]] const schema_node& node() 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: private:
std::string field_name_; std::string field_name_;
relation_type type_; relation_type type_;

View File

@ -21,12 +21,12 @@ utils::error make_error(error_code ec, const std::string& msg);
class schema; class schema;
template<typename Type> template<typename Type>
class type_analyzer final { class relation_completer final {
public: public:
using value_type = Type; using value_type = Type;
static void prepare_expected_nodes(schema &scm) { static void prepare_expected_nodes(schema &scm) {
type_analyzer analyzer(scm); relation_completer analyzer(scm);
Type obj; Type obj;
access::process(analyzer, obj); access::process(analyzer, obj);
@ -64,12 +64,14 @@ private:
void on_foreign_key(); void on_foreign_key();
private: private:
explicit type_analyzer(schema& schema) explicit relation_completer(schema& schema, const std::shared_ptr<schema_node> &node)
: schema_(schema) {}; : schema_(schema)
, node_(node){}
private: private:
schema &schema_; schema &schema_;
std::shared_ptr<schema_node> node_;
}; };
@ -97,7 +99,7 @@ public:
type_index_node_map_.insert({node->type_index(), node}); type_index_node_map_.insert({node->type_index(), node});
} else { } else {
// analyze node (collect unknown types by type index) // analyze node (collect unknown types by type index)
type_analyzer<Type>::prepare_expected_nodes(*this); relation_completer<Type>::prepare_expected_nodes(*this);
const auto node = schema_node::make_node<Type>(*this, name); const auto node = schema_node::make_node<Type>(*this, name);
if (auto result = attach_node(node, parent); !result) { if (auto result = attach_node(node, parent); !result) {
@ -196,7 +198,7 @@ private:
private: private:
template <typename Type> template <typename Type>
friend class type_analyzer; friend class relation_completer;
std::string name_; std::string name_;
std::shared_ptr<schema_node> root_; std::shared_ptr<schema_node> root_;
@ -208,13 +210,24 @@ private:
template<typename Type> template<typename Type>
template<class ForeignPointerType> template<class ForeignPointerType>
void type_analyzer<Type>::on_foreign_key() { void relation_completer<Type>::on_foreign_key() {
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type)); auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
if (schema_.has_node(ti) || schema_.expected_node_map_.count(ti) > 0) { if (const auto result = schema_.find_node(ti); !result.is_ok() && schema_.expected_node_map_.count(ti) == 0) {
return; schema_.expected_node_map_.insert({ti, schema_node::make_node<typename ForeignPointerType::value_type>(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<typename ForeignPointerType::value_type>(schema_, ti.name())});
} }
} }

View File

@ -64,7 +64,7 @@ private:
friend class schema; friend class schema;
friend class const_schema_node_iterator; friend class const_schema_node_iterator;
object::schema &schema_; schema &schema_;
std::unique_ptr<basic_object_info> info_; std::unique_ptr<basic_object_info> info_;
std::shared_ptr<schema_node> parent_; std::shared_ptr<schema_node> parent_;

View File

@ -43,21 +43,21 @@ std::shared_ptr<attribute_definition> basic_object_info::reference_column() cons
return pk_column_; return pk_column_;
} }
void basic_object_info::register_relation_endpoint(const std::type_index &tindex, const relation_endpoint &endpoint) { void basic_object_info::register_relation_endpoint(const std::type_index &type, const relation_endpoint &endpoint) {
relation_endpoints_.insert(std::make_pair(tindex, endpoint)); relation_endpoints_.insert(std::make_pair(type, endpoint));
} }
void basic_object_info::unregister_relation_endpoint(const std::type_index &tindex) { void basic_object_info::unregister_relation_endpoint(const std::type_index &type) {
relation_endpoints_.erase(tindex); relation_endpoints_.erase(type);
} }
basic_object_info::const_endpoint_iterator basic_object_info::const_endpoint_iterator
basic_object_info::find_relation_endpoint(const std::type_index &tindex) const { basic_object_info::find_relation_endpoint(const std::type_index &type) const {
return relation_endpoints_.find(tindex); return relation_endpoints_.find(type);
} }
basic_object_info::endpoint_iterator basic_object_info::find_relation_endpoint(const std::type_index &tindex) { basic_object_info::endpoint_iterator basic_object_info::find_relation_endpoint(const std::type_index &type) {
return relation_endpoints_.find(tindex); return relation_endpoints_.find(type);
} }
basic_object_info::const_endpoint_iterator basic_object_info::find_relation_endpoint(const std::string &field) const { basic_object_info::const_endpoint_iterator basic_object_info::find_relation_endpoint(const std::string &field) const {

View File

@ -18,4 +18,16 @@ relation_type relation_endpoint::type() const {
const schema_node &relation_endpoint::node() const { const schema_node &relation_endpoint::node() const {
return *node_; return *node_;
} }
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;
}
} }