#ifndef SCHEMA_HPP #define SCHEMA_HPP #include "matador/logger/log_manager.hpp" #include "matador/object/join_columns_collector.hpp" #include "matador/object/many_to_many_relation.hpp" #include "matador/object/error_code.hpp" #include "matador/object/schema_node.hpp" #include "matador/object/schema_node_iterator.hpp" #include "matador/utils/result.hpp" #include "matador/utils/error.hpp" #include "matador/logger/logger.hpp" #include #include #include #include namespace matador::object { utils::error make_error(error_code ec, const std::string &msg); class schema; /* * 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 * 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 * where MM as belongs to and BT as given type * * 2. has_many_to_many (MM1, MM2) * relation_table is needed * relation_table * 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 class relation_completer final { public: using value_type = Type; using endpoint_ptr = std::shared_ptr; static void complete(const std::shared_ptr &node) { relation_completer completer(node); Type obj; access::process(completer, obj); } template static void on_primary_key(const char * /*id*/, PrimaryKeyType &/*pk*/, std::enable_if_t && !std::is_same_v> * = nullptr) { } static void on_primary_key(const char * /*id*/, std::string &/*pk*/, size_t /*size*/) { } static void on_revision(const char * /*id*/, uint64_t &/*rev*/) { } template static void on_attribute(const char * /*id*/, AttributeType &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { } template static void on_attribute(const char * /*id*/, std::optional &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { } template void on_belongs_to(const char *id, ForeignPointerType &obj, const utils::foreign_attributes &attr); template void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/); template void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t::value> * = nullptr); template void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t::value> * = nullptr); template 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 void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr); private: explicit relation_completer(const std::shared_ptr &node) : node_(node) , schema_(node->schema_) , log_(logger::create_logger("relation_completer")) { } 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); private: std::shared_ptr node_; schema &schema_; logger::logger log_; join_columns_collector join_columns_collector_; }; class schema { public: typedef const_schema_node_iterator const_iterator; /**< Shortcut for the list const iterator. */ using node_ptr = std::shared_ptr; /** * Creates an empty schema */ explicit schema(std::string name = ""); template [[nodiscard]] utils::result 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")); // } auto node = schema_node::make_node(*this, name); relation_completer::complete(node); if (auto result = attach_node(node, parent); !result) { return utils::failure(result.err()); } return utils::ok(); } template [[nodiscard]] utils::result attach(const std::string &name) { const auto ti = std::type_index(typeid(SuperType)); auto result = find_node(ti); if (!result) { return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(ti.name()) + "' not found")); } return attach(name, (*result)->name()); } [[nodiscard]] utils::result detach(const node_ptr &node); // [[nodiscard]] utils::result detach(const std::string& name); /** * Return the first schema node. * * @return The first schema node iterator. */ [[nodiscard]] const_iterator begin() const; /** * Return the last schema node. * * @return The last schema node iterator. */ [[nodiscard]] const_iterator end() const; /** * Returns true if the schema contains * no schema nodes. * * @return True if the schema is empty */ [[nodiscard]] bool empty() const; /** * Returns the current number of the schema node. * * @return Number of schema nodes */ [[nodiscard]] size_t size() const; /** * Returns the name of the schema. * * @return The name of the schema */ [[nodiscard]] std::string name() const; template [[nodiscard]] utils::result, utils::error> info() const { auto result = find_node(std::type_index(typeid(Type))); if (!result) { return utils::failure(result.err()); } return utils::ok(result.value()->info()); } template [[nodiscard]] utils::result basic_info() const { auto result = find_node(std::type_index(typeid(Type))); if (!result) { return utils::failure(result.err()); } return utils::ok(basic_object_info_ref{result.value()->info()}); } [[nodiscard]] utils::result, utils::error> reference( const std::type_index &type_index) const; void dump(std::ostream &os) const; private: using t_node_map = std::unordered_map; using t_type_index_node_map = std::unordered_map; [[nodiscard]] utils::result attach_node(const std::shared_ptr &node, const std::string &parent); [[nodiscard]] utils::result attach_node(const std::shared_ptr &node, const std::type_index &type_index); [[nodiscard]] utils::result find_node(const std::string &name) const; [[nodiscard]] utils::result find_node(const std::type_index &type_index) const; [[nodiscard]] bool has_node(const std::string &name) const; [[nodiscard]] bool has_node(const std::type_index &index) const; [[nodiscard]] bool has_node(const std::type_index &index, const std::string &name) const; static void insert_node(const node_ptr &parent, const node_ptr &child); void remove_node(const node_ptr &node); private: template friend class relation_completer; std::string name_; std::shared_ptr root_; t_node_map nodes_by_name_; t_type_index_node_map nodes_by_type_; logger::logger log_; }; template template void relation_completer::on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &, std::enable_if_t::value> * /*unused*/) { using value_type = typename CollectionType::value_type::value_type; // Check if the object_ptr type is already inserted in the schema (by id) if (auto result = schema_.find_node(id); !result) { // Type was not found. // Create and attach the relation node. const std::type_index ti = typeid(many_to_many_relation); 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 >( schema_, id, [join_column] { return std::make_unique >(join_column, "id"); }); result = schema_.attach_node(node, ""); if (!result) { // Todo: throw internal error return; } const auto local_endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); const auto foreign_endpoint = std::make_shared(join_column, relation_type::BELONGS_TO, node); 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 (rit->second->is_belongs_to()) { rit->second->node_ = foreign_node; const auto endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); node_->info_->register_relation_endpoint(node_->type_index(), endpoint); link_relation_endpoints(endpoint, rit->second); } else { // Todo: throw internal error relation node has invalid type } } else { // Todo: throw internal error couldn't find relation node } } } template template void relation_completer::on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &, std::enable_if_t::value> * /*unused*/) { using value_type = typename CollectionType::value_type; using relation_value_type = many_to_relation; const auto node = schema_node::make_relation_node( schema_, id, [join_column] { return std::make_unique(join_column, "value"); }); const auto result = schema_.attach_node(node, ""); if (!result) { // Todo: throw internal exception } const auto local_endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); const auto foreign_endpoint = std::make_shared(join_column, relation_type::BELONGS_TO, node); 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); } template template void relation_completer::on_has_many_to_many(const char *id, CollectionType &/*collection*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) { auto result = schema_.find_node(id); if (result) { const auto &foreign_node = result.value(); const auto local_endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); 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 return; } link_relation_endpoints(local_endpoint, it->second); } else { using relation_value_type = many_to_many_relation; auto creator = [join_column, inverse_join_column] { return std::make_unique(join_column, inverse_join_column); }; auto node = schema_node::make_relation_node(schema_, id, std::move(creator)); auto local_endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); auto join_endpoint = std::make_shared(join_column, relation_type::BELONGS_TO, node); auto inverse_join_endpoint = std::make_shared(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(typename CollectionType::value_type::value_type), inverse_join_endpoint); result = schema_.attach_node(node, ""); if (!result) { // Todo: throw internal error return; } } } template template void relation_completer::on_has_many_to_many(const char *id, CollectionType &/*collection*/, const utils::foreign_attributes &/*attr*/) { auto result = schema_.find_node(id); if (!result) { const auto join_columns = join_columns_collector_.collect(); using relation_value_type = many_to_many_relation; auto creator = [&join_columns] { return std::make_unique(join_columns.inverse_join_column, join_columns.join_column); }; auto node = schema_node::make_relation_node(schema_, id, std::move(creator)); auto local_endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); auto join_endpoint = std::make_shared(join_columns.join_column, relation_type::BELONGS_TO, node); auto inverse_join_endpoint = std::make_shared(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(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 local_endpoint = std::make_shared(id, relation_type::HAS_MANY, node_); 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 return; } link_relation_endpoints(local_endpoint, it->second); } } template template void relation_completer::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.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(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 (it->second->is_belongs_to()) { it->second->node_ = foreign_node; const auto endpoint = std::make_shared(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; } } } } template template void relation_completer::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)); if (auto result = schema_.find_node(ti); !result) { // 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(id, relation_type::BELONGS_TO, schema::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(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(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)) { // 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(foreign_endpoint->node_); foreign_endpoint->node_ = node_; node_->info_->register_relation_endpoint(node_->type_index(), foreign_endpoint); } else { // check type } } else { // Relation node was not found, create only endpoint. const auto endpoint = std::make_shared(id, relation_type::BELONGS_TO, node_); node_->info_->register_relation_endpoint(ti, endpoint); } } } template void relation_completer::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 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); } } #endif //SCHEMA_HPP