query/source/core/object/schema_node.cpp

80 lines
2.4 KiB
C++

#include <utility>
#include "matador/object/schema_node.hpp"
namespace matador::object {
schema_node::schema_node(object::schema &tree)
: schema_(tree) {
}
schema_node::schema_node(object::schema &tree, std::string name)
: schema_(tree)
, first_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
, last_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
, name_(std::move(name)) {
first_child_->next_sibling_ = last_child_;
last_child_->previous_sibling_ = first_child_;
}
std::shared_ptr<schema_node> schema_node::make_null_node(object::schema &tree) {
auto node = std::shared_ptr<schema_node>(new schema_node(tree));
node->info_ = std::make_unique<null_info>(node, std::shared_ptr<attribute_definition>{});
return node;
}
std::string schema_node::name() const {
return name_;
}
std::type_index schema_node::type_index() const {
return info_->type_index();
}
const basic_object_info &schema_node::basic_info() const {
return *info_;
}
void schema_node::update_name(const std::string& name) {
name_ = name;
info_->reference_column()->name(name);
}
const object::schema& schema_node::schema() const {
return schema_;
}
schema_node::node_ptr schema_node::next() const {
// if we have a child, child is the next iterator to return
// (if we don't do iterate over the siblings)
if (first_child_ && first_child_->next_sibling_ != last_child_) {
return first_child_->next_sibling_;
}
// if there is no child, we check for sibling
// if there is a sibling, this is our next iterator to return
// if not, we go back to the parent
auto *node = this;
while (node->parent_ && node->next_sibling_ == node->parent_->last_child_) {
node = node->parent_.get();
}
return node->parent_ ? node->next_sibling_ : node->last_child_;
}
schema_node::node_ptr schema_node::prev() const {
// if node has a previous sibling, we set it
// as our next iterator. then we check if there
// are last children. if so, we set the last-last
// child as our iterator
if (previous_sibling_ && previous_sibling_->previous_sibling_) {
auto node = previous_sibling_;
while (node->last_child_ && node->first_child_->next_sibling_ != node->last_child_) {
node = node->last_child_->previous_sibling_;
}
return node;
}
// if there is no previous sibling, our next iterator
// is the parent of the node
return parent_->parent_ ? parent_ : parent_->first_child_->next_sibling_;
}
}