relation_completer progress
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
namespace matador::object {
|
||||
|
||||
class schema_node;
|
||||
class relation_endpoint;
|
||||
|
||||
class basic_object_info {
|
||||
public:
|
||||
@@ -26,7 +27,7 @@ public:
|
||||
[[nodiscard]] const object_definition& definition() const;
|
||||
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
|
||||
|
||||
bool has_primary_key() const;
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
|
||||
void register_relation_endpoint(const std::type_index &type, const relation_endpoint &endpoint);
|
||||
|
||||
@@ -33,5 +33,30 @@ private:
|
||||
object_ptr<ForeignType> remote_;
|
||||
};
|
||||
|
||||
template < class LocalType, class Type >
|
||||
class many_to_relation {
|
||||
public:
|
||||
many_to_relation() = default;
|
||||
many_to_relation(std::string local_name, std::string remote_name)
|
||||
: local_name_(std::move(local_name))
|
||||
, type_name_(std::move(remote_name)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
field::belongs_to(op, local_name_.c_str(), local_, utils::default_foreign_attributes);
|
||||
field::attribute(op, type_name_.c_str(), value_);
|
||||
}
|
||||
|
||||
object_ptr<LocalType> local() const { return local_; }
|
||||
Type value() const { return value_; }
|
||||
|
||||
private:
|
||||
std::string local_name_;
|
||||
std::string type_name_;
|
||||
object_ptr<LocalType> local_;
|
||||
Type value_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_HAS_MANY_TO_MANY_RELATION_HPP
|
||||
|
||||
@@ -10,21 +10,33 @@ class schema_node;
|
||||
template<typename Type>
|
||||
class object_info final : public basic_object_info {
|
||||
public:
|
||||
explicit object_info(const std::shared_ptr<schema_node>& node,
|
||||
std::shared_ptr<attribute_definition> &&ref_column)
|
||||
using create_func = std::function<std::unique_ptr<Type>()>;
|
||||
|
||||
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), {}) {
|
||||
}
|
||||
explicit object_info(const std::shared_ptr<schema_node>& node,
|
||||
utils::identifier &&pk,
|
||||
std::shared_ptr<attribute_definition> &&ref_column,
|
||||
object_definition &&definition)
|
||||
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)) {
|
||||
}
|
||||
object_info(const std::shared_ptr<schema_node>& node,
|
||||
utils::identifier &&pk,
|
||||
std::shared_ptr<attribute_definition> &&ref_column,
|
||||
object_definition &&definition,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, typeid(Type), std::move(pk), std::move(ref_column), std::move(definition))
|
||||
, creator_(std::move(creator)){
|
||||
}
|
||||
|
||||
const Type &prototype() const { return prototype_; }
|
||||
std::unique_ptr<Type> create() const { return creator_(); }
|
||||
|
||||
private:
|
||||
Type prototype_;
|
||||
create_func creator_{[]{ return std::make_unique<Type>(); }};
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
|
||||
@@ -45,6 +45,14 @@ private:
|
||||
utils::identifier pk_;
|
||||
};
|
||||
|
||||
template<typename>
|
||||
struct is_object_ptr : std::false_type
|
||||
{};
|
||||
|
||||
template<typename Type>
|
||||
struct is_object_ptr<object_ptr<Type>> : std::true_type
|
||||
{};
|
||||
|
||||
}
|
||||
|
||||
#endif //OBJECT_PTR_HPP
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#ifndef MATADOR_RELATION_ENDPOINT_HPP
|
||||
#define MATADOR_RELATION_ENDPOINT_HPP
|
||||
|
||||
#include "matador/object/schema_node.hpp"
|
||||
|
||||
#include "matador/utils/enum_mapper.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class schema_node;
|
||||
|
||||
enum class relation_type : uint8_t {
|
||||
BELONGS_TO,
|
||||
HAS_ONE,
|
||||
|
||||
@@ -58,11 +58,11 @@ class relation_completer final {
|
||||
public:
|
||||
using value_type = Type;
|
||||
|
||||
static void prepare_expected_nodes(schema &scm) {
|
||||
relation_completer analyzer(scm);
|
||||
static void prepare_expected_nodes(schema_node &node) {
|
||||
relation_completer completer(node);
|
||||
|
||||
Type obj;
|
||||
access::process(analyzer, obj);
|
||||
access::process(completer, obj);
|
||||
}
|
||||
|
||||
template < class PrimaryKeyType >
|
||||
@@ -86,11 +86,12 @@ public:
|
||||
}
|
||||
|
||||
template<class CollectionType>
|
||||
void on_has_many(const char * /*id*/, CollectionType &, const char *, const utils::foreign_attributes &/*attr*/);
|
||||
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 );
|
||||
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 );
|
||||
|
||||
template<class CollectionType>
|
||||
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 ContainerType>
|
||||
void on_has_many_to_many(const char *id, ContainerType &collection, const utils::foreign_attributes &attr);
|
||||
|
||||
@@ -99,14 +100,14 @@ private:
|
||||
void on_foreign_key();
|
||||
|
||||
private:
|
||||
explicit relation_completer(schema& schema, const std::shared_ptr<schema_node> &node)
|
||||
: schema_(schema)
|
||||
, node_(node){}
|
||||
explicit relation_completer(schema_node& node)
|
||||
: node_(node)
|
||||
, schema_(node.schema_){}
|
||||
|
||||
|
||||
private:
|
||||
schema &schema_;
|
||||
std::shared_ptr<schema_node> node_;
|
||||
schema_node &node_;
|
||||
schema& schema_;
|
||||
};
|
||||
|
||||
|
||||
@@ -134,9 +135,8 @@ public:
|
||||
type_index_node_map_.insert({node->type_index(), node});
|
||||
} else {
|
||||
// analyze node (collect unknown types by type index)
|
||||
relation_completer<Type>::prepare_expected_nodes(*this);
|
||||
|
||||
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());
|
||||
}
|
||||
@@ -245,8 +245,41 @@ private:
|
||||
|
||||
template<typename Type>
|
||||
template<class CollectionType>
|
||||
void relation_completer<Type>::on_has_many( const char*, CollectionType&, const char*, const utils::foreign_attributes& ) {
|
||||
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;
|
||||
|
||||
// Process foreign key has many relation
|
||||
// Check if foreign key type was already registered
|
||||
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>();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Process values has many relation
|
||||
// Register relation table
|
||||
|
||||
// many_to_relation<Type, value_type> *relation = new many_to_relation<Type, value_type>(join_column, "value");
|
||||
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>();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public:
|
||||
using node_ptr = std::shared_ptr<schema_node>;
|
||||
|
||||
template < typename Type >
|
||||
static std::shared_ptr<schema_node> make_node(schema& tree, const std::string& name) {
|
||||
static std::shared_ptr<schema_node> make_node(object::schema& tree, const std::string& name) {
|
||||
auto node = std::shared_ptr<schema_node>(new schema_node(tree, name));
|
||||
|
||||
primary_key_resolver resolver;
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
static std::shared_ptr<schema_node> make_relation_node(schema& tree, const std::string& name) {
|
||||
static std::shared_ptr<schema_node> make_relation_node(object::schema& tree, const std::string& name) {
|
||||
auto node = std::shared_ptr<schema_node>(new schema_node(tree, name));
|
||||
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
@@ -69,15 +69,19 @@ public:
|
||||
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
||||
}
|
||||
|
||||
const object::schema& schema() const;
|
||||
|
||||
private:
|
||||
explicit schema_node(schema& tree);
|
||||
schema_node(schema& tree, std::string name);
|
||||
explicit schema_node(object::schema& tree);
|
||||
schema_node(object::schema& tree, std::string name);
|
||||
|
||||
private:
|
||||
friend class schema;
|
||||
template <typename Type>
|
||||
friend class relation_completer;
|
||||
friend class const_schema_node_iterator;
|
||||
|
||||
schema &schema_;
|
||||
object::schema &schema_;
|
||||
std::unique_ptr<basic_object_info> info_;
|
||||
|
||||
std::shared_ptr<schema_node> parent_;
|
||||
|
||||
Reference in New Issue
Block a user