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

View File

@ -3,7 +3,6 @@
#include <memory> #include <memory>
#include "matador/utils/basic_type_converter.hpp"
#include "matador/utils/basic_types.hpp" #include "matador/utils/basic_types.hpp"
#include "matador/utils/default_type_traits.hpp" #include "matador/utils/default_type_traits.hpp"
#include "matador/utils/field_attributes.hpp" #include "matador/utils/field_attributes.hpp"
@ -27,6 +26,13 @@ public:
attribute_definition(attribute_definition&&) noexcept = default; attribute_definition(attribute_definition&&) noexcept = default;
attribute_definition& operator=(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> template<typename Type>
explicit attribute_definition(std::string name, const utils::field_attributes& attr) 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) : 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, 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> 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) 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}; null_option null_option_{null_option::NOT_NULL};
utils::value value_; utils::value value_;
std::shared_ptr<attribute_definition> reference_column_; 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); attribute_definition make_pk_column<std::string>(const std::string &name, size_t size);
template < typename Type > 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 }}; return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }};
} }
@ -170,7 +174,31 @@ template < typename Type >
} }
template <> 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);
} }

View File

@ -21,10 +21,10 @@ public:
fk_attribute_generator() = default; fk_attribute_generator() = default;
template<class Type> 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); 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> template<typename ValueType>
@ -84,13 +84,13 @@ public:
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) 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))); 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> template<class Pointer>
void on_has_one(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) 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))); 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 ContainerType> template<class ContainerType>
void on_has_many(const char *id, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {} void on_has_many(const char *id, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}

View File

@ -20,10 +20,11 @@ public:
[[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const; [[nodiscard]] std::shared_ptr<attribute_definition> reference_column() const;
protected: 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: 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 */ std::type_index type_index_; /**< type index of the represented object type */
object_definition definition_; object_definition definition_;
std::shared_ptr<attribute_definition> reference_column_; std::shared_ptr<attribute_definition> reference_column_;

View File

@ -10,9 +10,13 @@ class schema_node;
template<typename Type> template<typename Type>
class object_info final : public basic_object_info { class object_info final : public basic_object_info {
public: public:
explicit object_info(schema_node &node, object_definition &&definition, explicit object_info(object_definition &&definition,
std::shared_ptr<attribute_definition> &&ref_column) 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_; } const Type &prototype() const { return prototype_; }

View File

@ -90,10 +90,9 @@ public:
// analyze node (collect unknown types by type index) // analyze node (collect unknown types by type index)
type_analyzer<Type>::analyze(*this); 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 (auto result = attach_node(node, parent); !result) {
if (!result) {
return utils::failure(result.err()); return utils::failure(result.err());
} }
@ -206,7 +205,7 @@ void type_analyzer<Type>::on_has_one(const char *, ForeignPointerType &, const u
return; 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); known_types_.insert(ti);

View File

@ -5,23 +5,47 @@
namespace matador::object { namespace matador::object {
attribute_definition::attribute_definition(const char *name) attribute_definition::attribute_definition(const char *name)
: name_(name) : name_(name)
, attributes_(utils::null_attributes) { , attributes_(utils::null_attributes) {
} }
attribute_definition::attribute_definition(std::string name) attribute_definition::attribute_definition(std::string name)
: name_(std::move(name)) : name_(std::move(name))
, attributes_(utils::null_attributes) { , attributes_(utils::null_attributes) {
}
attribute_definition::attribute_definition(std::string name,
std::string table_name,
const utils::basic_type type,
const utils::field_attributes& attr)
: name_(std::move(name))
, table_(std::move(table_name))
, index_(0)
, attributes_(attr)
, value_(type, attr.size()) {
} }
attribute_definition::attribute_definition(std::string name, const utils::basic_type type, attribute_definition::attribute_definition(std::string name, const utils::basic_type type,
const utils::field_attributes &attr, const null_option null_opt, const utils::field_attributes &attr, const null_option null_opt,
const size_t index) const size_t index)
: name_(std::move(name)) : name_(std::move(name))
, index_(index) , index_(index)
, attributes_(attr) , attributes_(attr)
, null_option_(null_opt) , null_option_(null_opt)
, value_(type, attr.size()) { , value_(type, attr.size()) {
}
attribute_definition::attribute_definition(std::string name,
std::string table_name,
const utils::basic_type type,
const utils::field_attributes &attr, const null_option null_opt,
const size_t index)
: name_(std::move(name))
, table_(std::move(table_name))
, index_(index)
, attributes_(attr)
, null_option_(null_opt)
, value_(type, attr.size()) {
} }
attribute_definition::attribute_definition(std::string name, attribute_definition::attribute_definition(std::string name,
@ -29,12 +53,12 @@ attribute_definition::attribute_definition(std::string name,
const size_t index, const size_t index,
const std::shared_ptr<attribute_definition> &ref_column, const std::shared_ptr<attribute_definition> &ref_column,
const utils::field_attributes &attr, const null_option null_opt) const utils::field_attributes &attr, const null_option null_opt)
: name_(std::move(name)) : name_(std::move(name))
, index_(index) , index_(index)
, attributes_(attr) , attributes_(attr)
, null_option_(null_opt) , null_option_(null_opt)
, value_(type, attr.size()) , value_(type, attr.size())
, reference_column_(ref_column) { , reference_column_(ref_column) {
} }
const std::string &attribute_definition::name() const { const std::string &attribute_definition::name() const {
@ -140,7 +164,25 @@ attribute_definition make_pk_column<std::string>(const std::string &name, size_t
} }
template<> template<>
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) { attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL
};
}
template<>
attribute_definition make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
return {
name, utils::basic_type::type_varchar, 0,
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY),
{ 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL
};
}
template<>
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) {
const auto ref_column = std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY);
return { return {
name, utils::data_type_traits<std::string>::type(size), 0, ref_column, name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
{size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL {size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL

View File

@ -5,22 +5,26 @@
#include <algorithm> #include <algorithm>
namespace matador::object { namespace matador::object {
basic_object_info::basic_object_info(schema_node &node, basic_object_info::basic_object_info(const std::type_index type_index,
const std::type_index type_index,
object_definition &&definition, object_definition &&definition,
std::shared_ptr<attribute_definition> &&reference_column) std::shared_ptr<attribute_definition> &&reference_column)
: node_(node) : type_index_(type_index)
, type_index_(type_index)
, definition_(std::move(definition)) , definition_(std::move(definition))
, reference_column_(std::move(reference_column)) { , reference_column_(std::move(reference_column)) {
} }
basic_object_info::basic_object_info(const std::type_index type_index,
std::shared_ptr<attribute_definition> &&reference_column)
: type_index_(type_index)
, reference_column_(std::move(reference_column)) {
}
std::type_index basic_object_info::type_index() const { std::type_index basic_object_info::type_index() const {
return type_index_; return type_index_;
} }
std::string basic_object_info::name() const { std::string basic_object_info::name() const {
return node_.name(); return node_->name();
} }
const object_definition &basic_object_info::definition() const { const object_definition &basic_object_info::definition() const {

View File

@ -2,21 +2,18 @@
namespace matador::object { namespace matador::object {
object_definition::object_definition(const std::initializer_list<attribute_definition> columns) object_definition::object_definition(const std::initializer_list<attribute_definition> columns)
: columns_(columns) : columns_(columns) {
{
init(); init();
} }
object_definition::object_definition(const std::vector<attribute_definition> &columns) object_definition::object_definition(const std::vector<attribute_definition> &columns)
: columns_(columns) : columns_(columns) {
{
init(); init();
} }
object_definition::object_definition(const object_definition &x) object_definition::object_definition(const object_definition &x)
: columns_(x.columns_) : columns_(x.columns_)
, pk_index_(x.pk_index_) , pk_index_(x.pk_index_) {
{
for (auto& col : columns_) { for (auto& col : columns_) {
add_to_map(col, col.index()); add_to_map(col, col.index());
} }

View File

@ -45,7 +45,7 @@ utils::result<std::shared_ptr<attribute_definition>, utils::error> schema::refer
return utils::failure(make_error(error_code::Failure, "Primary key not found")); return utils::failure(make_error(error_code::Failure, "Primary key not found"));
} }
return utils::ok(std::make_pair(node->name(), node->basic_info().definition().primary_key()->name())); return utils::ok(std::make_shared<attribute_definition>(node->name(), node->basic_info().definition().primary_key()->name(), utils::basic_type::type_null, utils::constraints::FOREIGN_KEY));
} }
utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(const std::shared_ptr<schema_node> &node, utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(const std::shared_ptr<schema_node> &node,

View File

@ -5,13 +5,12 @@
namespace matador::object { namespace matador::object {
schema_node::schema_node(schema &tree) schema_node::schema_node(schema &tree)
: schema_(tree) : schema_(tree)
, info_(std::make_unique<null_info>(*this, object_definition{})) { , info_(std::make_unique<null_info>(object_definition{}, std::shared_ptr<attribute_definition>{})) {
} }
schema_node::schema_node(schema &tree, std::string name, std::unique_ptr<basic_object_info> &&info) schema_node::schema_node(schema &tree, std::string name, std::unique_ptr<basic_object_info> &&info)
: schema_(tree) : schema_(tree)
, info_(std::move(info)) , info_(std::move(info))
// , info_(std::make_unique<object_info<Type>>(*this, object_definition(attribute_definition_generator::generate<Type>(schema_))))
, first_child_(std::shared_ptr<schema_node>(new schema_node(tree))) , first_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
, last_child_(std::shared_ptr<schema_node>(new schema_node(tree))) , last_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
, name_(std::move(name)) { , name_(std::move(name)) {

View File

@ -229,11 +229,9 @@ void query_compiler::visit(internal::query_create_part &/*create_part*/)
query_.sql = dialect_->token_at(sql::dialect_token::CREATE); query_.sql = dialect_->token_at(sql::dialect_token::CREATE);
} }
struct fk_context struct fk_context {
{
std::string column; std::string column;
std::string ref_table; std::shared_ptr<object::attribute_definition> reference_column;
std::string ref_column;
}; };
struct column_context struct column_context
@ -273,7 +271,7 @@ void query_compiler::visit(internal::query_create_table_part &create_table_part)
result += ", CONSTRAINT FK_" + create_table_part.table().name; result += ", CONSTRAINT FK_" + create_table_part.table().name;
result += "_" + fk.column; result += "_" + fk.column;
result += " FOREIGN KEY (" + fk.column + ")"; result += " FOREIGN KEY (" + fk.column + ")";
result += " REFERENCES " + fk.ref_table + "(" + fk.ref_column + ")"; result += " REFERENCES " + fk.reference_column->table_name() + "(" + fk.reference_column->name() + ")";
} }
result += ")"; result += ")";
@ -340,7 +338,7 @@ std::string build_create_column(const object::attribute_definition &col, const s
context.primary_keys.emplace_back(col.name()); context.primary_keys.emplace_back(col.name());
} }
if (is_constraint_set(col.attributes().options(), utils::constraints::FOREIGN_KEY)) { if (is_constraint_set(col.attributes().options(), utils::constraints::FOREIGN_KEY)) {
context.foreign_contexts.push_back({col.name(), col.ref_table(), col.ref_column()}); context.foreign_contexts.push_back({col.name(), col.reference_column()});
} }
return result; return result;

View File

@ -11,8 +11,8 @@ TEST_CASE("Test create empty column", "[column]") {
REQUIRE(c.name() == "name"); REQUIRE(c.name() == "name");
REQUIRE(c.index() == -1); REQUIRE(c.index() == -1);
REQUIRE(c.type() == basic_type::type_null); REQUIRE(c.type() == basic_type::type_null);
REQUIRE(c.ref_table().empty()); REQUIRE(c.reference_column()->table_name().empty());
REQUIRE(c.ref_column().empty()); REQUIRE(c.reference_column()->name().empty());
c.set(std::string{"george"}, 255); c.set(std::string{"george"}, 255);
REQUIRE(c.type() == basic_type::type_varchar); REQUIRE(c.type() == basic_type::type_varchar);
@ -30,8 +30,8 @@ TEST_CASE("Test copy and move column", "[column]") {
c.set(std::string{"george"}, 255); c.set(std::string{"george"}, 255);
REQUIRE(c.name() == "name"); REQUIRE(c.name() == "name");
REQUIRE(c.index() == -1); REQUIRE(c.index() == -1);
REQUIRE(c.ref_table().empty()); REQUIRE(c.reference_column()->table_name().empty());
REQUIRE(c.ref_column().empty()); REQUIRE(c.reference_column()->name().empty());
REQUIRE(c.type() == basic_type::type_varchar); REQUIRE(c.type() == basic_type::type_varchar);
REQUIRE(c.as<std::string>() == "george"); REQUIRE(c.as<std::string>() == "george");
REQUIRE(c.attributes().size() == 255); REQUIRE(c.attributes().size() == 255);
@ -39,8 +39,8 @@ TEST_CASE("Test copy and move column", "[column]") {
auto c2 = c; auto c2 = c;
REQUIRE(c2.name() == "name"); REQUIRE(c2.name() == "name");
REQUIRE(c2.index() == -1); REQUIRE(c2.index() == -1);
REQUIRE(c2.ref_table().empty()); REQUIRE(c2.reference_column()->table_name().empty());
REQUIRE(c2.ref_column().empty()); REQUIRE(c2.reference_column()->name().empty());
REQUIRE(c2.type() == basic_type::type_varchar); REQUIRE(c2.type() == basic_type::type_varchar);
REQUIRE(c2.as<std::string>() == "george"); REQUIRE(c2.as<std::string>() == "george");
REQUIRE(c2.attributes().size() == 255); REQUIRE(c2.attributes().size() == 255);
@ -48,16 +48,16 @@ TEST_CASE("Test copy and move column", "[column]") {
auto c3 = std::move(c2); auto c3 = std::move(c2);
REQUIRE(c3.name() == "name"); REQUIRE(c3.name() == "name");
REQUIRE(c3.index() == -1); REQUIRE(c3.index() == -1);
REQUIRE(c3.ref_table().empty()); REQUIRE(c3.reference_column()->table_name().empty());
REQUIRE(c3.ref_column().empty()); REQUIRE(c3.reference_column()->name().empty());
REQUIRE(c3.type() == basic_type::type_varchar); REQUIRE(c3.type() == basic_type::type_varchar);
REQUIRE(c3.as<std::string>() == "george"); REQUIRE(c3.as<std::string>() == "george");
REQUIRE(c3.attributes().size() == 255); REQUIRE(c3.attributes().size() == 255);
REQUIRE(c2.name().empty()); REQUIRE(c2.name().empty());
REQUIRE(c2.index() == -1); REQUIRE(c2.index() == -1);
REQUIRE(c2.ref_table().empty()); REQUIRE(c2.reference_column()->table_name().empty());
REQUIRE(c2.ref_column().empty()); REQUIRE(c2.reference_column()->name().empty());
REQUIRE(c2.type() == basic_type::type_null); REQUIRE(c2.type() == basic_type::type_null);
// REQUIRE(!c2.as<std::string>().has_value()); // REQUIRE(!c2.as<std::string>().has_value());
REQUIRE(c2.attributes().size() == 255); REQUIRE(c2.attributes().size() == 255);