query/include/matador/object/schema.hpp

334 lines
12 KiB
C++

#ifndef SCHEMA_HPP
#define SCHEMA_HPP
#include "matador/object/many_to_many_relation.hpp"
#include "matador/object/primary_key_resolver.hpp"
#include "matador/object/error_code.hpp"
#include "matador/object/schema_node.hpp"
#include "matador/object/schema_node_iterator.hpp"
#include "matador/utils/result.hpp"
#include "matador/utils/error.hpp"
#include <memory>
#include <stack>
#include <string>
#include <unordered_set>
namespace matador::object {
utils::error make_error(error_code ec, const std::string& msg);
class schema;
/*
* 1. has_many (MM)
* no belongs to
* relation table is needed
* - element type is a foreign table (FT),
* then relation table must look like follows:
* relation_table<MM, FT>
* where MM and FT must be defined as belongs to
* - element type if a builtin type BT (i.e. string, int, etc.),
* then the relation table must look like follows:
* relation_table<MM, BT>
* where MM as belongs to and BT as given type
*
* 2. has_many_to_many (MM1, MM2)
* relation_table is needed
* relation_table<MM1, MM2>
* where MM1 and MM2 must be defined as belongs to
*
* 3. hans_many (MM) <-> belongs_to (BT)
* belongs_to has foreign key to the has_many side
* no relation table needed
*
* 4. has_one to belongs_to
* no relation table is needed
*
* 5. has_many (MM) <-> has_one (HO)
* invalid relation -> error
*
* 6. has_one
* no has_many or belongs_to
* invalid relation -> error
*/
template<typename Type>
class relation_completer final {
public:
using value_type = Type;
static void prepare_expected_nodes(schema_node &node) {
relation_completer completer(node);
Type obj;
access::process(completer, obj);
}
template < class PrimaryKeyType >
static void on_primary_key(const char * /*id*/, PrimaryKeyType &/*pk*/, std::enable_if_t<std::is_integral_v<PrimaryKeyType> && !std::is_same_v<bool, PrimaryKeyType>>* = nullptr) {}
static void on_primary_key(const char * /*id*/, std::string &/*pk*/, size_t /*size*/) {}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template<typename AttributeType>
static void on_attribute(const char * /*id*/, AttributeType &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<typename AttributeType>
static void on_attribute(const char * /*id*/, std::optional<AttributeType> &/*val*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class ForeignPointerType>
void on_belongs_to(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {
on_foreign_key<ForeignPointerType>();
}
template<class ForeignPointerType>
void on_has_one(const char * /*id*/, ForeignPointerType &/*obj*/, const utils::foreign_attributes &/*attr*/) {
on_foreign_key<ForeignPointerType>();
}
template<class CollectionType>
void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value>* = nullptr );
template<class CollectionType>
void on_has_many(const char *id, CollectionType &, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>* = nullptr );
template<class CollectionType>
void on_has_many_to_many(const char *id, CollectionType &collection, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr);
template<class ContainerType>
void on_has_many_to_many(const char *id, ContainerType &collection, const utils::foreign_attributes &attr);
private:
template<class ForeignPointerType>
void on_foreign_key();
private:
explicit relation_completer(schema_node& node)
: node_(node)
, schema_(node.schema_){}
private:
schema_node &node_;
schema& schema_;
};
class schema {
public:
typedef const_schema_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
/**
* Creates an empty schema
*/
explicit schema( std::string name = "");
template <typename Type>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string& name, const std::string &parent = "") {
if (has_node(name)) {
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
}
if (const auto it = expected_node_map_.find(typeid(Type)); it != expected_node_map_.end()) {
const auto node = it->second;
expected_node_map_.erase(it);
node->update_name(name);
node_map_.insert({node->name(), node})/*.first*/;
type_index_node_map_.insert({node->type_index(), node});
} else {
// analyze node (collect unknown types by type index)
const auto node = schema_node::make_node<Type>(*this, name);
relation_completer<Type>::prepare_expected_nodes(*node);
if (auto result = attach_node(node, parent); !result) {
return utils::failure(result.err());
}
}
return utils::ok<void>();
}
template <typename Type, typename SuperType>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string name) {
const auto ti = std::type_index(typeid(SuperType));
auto result = find_node(ti);
if (!result) {
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(ti.name()) + "' not found"));
}
return attach<Type>(name, (*result)->name());
}
/**
* 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;
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());
}
return utils::ok(result.value()->info<Type>());
}
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()->basic_info()});
}
[[nodiscard]] utils::result<std::shared_ptr<attribute_definition>, utils::error> reference(const std::type_index &type_index) const;
private:
using node_ptr = std::shared_ptr<schema_node>;
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<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
const std::string &parent);
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
const std::type_index &type_index);
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::type_index &type_index) const;
[[nodiscard]] bool has_node(const std::string &name) const;
[[nodiscard]] bool has_node(const std::type_index& index) const;
[[nodiscard]] bool has_node(const std::type_index& index, const std::string &name) const;
static void push_back_child(const node_ptr &parent, const node_ptr &child);
private:
template <typename Type>
friend class relation_completer;
std::string name_;
std::shared_ptr<schema_node> root_;
t_node_map node_map_;
t_type_index_node_map type_index_node_map_;
t_type_index_node_map expected_node_map_;
};
template<typename Type>
template<class CollectionType>
void relation_completer<Type>::on_has_many( const char *id, CollectionType&, const char *join_column, const utils::foreign_attributes&, std::enable_if_t<is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/ ) {
using value_type = typename CollectionType::value_type;
// Process foreign key has many relation
// Check if foreign key type was already registered
auto result = schema_.find_node(typeid(value_type));
if (result) {
} else {
//
using relation_type = many_to_many_relation<value_type, Type>;
auto creator = [] {
return new many_to_many_relation<value_type, Type>();
};
}
}
template<typename Type>
template<class CollectionType>
void relation_completer<Type>::on_has_many( const char *id, CollectionType&, const char *join_column, const utils::foreign_attributes&, std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/ ) {
using value_type = typename CollectionType::value_type;
// Process values has many relation
// Register relation table
// many_to_relation<Type, value_type> *relation = new many_to_relation<Type, value_type>(join_column, "value");
auto result = schema_.find_node(typeid(value_type));
if (result) {
} else {
//
using relation_type = many_to_many_relation<value_type, Type>;
auto creator = [] {
return new many_to_many_relation<value_type, Type>();
};
}
}
template<typename Type>
template<class CollectionType>
void relation_completer<Type>::on_has_many_to_many( const char *id, CollectionType &/*collection*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr) {
auto result = schema_.find_node(id);
if (result) {
} else {
//
using relation_type = many_to_many_relation<typename CollectionType::value_type, Type>;
auto creator = [join_column, inverse_join_column] {
return new many_to_many_relation<typename CollectionType::value_type, Type>(join_column, inverse_join_column);
};
auto node = schema_node::make_relation_node<relation_type>(schema_, id);
schema_.attach_node(node, typeid(relation_type));
}
}
template<typename Type>
template<class ContainerType>
void relation_completer<Type>::on_has_many_to_many( const char *id, ContainerType &collection, const utils::foreign_attributes &attr ) {
}
template<typename Type>
template<class ForeignPointerType>
void relation_completer<Type>::on_foreign_key() {
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
if (const auto result = schema_.find_node(ti); !result.is_ok() && schema_.expected_node_map_.count(ti) == 0) {
schema_.expected_node_map_.insert({ti, schema_node::make_node<typename ForeignPointerType::value_type>(schema_, ti.name())});
} else {
const auto& foreign_node = result.value();
if (const auto rit = foreign_node->basic_info().find_relation_endpoint(ti); rit != foreign_node->basic_info().endpoint_end()) {
if (rit->second.is_has_many()) {
} else if (rit->second.is_has_one()) {
} else {
}
}
}
}
}
#endif //SCHEMA_HPP