schema node analyze progress

This commit is contained in:
Sascha Kühl
2025-02-10 16:04:49 +01:00
parent 504e111491
commit e3e0eb932c
12 changed files with 137 additions and 65 deletions
@@ -3,7 +3,6 @@
#include <memory>
#include "matador/utils/basic_type_converter.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/utils/default_type_traits.hpp"
#include "matador/utils/field_attributes.hpp"
@@ -27,6 +26,13 @@ public:
attribute_definition(attribute_definition&&) noexcept = default;
attribute_definition& operator=(attribute_definition&&) noexcept = default;
template<typename Type>
explicit attribute_definition(std::string name, std::string table_name, const utils::field_attributes& attr)
: attribute_definition(std::move(name), std::move(table_name), utils::data_type_traits<Type>::type(attr.size()), attr)
{}
attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes& attr);
template<typename Type>
explicit attribute_definition(std::string name, const utils::field_attributes& attr)
: attribute_definition(std::move(name), utils::data_type_traits<Type>::type(attr.size()), attr)
@@ -43,6 +49,7 @@ public:
{}
attribute_definition(std::string name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, size_t index = 0);
attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, size_t index = 0);
template<typename Type>
attribute_definition(std::string name, const std::shared_ptr<attribute_definition> &ref_column, const utils::field_attributes& attr, null_option null_opt)
@@ -126,8 +133,6 @@ private:
null_option null_option_{null_option::NOT_NULL};
utils::value value_;
std::shared_ptr<attribute_definition> reference_column_;
// std::string ref_table_;
// std::string ref_column_;
};
/**
@@ -158,8 +163,7 @@ template <>
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size);
template < typename Type >
attribute_definition make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column)
{
attribute_definition make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }};
}
@@ -170,7 +174,31 @@ template < typename Type >
}
template <>
attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column);
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column);
template < typename Type >
[[maybe_unused]] attribute_definition make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
return {
name, utils::data_type_traits<Type>::type(size), 0,
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL
};
}
template < typename Type >
[[maybe_unused]] attribute_definition make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) {
return {
name, utils::data_type_traits<Type>::type(0), 0,
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(0), utils::constraints::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL
};
}
template <>
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name);
template <>
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name);
}
@@ -21,10 +21,10 @@ public:
fk_attribute_generator() = default;
template<class Type>
attribute_definition generate(const char *id, Type &x, const std::string &ref_table, const std::string &ref_column)
attribute_definition generate(const char *id, Type &x, const std::shared_ptr<attribute_definition> &ref_column)
{
access::process(*this, x);
return attribute_definition{id, type_, 0, ref_table, ref_column, {utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL};
return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL};
}
template<typename ValueType>
@@ -84,13 +84,13 @@ public:
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/)
{
const auto ref_column = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type)));
columns_.push_back(fk_column_generator_.generate(id, *x, ref_table, ref_column));
columns_.push_back(fk_column_generator_.generate(id, *x, ref_column));
}
template<class Pointer>
void on_has_one(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/)
{
const auto [ref_table, ref_column] = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type)));
columns_.push_back(fk_column_generator_.generate(id, *x, ref_table, ref_column));
const auto ref_column = determine_foreign_ref(std::type_index(typeid(typename Pointer::value_type)));
columns_.push_back(fk_column_generator_.generate(id, *x, ref_column));
}
template<class ContainerType>
void on_has_many(const char *id, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
+3 -2
View File
@@ -20,10 +20,11 @@ public:
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
protected:
basic_object_info(schema_node &node, std::type_index type_index, object_definition &&definition, std::shared_ptr<attribute_definition> &&reference_column);
basic_object_info(std::type_index type_index, object_definition &&definition, std::shared_ptr<attribute_definition> &&reference_column);
basic_object_info(std::type_index type_index, std::shared_ptr<attribute_definition> &&reference_column);
protected:
schema_node &node_; /**< prototype node of the represented object type */
std::shared_ptr<schema_node> node_; /**< prototype node of the represented object type */
std::type_index type_index_; /**< type index of the represented object type */
object_definition definition_;
std::shared_ptr<attribute_definition> reference_column_;
+6 -2
View File
@@ -10,9 +10,13 @@ class schema_node;
template<typename Type>
class object_info final : public basic_object_info {
public:
explicit object_info(schema_node &node, object_definition &&definition,
explicit object_info(object_definition &&definition,
std::shared_ptr<attribute_definition> &&ref_column)
: basic_object_info(node, typeid(Type), std::move(definition), std::move(ref_column)) {
: basic_object_info(typeid(Type), std::move(definition), std::move(ref_column)) {
}
explicit object_info(schema_node &node,
std::shared_ptr<attribute_definition> &&ref_column)
: basic_object_info(typeid(Type), std::move(ref_column)) {
}
const Type &prototype() const { return prototype_; }
+3 -4
View File
@@ -90,10 +90,9 @@ public:
// analyze node (collect unknown types by type index)
type_analyzer<Type>::analyze(*this);
auto node = schema_node::make_node<Type>(*this, name);
const auto node = schema_node::make_node(*this, name, {});
auto result = attach_node(node, parent);
if (!result) {
if (auto result = attach_node(node, parent); !result) {
return utils::failure(result.err());
}
@@ -206,7 +205,7 @@ void type_analyzer<Type>::on_has_one(const char *, ForeignPointerType &, const u
return;
}
auto info = std::make_unique<object_info<typename ForeignPointerType::value_type>>();
auto info = std::make_unique<object_info<typename ForeignPointerType::value_type>>(object_definition{}, std::make_unique<attribute_definition>(std::string(""), std::string(ti.name()), utils::basic_type::type_null, utils::constraints::FOREIGN_KEY));
known_types_.insert(ti);