progress on combining foreign_node_completer and relation_completer
This commit is contained in:
@@ -19,6 +19,7 @@ namespace matador::object {
|
||||
* that all foreign nodes needed by the given node
|
||||
* relations are attached in schema.
|
||||
*/
|
||||
template < typename NodeType >
|
||||
class foreign_node_completer final {
|
||||
private:
|
||||
using node_ptr = std::shared_ptr<repository_node>;
|
||||
@@ -27,9 +28,9 @@ public:
|
||||
template<class Type>
|
||||
static void complete(const std::shared_ptr<repository_node> &node) {
|
||||
internal::shadow_repository shadow(node->repo_);
|
||||
foreign_node_completer completer(shadow);
|
||||
foreign_node_completer<Type> completer(shadow);
|
||||
|
||||
completer.complete_node<Type>(node);
|
||||
completer.complete_node(node);
|
||||
}
|
||||
|
||||
template<class PrimaryKeyType>
|
||||
@@ -56,13 +57,14 @@ public:
|
||||
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
|
||||
|
||||
private:
|
||||
explicit foreign_node_completer(internal::shadow_repository &shadow);
|
||||
explicit foreign_node_completer(internal::shadow_repository &shadow)
|
||||
: repo_(shadow)
|
||||
, log_(logger::create_logger("node_completer")) {}
|
||||
|
||||
template<typename Type>
|
||||
void complete_node(const std::shared_ptr<repository_node> &node) {
|
||||
nodes_.push(node);
|
||||
|
||||
Type obj;
|
||||
NodeType obj;
|
||||
access::process(*this, obj);
|
||||
nodes_.pop();
|
||||
}
|
||||
@@ -77,6 +79,21 @@ private:
|
||||
complete<Type>(result.value());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attach_relation_node(const std::string &name) {
|
||||
if (repo_.contains(typeid(Type))) {
|
||||
return;
|
||||
}
|
||||
const auto node = repository_node::make_node<Type>(repo_.repo(), "");
|
||||
if (auto result = repo_.attach_node(node)) {
|
||||
complete<Type>(result.value());
|
||||
}
|
||||
}
|
||||
private:
|
||||
template< typename Type >
|
||||
friend class foreign_node_completer;
|
||||
|
||||
private:
|
||||
std::stack<node_ptr> nodes_;
|
||||
internal::shadow_repository &repo_;
|
||||
@@ -85,29 +102,34 @@ private:
|
||||
};
|
||||
|
||||
|
||||
template < typename NodeType >
|
||||
template<class ForeignPointerType>
|
||||
void foreign_node_completer::on_belongs_to( const char* /*id*/, ForeignPointerType&, const utils::foreign_attributes& ) {
|
||||
void foreign_node_completer<NodeType>::on_belongs_to( const char* /*id*/, ForeignPointerType&, const utils::foreign_attributes& ) {
|
||||
attach_node<typename ForeignPointerType::value_type>();
|
||||
}
|
||||
|
||||
template < typename NodeType >
|
||||
template<class ForeignPointerType>
|
||||
void foreign_node_completer::on_has_one( const char*, ForeignPointerType&, const utils::foreign_attributes& ) {
|
||||
void foreign_node_completer<NodeType>::on_has_one( const char*, ForeignPointerType&, const utils::foreign_attributes& ) {
|
||||
attach_node<typename ForeignPointerType::value_type>();
|
||||
}
|
||||
|
||||
template < typename NodeType >
|
||||
template<class CollectionType>
|
||||
void foreign_node_completer::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>* ) {
|
||||
void foreign_node_completer<NodeType>::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>* ) {
|
||||
attach_node<typename CollectionType::value_type::value_type>();
|
||||
}
|
||||
|
||||
template < typename NodeType >
|
||||
template<class CollectionType>
|
||||
void foreign_node_completer::on_has_many_to_many( const char* /*id*/, CollectionType& /*collection*/, const char* /*join_column*/, const char* /*inverse_join_column*/, const utils::foreign_attributes& /*attr*/ ) {
|
||||
attach_node<typename CollectionType::value_type::value_type>(/*id*/);
|
||||
void foreign_node_completer<NodeType>::on_has_many_to_many( const char* id, CollectionType& /*collection*/, const char* /*join_column*/, const char* /*inverse_join_column*/, const utils::foreign_attributes& /*attr*/ ) {
|
||||
attach_relation_node<typename CollectionType::value_type::value_type>(id);
|
||||
}
|
||||
|
||||
template < typename NodeType >
|
||||
template<class CollectionType>
|
||||
void foreign_node_completer::on_has_many_to_many( const char* /*id*/, CollectionType& /*collection*/, const utils::foreign_attributes& /*attr*/ ) {
|
||||
attach_node<typename CollectionType::value_type::value_type>(/*id*/);
|
||||
void foreign_node_completer<NodeType>::on_has_many_to_many( const char* id, CollectionType& /*collection*/, const utils::foreign_attributes& /*attr*/ ) {
|
||||
attach_relation_node<typename CollectionType::value_type::value_type>(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user