127 lines
4.2 KiB
C++
127 lines
4.2 KiB
C++
#include <utility>
|
|
|
|
#include "matador/object/repository.hpp"
|
|
#include "matador/object/repository_node.hpp"
|
|
|
|
namespace matador::object {
|
|
repository_node::repository_node(object::repository &repo)
|
|
: repo_(repo)
|
|
, type_index_(typeid(detail::null_type)){
|
|
}
|
|
|
|
repository_node::repository_node(object::repository &repo, const std::type_index& ti)
|
|
: repo_(repo)
|
|
, type_index_(ti) {
|
|
}
|
|
|
|
repository_node::repository_node(object::repository &repo, std::string name, const std::type_index& ti)
|
|
: repo_(repo)
|
|
, type_index_(ti)
|
|
, first_child_(std::shared_ptr<repository_node>(new repository_node(repo)))
|
|
, last_child_(std::shared_ptr<repository_node>(new repository_node(repo)))
|
|
, name_(std::move(name)) {
|
|
first_child_->next_sibling_ = last_child_;
|
|
last_child_->previous_sibling_ = first_child_;
|
|
}
|
|
|
|
std::shared_ptr<repository_node> repository_node::make_null_node(object::repository &repo) {
|
|
auto node = std::shared_ptr<repository_node>(new repository_node(repo));
|
|
node->info_ = std::make_unique<null_info>(node, std::shared_ptr<attribute_definition>{});
|
|
|
|
return node;
|
|
}
|
|
|
|
std::string repository_node::name() const {
|
|
return name_;
|
|
}
|
|
|
|
std::type_index repository_node::type_index() const {
|
|
return type_index_;
|
|
}
|
|
|
|
const basic_object_info &repository_node::info() const {
|
|
return *info_;
|
|
}
|
|
|
|
void repository_node::update_name(const std::string& name) {
|
|
name_ = name;
|
|
if (info_->reference_column() && info_->reference_column()->object() != nullptr) {
|
|
info_->reference_column()->object()->name_ = name;
|
|
}
|
|
}
|
|
|
|
const repository& repository_node::schema() const {
|
|
return repo_;
|
|
}
|
|
|
|
bool repository_node::has_children() const {
|
|
return first_child_->next_sibling_ != last_child_;
|
|
}
|
|
|
|
repository_node::node_ptr repository_node::next() const {
|
|
// if we have a child, child is the next iterator to return
|
|
// (if we don't 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_;
|
|
}
|
|
|
|
repository_node::node_ptr repository_node::prev() const {
|
|
// if the 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_;
|
|
}
|
|
|
|
void repository_node::unlink() {
|
|
previous_sibling_->next_sibling_ = next_sibling_;
|
|
next_sibling_->previous_sibling_ = previous_sibling_;
|
|
next_sibling_.reset();
|
|
previous_sibling_.reset();
|
|
}
|
|
|
|
utils::result<repository_node::node_ptr, utils::error> repository_node::make_and_attach_node(repository& repo, const std::string& name, const std::type_index& ti) {
|
|
const auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, ti));
|
|
|
|
return repo.attach_node(node, "");
|
|
}
|
|
|
|
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti, const std::shared_ptr<object_definition>& obj, const primary_key_info& pk_info, repository& repo) {
|
|
const auto it = repo.missing_references_.find(ti);
|
|
if (it == repo.missing_references_.end()) {
|
|
return std::make_shared<attribute_definition>(pk_info.pk_column_name, pk_info.type, attribute_options{utils::constraints::FOREIGN_KEY});
|
|
}
|
|
|
|
auto ref_column = it->second;
|
|
repo.missing_references_.erase(it);
|
|
ref_column->name(pk_info.pk_column_name);
|
|
ref_column->object(obj);
|
|
ref_column->change_type(pk_info.type);
|
|
ref_column->attributes() = utils::constraints::FOREIGN_KEY;
|
|
|
|
if (obj->name().empty()) {
|
|
repo.missing_references_.insert({ti, ref_column});
|
|
}
|
|
|
|
return ref_column;
|
|
}
|
|
}
|