creating tables progress
This commit is contained in:
@@ -9,10 +9,55 @@
|
||||
|
||||
#include "matador/logger/log_manager.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*/, std::enable_if_t<std::is_integral_v<PrimaryKeyType> && !std::is_same_v<bool, PrimaryKeyType>> * = 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<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
|
||||
@@ -110,6 +155,18 @@ private:
|
||||
join_columns_collector join_columns_collector_{};
|
||||
};
|
||||
|
||||
inline void dump(std::ostream &os, const std::shared_ptr<schema_node> &node) {
|
||||
os << "node [" << node->name() << "] (" /*<< node->type_index().name()*/ << ")\n";
|
||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
os << " " << node->name() << "::" << it->second->field_name() << " (" << it->second->type_name() << ")";
|
||||
if (it->second->foreign_endpoint()) {
|
||||
os << " <---> " << it->second->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
||||
} else {
|
||||
os << " -> " << it->second->node().type_index().name() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
template<typename Type>
|
||||
template<class CollectionType>
|
||||
void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
@@ -118,7 +175,7 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
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<value_type, 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 result = schema_.find_node(typeid(value_type));
|
||||
@@ -126,17 +183,79 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
// 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 foreign_node = result.value();
|
||||
// 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::HAS_MANY, 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::HAS_MANY, foreign_node);
|
||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||
} else {
|
||||
// A relation table is necessary
|
||||
// } else if (const auto endpoint = nodes_.top()->info().find_relation_endpoint(typeid(relation_value_type)); endpoint == nodes_.top()->info().endpoint_end()) {
|
||||
// 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);
|
||||
result = schema_node::make_relation_node<relation_value_type>(
|
||||
schema_.schema(), id, [join_column] {
|
||||
return std::make_unique<relation_value_type>("id", join_column);
|
||||
});
|
||||
if (!result) {
|
||||
// Todo: throw internal error
|
||||
return;
|
||||
}
|
||||
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);
|
||||
const auto foreign_endpoint = std::make_shared<relation_endpoint>("id", relation_type::BELONGS_TO, nodes_.top());
|
||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||
result.value()->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::BELONGS_TO, foreign_node);
|
||||
result.value()->info_->register_relation_endpoint(typeid(value_type), foreign_value_endpoint);
|
||||
|
||||
// dump( std::cout, nodes_.top() );
|
||||
// dump( std::cout, result.value() );
|
||||
// if (const auto detach_result = schema_.detach_node(foreign_node); !detach_result) {
|
||||
// // Todo: throw internal error
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
// } else {
|
||||
// if (const auto rit = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index());
|
||||
// rit != foreign_node->info().endpoint_end()) {
|
||||
// if (rit->second->is_belongs_to()) {
|
||||
// rit->second->node_ = foreign_node;
|
||||
// const auto localEndpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, nodes_.top());
|
||||
// nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), localEndpoint);
|
||||
// link_relation_endpoints(localEndpoint, rit->second);
|
||||
// } else {
|
||||
// // Todo: throw internal error relation node has invalid type
|
||||
// }
|
||||
// } else {
|
||||
// // Todo: throw internal error couldn't find relation node
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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()) {
|
||||
@@ -308,13 +427,16 @@ void relation_completer<Type>::on_belongs_to(const char *id,
|
||||
// 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()) {
|
||||
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);
|
||||
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->is_has_many()) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user