|
|
|
@@ -5,7 +5,6 @@
|
|
|
|
|
|
|
|
|
|
#include "matador/object/join_columns_collector.hpp"
|
|
|
|
|
#include "matador/object/many_to_many_relation.hpp"
|
|
|
|
|
#include "matador/object/primary_key_resolver.hpp"
|
|
|
|
|
#include "matador/object/error_code.hpp"
|
|
|
|
|
#include "matador/object/schema_node.hpp"
|
|
|
|
|
#include "matador/object/schema_node_iterator.hpp"
|
|
|
|
@@ -121,6 +120,7 @@ private:
|
|
|
|
|
const endpoint_ptr &other_endpoint);
|
|
|
|
|
static void link_relation_endpoints(const endpoint_ptr &endpoint,
|
|
|
|
|
const endpoint_ptr &other_endpoint);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::shared_ptr<schema_node> node_;
|
|
|
|
|
schema &schema_;
|
|
|
|
@@ -143,7 +143,7 @@ public:
|
|
|
|
|
template<typename Type>
|
|
|
|
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
|
|
|
|
|
// if (has_node(name)) {
|
|
|
|
|
// return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
|
|
|
|
|
// return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
|
|
|
|
|
// }
|
|
|
|
|
auto node = schema_node::make_node<Type>(*this, name);
|
|
|
|
|
relation_completer<Type>::complete(node);
|
|
|
|
@@ -167,7 +167,7 @@ public:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr& node);
|
|
|
|
|
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr &node);
|
|
|
|
|
// [[nodiscard]] utils::result<void, utils::error> detach(const std::string& name);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -275,8 +275,7 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
|
|
|
|
// Type was not found.
|
|
|
|
|
// Create and attach the relation node.
|
|
|
|
|
const std::type_index ti = typeid(many_to_many_relation<value_type, Type>);
|
|
|
|
|
const auto endpoint = node_->info().find_relation_endpoint(ti);
|
|
|
|
|
if (endpoint == node_->info().endpoint_end()) {
|
|
|
|
|
if (const auto endpoint = node_->info().find_relation_endpoint(ti); endpoint == node_->info().endpoint_end()) {
|
|
|
|
|
// Endpoint was not found
|
|
|
|
|
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", node_->name().c_str(), id, join_column);
|
|
|
|
|
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type> >(
|
|
|
|
@@ -293,13 +292,13 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
|
|
|
|
foreign_endpoint->node_->info_->register_relation_endpoint(node_->type_index(), foreign_endpoint);
|
|
|
|
|
link_relation_endpoints(local_endpoint, foreign_endpoint);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Type was found.
|
|
|
|
|
// Check if the relation node is already attached.
|
|
|
|
|
const auto &foreign_node = result.value();
|
|
|
|
|
if (const auto rit = foreign_node->info_->find_relation_endpoint(node_->type_index()); rit != foreign_node->info().endpoint_end()) {
|
|
|
|
|
if (const auto rit = foreign_node->info_->find_relation_endpoint(node_->type_index());
|
|
|
|
|
rit != foreign_node->info().endpoint_end()) {
|
|
|
|
|
if (rit->second->is_belongs_to()) {
|
|
|
|
|
rit->second->node_ = foreign_node;
|
|
|
|
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
|
|
|
|
@@ -318,7 +317,8 @@ template<typename Type>
|
|
|
|
|
template<class CollectionType>
|
|
|
|
|
void relation_completer<Type>::on_has_many(const char *id, CollectionType &, const char *join_column,
|
|
|
|
|
const utils::foreign_attributes &,
|
|
|
|
|
std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/) {
|
|
|
|
|
std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>
|
|
|
|
|
* /*unused*/) {
|
|
|
|
|
using value_type = typename CollectionType::value_type;
|
|
|
|
|
using relation_value_type = many_to_relation<Type, value_type>;
|
|
|
|
|
|
|
|
|
@@ -348,9 +348,9 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
|
|
|
|
|
const utils::foreign_attributes &/*attr*/) {
|
|
|
|
|
auto result = schema_.find_node(id);
|
|
|
|
|
if (result) {
|
|
|
|
|
const auto foreign_node = result.value();
|
|
|
|
|
const auto &foreign_node = result.value();
|
|
|
|
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
const auto it = foreign_node->info_->find_relation_endpoint(node_->type_index());
|
|
|
|
|
if (it == foreign_node->info().endpoint_end()) {
|
|
|
|
|
// Todo: Throw error
|
|
|
|
@@ -367,11 +367,13 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
|
|
|
|
|
|
|
|
|
|
auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
|
|
|
|
|
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);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO,
|
|
|
|
|
node);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
node->info_->register_relation_endpoint(node_->type_index(), join_endpoint);
|
|
|
|
|
link_relation_endpoints(local_endpoint, join_endpoint);
|
|
|
|
|
node->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), inverse_join_endpoint);
|
|
|
|
|
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type),
|
|
|
|
|
inverse_join_endpoint);
|
|
|
|
|
result = schema_.attach_node(node, "");
|
|
|
|
|
if (!result) {
|
|
|
|
|
// Todo: throw internal error
|
|
|
|
@@ -390,27 +392,28 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
|
|
|
|
|
const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
|
|
|
|
|
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
|
|
|
|
|
auto creator = [&join_columns] {
|
|
|
|
|
return std::make_unique<relation_value_type>(join_columns.inverse_join_column, join_columns.join_column);
|
|
|
|
|
return std::make_unique<relation_value_type>(join_columns.inverse_join_column, join_columns.join_column);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto node = schema_node::make_relation_node<relation_value_type>(schema_, id, std::move(creator));
|
|
|
|
|
|
|
|
|
|
auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
|
|
|
|
|
auto join_endpoint = std::make_shared<relation_endpoint>(join_columns.join_column, relation_type::BELONGS_TO, node);
|
|
|
|
|
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(join_columns.inverse_join_column, relation_type::BELONGS_TO, node);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(join_columns.inverse_join_column,
|
|
|
|
|
relation_type::BELONGS_TO, node);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
node->info_->register_relation_endpoint(node_->type_index(), inverse_join_endpoint);
|
|
|
|
|
link_relation_endpoints(local_endpoint, inverse_join_endpoint);
|
|
|
|
|
node->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), join_endpoint);
|
|
|
|
|
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), join_endpoint);
|
|
|
|
|
result = schema_.attach_node(node, "");
|
|
|
|
|
if (!result) {
|
|
|
|
|
// Todo: throw internal error
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const auto foreign_node = result.value();
|
|
|
|
|
const auto &foreign_node = result.value();
|
|
|
|
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
|
|
|
|
|
const auto it = foreign_node->info_->find_relation_endpoint(node_->type_index());
|
|
|
|
|
if (it == foreign_node->info().endpoint_end()) {
|
|
|
|
|
// Todo: Throw internal error
|
|
|
|
@@ -425,20 +428,22 @@ template<class ForeignPointerType>
|
|
|
|
|
void relation_completer<Type>::on_has_one(const char *id,
|
|
|
|
|
ForeignPointerType &/*obj*/,
|
|
|
|
|
const utils::foreign_attributes &/*attr*/) {
|
|
|
|
|
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
|
|
|
|
|
const auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
|
|
|
|
|
if (const auto result = schema_.find_node(ti); !result.is_ok()) {
|
|
|
|
|
// Node was not found
|
|
|
|
|
// Create node without the foreign relation endpoint
|
|
|
|
|
log_.debug("node '%s' has foreign key '%s' has one '%s'", node_->name().c_str(), id, ti.name());
|
|
|
|
|
node_->info_->register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_));
|
|
|
|
|
// Node was not found
|
|
|
|
|
// Create node without the foreign relation endpoint
|
|
|
|
|
log_.debug("node '%s' has foreign key '%s' has one '%s'", node_->name().c_str(), id, ti.name());
|
|
|
|
|
node_->info_->
|
|
|
|
|
register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_));
|
|
|
|
|
} else {
|
|
|
|
|
const auto &foreign_node = result.value();
|
|
|
|
|
if (const auto it = foreign_node->info().find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
|
|
|
|
|
if (const auto it = foreign_node->info().find_relation_endpoint(typeid(Type));
|
|
|
|
|
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, node_);
|
|
|
|
|
node_->info_->register_relation_endpoint(ti, endpoint);
|
|
|
|
|
link_relation_endpoints(endpoint, it->second);
|
|
|
|
|
it->second->node_ = foreign_node;
|
|
|
|
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_);
|
|
|
|
|
node_->info_->register_relation_endpoint(ti, endpoint);
|
|
|
|
|
link_relation_endpoints(endpoint, it->second);
|
|
|
|
|
} else {
|
|
|
|
|
// Todo: Throw internal error
|
|
|
|
|
return;
|
|
|
|
@@ -458,18 +463,21 @@ void relation_completer<Type>::on_belongs_to(const char *id,
|
|
|
|
|
// Type was not found
|
|
|
|
|
// Create node without the foreign relation endpoint
|
|
|
|
|
log_.debug("node '%s' has foreign key '%s' belongs to '%s'", node_->name().c_str(), id, ti.name());
|
|
|
|
|
node_->info_->register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, schema::node_ptr{}));
|
|
|
|
|
node_->info_->register_relation_endpoint(
|
|
|
|
|
ti, std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, schema::node_ptr{}));
|
|
|
|
|
} else {
|
|
|
|
|
// Type was found
|
|
|
|
|
const auto& foreign_node = result.value();
|
|
|
|
|
const auto &foreign_node = result.value();
|
|
|
|
|
// Check foreign node and relation endpoint
|
|
|
|
|
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
|
|
|
|
|
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type));
|
|
|
|
|
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, node_);
|
|
|
|
|
node_->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>)) {
|
|
|
|
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_);
|
|
|
|
|
node_->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
|
|
|
|
@@ -490,14 +498,16 @@ void relation_completer<Type>::on_belongs_to(const char *id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
|
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
|
|
|
|
|
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
|
|
|
|
const endpoint_ptr &other_endpoint) {
|
|
|
|
|
endpoint->node_->info_->register_relation_endpoint(other_endpoint->node_->type_index(), endpoint);
|
|
|
|
|
other_endpoint->node_->info_->register_relation_endpoint(endpoint->node_->type_index(), other_endpoint);
|
|
|
|
|
link_relation_endpoints(endpoint, other_endpoint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
|
void relation_completer<Type>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
|
|
|
|
|
void relation_completer<
|
|
|
|
|
Type>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
|
|
|
|
|
endpoint->link_foreign_endpoint(other_endpoint);
|
|
|
|
|
other_endpoint->link_foreign_endpoint(endpoint);
|
|
|
|
|
}
|
|
|
|
|