query/include/matador/object/relation_completer.hpp

420 lines
20 KiB
C++

#ifndef RELATION_COMPLETER_HPP
#define RELATION_COMPLETER_HPP
#include "matador/object/internal/shadow_repository.hpp"
#include "matador/object/foreign_node_completer.hpp"
#include "matador/object/many_to_many_relation.hpp"
#include "matador/object/join_columns_collector.hpp"
#include "matador/object/object_ptr.hpp"
#include "matador/object/repository_node.hpp"
#include "matador/logger/log_manager.hpp"
#include "matador/utils/primary_key_attribute.hpp"
#include <iostream>
#include <stack>
#include <utility>
namespace matador::object {
class join_column_finder final {
public:
template<typename Type>
static bool has_join_column(const std::string &join_column) {
join_column_finder finder(join_column);
Type obj;
finder.found_ = false;
access::process(finder, obj);
return finder.found_;
}
template<class PrimaryKeyType>
static void on_primary_key(const char * /*id*/, PrimaryKeyType &/*pk*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template<typename AttributeType>
static void on_attribute(const char * /*id*/, AttributeType &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<typename AttributeType>
static void on_attribute(const char * /*id*/, std::optional<AttributeType> &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class ForeignPointerType>
void on_belongs_to(const char *id, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {
found_ = requested_join_column_ == id;
}
template<class ForeignPointerType>
static void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {}
template<class CollectionType>
static void on_has_many(const char * /*id*/, CollectionType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
template<class CollectionType>
static void on_has_many_to_many(const char * /*id*/, CollectionType &/*collection*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
template<class CollectionType>
static void on_has_many_to_many(const char * /*id*/, CollectionType & /*collection*/, const utils::foreign_attributes &/*attr*/) {}
private:
explicit join_column_finder(std::string join_column)
: requested_join_column_(std::move(join_column)) {}
private:
std::string requested_join_column_;
bool found_ = false;
};
/*
* 1. has_many (MM)
* no belongs to
* relation table is needed
* - element type is a foreign table (FT),
* then relation table must look like follows:
* relation_table<MM, FT>
* where MM and FT must be defined as belongs to
* - element type if a builtin type BT (i.e. string, int, etc.),
* then the relation table must look like follows:
* relation_table<MM, BT>
* where MM as belongs to and BT as given type
*
* 2. has_many_to_many (MM1, MM2)
* relation_table is needed
* relation_table<MM1, MM2>
* where MM1 and MM2 must be defined as belongs to
*
* 3. hans_many (MM) <-> belongs_to (BT)
* belongs_to has foreign key to the has_many side
* no relation table needed
*
* 4. has_one to belongs_to
* no relation table is needed
*
* 5. has_many (MM) <-> has_one (HO)
* invalid relation -> error
*
* 6. has_one
* no has_many or belongs_to
* invalid relation -> error
*/
template<typename Type, template<typename> typename... Observers>
class relation_completer final {
private:
using node_ptr = std::shared_ptr<repository_node>;
public:
using endpoint_ptr = std::shared_ptr<relation_endpoint>;
static void complete(const std::shared_ptr<repository_node> &node, const std::vector<std::unique_ptr<observer<Type>>> &observers) {
internal::shadow_repository shadow(node->repo_);
relation_completer completer(shadow, observers);
completer.complete_node_relations(node);
}
template<class PrimaryKeyType>
static void on_primary_key(const char * /*id*/, PrimaryKeyType &/*pk*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template<typename AttributeType>
static void on_attribute(const char * /*id*/, AttributeType &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<typename AttributeType>
static void on_attribute(const char * /*id*/, std::optional<AttributeType> &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class ForeignPointerType>
void on_belongs_to(const char *id, ForeignPointerType &obj, const utils::foreign_attributes &attr);
template<class ForeignPointerType>
void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/);
template<class CollectionType>
void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value> * = nullptr);
template<class CollectionType>
void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value> * = nullptr);
template<class CollectionType>
void on_has_many_to_many(const char *id, CollectionType &collection, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr);
template<class CollectionType>
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
private:
explicit relation_completer(internal::shadow_repository &shadow, const std::vector<std::unique_ptr<observer<Type>>>& observers)
: schema_(shadow)
, log_(logger::create_logger("relation_completer"))
, observers_(observers) {}
void complete_node_relations(const std::shared_ptr<repository_node> &node) {
nodes_.push(node);
Type obj;
access::process(*this, obj);
nodes_.pop();
}
template<typename NodeType>
void attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column);
static void register_relation_endpoints(const endpoint_ptr &endpoint,
const endpoint_ptr &other_endpoint);
static void link_relation_endpoints(const endpoint_ptr &endpoint,
const endpoint_ptr &other_endpoint);
node_ptr find_node(const std::type_index &ti) const;
private:
std::stack<node_ptr> nodes_;
internal::shadow_repository &schema_;
logger::logger log_;
join_columns_collector join_columns_collector_{};
const std::vector<std::unique_ptr<observer<Type>>>& observers_;
};
template<typename Type, template<typename> typename... Observers>
template<class CollectionType>
void relation_completer<Type, Observers...>::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*/) {
// Shortcut to a value type of object_ptr::value_type in a collection
using value_type = typename CollectionType::value_type::value_type;
using relation_value_type = many_to_many_relation<Type, value_type>;
// Check if the object_ptr type is already inserted in the schema (by id)
auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
return;
}
// has foreign node corresponding join column (join_column)
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
// corresponding belongs_to is available and was called (has_many <-> belongs_to)
// complete the relation
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, foreign_node);
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
link_relation_endpoints(local_endpoint, it->second);
} else if (join_column_finder::has_join_column<value_type>(join_column)) {
// corresponding belongs_to is available but was not called (has_many <-> belongs_to)
// prepare the relation
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, foreign_node);
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
} else {
// A relation table is necessary
// Endpoint was not found.
// Always attach a many-to-many relation type. If later a
// belongs-to relation handles this relation, the many-to-many
// relation is maybe detached.
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
return std::make_unique<relation_value_type>("id", join_column);
}, {});
const auto result = schema_.attach_node(node);
if (!result) {
// Todo: throw internal error
return;
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>("id", relation_type::BelongsTo, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
const auto foreign_value_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, foreign_node);
node->info_->register_relation_endpoint(typeid(value_type), foreign_value_endpoint);
}
}
template<typename Type, template<typename> typename... Observers>
template<class CollectionType>
void relation_completer<Type, Observers...>::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*/) {
using value_type = typename CollectionType::value_type;
using relation_value_type = many_to_relation<Type, value_type>;
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
return std::make_unique<relation_value_type>(join_column, "value");
}, {});
const auto result = schema_.attach_node(node);
if (!result) {
// Todo: throw internal exception
return;
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
}
template<typename Type, template<typename> typename... Observers>
template<class CollectionType>
void relation_completer<Type, Observers...>::on_has_many_to_many(const char *id,
CollectionType &/*collection*/,
const char *join_column,
const char *inverse_join_column,
const utils::foreign_attributes &/*attr*/) {
if (!schema_.expecting_relation_node(id)) {
schema_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
} else {
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
schema_.remove_expected_relation_node(id);
}
}
template<typename Type, template<typename> typename... Observers>
template<class CollectionType>
void relation_completer<Type, Observers...>::on_has_many_to_many(const char *id,
CollectionType &collection,
const utils::foreign_attributes &attr) {
const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
on_has_many_to_many(
id,
collection,
join_columns.inverse_join_column.c_str(),
join_columns.join_column.c_str(),
attr);
}
template<typename Type, template<typename> typename... Observers>
template<class ForeignPointerType>
void relation_completer<Type, Observers...>::on_has_one(const char *id,
ForeignPointerType &/*obj*/,
const utils::foreign_attributes &/*attr*/) {
using value_type = typename ForeignPointerType::value_type;
auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
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::HasOne, foreign_node);
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
}
if (const auto foreign_it = foreign_node->info().find_relation_endpoint(typeid(Type)); foreign_it != foreign_node->info().endpoint_end()) {
link_relation_endpoints(local_it->second, foreign_it->second);
}
}
template<typename Type, template<typename> typename... Observers>
template<class ForeignPointerType>
void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
ForeignPointerType & /*obj*/,
const utils::foreign_attributes & /*attr*/) {
using value_type = typename ForeignPointerType::value_type;
const auto ti = std::type_index(typeid(value_type));
auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
return;
}
// Type was found
// 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::BelongsTo, foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->is_has_many()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, 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::BelongsTo, foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
}
}
template<typename Type, template<typename> typename... Observers>
template<typename NodeType>
void relation_completer<Type, Observers...>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
using relation_value_type = many_to_many_relation<NodeType, Type>;
using value_type = NodeType;
// Check if the object_ptr type is already inserted in the schema (by id)
auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
return;
}
auto result = schema_.find_node(name);
if (result) {
return;
}
// Relation does not exist. Create it.
auto creator = [join_column, inverse_join_column] {
return std::make_unique<relation_value_type>(join_column, inverse_join_column);
};
auto observers = internal::observer_list_copy_creator<Type, relation_value_type, Observers...>::copy_create(observers_);
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), name, std::move(creator), std::move(observers));
result = schema_.attach_node(node);
if (!result) {
// Todo: throw internal error
return;
}
foreign_node_completer<relation_value_type>::complete(result.value(), {});
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, foreign_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
// register relation endpoint in local node
nodes_.top()->info_->register_relation_endpoint(typeid(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(value_type), inverse_join_endpoint);
// link endpoints
link_relation_endpoints(local_endpoint, join_endpoint);
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
}
template<typename Type, template<typename> typename... Observers>
void relation_completer<Type, Observers...>::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, template<typename> typename... Observers>
void relation_completer<Type, Observers...>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
endpoint->link_foreign_endpoint(other_endpoint);
other_endpoint->link_foreign_endpoint(endpoint);
}
template<typename Type, template <typename> class ... Observers>
typename relation_completer<Type, Observers...>::node_ptr relation_completer<Type, Observers...>::find_node(const std::type_index &ti) const {
node_ptr foreign_node;
if (auto result = schema_.find_node(ti); result) {
return result.value();
}
if (!schema_.is_node_announced(ti)) {
return {};
}
return schema_.announce_node(ti);
}
}
#endif //RELATION_COMPLETER_HPP