progress on relation completer
This commit is contained in:
@@ -14,13 +14,15 @@ public:
|
||||
|
||||
object_info(const std::shared_ptr<schema_node>& node,
|
||||
std::shared_ptr<attribute_definition> &&ref_column)
|
||||
: basic_object_info(node, typeid(Type), {}, std::move(ref_column), {}) {
|
||||
: basic_object_info(node, typeid(Type), {}, std::move(ref_column), {})
|
||||
, creator_([]{return std::make_unique<Type>(); }){
|
||||
}
|
||||
object_info(const std::shared_ptr<schema_node>& node,
|
||||
utils::identifier &&pk,
|
||||
std::shared_ptr<attribute_definition> &&ref_column,
|
||||
object_definition &&definition)
|
||||
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition)) {
|
||||
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition))
|
||||
, creator_([]{return std::make_unique<Type>(); }){
|
||||
}
|
||||
object_info(const std::shared_ptr<schema_node>& node,
|
||||
utils::identifier &&pk,
|
||||
@@ -30,6 +32,12 @@ public:
|
||||
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition))
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
object_info(const std::shared_ptr<schema_node>& node,
|
||||
object_definition &&definition,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, typeid(Type), std::move(definition))
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
|
||||
const Type &prototype() const { return prototype_; }
|
||||
std::unique_ptr<Type> create() const { return creator_(); }
|
||||
|
||||
@@ -61,7 +61,7 @@ class relation_completer final {
|
||||
public:
|
||||
using value_type = Type;
|
||||
|
||||
static void prepare_expected_nodes(schema_node &node) {
|
||||
static void complete(const std::shared_ptr<schema_node> &node) {
|
||||
relation_completer completer(node);
|
||||
|
||||
Type obj;
|
||||
@@ -95,13 +95,13 @@ public:
|
||||
void on_has_many_to_many(const char *id, ContainerType &collection, const utils::foreign_attributes &attr);
|
||||
|
||||
private:
|
||||
explicit relation_completer(schema_node& node)
|
||||
explicit relation_completer(const std::shared_ptr<schema_node>& node)
|
||||
: node_(node)
|
||||
, schema_(node.schema_)
|
||||
, schema_(node->schema_)
|
||||
, log_(logger::create_logger("relation_completer")) {}
|
||||
|
||||
private:
|
||||
schema_node &node_;
|
||||
std::shared_ptr<schema_node> node_;
|
||||
schema& schema_;
|
||||
logger::logger log_;
|
||||
};
|
||||
@@ -118,25 +118,16 @@ 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"));
|
||||
// if (has_node(name)) {
|
||||
// return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
|
||||
// }
|
||||
auto node = acquire_node<Type>(name);
|
||||
log_.info("attach node %s", name.c_str());
|
||||
relation_completer<Type>::complete(node);
|
||||
if (auto result = attach_node(node, parent); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
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);
|
||||
|
||||
node_map_.insert({node->name(), node})/*.first*/;
|
||||
type_index_node_map_.insert({node->type_index(), node});
|
||||
} else {
|
||||
// analyze node (collect unknown types by type index)
|
||||
const auto node = schema_node::make_node<Type>(*this, name);
|
||||
relation_completer<Type>::prepare_expected_nodes(*node);
|
||||
if (auto result = attach_node(node, parent); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
@@ -204,7 +195,7 @@ public:
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{result.value()->basic_info()});
|
||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> reference(const std::type_index &type_index) const;
|
||||
@@ -221,6 +212,20 @@ private:
|
||||
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
|
||||
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::type_index &type_index) const;
|
||||
|
||||
template <typename Type>
|
||||
std::shared_ptr<schema_node> 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;
|
||||
@@ -234,22 +239,40 @@ private:
|
||||
std::string name_;
|
||||
std::shared_ptr<schema_node> root_;
|
||||
|
||||
t_node_map node_map_;
|
||||
t_type_index_node_map type_index_node_map_;
|
||||
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_;
|
||||
};
|
||||
|
||||
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*/ ) {
|
||||
using value_type = typename CollectionType::value_type;
|
||||
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*/ ) {
|
||||
using value_type = typename CollectionType::value_type::value_type;
|
||||
|
||||
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type>>(schema_, id, [join_column] {
|
||||
return new many_to_many_relation<value_type, Type>(join_column, "id");
|
||||
});
|
||||
|
||||
// schema_.attach_node(node, typeid(many_to_many_relation<value_type, Type>));
|
||||
// Check if the object_ptr type is already inserted in the schema (by id)
|
||||
if (auto result = schema_.find_node(id); !result) {
|
||||
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()) {
|
||||
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>>(schema_, id, [join_column] {
|
||||
return std::make_unique<many_to_many_relation<value_type, Type>>(join_column, "id");
|
||||
});
|
||||
log_.debug("node '%s' many to many type: %s", node_->name().c_str(), typeid(many_to_many_relation<value_type, Type>).name());
|
||||
result = schema_.attach_node(node, "");
|
||||
if (!result) {
|
||||
// Todo: throw internal error
|
||||
return;
|
||||
}
|
||||
node_->info_->register_relation_endpoint(node->type_index(), relation_endpoint(id, relation_type::BELONGS_TO, node));
|
||||
node->info_->register_relation_endpoint(node_->type_index(), relation_endpoint(id, relation_type::HAS_MANY, node_));
|
||||
}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
@@ -307,7 +330,7 @@ void relation_completer<Type>::on_has_one(const char * id, ForeignPointerType &/
|
||||
schema_.expected_node_map_.insert({ti, schema_node::make_node<typename ForeignPointerType::value_type>(schema_, ti.name())});
|
||||
} else {
|
||||
const auto& foreign_node = result.value();
|
||||
if (const auto rit = foreign_node->basic_info().find_relation_endpoint(ti); rit != foreign_node->basic_info().endpoint_end()) {
|
||||
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()) {
|
||||
@@ -322,8 +345,24 @@ 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, const utils::foreign_attributes& attr ) {
|
||||
|
||||
void relation_completer<Type>::on_belongs_to(const char* id, ForeignPointerType& /*obj*/, const utils::foreign_attributes& /*attr*/) {
|
||||
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
|
||||
if (auto result = schema_.find_node(ti); !result) {
|
||||
log_.debug("node '%s' has foreign key '%s' belongs to '%s'", node_->name().c_str(), id, typeid(typename ForeignPointerType::value_type).name());
|
||||
} else {
|
||||
const auto foreign_node = result.value();
|
||||
auto it = foreign_node->info().find_relation_endpoint(node_->type_index());
|
||||
if (it != foreign_node->info().endpoint_end()) {
|
||||
auto endpoint = it->second;
|
||||
}
|
||||
using TP = many_to_many_relation<typename ForeignPointerType::value_type, Type>;
|
||||
log_.debug("node '%s', many to many type: %s", node_->name().c_str(), typeid(TP).name());
|
||||
log_.debug("foreign node '%s', many to many type: %s", foreign_node->name().c_str(), ti.name());
|
||||
if (foreign_node->type_index() == ti) {
|
||||
log_.debug("node '%s' has foreign key '%s' belongs to '%s': already inserted", node_->name().c_str(), id, result.value()->name().c_str());
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,8 @@ public:
|
||||
node,
|
||||
std::move(pk_info.pk),
|
||||
std::make_shared<attribute_definition>(pk_info.pk_column_name, name, pk_info.type, utils::constraints::FOREIGN_KEY),
|
||||
object_definition{attribute_definition_generator::generate<Type>(tree)}
|
||||
object_definition{attribute_definition_generator::generate<Type>(tree)},
|
||||
[]{ return std::make_unique<Type>(); }
|
||||
);
|
||||
node->info_ = std::move(info);
|
||||
|
||||
@@ -37,11 +38,12 @@ public:
|
||||
static std::shared_ptr<schema_node> make_relation_node(object::schema& tree, const std::string& name, CreatorFunc &&creator) {
|
||||
auto node = std::shared_ptr<schema_node>(new schema_node(tree, name));
|
||||
|
||||
// auto info = std::make_unique<object_info<Type>>(
|
||||
// node,
|
||||
// object_definition{attribute_definition_generator::generate<Type>(tree)}
|
||||
// );
|
||||
// node->info_ = std::move(info);
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
node,
|
||||
object_definition{attribute_definition_generator::generate<Type>(tree)},
|
||||
std::move(creator)
|
||||
);
|
||||
node->info_ = std::move(info);
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -60,7 +62,7 @@ public:
|
||||
[[nodiscard]] node_ptr next() const;
|
||||
[[nodiscard]] node_ptr prev() const;
|
||||
|
||||
[[nodiscard]] const basic_object_info& basic_info() const;
|
||||
[[nodiscard]] const basic_object_info& info() const;
|
||||
|
||||
void update_name(const std::string& name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user