105 lines
3.0 KiB
C++
105 lines
3.0 KiB
C++
#ifndef SCHEMA_NODE_HPP
|
|
#define SCHEMA_NODE_HPP
|
|
|
|
#include "matador/object/attribute_definition_generator.hpp"
|
|
#include "matador/object/object_info.hpp"
|
|
#include "matador/object/primary_key_resolver.hpp"
|
|
|
|
#include <memory>
|
|
|
|
namespace matador::object {
|
|
|
|
class basic_object_info;
|
|
class schema;
|
|
|
|
class schema_node final {
|
|
public:
|
|
using node_ptr = std::shared_ptr<schema_node>;
|
|
|
|
template < typename Type >
|
|
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;
|
|
auto pk_info = resolver.resolve<Type>();
|
|
auto info = std::make_unique<object_info<Type>>(
|
|
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)},
|
|
[]{ return std::make_unique<Type>(); }
|
|
);
|
|
node->info_ = std::move(info);
|
|
|
|
return node;
|
|
}
|
|
|
|
template < typename Type, typename CreatorFunc >
|
|
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)},
|
|
std::move(creator)
|
|
);
|
|
node->info_ = std::move(info);
|
|
|
|
return node;
|
|
}
|
|
|
|
static std::shared_ptr<schema_node> make_null_node(schema& tree);
|
|
|
|
schema_node(const schema_node& other) = delete;
|
|
schema_node(schema_node&& other) = default;
|
|
schema_node& operator=(const schema_node& other) = delete;
|
|
schema_node& operator=(schema_node&& other) = delete;
|
|
~schema_node() = default;
|
|
|
|
[[nodiscard]] std::string name() const;
|
|
[[nodiscard]] std::type_index type_index() const;
|
|
|
|
[[nodiscard]] node_ptr next() const;
|
|
[[nodiscard]] node_ptr prev() const;
|
|
|
|
[[nodiscard]] const basic_object_info& info() const;
|
|
|
|
void update_name(const std::string& name);
|
|
|
|
template <typename Type>
|
|
object_info_ref<Type> info() const {
|
|
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
|
}
|
|
|
|
[[nodiscard]] const object::schema& schema() const;
|
|
|
|
[[nodiscard]] bool has_children() const;
|
|
|
|
private:
|
|
explicit schema_node(object::schema& tree);
|
|
schema_node(object::schema& tree, std::string name);
|
|
|
|
void unlink();
|
|
|
|
private:
|
|
friend class schema;
|
|
template <typename Type>
|
|
friend class relation_completer;
|
|
friend class const_schema_node_iterator;
|
|
|
|
object::schema &schema_;
|
|
std::unique_ptr<basic_object_info> info_;
|
|
|
|
std::shared_ptr<schema_node> parent_;
|
|
std::shared_ptr<schema_node> previous_sibling_;
|
|
std::shared_ptr<schema_node> next_sibling_;
|
|
std::shared_ptr<schema_node> first_child_;
|
|
std::shared_ptr<schema_node> last_child_;
|
|
|
|
std::string name_;
|
|
size_t depth_{0};
|
|
};
|
|
|
|
}
|
|
#endif //SCHEMA_NODE_HPP
|