relation_completer progress
This commit is contained in:
parent
38cbffc18b
commit
c4a00f19fd
|
|
@ -33,11 +33,14 @@ public:
|
||||||
[[nodiscard]] bool is_has_many() const;
|
[[nodiscard]] bool is_has_many() const;
|
||||||
[[nodiscard]] bool is_belongs_to() const;
|
[[nodiscard]] bool is_belongs_to() const;
|
||||||
|
|
||||||
|
[[nodiscard]] std::shared_ptr<relation_endpoint> foreign_endpoint() const;
|
||||||
|
void link_foreign_endpoint(const std::shared_ptr<relation_endpoint>& endpoint);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string field_name_;
|
std::string field_name_;
|
||||||
relation_type type_;
|
relation_type type_;
|
||||||
std::shared_ptr<schema_node> node_;
|
std::shared_ptr<schema_node> node_;
|
||||||
std::weak_ptr<relation_endpoint> foreign_endpoint;
|
std::shared_ptr<relation_endpoint> foreign_endpoint_;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,9 +81,7 @@ public:
|
||||||
on_foreign_key<ForeignPointerType>();
|
on_foreign_key<ForeignPointerType>();
|
||||||
}
|
}
|
||||||
template<class ForeignPointerType>
|
template<class ForeignPointerType>
|
||||||
void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {
|
void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/);
|
||||||
on_foreign_key<ForeignPointerType>();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class CollectionType>
|
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 );
|
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 );
|
||||||
|
|
@ -248,18 +246,11 @@ 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*/ ) {
|
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;
|
using value_type = typename CollectionType::value_type;
|
||||||
|
|
||||||
// Process foreign key has many relation
|
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type>>(schema_, id, [join_column] {
|
||||||
// Check if foreign key type was already registered
|
return new many_to_many_relation<value_type, Type>(join_column, "id");
|
||||||
auto result = schema_.find_node(typeid(value_type));
|
});
|
||||||
if (result) {
|
|
||||||
} else {
|
|
||||||
//
|
|
||||||
using relation_type = many_to_many_relation<value_type, Type>;
|
|
||||||
auto creator = [] {
|
|
||||||
return new many_to_many_relation<value_type, Type>();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
schema_.attach_node(node, typeid(many_to_many_relation<value_type, Type>));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
|
|
@ -267,20 +258,14 @@ 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*/ ) {
|
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;
|
using value_type = typename CollectionType::value_type;
|
||||||
|
|
||||||
// Process values has many relation
|
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type>>(schema_, id, [join_column] {
|
||||||
// Register relation table
|
return new many_to_many_relation<value_type, Type>(join_column, "value");
|
||||||
|
});
|
||||||
|
|
||||||
// many_to_relation<Type, value_type> *relation = new many_to_relation<Type, value_type>(join_column, "value");
|
const auto result = schema_.attach<many_to_many_relation<value_type, Type>>(id);
|
||||||
auto result = schema_.find_node(typeid(value_type));
|
if (!result) {
|
||||||
if (result) {
|
// Todo: throw internal exception
|
||||||
} else {
|
|
||||||
//
|
|
||||||
using relation_type = many_to_many_relation<value_type, Type>;
|
|
||||||
auto creator = [] {
|
|
||||||
return new many_to_many_relation<value_type, Type>();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
|
|
@ -308,7 +293,8 @@ void relation_completer<Type>::on_has_many_to_many( const char *id, ContainerTyp
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
template<class ForeignPointerType>
|
template<class ForeignPointerType>
|
||||||
void relation_completer<Type>::on_foreign_key() {
|
void relation_completer<Type>::on_has_one(const char * id, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {
|
||||||
|
auto endpoint = std::make_shared<relation_endpoint>(std::string(id), relation_type::HAS_ONE, std::shared_ptr<schema_node>());
|
||||||
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
|
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) {
|
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())});
|
schema_.expected_node_map_.insert({ti, schema_node::make_node<typename ForeignPointerType::value_type>(schema_, ti.name())});
|
||||||
|
|
|
||||||
|
|
@ -33,8 +33,8 @@ public:
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type, typename CreatorFunc >
|
||||||
static std::shared_ptr<schema_node> make_relation_node(object::schema& tree, const std::string& name) {
|
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 node = std::shared_ptr<schema_node>(new schema_node(tree, name));
|
||||||
|
|
||||||
auto info = std::make_unique<object_info<Type>>(
|
auto info = std::make_unique<object_info<Type>>(
|
||||||
|
|
@ -69,7 +69,7 @@ public:
|
||||||
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
||||||
}
|
}
|
||||||
|
|
||||||
const object::schema& schema() const;
|
[[nodiscard]] const object::schema& schema() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit schema_node(object::schema& tree);
|
explicit schema_node(object::schema& tree);
|
||||||
|
|
|
||||||
|
|
@ -32,4 +32,12 @@ bool relation_endpoint::is_has_many() const {
|
||||||
bool relation_endpoint::is_belongs_to() const {
|
bool relation_endpoint::is_belongs_to() const {
|
||||||
return type_ == relation_type::BELONGS_TO;
|
return type_ == relation_type::BELONGS_TO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<relation_endpoint> relation_endpoint::foreign_endpoint() const {
|
||||||
|
return foreign_endpoint_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void relation_endpoint::link_foreign_endpoint( const std::shared_ptr<relation_endpoint>& endpoint ) {
|
||||||
|
foreign_endpoint_ = endpoint;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue