attaching schema nodes progress

This commit is contained in:
Sascha Kühl
2025-07-15 16:04:43 +02:00
parent dd737623f4
commit a0bfa3261b
11 changed files with 147 additions and 100 deletions
+91 -75
View File
@@ -126,7 +126,20 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
// Todo: throw internal error or attach node
return;
}
if (const auto endpoint = nodes_.top()->info().find_relation_endpoint(typeid(relation_value_type)); endpoint == nodes_.top()->info().endpoint_end()) {
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
if (local_it == nodes_.top()->info().endpoint_end()) {
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, result.value());
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
}
auto foreign_it = result.value()->info().find_relation_endpoint(typeid(value_type));
if (foreign_it == result.value()->info().endpoint_end()) {
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, nodes_.top());
foreign_it = result.value()->info_->register_relation_endpoint(typeid(Type), foreign_endpoint);
}
link_relation_endpoints(local_it->second, foreign_it->second);
/*
if (const auto endpoint = nodes_.top()->info().find_relation_endpoint(typeid(relation_value_type)); endpoint == nodes_.top()->info().endpoint_end()) {
// Endpoint was not found
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
result = schema_node::make_relation_node<relation_value_type>(
@@ -159,6 +172,7 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
}
}
*/
}
template<typename Type>
@@ -179,10 +193,13 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &, con
// Todo: throw internal exception
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, result.value());
// const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
// const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, result.value());
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, result.value());
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
foreign_endpoint->node_->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
result.value()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
}
@@ -194,20 +211,18 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
const char *inverse_join_column,
const utils::foreign_attributes &/*attr*/) {
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
auto result = schema_.find_node(typeid(relation_value_type));
if (result) {
// Found relation node.
// Complete relation links
const auto &foreign_node = result.value();
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index());
if (it == foreign_node->info().endpoint_end()) {
// Todo: Throw error
return;
}
link_relation_endpoints(local_endpoint, it->second);
} else {
using value_type = typename CollectionType::value_type::value_type;
// Check if the object_ptr type is already inserted in the schema (by id)
auto result = schema_.find_node(typeid(value_type));
if (!result) {
// Todo: throw internal error or attach node
return;
}
const auto foreign_node = result.value();
result = schema_.find_node(id);
if (!result) {
// Relation not found.
auto creator = [join_column, inverse_join_column] {
return std::make_unique<relation_value_type>(join_column, inverse_join_column);
@@ -220,15 +235,21 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
}
auto& node = result.value();
auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, node);
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO,
node);
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node);
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, nodes_.top());
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO, foreign_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node);
// register relation endpoint in local node
nodes_.top()->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
// register relation endpoint in foreign node
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
// register endpoints in relation node
node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), inverse_join_endpoint);
// link endpoints
link_relation_endpoints(local_endpoint, join_endpoint);
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type),
inverse_join_endpoint);
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
}
}
@@ -251,28 +272,24 @@ template<class ForeignPointerType>
void relation_completer<Type>::on_has_one(const char *id,
ForeignPointerType &/*obj*/,
const utils::foreign_attributes &/*attr*/) {
const auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
if (const auto result = schema_.find_node(ti); !result) {
using value_type = typename ForeignPointerType::value_type;
const auto ti = std::type_index(typeid(value_type));
const auto result = schema_.find_node(ti);
if (!result) {
// Node was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' has one '%s'", nodes_.top()->name().c_str(), id, ti.name());
nodes_.top()->info_->
register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, nodes_.top()));
} else {
const auto &foreign_node = result.value();
if (const auto it = foreign_node->info().find_relation_endpoint(nodes_.top()->type_index());
it != foreign_node->info().endpoint_end()) {
if (it->second->is_belongs_to()) {
it->second->node_ = foreign_node;
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else {
// Todo: Throw internal error
return;
}
}
// Todo: Throw internal error
return;
}
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
if (local_it == nodes_.top()->info().endpoint_end()) {
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, result.value());
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
}
if (const auto foreign_it = result.value()->info().find_relation_endpoint(typeid(Type)); foreign_it != result.value()->info().endpoint_end()) {
link_relation_endpoints(local_it->second, foreign_it->second);
}
}
template<typename Type>
@@ -282,40 +299,39 @@ void relation_completer<Type>::on_belongs_to(const char *id,
const utils::foreign_attributes & /*attr*/) {
using value_type = typename ForeignPointerType::value_type;
const auto ti = std::type_index(typeid(value_type));
if (auto result = schema_.find_node(ti); !result) {
auto result = schema_.find_node(ti);
if (!result) {
// Type was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' belongs to '%s'", nodes_.top()->name().c_str(), id, ti.name());
nodes_.top()->info_->register_relation_endpoint(
ti, std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_ptr{}));
} else {
// Type was found
const auto &foreign_node = result.value();
// Check foreign node and relation endpoint
if (const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index());
it != foreign_node->info().endpoint_end()) {
// Found corresponding relation endpoint in the foreign node
if (it->second->is_has_one()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
// Endpoint is a "many_to_many_relation". This means there
// is a "many_to_many_relation" node attached. Because of being a
// "belongs_to"-relation the "many_to_many_relation" can be removed
// (detach), and the endpoints must be adjusted
const auto foreign_endpoint = it->second->foreign_endpoint();
const auto detach_result = schema_.detach_node(foreign_endpoint->node_);
foreign_endpoint->node_ = nodes_.top();
nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
} else {
// check type
}
} else {
// Relation node was not found, create only endpoint.
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, nodes_.top());
// Todo: Throw internal error
return;
}
// Type was found
const auto &foreign_node = result.value();
// Check foreign node and relation endpoint
if (const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index());
it != foreign_node->info().endpoint_end()) {
// Found corresponding relation endpoint in the foreign node
if (it->second->is_has_one()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
// Endpoint is a "many_to_many_relation". This means there
// is a "many_to_many_relation" node attached. Because of being a
// "belongs_to"-relation the "many_to_many_relation" can be removed
// (detach), and the endpoints must be adjusted
const auto foreign_endpoint = it->second->foreign_endpoint();
const auto detach_result = schema_.detach_node(foreign_endpoint->node_);
foreign_endpoint->node_ = foreign_node;
// foreign_endpoint->node_ = nodes_.top();
nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
} else {
// check type
}
} else {
// Relation node was not found, create only endpoint.
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
}
}