query/include/matador/object/schema_node.hpp

99 lines
2.8 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)}
);
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)}
// );
// 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& basic_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;
private:
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;
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