added date_type_t, time_type_t and timestamp, ensure allocated postgres resources are released, split repository into basic_repository and repository.
This commit is contained in:
@@ -28,7 +28,8 @@ void attribute::name( const std::string& n ) {
|
||||
}
|
||||
|
||||
std::string attribute::full_name() const {
|
||||
return owner_ ? owner_->name() + "." + name_ : name_;
|
||||
const auto owner = owner_.lock();
|
||||
return owner ? owner->name() + "." + name_ : name_;
|
||||
}
|
||||
|
||||
const utils::field_attributes &attribute::attributes() const {
|
||||
@@ -48,7 +49,7 @@ utils::basic_type attribute::type() const {
|
||||
}
|
||||
|
||||
std::shared_ptr<object> attribute::owner() const {
|
||||
return owner_;
|
||||
return owner_.lock();
|
||||
}
|
||||
|
||||
void attribute::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
||||
|
||||
@@ -5,16 +5,16 @@
|
||||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node, std::shared_ptr<class object>&& obj)
|
||||
: object_(std::move(obj))
|
||||
, node_(std::move(node)) {}
|
||||
basic_object_info::basic_object_info(const repository_node& node, const std::shared_ptr<class object>& obj)
|
||||
: object_(obj)
|
||||
, node_(node) {}
|
||||
|
||||
std::type_index basic_object_info::type_index() const {
|
||||
return node_->type_index();
|
||||
return node_.type_index();
|
||||
}
|
||||
|
||||
std::string basic_object_info::name() const {
|
||||
return node_->name();
|
||||
return node_.name();
|
||||
}
|
||||
|
||||
std::shared_ptr<class object> basic_object_info::object() const {
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
#include "matador/object/basic_repository.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
#include "matador/object/repository_node_iterator.hpp"
|
||||
|
||||
#include "matador/logger/log_manager.hpp"
|
||||
|
||||
#include <stack>
|
||||
|
||||
namespace matador::object {
|
||||
utils::error make_error(const error_code ec, const std::string &msg) {
|
||||
return utils::error(ec, msg);
|
||||
}
|
||||
|
||||
basic_repository::basic_repository(std::string name)
|
||||
: name_(std::move(name))
|
||||
, root_(new repository_node(*this))
|
||||
, log_(logger::create_logger("schema")) {
|
||||
root_->first_child_ = std::make_unique<repository_node>(*this);
|
||||
root_->last_child_ = std::make_unique<repository_node>(*this);
|
||||
root_->first_child_->next_sibling_ = root_->last_child_.get();
|
||||
root_->last_child_->previous_sibling_ = root_->first_child_.get();
|
||||
root_->info_ = std::make_unique<null_info>(*root_);
|
||||
}
|
||||
|
||||
basic_repository::~basic_repository() {
|
||||
if (!root_) {
|
||||
return;
|
||||
}
|
||||
while (root_->first_child_->next_sibling_ != root_->last_child_.get()) {
|
||||
remove_node(root_->first_child_->next_sibling_);
|
||||
}
|
||||
delete root_;
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> basic_repository::detach(repository_node *node) {
|
||||
log_.debug("detach node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
||||
|
||||
remove_node(node);
|
||||
|
||||
node->on_detach();
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
basic_repository::const_iterator basic_repository::begin() const {
|
||||
return const_iterator(root_->first_child_->next_sibling_);
|
||||
}
|
||||
|
||||
basic_repository::const_iterator basic_repository::end() const {
|
||||
return const_iterator(root_->last_child_.get());
|
||||
}
|
||||
|
||||
bool basic_repository::empty() const {
|
||||
return root_->first_child_.get() == root_->last_child_->previous_sibling_;
|
||||
}
|
||||
|
||||
size_t basic_repository::size() const {
|
||||
return static_cast<size_t>(std::distance(begin(), end()));
|
||||
}
|
||||
|
||||
std::string basic_repository::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
bool basic_repository::contains( const std::string& name ) const {
|
||||
return nodes_by_name_.count(name) > 0;
|
||||
}
|
||||
|
||||
bool basic_repository::contains( const std::type_index& index ) const {
|
||||
return nodes_by_type_.count(index) > 0;
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> basic_repository::basic_info(const std::type_index &ti) const {
|
||||
const auto it = find_node(ti);
|
||||
if (it == end()) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound, "Node '" + std::string(ti.name()) + "' not found."));
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{it->info()});
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> basic_repository::basic_info(const std::string& name) const {
|
||||
const auto it = find_node(name);
|
||||
if (it == end()) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound, "Node '" + name + "' not found."));
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{it->info()});
|
||||
}
|
||||
|
||||
utils::result<attribute*, utils::error> basic_repository::primary_key_attribute(const std::type_index &ti) const {
|
||||
const auto it = find_node(ti);
|
||||
if (it == end()) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound, "Node '" + std::string(ti.name()) + "' not found."));
|
||||
}
|
||||
|
||||
if (!it->info().has_primary_key()) {
|
||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Object '" + it->name() + "' does not have a primary key."));
|
||||
}
|
||||
return utils::ok(it->info().primary_key_attribute());
|
||||
}
|
||||
|
||||
void basic_repository::dump(std::ostream &os) const {
|
||||
for (const auto &node : *this) {
|
||||
dump(os, node);
|
||||
}
|
||||
os << "\n";
|
||||
}
|
||||
|
||||
void basic_repository::dump( std::ostream& os, const repository_node& node ) {
|
||||
os << "node [" << node.name() << "] (" << node.type_index().name() << ")\n";
|
||||
for (auto it = node.info().endpoint_begin(); it != node.info().endpoint_end(); ++it) {
|
||||
os << " " << node.name() << "::" << it->second->field_name() << " (" << it->second->type_name() << ")";
|
||||
if (it->second->foreign_endpoint()) {
|
||||
os << " <---> " << it->second->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
||||
} else {
|
||||
os << " -> " << it->second->node().name() << " (type: " << it->second->node().type_index().name() << ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils::result<repository_node*, utils::error> basic_repository::attach_node(repository_node *node, const std::string &parent) {
|
||||
if (has_node(node)) {
|
||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + node->name() + "' already exists."));
|
||||
}
|
||||
|
||||
log_.info("attach: insert node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
||||
|
||||
// set node to root node
|
||||
auto parent_node = root_;
|
||||
if (!parent.empty()) {
|
||||
const auto result = find_node(parent);
|
||||
if (result == end()) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + parent + "' not found."));
|
||||
}
|
||||
parent_node = result.get();
|
||||
}
|
||||
|
||||
insert_node(parent_node, node);
|
||||
|
||||
// Todo: check return value
|
||||
nodes_by_name_.insert({node->name(), node});
|
||||
nodes_by_type_.insert({node->type_index(), node});
|
||||
|
||||
node->on_attach();
|
||||
|
||||
return utils::ok(node);
|
||||
}
|
||||
|
||||
basic_repository::const_iterator basic_repository::find_node(const std::string &name) const {
|
||||
// first search in the prototype map
|
||||
const auto i = nodes_by_name_.find(name);
|
||||
if (i == nodes_by_name_.end()) {
|
||||
return end();
|
||||
}
|
||||
return const_iterator(i->second);
|
||||
}
|
||||
|
||||
basic_repository::const_iterator basic_repository::find_node(const std::type_index &type_index) const {
|
||||
const auto i = nodes_by_type_.find(type_index);
|
||||
if (i == nodes_by_type_.end()) {
|
||||
return end();
|
||||
}
|
||||
return const_iterator(i->second);
|
||||
}
|
||||
|
||||
void basic_repository::insert_node(repository_node *parent, repository_node *child) {
|
||||
child->parent_ = parent;
|
||||
child->previous_sibling_ = parent->last_child_->previous_sibling_;
|
||||
child->next_sibling_ = parent->last_child_.get();
|
||||
/*
|
||||
* +-----------------------------<- (first) parent (last) -> ----------------------------+
|
||||
* | |
|
||||
* first (next) -> <- (prev) child_1 (next) -> <- (prev) new_child (next) -> <- (prev) last
|
||||
* ^^^^^^^ inserted ^^^^^^
|
||||
*/
|
||||
if (const auto prev_sib = parent->last_child_->previous_sibling_) {
|
||||
prev_sib->next_sibling_ = child;
|
||||
}
|
||||
parent->last_child_->previous_sibling_ = child;
|
||||
// set depth
|
||||
// child->depth = depth + 1;
|
||||
}
|
||||
|
||||
void basic_repository::remove_node(repository_node *node) {
|
||||
// auto next = node->next();
|
||||
|
||||
std::stack<repository_node*> nodes_to_remove;
|
||||
nodes_to_remove.push(node);
|
||||
|
||||
while (!nodes_to_remove.empty()) {
|
||||
const auto current = nodes_to_remove.top();
|
||||
|
||||
if (current->has_children()) {
|
||||
// Push all children to the stack (from right to left to maintain order)
|
||||
auto child = current->last_child_->previous_sibling_;
|
||||
while (child && child != current->first_child_.get()) {
|
||||
nodes_to_remove.push(child);
|
||||
child = child->previous_sibling_;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// No children left, safe to remove this node
|
||||
nodes_to_remove.pop();
|
||||
current->unlink();
|
||||
nodes_by_name_.erase(current->name());
|
||||
nodes_by_type_.erase(current->type_index());
|
||||
delete current;
|
||||
}
|
||||
}
|
||||
|
||||
bool basic_repository::has_node(const std::string &name) const {
|
||||
return nodes_by_name_.count(name) > 0;
|
||||
}
|
||||
|
||||
bool basic_repository::has_node(const std::type_index &index) const {
|
||||
return nodes_by_type_.count(index) > 0;
|
||||
}
|
||||
|
||||
bool basic_repository::has_node(const repository_node* node) const {
|
||||
return nodes_by_name_.count(node->name()) > 0 || nodes_by_type_.count(node->type_index()) > 0;
|
||||
}
|
||||
|
||||
bool basic_repository::expecting_relation_node( const std::string& name ) const {
|
||||
return expected_relation_nodes_.count(name) > 0;
|
||||
}
|
||||
|
||||
void basic_repository::expect_relation_node(const std::string &name, const std::type_index &ti) {
|
||||
expected_relation_nodes_.insert({name, ti});
|
||||
}
|
||||
|
||||
void basic_repository::remove_expected_relation_node( const std::string& name ) {
|
||||
expected_relation_nodes_.erase(name);
|
||||
}
|
||||
|
||||
std::shared_ptr<object> basic_repository::provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj) {
|
||||
return object_by_type_.insert({ti, obj}).first->second;
|
||||
}
|
||||
|
||||
bool basic_repository::has_object_for_type(const std::type_index &ti) const {
|
||||
return object_by_type_.count(ti) > 0;
|
||||
}
|
||||
|
||||
std::shared_ptr<object> basic_repository::object_for_type(const std::type_index &ti) const {
|
||||
return object_by_type_.at(ti);
|
||||
}
|
||||
|
||||
void basic_repository::remove_object_for_type(const std::type_index &ti) {
|
||||
object_by_type_.erase(ti);
|
||||
}
|
||||
|
||||
bool basic_repository::is_node_announced(const std::type_index &ti) const {
|
||||
return announced_node_.count(ti) > 0;
|
||||
}
|
||||
|
||||
void basic_repository::push_announce_node(const std::type_index &ti, std::unique_ptr<repository_node> &&node) {
|
||||
announced_node_.insert({ti, std::move(node)});
|
||||
}
|
||||
|
||||
repository_node* basic_repository::announce_node(const std::type_index &ti) const {
|
||||
return announced_node_.find(ti)->second.get();
|
||||
}
|
||||
|
||||
std::unique_ptr<repository_node> basic_repository::pop_announce_node(const std::type_index &ti) {
|
||||
const auto it = announced_node_.find(ti);
|
||||
if (it == announced_node_.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto node = std::move(it->second);
|
||||
announced_node_.erase(it);
|
||||
return node;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
#include "matador/object/internal/shadow_repository.hpp"
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
namespace matador::object::internal {
|
||||
shadow_repository::shadow_repository(repository& s)
|
||||
: repo_(s) {}
|
||||
|
||||
repository& shadow_repository::repo() const {
|
||||
return repo_;
|
||||
}
|
||||
|
||||
bool shadow_repository::contains( const std::type_index& ti ) const {
|
||||
return repo_.contains(ti);
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> shadow_repository::basic_info(const std::type_index& ti) const {
|
||||
return repo_.basic_info(ti);
|
||||
}
|
||||
|
||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
||||
return repo_.find_node(type_index);
|
||||
}
|
||||
|
||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::string& name ) const {
|
||||
return repo_.find_node(name);
|
||||
}
|
||||
|
||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::attach_node( const std::shared_ptr<repository_node>& node ) const {
|
||||
return repo_.attach_node(node, "");
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> shadow_repository::detach_node( const std::shared_ptr<repository_node>& node ) const {
|
||||
return repo_.detach(node);
|
||||
}
|
||||
|
||||
bool shadow_repository::expecting_relation_node(const std::string &name) const {
|
||||
return repo_.expecting_relation_node(name);
|
||||
}
|
||||
|
||||
void shadow_repository::expect_relation_node(const std::string &name, const std::type_index &ti) const {
|
||||
repo_.expect_relation_node(name, ti);
|
||||
}
|
||||
|
||||
void shadow_repository::remove_expected_relation_node(const std::string &name) const {
|
||||
repo_.remove_expected_relation_node(name);
|
||||
}
|
||||
|
||||
std::shared_ptr<object> shadow_repository::provide_object_in_advance(const std::type_index& ti, const std::shared_ptr<object>& obj) const {
|
||||
return repo_.provide_object_in_advance(ti, obj);
|
||||
}
|
||||
|
||||
bool shadow_repository::has_object_for_type(const std::type_index& ti) const {
|
||||
return repo_.has_object_for_type(ti);
|
||||
}
|
||||
|
||||
std::shared_ptr<object> shadow_repository::object_for_type(const std::type_index& ti) const {
|
||||
return repo_.object_for_type(ti);
|
||||
}
|
||||
|
||||
void shadow_repository::remove_object_for_type(const std::type_index& ti) const {
|
||||
repo_.remove_object_for_type(ti);
|
||||
}
|
||||
|
||||
bool shadow_repository::is_node_announced(const std::type_index &ti) const {
|
||||
return repo_.is_node_announced(ti);
|
||||
}
|
||||
|
||||
void shadow_repository::push_announce_node(const std::type_index &ti, const node_ptr &node) const {
|
||||
repo_.push_announce_node(ti, node);
|
||||
}
|
||||
|
||||
shadow_repository::node_ptr shadow_repository::announce_node(const std::type_index &ti) const {
|
||||
return repo_.announce_node(ti);
|
||||
}
|
||||
|
||||
shadow_repository::node_ptr shadow_repository::pop_announce_node(const std::type_index &ti) const {
|
||||
return repo_.pop_announce_node(ti);
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,11 @@
|
||||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
object_generator::object_generator(const repository& repo, const std::shared_ptr<object>& object)
|
||||
object_generator::object_generator(basic_repository& repo, const std::shared_ptr<object>& object)
|
||||
: repo_(repo)
|
||||
, object_(object) {}
|
||||
|
||||
std::shared_ptr<object> object_generator::acquire_object(repository &repo, const std::type_index &ti, const std::string& name) {
|
||||
std::shared_ptr<object> object_generator::acquire_object(basic_repository &repo, const std::type_index &ti, const std::string& name) {
|
||||
if (repo.has_object_for_type(ti)) {
|
||||
auto obj = repo.object_for_type(ti);
|
||||
repo.remove_object_for_type(ti);
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
relation_endpoint::relation_endpoint(std::string field_name, const relation_type type, const std::shared_ptr<repository_node> &node)
|
||||
relation_endpoint::relation_endpoint(std::string field_name, const relation_type type, repository_node &node)
|
||||
: field_name_(std::move(field_name))
|
||||
, type_(type)
|
||||
, node_(node) {
|
||||
, node_(&node) {
|
||||
}
|
||||
|
||||
std::string relation_endpoint::field_name() const {
|
||||
@@ -25,10 +25,6 @@ const repository_node &relation_endpoint::node() const {
|
||||
return *node_;
|
||||
}
|
||||
|
||||
std::shared_ptr<repository_node> relation_endpoint::node_ptr() const {
|
||||
return node_;
|
||||
}
|
||||
|
||||
bool relation_endpoint::is_has_one() const {
|
||||
return type_ == relation_type::HasOne;
|
||||
}
|
||||
@@ -42,7 +38,7 @@ bool relation_endpoint::is_belongs_to() const {
|
||||
}
|
||||
|
||||
std::shared_ptr<relation_endpoint> relation_endpoint::foreign_endpoint() const {
|
||||
return foreign_endpoint_;
|
||||
return foreign_endpoint_.lock();
|
||||
}
|
||||
|
||||
void relation_endpoint::link_foreign_endpoint( const std::shared_ptr<relation_endpoint>& endpoint ) {
|
||||
|
||||
@@ -1,255 +0,0 @@
|
||||
#include <utility>
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
utils::error make_error(const error_code ec, const std::string &msg) {
|
||||
return utils::error(ec, msg);
|
||||
}
|
||||
|
||||
repository::repository(std::string name)
|
||||
: name_(std::move(name))
|
||||
, root_(repository_node::make_null_node(*this))
|
||||
, log_(logger::create_logger("schema")) {
|
||||
root_->first_child_ = std::shared_ptr<repository_node>(new repository_node(*this));
|
||||
root_->last_child_ = std::shared_ptr<repository_node>(new repository_node(*this));
|
||||
root_->first_child_->next_sibling_ = root_->last_child_;
|
||||
root_->last_child_->previous_sibling_ = root_->first_child_;
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> repository::detach(const node_ptr &node) {
|
||||
log_.debug("detach node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
||||
|
||||
remove_node(node);
|
||||
|
||||
node->on_detach();
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
repository::const_iterator repository::begin() const {
|
||||
return const_iterator(root_->first_child_->next_sibling_);
|
||||
}
|
||||
|
||||
repository::const_iterator repository::end() const {
|
||||
return const_iterator(root_->last_child_);
|
||||
}
|
||||
|
||||
bool repository::empty() const {
|
||||
return root_->first_child_ == root_->last_child_->previous_sibling_;
|
||||
}
|
||||
|
||||
size_t repository::size() const {
|
||||
return static_cast<size_t>(std::distance(begin(), end()));
|
||||
}
|
||||
|
||||
std::string repository::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
bool repository::contains( const std::string& name ) const {
|
||||
return nodes_by_name_.count(name) > 0;
|
||||
}
|
||||
|
||||
bool repository::contains( const std::type_index& index ) const {
|
||||
return nodes_by_type_.count(index) > 0;
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> repository::basic_info(const std::type_index &ti) const {
|
||||
auto result = find_node(ti);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> repository::basic_info(const std::string& name) const {
|
||||
auto result = find_node(name);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
utils::result<attribute*, utils::error> repository::primary_key_attribute(const std::type_index &type_index) const {
|
||||
const auto result = find_node(type_index);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
if (!result.value()->info().has_primary_key()) {
|
||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Object '" + result.value()->name() + "' does not have a primary key."));
|
||||
}
|
||||
return utils::ok((*result)->info().primary_key_attribute());
|
||||
}
|
||||
|
||||
void repository::dump(std::ostream &os) const {
|
||||
for (const auto &node : *this) {
|
||||
dump(os, node);
|
||||
}
|
||||
os << "\n";
|
||||
}
|
||||
|
||||
void repository::dump( std::ostream& os, const node_ptr& node ) {
|
||||
os << "node [" << node->name() << "] (" << node->type_index().name() << ")\n";
|
||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
os << " " << node->name() << "::" << it->second->field_name() << " (" << it->second->type_name() << ")";
|
||||
if (it->second->foreign_endpoint()) {
|
||||
os << " <---> " << it->second->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
||||
} else {
|
||||
os << " -> " << it->second->node().name() << " (type: " << it->second->node().type_index().name() << ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils::result<repository::node_ptr, utils::error> repository::attach_node(const std::shared_ptr<repository_node> &node, const std::string &parent) {
|
||||
if (has_node(node)) {
|
||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + node->name() + "' already exists."));
|
||||
}
|
||||
|
||||
log_.info("attach: insert node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
||||
|
||||
// set node to root node
|
||||
auto parent_node = root_;
|
||||
if (!parent.empty()) {
|
||||
auto result = find_node(parent);
|
||||
if (!result.is_ok() && result.err().ec() != error_code::NodeNotFound) {
|
||||
return result;
|
||||
}
|
||||
parent_node = *result;
|
||||
}
|
||||
|
||||
insert_node(parent_node, node);
|
||||
|
||||
// Todo: check return value
|
||||
nodes_by_name_.insert({node->name(), node});
|
||||
nodes_by_type_.insert({node->type_index(), node});
|
||||
|
||||
node->on_attach();
|
||||
|
||||
return utils::ok(node);
|
||||
}
|
||||
|
||||
utils::result<repository::node_ptr, utils::error> repository::find_node(const std::string &name) const {
|
||||
// first search in the prototype map
|
||||
const auto i = nodes_by_name_.find(name);
|
||||
if (i == nodes_by_name_.end()) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound, "Couldn't find node by name '" + name + "'"));
|
||||
}
|
||||
return utils::ok(i->second);
|
||||
}
|
||||
|
||||
utils::result<repository::node_ptr, utils::error> repository::find_node(const std::type_index &type_index) const {
|
||||
const auto i = nodes_by_type_.find(type_index);
|
||||
if (i == nodes_by_type_.end()) {
|
||||
return utils::failure(make_error(error_code::NodeNotFound,
|
||||
"Couldn't find node by type '" + std::string(type_index.name()) + "'"));
|
||||
}
|
||||
return utils::ok(i->second);
|
||||
}
|
||||
|
||||
void repository::insert_node(const node_ptr &parent, const node_ptr &child) {
|
||||
child->parent_ = parent;
|
||||
child->previous_sibling_ = parent->last_child_->previous_sibling_;
|
||||
child->next_sibling_ = parent->last_child_;
|
||||
/*
|
||||
* +-----------------------------<- (first) parent (last) -> ----------------------------+
|
||||
* | |
|
||||
* first (next) -> <- (prev) child_1 (next) -> <- (prev) new_child (next) -> <- (prev) last
|
||||
* ^^^^^^^ inserted ^^^^^^
|
||||
*/
|
||||
parent->last_child_->previous_sibling_->next_sibling_ = child;
|
||||
parent->last_child_->previous_sibling_ = child;
|
||||
// set depth
|
||||
// child->depth = depth + 1;
|
||||
}
|
||||
|
||||
void repository::remove_node(const node_ptr &node) {
|
||||
auto next = node->next();
|
||||
|
||||
std::stack<node_ptr> nodes_to_remove;
|
||||
nodes_to_remove.push(node);
|
||||
|
||||
while (!nodes_to_remove.empty()) {
|
||||
const auto current = nodes_to_remove.top();
|
||||
|
||||
if (current->has_children()) {
|
||||
// Push all children to the stack (from right to left to maintain order)
|
||||
auto child = current->last_child_->previous_sibling_;
|
||||
while (child != current->first_child_) {
|
||||
nodes_to_remove.push(child);
|
||||
child = child->previous_sibling_;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// No children left, safe to remove this node
|
||||
nodes_to_remove.pop();
|
||||
current->unlink();
|
||||
nodes_by_name_.erase(current->name());
|
||||
nodes_by_type_.erase(current->type_index());
|
||||
}
|
||||
}
|
||||
|
||||
bool repository::has_node(const std::string &name) const {
|
||||
return nodes_by_name_.count(name) > 0;
|
||||
}
|
||||
|
||||
bool repository::has_node(const std::type_index &index) const {
|
||||
return nodes_by_type_.count(index) > 0;
|
||||
}
|
||||
|
||||
bool repository::has_node(const node_ptr& node) const {
|
||||
return nodes_by_name_.count(node->name()) > 0 || nodes_by_type_.count(node->type_index()) > 0;
|
||||
}
|
||||
|
||||
bool repository::expecting_relation_node( const std::string& name ) const {
|
||||
return expected_relation_nodes_.count(name) > 0;
|
||||
}
|
||||
|
||||
void repository::expect_relation_node(const std::string &name, const std::type_index &ti) {
|
||||
expected_relation_nodes_.insert({name, ti});
|
||||
}
|
||||
|
||||
void repository::remove_expected_relation_node( const std::string& name ) {
|
||||
expected_relation_nodes_.erase(name);
|
||||
}
|
||||
|
||||
std::shared_ptr<object> repository::provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj) {
|
||||
return object_by_type_.insert({ti, obj}).first->second;
|
||||
}
|
||||
|
||||
bool repository::has_object_for_type(const std::type_index &ti) const {
|
||||
return object_by_type_.count(ti) > 0;
|
||||
}
|
||||
|
||||
std::shared_ptr<object> repository::object_for_type(const std::type_index &ti) const {
|
||||
return object_by_type_.at(ti);
|
||||
}
|
||||
|
||||
void repository::remove_object_for_type(const std::type_index &ti) {
|
||||
object_by_type_.erase(ti);
|
||||
}
|
||||
|
||||
bool repository::is_node_announced(const std::type_index &ti) const {
|
||||
return announced_node_.count(ti) > 0;
|
||||
}
|
||||
|
||||
void repository::push_announce_node(const std::type_index &ti, const node_ptr &node) {
|
||||
announced_node_.insert({ti, node});
|
||||
}
|
||||
|
||||
repository::node_ptr repository::announce_node(const std::type_index &ti) {
|
||||
return announced_node_.find(ti)->second;
|
||||
}
|
||||
|
||||
repository::node_ptr repository::pop_announce_node(const std::type_index &ti) {
|
||||
const auto it = announced_node_.find(ti);
|
||||
if (it != announced_node_.end()) {
|
||||
announced_node_.erase(it);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
} // namespace matador::object
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
#include "matador/object/basic_repository.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
repository_node::repository_node(repository &repo)
|
||||
repository_node::repository_node(basic_repository &repo)
|
||||
: repo_(repo)
|
||||
, type_index_(typeid(detail::null_type)){
|
||||
}
|
||||
|
||||
repository_node::repository_node(repository &repo, const std::type_index& ti)
|
||||
repository_node::repository_node(basic_repository &repo, const std::type_index& ti)
|
||||
: repo_(repo)
|
||||
, type_index_(ti) {
|
||||
}
|
||||
|
||||
repository_node::repository_node(repository &repo, std::string name, const std::type_index& ti)
|
||||
repository_node::repository_node(basic_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)))
|
||||
, first_child_(std::make_unique<repository_node>(repo))
|
||||
, last_child_(std::make_unique<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(repository &repo) {
|
||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo));
|
||||
node->info_ = std::make_unique<null_info>(node);
|
||||
|
||||
return node;
|
||||
first_child_->next_sibling_ = last_child_.get();
|
||||
last_child_->previous_sibling_ = first_child_.get();
|
||||
}
|
||||
|
||||
std::string repository_node::name() const {
|
||||
@@ -48,32 +42,35 @@ void repository_node::update_name(const std::string& name) {
|
||||
info_->update_name(name);
|
||||
}
|
||||
|
||||
const repository& repository_node::schema() const {
|
||||
const basic_repository& repository_node::schema() const {
|
||||
return repo_;
|
||||
}
|
||||
|
||||
bool repository_node::has_children() const {
|
||||
return first_child_->next_sibling_ != last_child_;
|
||||
return first_child_->next_sibling_ != last_child_.get();
|
||||
}
|
||||
void repository_node::on_attach() const {
|
||||
info_->on_attach();
|
||||
}
|
||||
|
||||
void repository_node::on_detach() const {}
|
||||
void repository_node::on_detach() const {
|
||||
info_->on_detach();
|
||||
}
|
||||
|
||||
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_) {
|
||||
if (first_child_ && first_child_->next_sibling_ != last_child_.get()) {
|
||||
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();
|
||||
while (node->parent_ && node->next_sibling_ == node->parent_->last_child_.get()) {
|
||||
node = node->parent_;
|
||||
}
|
||||
return node->parent_ ? node->next_sibling_ : node->last_child_;
|
||||
return node->parent_ ? node->next_sibling_ : node->last_child_.get();
|
||||
}
|
||||
|
||||
repository_node::node_ptr repository_node::prev() const {
|
||||
@@ -83,7 +80,7 @@ repository_node::node_ptr repository_node::prev() const {
|
||||
// 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_) {
|
||||
while (node->last_child_ && node->first_child_->next_sibling_ != node->last_child_.get()) {
|
||||
node = node->last_child_->previous_sibling_;
|
||||
}
|
||||
return node;
|
||||
@@ -96,7 +93,7 @@ repository_node::node_ptr repository_node::prev() const {
|
||||
void repository_node::unlink() {
|
||||
previous_sibling_->next_sibling_ = next_sibling_;
|
||||
next_sibling_->previous_sibling_ = previous_sibling_;
|
||||
next_sibling_.reset();
|
||||
previous_sibling_.reset();
|
||||
next_sibling_ = nullptr;
|
||||
previous_sibling_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,78 +1,66 @@
|
||||
#include <utility>
|
||||
|
||||
#include "matador/object/repository_node_iterator.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
const_repository_node_iterator::const_repository_node_iterator(value_type* node)
|
||||
: node_(node) {
|
||||
}
|
||||
|
||||
const_repository_node_iterator::const_repository_node_iterator(const value_type& node)
|
||||
: node_(node)
|
||||
{}
|
||||
|
||||
bool const_repository_node_iterator::operator==(const const_repository_node_iterator &i) const
|
||||
{
|
||||
bool const_repository_node_iterator::operator==(const const_repository_node_iterator &i) const {
|
||||
return (node_ == i.node_);
|
||||
}
|
||||
|
||||
bool const_repository_node_iterator::operator!=(const const_repository_node_iterator &i) const
|
||||
{
|
||||
bool const_repository_node_iterator::operator!=(const const_repository_node_iterator &i) const {
|
||||
return !operator==(i);
|
||||
}
|
||||
|
||||
const_repository_node_iterator& const_repository_node_iterator::operator++()
|
||||
{
|
||||
const_repository_node_iterator &const_repository_node_iterator::operator++() {
|
||||
increment();
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_repository_node_iterator const_repository_node_iterator::operator++( int ) {
|
||||
const value_type tmp = node_;
|
||||
const_repository_node_iterator const_repository_node_iterator::operator++(int) {
|
||||
value_type* tmp = node_;
|
||||
increment();
|
||||
return const_repository_node_iterator(tmp);
|
||||
}
|
||||
|
||||
const_repository_node_iterator& const_repository_node_iterator::operator--()
|
||||
{
|
||||
const_repository_node_iterator &const_repository_node_iterator::operator--() {
|
||||
decrement();
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_repository_node_iterator const_repository_node_iterator::operator--(int)
|
||||
{
|
||||
const value_type tmp = node_;
|
||||
const_repository_node_iterator const_repository_node_iterator::operator--(int) {
|
||||
value_type* tmp = node_;
|
||||
decrement();
|
||||
return const_repository_node_iterator(tmp);
|
||||
}
|
||||
|
||||
const_repository_node_iterator::pointer const_repository_node_iterator::operator->() const
|
||||
{
|
||||
return node_.get();
|
||||
}
|
||||
|
||||
const_repository_node_iterator::reference const_repository_node_iterator::operator*() const
|
||||
{
|
||||
const_repository_node_iterator::pointer const_repository_node_iterator::operator->() const {
|
||||
return node_;
|
||||
}
|
||||
|
||||
const_repository_node_iterator::pointer const_repository_node_iterator::get() const
|
||||
{
|
||||
return node_.get();
|
||||
const_repository_node_iterator::reference const_repository_node_iterator::operator*() const {
|
||||
return *node_;
|
||||
}
|
||||
|
||||
void const_repository_node_iterator::increment()
|
||||
{
|
||||
const_repository_node_iterator::pointer const_repository_node_iterator::get() const {
|
||||
return node_;
|
||||
}
|
||||
|
||||
void const_repository_node_iterator::increment() {
|
||||
if (!node_) {
|
||||
return;
|
||||
}
|
||||
|
||||
node_ = node_->next();
|
||||
}
|
||||
void const_repository_node_iterator::decrement()
|
||||
{
|
||||
|
||||
void const_repository_node_iterator::decrement() {
|
||||
if (!node_) {
|
||||
return;
|
||||
}
|
||||
|
||||
node_ = node_->prev();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ std::string restriction::column_name() const {
|
||||
return attr_.name();
|
||||
}
|
||||
std::shared_ptr<object> restriction::owner() const {
|
||||
return owner_;
|
||||
return owner_.lock();
|
||||
}
|
||||
|
||||
bool restriction::is_primary_key_constraint() const {
|
||||
@@ -30,12 +30,14 @@ bool restriction::is_unique_constraint() const {
|
||||
return utils::is_constraint_set(options_, utils::constraints::Unique);
|
||||
}
|
||||
|
||||
const std::string& restriction::ref_table_name() const {
|
||||
return reference_->name();
|
||||
std::string restriction::ref_table_name() const {
|
||||
const auto ref = reference_.lock();
|
||||
return ref ? ref->name() : "";
|
||||
}
|
||||
|
||||
const std::string& restriction::ref_column_name() const {
|
||||
return reference_->primary_key_attribute()->name();
|
||||
std::string restriction::ref_column_name() const {
|
||||
const auto ref = reference_.lock();
|
||||
return ref ? ref->primary_key_attribute()->name() : "";
|
||||
}
|
||||
|
||||
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
||||
@@ -58,43 +60,4 @@ std::string restriction::type_string() const {
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
// constraint_builder & constraint_builder::constraint(std::string name) {
|
||||
// constraint_name = std::move(name);
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder & constraint_builder::primary_key(std::string name) {
|
||||
// column_name = std::move(name);
|
||||
// options_ |= utils::constraints::PrimaryKey;
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder & constraint_builder::foreign_key(std::string name) {
|
||||
// column_name = std::move(name);
|
||||
// options_ |= utils::constraints::ForeignKey;
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder & constraint_builder::references(std::string table, std::string column) {
|
||||
// ref_table_name = std::move(table);
|
||||
// ref_column_name = std::move(column);
|
||||
//
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder::operator class restriction() const {
|
||||
// class restriction c;
|
||||
// c.name_ = constraint_name;
|
||||
// c.attr_ = column_name;
|
||||
// c.options_ = options_;
|
||||
// c.ref_column_name_ = ref_column_name;
|
||||
// c.ref_table_name_ = ref_table_name;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// constraint_builder constraint(std::string name) {
|
||||
// constraint_builder builder;
|
||||
// return builder.constraint(std::move(name));
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user