finished relation completer

This commit is contained in:
Sascha Kühl
2025-07-07 15:50:09 +02:00
parent c974628bee
commit e85543719c
17 changed files with 396 additions and 142 deletions
@@ -0,0 +1,51 @@
#ifndef JOIN_COLUMNS_COLLECTOR_HPP
#define JOIN_COLUMNS_COLLECTOR_HPP
#include "matador/utils/access.hpp"
#include "matador/utils/field_attributes.hpp"
#include <string>
namespace matador::object {
struct join_columns {
std::string join_column;
std::string inverse_join_column;
};
class join_columns_collector final {
public:
template<class Type>
join_columns collect() {
join_columns_ = {};
Type obj;
access::process(*this, obj);
return join_columns_;
}
template < class V >
void on_primary_key(const char * /*id*/, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr) {}
static void on_primary_key(const char * /*id*/, std::string &, size_t) {}
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
template<typename Type>
static void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class Pointer>
static void on_belongs_to(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
template<class Pointer>
static void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
template<class ContainerType>
static void on_has_many(ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {}
template<class ContainerType>
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
join_columns_.join_column = join_column;
join_columns_.inverse_join_column = inverse_join_column;
}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
private:
join_columns join_columns_;
};
}
#endif //JOIN_COLUMNS_COLLECTOR_HPP
+116 -64
View File
@@ -2,6 +2,8 @@
#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/primary_key_resolver.hpp"
#include "matador/object/error_code.hpp"
@@ -105,8 +107,8 @@ public:
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 ContainerType>
void on_has_many_to_many(const char *id, ContainerType &collection, 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(const std::shared_ptr<schema_node> &node)
@@ -123,6 +125,7 @@ private:
std::shared_ptr<schema_node> node_;
schema &schema_;
logger::logger log_;
join_columns_collector join_columns_collector_;
};
@@ -140,9 +143,9 @@ 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 = acquire_node<Type>(name);
auto node = schema_node::make_node<Type>(*this, name);
relation_completer<Type>::complete(node);
if (auto result = attach_node(node, parent); !result) {
return utils::failure(result.err());
@@ -239,25 +242,11 @@ private:
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
template<typename Type>
node_ptr acquire_node(const std::string &name) {
if (const auto it = expected_node_map_.find(typeid(Type)); it != expected_node_map_.end()) {
const auto node = it->second;
expected_node_map_.erase(it);
node->update_name(name);
return node;
}
return schema_node::make_node<Type>(*this, name);
}
[[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 push_back_child(const node_ptr &parent, const node_ptr &child);
static void insert_node(const node_ptr &parent, const node_ptr &child);
void remove_node(const node_ptr &node);
private:
@@ -269,7 +258,6 @@ private:
t_node_map nodes_by_name_;
t_type_index_node_map nodes_by_type_;
t_type_index_node_map expected_node_map_;
logger::logger log_;
};
@@ -330,70 +318,130 @@ 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>;
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type> >(
const auto node = schema_node::make_relation_node<relation_value_type>(
schema_, id, [join_column] {
return new many_to_many_relation<value_type, Type>(join_column, "value");
return std::make_unique<relation_value_type>(join_column, "value");
});
const auto result = schema_.attach<many_to_relation<Type, value_type> >(id);
const auto result = schema_.attach_node(node, "");
if (!result) {
// Todo: throw internal exception
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(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<typename Type>
template<class CollectionType>
void relation_completer<Type>::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<relation_endpoint>(id, relation_type::HAS_MANY, node_);
node_->info_->register_relation_endpoint(typeid(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<typename CollectionType::value_type::value_type, Type>;
auto creator = [join_column, inverse_join_column] {
return std::make_unique<relation_value_type>(join_column, inverse_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_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);
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);
result = schema_.attach_node(node, "");
if (!result) {
// Todo: throw internal error
return;
}
}
}
template<typename Type>
template<class CollectionType>
void relation_completer<Type>::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) {
} else {
//
// using relation_type = many_to_many_relation<typename CollectionType::value_type, Type>;
// auto creator = [join_column, inverse_join_column] {
// return new many_to_many_relation<typename CollectionType::value_type, Type>(join_column, inverse_join_column);
// };
// auto node = schema_node::make_relation_node<relation_type>(schema_, id);
// schema_.attach_node(node, typeid(relation_type));
}
}
template<typename Type>
template<class ContainerType>
void relation_completer<Type>::on_has_many_to_many(const char *id, ContainerType &collection,
const utils::foreign_attributes &attr) {
void relation_completer<Type>::on_has_many_to_many(const char *id,
CollectionType &/*collection*/,
const utils::foreign_attributes &/*attr*/) {
auto result = schema_.find_node(id);
if (!result) {
// using relation_type = many_to_many_relation<typename ContainerType::value_type, Type>;
// auto creator = [attr] {
// return new relation_type(attr.join_column, attr.inverse_join_column);
// };
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);
};
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);
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);
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<relation_endpoint>(id, relation_type::HAS_MANY, node_);
node_->info_->register_relation_endpoint(typeid(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<typename Type>
template<class ForeignPointerType>
void relation_completer<Type>::on_has_one(const char *id, ForeignPointerType &/*obj*/,
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));
if (const auto result = schema_.find_node(ti); !result.is_ok() && schema_.expected_node_map_.count(ti) == 0) {
schema_.expected_node_map_.insert({
ti, schema_node::make_node<typename ForeignPointerType::value_type>(schema_, ti.name())
});
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_));
} else {
const auto &foreign_node = result.value();
if (const auto rit = foreign_node->info().find_relation_endpoint(ti); rit != foreign_node->info().endpoint_end()) {
if (rit->second->is_has_many()) {
} else if (rit->second->is_has_one()) {
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);
} else {
// Todo: Throw internal error
return;
}
}
}
@@ -401,7 +449,8 @@ void relation_completer<Type>::on_has_one(const char *id, ForeignPointerType &/*
template<typename Type>
template<class ForeignPointerType>
void relation_completer<Type>::on_belongs_to(const char *id, ForeignPointerType & /*obj*/,
void relation_completer<Type>::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));
@@ -416,7 +465,11 @@ void relation_completer<Type>::on_belongs_to(const char *id, ForeignPointerType
// 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->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
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>)) {
// 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
@@ -429,10 +482,9 @@ void relation_completer<Type>::on_belongs_to(const char *id, ForeignPointerType
// check type
}
} else {
// Relation node was not found, create endpoint.
// Relation node was not found, create only endpoint.
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);
}
}
}
+3 -44
View File
@@ -8,6 +8,7 @@
#include "matador/sql/connection.hpp"
#include "matador/object/join_columns_collector.hpp"
#include "matador/object/schema.hpp"
#include "matador/utils/result.hpp"
@@ -18,48 +19,6 @@
namespace matador::orm {
struct join_columns
{
std::string join_column;
std::string inverse_join_column;
};
class join_column_collector
{
public:
template<class Type>
join_columns collect() {
join_columns_ = {};
Type obj;
access::process(*this, obj);
return join_columns_;
}
template < class V >
void on_primary_key(const char * /*id*/, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr) {}
static void on_primary_key(const char * /*id*/, std::string &, size_t) {}
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
template<typename Type>
static void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class Pointer>
static void on_belongs_to(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
template<class Pointer>
static void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
template<class ContainerType>
static void on_has_many(ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {}
template<class ContainerType>
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
join_columns_.join_column = join_column;
join_columns_.inverse_join_column = inverse_join_column;
}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
private:
join_columns join_columns_;
};
struct entity_query_data {
std::shared_ptr<sql::table> root_table;
std::string pk_column_name{};
@@ -255,7 +214,7 @@ public:
throw query_builder_exception{query_build_error::MissingPrimaryKey};
}
const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>();
const auto join_columns = join_columns_collector_.collect<typename ContainerType::value_type::value_type>();
append_join(
sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
@@ -288,7 +247,7 @@ private:
entity_query_data entity_query_data_;
unsigned int column_index{0};
unsigned int table_index{0};
join_column_collector join_column_collector_;
object::join_columns_collector join_columns_collector_;
};
template<class Pointer>