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:
2026-01-06 15:43:20 +01:00
parent f07a360da2
commit 0c6b5a0a93
60 changed files with 1132 additions and 1225 deletions
+55 -58
View File
@@ -1,7 +1,7 @@
#ifndef RELATION_COMPLETER_HPP
#define RELATION_COMPLETER_HPP
#include "matador/object/internal/shadow_repository.hpp"
#include "matador/object/basic_repository.hpp"
#include "matador/object/foreign_node_completer.hpp"
#include "matador/object/many_to_many_relation.hpp"
#include "matador/object/join_columns_collector.hpp"
@@ -94,15 +94,11 @@ private:
*/
template<typename Type, template<typename> typename... Observers>
class relation_completer final {
private:
using node_ptr = std::shared_ptr<repository_node>;
public:
using endpoint_ptr = std::shared_ptr<relation_endpoint>;
static void complete(const std::shared_ptr<repository_node> &node, const std::vector<std::unique_ptr<observer<Type>>> &observers) {
internal::shadow_repository shadow(node->repo_);
relation_completer completer(shadow, observers);
static void complete(repository_node *node, const std::vector<std::unique_ptr<observer<Type>>> &observers) {
relation_completer completer(node->repo_, observers);
completer.complete_node_relations(node);
}
@@ -131,12 +127,12 @@ public:
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
private:
explicit relation_completer(internal::shadow_repository &shadow, const std::vector<std::unique_ptr<observer<Type>>>& observers)
: schema_(shadow)
explicit relation_completer(basic_repository &repo, const std::vector<std::unique_ptr<observer<Type>>>& observers)
: repo_(repo)
, log_(logger::create_logger("relation_completer"))
, observers_(observers) {}
void complete_node_relations(const std::shared_ptr<repository_node> &node) {
void complete_node_relations(repository_node* node) {
nodes_.push(node);
Type obj;
@@ -152,11 +148,11 @@ private:
static void link_relation_endpoints(const endpoint_ptr &endpoint,
const endpoint_ptr &other_endpoint);
node_ptr find_node(const std::type_index &ti) const;
[[nodiscard]] repository_node* find_node(const std::type_index &ti) const;
private:
std::stack<node_ptr> nodes_;
internal::shadow_repository &schema_;
std::stack<repository_node*> nodes_;
basic_repository &repo_;
logger::logger log_;
join_columns_collector join_columns_collector_{};
const std::vector<std::unique_ptr<observer<Type>>>& observers_;
@@ -183,13 +179,13 @@ void relation_completer<Type, Observers...>::on_has_many(const char *id, Collect
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
// corresponding belongs_to is available and was called (has_many <-> belongs_to)
// complete the relation
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, foreign_node);
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *foreign_node);
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
link_relation_endpoints(local_endpoint, it->second);
} else if (join_column_finder::has_join_column<value_type>(join_column)) {
// corresponding belongs_to is available but was not called (has_many <-> belongs_to)
// prepare the relation
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, foreign_node);
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *foreign_node);
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
} else {
// A relation table is necessary
@@ -198,21 +194,23 @@ void relation_completer<Type, Observers...>::on_has_many(const char *id, Collect
// belongs-to relation handles this relation, the many-to-many
// relation is maybe detached.
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
auto node = repository_node::make_node<relation_value_type>(repo_, id, [join_column] {
return std::make_unique<relation_value_type>("id", join_column);
}, {});
const auto result = schema_.attach_node(node);
const auto result = repo_.attach_node(node.release(), "");
if (!result) {
// Todo: throw internal error
return;
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>("id", relation_type::BelongsTo, nodes_.top());
auto *attached_node = result.value();
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *attached_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>("id", relation_type::BelongsTo, *nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
attached_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
const auto foreign_value_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, foreign_node);
const auto foreign_value_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, *foreign_node);
node->info_->register_relation_endpoint(typeid(value_type), foreign_value_endpoint);
}
}
@@ -226,20 +224,21 @@ void relation_completer<Type, Observers...>::on_has_many(const char *id, Collect
using value_type = typename CollectionType::value_type;
using relation_value_type = many_to_relation<Type, value_type>;
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
auto node = repository_node::make_node<relation_value_type>(repo_, id, [join_column] {
return std::make_unique<relation_value_type>(join_column, "value");
}, {});
const auto result = schema_.attach_node(node);
const auto result = repo_.attach_node(node.release(), "");
if (!result) {
// Todo: throw internal exception
return;
}
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
auto *attached_node = result.value();
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *attached_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, *nodes_.top());
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
attached_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
}
@@ -250,11 +249,11 @@ void relation_completer<Type, Observers...>::on_has_many_to_many(const char *id,
const char *join_column,
const char *inverse_join_column,
const utils::foreign_attributes &/*attr*/) {
if (!schema_.expecting_relation_node(id)) {
schema_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
if (!repo_.expecting_relation_node(id)) {
repo_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
} else {
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
schema_.remove_expected_relation_node(id);
repo_.remove_expected_relation_node(id);
}
}
@@ -278,7 +277,7 @@ void relation_completer<Type, Observers...>::on_has_one(const char *id,
ForeignPointerType &/*obj*/,
const utils::foreign_attributes &/*attr*/) {
using value_type = typename ForeignPointerType::value_type;
auto foreign_node = find_node(typeid(value_type));
const auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
return;
@@ -286,7 +285,7 @@ void relation_completer<Type, Observers...>::on_has_one(const char *id,
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
if (local_it == nodes_.top()->info().endpoint_end()) {
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasOne, foreign_node);
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasOne, *foreign_node);
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
}
if (const auto foreign_it = foreign_node->info().find_relation_endpoint(typeid(Type)); foreign_it != foreign_node->info().endpoint_end()) {
@@ -302,7 +301,7 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
const utils::foreign_attributes & /*attr*/) {
using value_type = typename ForeignPointerType::value_type;
const auto ti = std::type_index(typeid(value_type));
auto foreign_node = find_node(typeid(value_type));
const auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
return;
@@ -313,11 +312,11 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
if (const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index()); it != foreign_node->info().endpoint_end()) {
// Found corresponding relation endpoint in the foreign node
if (it->second->is_has_one()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, foreign_node);
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, *foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->is_has_many()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, foreign_node);
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, *foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
@@ -326,16 +325,15 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
// "belongs_to"-relation the "many_to_many_relation" can be removed
// (detach), and the endpoints must be adjusted
const auto foreign_endpoint = it->second->foreign_endpoint();
const auto detach_result = schema_.detach_node(foreign_endpoint->node_);
const auto detach_result = repo_.detach(foreign_endpoint->node_);
foreign_endpoint->node_ = foreign_node;
// foreign_endpoint->node_ = nodes_.top();
nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
} else {
// check type
}
} else {
// Relation node was not found, create only endpoint.
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, foreign_node);
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, *foreign_node);
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
}
}
@@ -343,18 +341,17 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
template<typename Type, template<typename> typename... Observers>
template<typename NodeType>
void relation_completer<Type, Observers...>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
using relation_value_type = many_to_many_relation<NodeType, Type>;
using relation_value_type = many_to_many_relation<Type, NodeType>;
using value_type = NodeType;
// Check if the object_ptr type is already inserted in the schema (by id)
auto foreign_node = find_node(typeid(value_type));
const auto foreign_node = find_node(typeid(value_type));
if (!foreign_node) {
// Todo: throw internal error or attach node
return;
}
auto result = schema_.find_node(name);
if (result) {
if (const auto it = repo_.find_node(name); it != repo_.end()) {
return;
}
// Relation does not exist. Create it.
@@ -364,26 +361,27 @@ void relation_completer<Type, Observers...>::attach_relation_node(const std::str
auto observers = internal::observer_list_copy_creator<Type, relation_value_type, Observers...>::copy_create(observers_);
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), name, std::move(creator), std::move(observers));
result = schema_.attach_node(node);
auto node = repository_node::make_node<relation_value_type>(repo_, name, std::move(creator), std::move(observers));
auto result = repo_.attach_node(node.release(), "");
if (!result) {
// Todo: throw internal error
return;
}
auto *attached_node = result.value();
foreign_node_completer<relation_value_type>::complete(result.value(), {});
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, foreign_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, *attached_node);
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, *nodes_.top());
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, *foreign_node);
const auto foreign_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, *attached_node);
// register relation endpoint in local node
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
// register relation endpoint in foreign node
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
// register endpoints in relation node
node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
node->info_->register_relation_endpoint(typeid(value_type), inverse_join_endpoint);
attached_node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
attached_node->info_->register_relation_endpoint(typeid(value_type), inverse_join_endpoint);
// link endpoints
link_relation_endpoints(local_endpoint, join_endpoint);
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
@@ -392,8 +390,8 @@ void relation_completer<Type, Observers...>::attach_relation_node(const std::str
template<typename Type, template<typename> typename... Observers>
void relation_completer<Type, Observers...>::register_relation_endpoints(const endpoint_ptr &endpoint,
const endpoint_ptr &other_endpoint) {
endpoint->node_->info_->register_relation_endpoint(other_endpoint->node_->type_index(), endpoint);
other_endpoint->node_->info_->register_relation_endpoint(endpoint->node_->type_index(), other_endpoint);
endpoint->node().info_->register_relation_endpoint(other_endpoint->node().type_index(), endpoint);
other_endpoint->node().info_->register_relation_endpoint(endpoint->node().type_index(), other_endpoint);
link_relation_endpoints(endpoint, other_endpoint);
}
@@ -404,16 +402,15 @@ void relation_completer<Type, Observers...>::link_relation_endpoints(const endpo
}
template<typename Type, template <typename> class ... Observers>
typename relation_completer<Type, Observers...>::node_ptr relation_completer<Type, Observers...>::find_node(const std::type_index &ti) const {
node_ptr foreign_node;
if (auto result = schema_.find_node(ti); result) {
return result.value();
repository_node* relation_completer<Type, Observers...>::find_node(const std::type_index &ti) const {
if (const auto it = repo_.find_node(ti); it != repo_.end()) {
return it.get();
}
if (!schema_.is_node_announced(ti)) {
return {};
if (!repo_.is_node_announced(ti)) {
return nullptr;
}
return schema_.announce_node(ti);
return repo_.announce_node(ti);
}
}
#endif //RELATION_COMPLETER_HPP