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
+20 -163
View File
@@ -1,42 +1,23 @@
#ifndef SCHEMA_HPP
#define SCHEMA_HPP
#ifndef REPOSITORY_HPP
#define REPOSITORY_HPP
#include "matador/logger/log_manager.hpp"
#include "matador/object/basic_repository.hpp"
#include "matador/object/error_code.hpp"
#include "matador/object/foreign_node_completer.hpp"
#include "matador/object/object_info.hpp"
#include "matador/object/observer.hpp"
#include "matador/object/relation_completer.hpp"
#include "matador/object/repository_node.hpp"
#include "matador/object/repository_node_iterator.hpp"
#include "matador/utils/result.hpp"
#include "matador/utils/error.hpp"
#include "matador/logger/logger.hpp"
#include <memory>
#include <string>
#include <unordered_set>
namespace matador::object {
namespace internal {
class shadow_repository;
}
utils::error make_error(error_code ec, const std::string &msg);
class repository {
class repository : public basic_repository {
public:
typedef const_repository_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
using node_ptr = std::shared_ptr<repository_node>;
/**
* Creates an empty schema
*/
explicit repository(std::string name = "");
using basic_repository::basic_repository;
template<typename Type, template<typename> typename... Observers>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers<Type>&&... observers) {
@@ -46,12 +27,12 @@ public:
template<typename Type, typename SuperType, template<typename> typename... Observers>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers<Type>&&... observers) {
const auto ti = std::type_index(typeid(SuperType));
auto result = find_node(ti);
if (!result) {
const auto it = find_node(ti);
if (it == end()) {
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(ti.name()) + "' not found"));
}
return attach_type<Type, Observers...>(name, (*result)->name(), std::forward<Observers<Type>>(observers)...);
return attach_type<Type, Observers...>(name, it->name(), std::forward<Observers<Type>>(observers)...);
}
template<typename Type, template<typename> typename... Observers>
@@ -65,20 +46,22 @@ public:
obs.reserve(sizeof...(Observers));
(obs.push_back(std::unique_ptr<observer<Type>>(new Observers<Type>(std::forward<Observers<Type>>(observers)))), ...);
// if the type was not found
std::shared_ptr<repository_node> node;
std::unique_ptr<repository_node> node;
if (is_node_announced(typeid(Type))) {
node = pop_announce_node(typeid(Type));
node->update_name(name);
} else {
node = repository_node::make_node<Type>(*this, name, []{ return std::make_unique<Type>(); }, std::move(obs));
}
if (auto result = attach_node(node, parent); !result) {
auto result = attach_node(node.release(), parent);
if (!result) {
return utils::failure(result.err());
}
const auto info = node->template info<Type>();
foreign_node_completer<Type, Observers...>::complete(node, info.get().observers());
relation_completer<Type, Observers...>::complete(node, info.get().observers());
repository_node* attached_node = result.value();
const auto info = attached_node->template info<Type>();
foreign_node_completer<Type, Observers...>::complete(attached_node, info.get().observers());
relation_completer<Type, Observers...>::complete(attached_node, info.get().observers());
} else if (!has_node(name)) {
it->second->update_name(name);
nodes_by_name_[name] = it->second;
@@ -95,142 +78,16 @@ public:
return utils::ok<void>();
}
/**
* Detaches a given node from the schema. If the
* node is a parent of other nodes, these nodes are
* detached as well.
*
* @param node Node to detach from schema
* @return Result object indicating success or failure
*/
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr &node);
/**
* Return the first schema node.
*
* @return The first schema node iterator.
*/
[[nodiscard]] const_iterator begin() const;
/**
* Return the last schema node.
*
* @return The last schema node iterator.
*/
[[nodiscard]] const_iterator end() const;
/**
* Returns true if the schema contains
* no schema nodes.
*
* @return True if the schema is empty
*/
[[nodiscard]] bool empty() const;
/**
* Returns the current number of the schema node.
*
* @return Number of schema nodes
*/
[[nodiscard]] size_t size() const;
/**
* Returns the name of the schema.
*
* @return The name of the schema
*/
[[nodiscard]] std::string name() const;
[[nodiscard]] bool contains(const std::string &name) const;
[[nodiscard]] bool contains(const std::type_index &index) const;
template < typename Type >
[[nodiscard]] bool contains() const {
return contains(std::type_index(typeid(Type)));
}
template<typename Type>
[[nodiscard]] utils::result<object_info_ref<Type>, utils::error> info() const {
auto result = find_node(std::type_index(typeid(Type)));
if (!result) {
return utils::failure(result.err());
const auto it = find_node(std::type_index(typeid(Type)));
if (it == end()) {
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(typeid(Type).name()) + "' not found"));
}
return utils::ok(result.value()->info<Type>());
return utils::ok(it->info<Type>());
}
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::type_index& ti) const;
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::string &name) const;
template<typename Type>
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info() const {
auto result = find_node(std::type_index(typeid(Type)));
if (!result) {
return utils::failure(result.err());
}
return utils::ok(basic_object_info_ref{result.value()->info()});
}
[[nodiscard]] utils::result<attribute*, utils::error> primary_key_attribute(const std::type_index &type_index) const;
void dump(std::ostream &os) const;
static void dump(std::ostream &os, const node_ptr& node);
private:
using t_node_map = std::unordered_map<std::string, node_ptr>;
using t_type_index_node_map = std::unordered_map<std::type_index, node_ptr>;
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const node_ptr &node, const std::string &parent);
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
template<typename Type>
[[nodiscard]] utils::result<node_ptr, utils::error> find_node() const {
return find_node(std::type_index(typeid(Type)));
}
[[nodiscard]] bool has_node(const std::string &name) const;
[[nodiscard]] bool has_node(const std::type_index &index) const;
[[nodiscard]] bool has_node(const node_ptr& node) const;
static void insert_node(const node_ptr &parent, const node_ptr &child);
void remove_node(const node_ptr &node);
[[nodiscard]] bool expecting_relation_node(const std::string &name) const;
void expect_relation_node(const std::string &name, const std::type_index &ti);
void remove_expected_relation_node(const std::string &name);
[[nodiscard]] std::shared_ptr<object> provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj);
[[nodiscard]] bool has_object_for_type(const std::type_index &ti) const;
[[nodiscard]] std::shared_ptr<object> object_for_type(const std::type_index &ti) const;
void remove_object_for_type(const std::type_index &ti);
[[nodiscard]] bool is_node_announced(const std::type_index &ti) const;
void push_announce_node(const std::type_index &ti, const node_ptr &node);
[[nodiscard]] node_ptr announce_node(const std::type_index &ti);
[[nodiscard]] node_ptr pop_announce_node(const std::type_index &ti);
private:
friend class internal::shadow_repository;
friend class repository_node;
template < typename NodeType, template<typename> typename ...Observers >
friend class foreign_node_completer;
friend class object_generator;
std::string name_;
std::shared_ptr<repository_node> root_;
t_node_map nodes_by_name_;
t_type_index_node_map nodes_by_type_;
logger::logger log_;
std::unordered_map<std::type_index, std::shared_ptr<repository_node>> announced_node_;
std::unordered_map<std::type_index, attribute*> missing_references_;
std::unordered_map<std::string, std::type_index> expected_relation_nodes_;
std::unordered_map<std::type_index, std::shared_ptr<object>> object_by_type_;
};
}
#endif //SCHEMA_HPP
#endif //REPOSITORY_HPP