schema progress (does not compile)
This commit is contained in:
@@ -92,7 +92,7 @@ private:
|
||||
* no has_many or belongs_to
|
||||
* invalid relation -> error
|
||||
*/
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
class relation_completer final {
|
||||
private:
|
||||
using node_ptr = std::shared_ptr<repository_node>;
|
||||
@@ -100,9 +100,9 @@ private:
|
||||
public:
|
||||
using endpoint_ptr = std::shared_ptr<relation_endpoint>;
|
||||
|
||||
static void complete(const std::shared_ptr<repository_node> &node) {
|
||||
static void complete(const std::shared_ptr<repository_node> &node, const std::vector<std::unique_ptr<observer<Type>>> &observers) {
|
||||
internal::shadow_repository shadow(node->repo_);
|
||||
relation_completer completer(shadow);
|
||||
relation_completer completer(shadow, observers);
|
||||
|
||||
completer.complete_node_relations(node);
|
||||
}
|
||||
@@ -131,10 +131,10 @@ public:
|
||||
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
|
||||
|
||||
private:
|
||||
explicit relation_completer(internal::shadow_repository &shadow)
|
||||
: schema_(shadow)
|
||||
, log_(logger::create_logger("relation_completer")) {
|
||||
}
|
||||
explicit relation_completer(internal::shadow_repository &shadow, const std::vector<std::unique_ptr<observer<Type>>>& observers)
|
||||
: schema_(shadow)
|
||||
, log_(logger::create_logger("relation_completer"))
|
||||
, observers_(observers) {}
|
||||
|
||||
void complete_node_relations(const std::shared_ptr<repository_node> &node) {
|
||||
nodes_.push(node);
|
||||
@@ -152,16 +152,19 @@ private:
|
||||
static void link_relation_endpoints(const endpoint_ptr &endpoint,
|
||||
const endpoint_ptr &other_endpoint);
|
||||
|
||||
node_ptr find_node(const std::type_index &ti) const;
|
||||
|
||||
private:
|
||||
std::stack<node_ptr> nodes_;
|
||||
internal::shadow_repository &schema_;
|
||||
logger::logger log_;
|
||||
join_columns_collector join_columns_collector_{};
|
||||
const std::vector<std::unique_ptr<observer<Type>>>& observers_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<class CollectionType>
|
||||
void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
void relation_completer<Type, Observers...>::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*/) {
|
||||
@@ -170,12 +173,11 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
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));
|
||||
if (!result) {
|
||||
auto foreign_node = find_node(typeid(value_type));
|
||||
if (!foreign_node) {
|
||||
// Todo: throw internal error or attach node
|
||||
return;
|
||||
}
|
||||
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()) {
|
||||
@@ -198,8 +200,8 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
|
||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
|
||||
return std::make_unique<relation_value_type>("id", join_column);
|
||||
});
|
||||
result = schema_.attach_node(node);
|
||||
}, {});
|
||||
const auto result = schema_.attach_node(node);
|
||||
if (!result) {
|
||||
// Todo: throw internal error
|
||||
return;
|
||||
@@ -215,9 +217,9 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<class CollectionType>
|
||||
void relation_completer<Type>::on_has_many(const char *id, CollectionType &, const char *join_column,
|
||||
void relation_completer<Type, Observers...>::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*/) {
|
||||
@@ -226,7 +228,7 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &, con
|
||||
|
||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
|
||||
return std::make_unique<relation_value_type>(join_column, "value");
|
||||
});
|
||||
}, {});
|
||||
const auto result = schema_.attach_node(node);
|
||||
if (!result) {
|
||||
// Todo: throw internal exception
|
||||
@@ -241,9 +243,9 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &, con
|
||||
link_relation_endpoints(local_endpoint, foreign_endpoint);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<class CollectionType>
|
||||
void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||
void relation_completer<Type, Observers...>::on_has_many_to_many(const char *id,
|
||||
CollectionType &/*collection*/,
|
||||
const char *join_column,
|
||||
const char *inverse_join_column,
|
||||
@@ -256,9 +258,9 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<class CollectionType>
|
||||
void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||
void relation_completer<Type, Observers...>::on_has_many_to_many(const char *id,
|
||||
CollectionType &collection,
|
||||
const utils::foreign_attributes &attr) {
|
||||
const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
|
||||
@@ -270,46 +272,43 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||
attr);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<class ForeignPointerType>
|
||||
void relation_completer<Type>::on_has_one(const char *id,
|
||||
void relation_completer<Type, Observers...>::on_has_one(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));
|
||||
const auto result = schema_.find_node(ti);
|
||||
if (!result) {
|
||||
// Node was not found
|
||||
// Todo: Throw internal error
|
||||
auto foreign_node = find_node(typeid(value_type));
|
||||
if (!foreign_node) {
|
||||
// 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 local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasOne, result.value());
|
||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasOne, foreign_node);
|
||||
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||
}
|
||||
if (const auto foreign_it = result.value()->info().find_relation_endpoint(typeid(Type)); foreign_it != result.value()->info().endpoint_end()) {
|
||||
if (const auto foreign_it = foreign_node->info().find_relation_endpoint(typeid(Type)); foreign_it != foreign_node->info().endpoint_end()) {
|
||||
link_relation_endpoints(local_it->second, foreign_it->second);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<class ForeignPointerType>
|
||||
void relation_completer<Type>::on_belongs_to(const char *id,
|
||||
void relation_completer<Type, Observers...>::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));
|
||||
auto result = schema_.find_node(ti);
|
||||
if (!result) {
|
||||
// Type was not found
|
||||
// Todo: Throw internal error
|
||||
auto foreign_node = find_node(typeid(value_type));
|
||||
if (!foreign_node) {
|
||||
// Todo: throw internal error or attach node
|
||||
return;
|
||||
}
|
||||
|
||||
// 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()) {
|
||||
// Found corresponding relation endpoint in the foreign node
|
||||
@@ -341,21 +340,20 @@ void relation_completer<Type>::on_belongs_to(const char *id,
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
template<typename NodeType>
|
||||
void relation_completer<Type>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
||||
void relation_completer<Type, Observers...>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
||||
using relation_value_type = many_to_many_relation<NodeType, Type>;
|
||||
using value_type = NodeType;
|
||||
|
||||
// Check if the object_ptr type is already inserted in the schema (by id)
|
||||
auto result = schema_.find_node(typeid(value_type));
|
||||
if (!result) {
|
||||
auto foreign_node = find_node(typeid(value_type));
|
||||
if (!foreign_node) {
|
||||
// Todo: throw internal error or attach node
|
||||
return;
|
||||
}
|
||||
|
||||
const auto foreign_node = result.value();
|
||||
result = schema_.find_node(name);
|
||||
auto result = schema_.find_node(name);
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
@@ -364,14 +362,16 @@ void relation_completer<Type>::attach_relation_node(const std::string &name, con
|
||||
return std::make_unique<relation_value_type>(join_column, inverse_join_column);
|
||||
};
|
||||
|
||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), name, std::move(creator));
|
||||
auto observers = internal::observer_list_copy_creator<Type, relation_value_type, Observers...>::copy_create(observers_);
|
||||
|
||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), name, std::move(creator), std::move(observers));
|
||||
result = schema_.attach_node(node);
|
||||
if (!result) {
|
||||
// Todo: throw internal error
|
||||
return;
|
||||
}
|
||||
|
||||
foreign_node_completer<relation_value_type>::complete(result.value());
|
||||
foreign_node_completer<relation_value_type>::complete(result.value(), {});
|
||||
|
||||
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
|
||||
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
|
||||
@@ -389,18 +389,31 @@ void relation_completer<Type>::attach_relation_node(const std::string &name, con
|
||||
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
void relation_completer<Type, Observers...>::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<typename Type>
|
||||
void relation_completer<Type>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
|
||||
template<typename Type, template<typename> typename... Observers>
|
||||
void relation_completer<Type, Observers...>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
|
||||
endpoint->link_foreign_endpoint(other_endpoint);
|
||||
other_endpoint->link_foreign_endpoint(endpoint);
|
||||
}
|
||||
|
||||
template<typename Type, template <typename> class ... Observers>
|
||||
typename relation_completer<Type, Observers...>::node_ptr relation_completer<Type, Observers...>::find_node(const std::type_index &ti) const {
|
||||
node_ptr foreign_node;
|
||||
if (auto result = schema_.find_node(ti); result) {
|
||||
return result.value();
|
||||
}
|
||||
|
||||
if (!schema_.is_node_announced(ti)) {
|
||||
return {};
|
||||
}
|
||||
return schema_.announce_node(ti);
|
||||
}
|
||||
}
|
||||
#endif //RELATION_COMPLETER_HPP
|
||||
|
||||
Reference in New Issue
Block a user