object generating progress

This commit is contained in:
Sascha Kühl 2025-12-01 16:14:13 +01:00
parent 19b7044773
commit 422df82b7c
10 changed files with 242 additions and 33 deletions

View File

@ -71,6 +71,7 @@ public:
private: private:
friend class object; friend class object;
friend class attribute_generator; friend class attribute_generator;
friend class object_generator;
std::string name_; std::string name_;
object *owner_{nullptr}; object *owner_{nullptr};

View File

@ -62,11 +62,8 @@ public:
template < class Type > template < class Type >
static std::vector<attribute> generate(const repository &repo, object &obj) { static std::vector<attribute> generate(const repository &repo, object &obj) {
std::vector<attribute> columns;
attribute_generator gen(columns, repo, obj);
Type t; Type t;
access::process(gen, t); return generate(t, repo, obj);
return columns;
} }
template < class Type > template < class Type >

View File

@ -22,6 +22,7 @@ public:
private: private:
friend class constraint_builder; friend class constraint_builder;
friend class constraints_generator; friend class constraints_generator;
friend class object_generator;
friend class object; friend class object;
std::string name_; std::string name_;

View File

@ -24,9 +24,14 @@ public:
template < typename Type > template < typename Type >
static std::vector<class constraint> generate(const repository &repo, object &obj) { static std::vector<class constraint> generate(const repository &repo, object &obj) {
Type t;
return generate(t, repo, obj);
}
template < typename Type >
static std::vector<class constraint> generate(const Type& t, const repository &repo, object &obj) {
std::vector<class constraint> constraints; std::vector<class constraint> constraints;
constraints_generator gen(constraints, repo, obj); constraints_generator gen(constraints, repo, obj);
Type t;
access::process(gen, t); access::process(gen, t);
return constraints; return constraints;
} }

View File

@ -1,10 +1,10 @@
#ifndef MATADOR_OBJECT_GENERATOR_HPP #ifndef MATADOR_OBJECT_HPP
#define MATADOR_OBJECT_GENERATOR_HPP #define MATADOR_OBJECT_HPP
#include "matador/object/attribute.hpp" #include "matador/object/attribute.hpp"
#include "matador/object/attribute_generator.hpp" // #include "matador/object/attribute_generator.hpp"
#include "matador/object/constraint.hpp" #include "matador/object/constraint.hpp"
#include "matador/object/constraints_generator.hpp" // #include "matador/object/constraints_generator.hpp"
#include "matador/utils/identifier.hpp" #include "matador/utils/identifier.hpp"
@ -16,21 +16,21 @@ class object {
public: public:
explicit object(std::string name, std::string alias = ""); explicit object(std::string name, std::string alias = "");
template<typename Type> // template<typename Type>
static std::unique_ptr<object> generate(const repository& repo, std::string name, std::string alias = "") { // static std::unique_ptr<object> generate(const repository& repo, std::string name, std::string alias = "") {
auto obj = std::make_unique<object>(std::move(name), std::move(alias)); // auto obj = std::make_unique<object>(std::move(name), std::move(alias));
obj->attributes_ = std::move(attribute_generator::generate<Type>(repo, *obj)); // obj->attributes_ = std::move(attribute_generator::generate<Type>(repo, *obj));
obj->constraints_ = std::move(constraints_generator::generate<Type>(repo, *obj)); // obj->constraints_ = std::move(constraints_generator::generate<Type>(repo, *obj));
return obj; // return obj;
} // }
//
template<typename Type> // template<typename Type>
static std::unique_ptr<object> generate(const repository& repo, std::string name, std::string alias = "") { // static std::unique_ptr<object> generate(const std::unique_ptr<Type>& ptr, const repository& repo, std::string name, std::string alias = "") {
auto obj = std::make_unique<object>(std::move(name), std::move(alias)); // auto obj = std::make_unique<object>(std::move(name), std::move(alias));
obj->attributes_ = std::move(attribute_generator::generate<Type>(repo, *obj)); // obj->attributes_ = std::move(attribute_generator::generate(*ptr, repo, *obj));
obj->constraints_ = std::move(constraints_generator::generate<Type>(repo, *obj)); // obj->constraints_ = std::move(constraints_generator::generate(*ptr, repo, *obj));
return obj; // return obj;
} // }
static const attribute& create_attribute(std::string name, object& obj); static const attribute& create_attribute(std::string name, object& obj);
@ -57,6 +57,7 @@ public:
private: private:
friend class constraints_generator; friend class constraints_generator;
friend class attribute_generator; friend class attribute_generator;
friend class object_generator;
std::string name_; std::string name_;
std::string alias_; std::string alias_;
@ -67,4 +68,4 @@ private:
std::vector<class constraint> constraints_; std::vector<class constraint> constraints_;
}; };
} }
#endif //MATADOR_OBJECT_GENERATOR_HPP #endif //MATADOR_OBJECT_HPP

View File

@ -0,0 +1,142 @@
#ifndef MATADOR_OBJECT_GENERATOR_HPP
#define MATADOR_OBJECT_GENERATOR_HPP
#include "matador/object/object.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/primary_key_attribute.hpp"
#include <memory>
namespace matador::object {
class repository;
class pk_type_determinator {
private:
pk_type_determinator() = default;
public:
template<class Type>
static utils::basic_type determine() {
pk_type_determinator determinator;
Type t;
access::process(determinator, t);
return determinator.type_;
}
template<typename ValueType>
void on_primary_key(const char *, ValueType &/*pk*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
type_ = utils::data_type_traits<ValueType>::type(attr.size());
}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template < class Type >
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
static void on_attribute(const char * /*id*/, char * /*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template<class Pointer>
static void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
template<class Pointer>
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
private:
utils::basic_type type_{};
};
class object_generator {
private:
explicit object_generator(const repository& repo, std::unique_ptr<object> &&object);
public:
template < class Type >
static std::unique_ptr<object> generate(const repository &repo, const std::string &name, const std::string &alias = "") {
return generate(std::make_unique<Type>(), repo, name, alias);
}
template < class Type >
static std::unique_ptr<object> generate(std::unique_ptr<Type>&& t, const repository &repo, const std::string &name, const std::string &alias = "") {
object_generator gen(repo, std::make_unique<object>(name, alias));
access::process(gen, *t);
return gen.move();
}
[[nodiscard]] std::unique_ptr<object> move() { return std::move(object_); }
template < class Type >
void on_primary_key(const char *, Type &x, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
void on_revision(const char *id, uint64_t &rev);
template<typename Type>
void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes);
template<typename Type>
void on_attribute(const char *id, std::optional<Type> &x, const utils::field_attributes &attr = utils::null_attributes);
template<class Pointer>
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
on_foreign_key(id, x);
create_fk_constraint( typeid(typename Pointer::value_type), id);
}
template<class Pointer>
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
template <class Pointer>
void on_foreign_key(const char *id, Pointer &x) {
const auto type = pk_type_determinator::determine<typename Pointer::value_type>();
auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NOT_NULL);
ref.owner_ = object_.get();
create_fk_constraint(typeid(typename Pointer::value_type), id);
}
template<class ContainerType>
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
private:
template<typename ValueType>
attribute &emplace_attribute(const char *id, const utils::field_attributes& attr, null_option_type null_option) {
auto &ref = object_->attributes_.emplace_back(id, utils::data_type_traits<ValueType>::type(attr.size()), attr, null_option);
ref.owner_ = object_.get();
return ref;
}
void create_pk_constraint(const std::string& name) const;
void create_fk_constraint(const std::type_index& ti, const std::string& name) const;
void create_unique_constraint(const std::string& name) const;
[[nodiscard]] std::vector<attribute>::iterator find_attribute_by_name(const std::string &name) const;
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
private:
const repository &repo_;
std::unique_ptr<object> object_;
};
template<typename ValueType>
void object_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NOT_NULL);
prepare_primary_key(ref, utils::identifier(x));
create_pk_constraint(id);
}
template<typename Type>
void object_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NOT_NULL);
}
template<typename Type>
void object_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NULLABLE);
}
}
#endif //MATADOR_OBJECT_GENERATOR_HPP

View File

@ -2,7 +2,7 @@
#define SCHEMA_NODE_HPP #define SCHEMA_NODE_HPP
#include "matador/object/attribute_generator.hpp" #include "matador/object/attribute_generator.hpp"
#include "matador/object/object.hpp" #include "matador/object/object_generator.hpp"
#include "matador/object/object_info.hpp" #include "matador/object/object_info.hpp"
#include "matador/object/primary_key_resolver.hpp" #include "matador/object/primary_key_resolver.hpp"
@ -23,7 +23,7 @@ public:
primary_key_resolver resolver; primary_key_resolver resolver;
auto pk_info = resolver.resolve<Type>(); auto pk_info = resolver.resolve<Type>();
auto obj = object::generate<Type>(repo, name); auto obj = object_generator::generate<Type>(repo, name);
auto info = std::make_unique<object_info<Type>>( auto info = std::make_unique<object_info<Type>>(
node, node,
std::move(obj), std::move(obj),
@ -41,11 +41,10 @@ public:
return result; return result;
} }
auto obj = creator(); auto obj = object_generator::generate(creator(), repo, name);
object o(name);
auto info = std::make_unique<object_info<Type>>( auto info = std::make_unique<object_info<Type>>(
result.value(), result.value(),
attribute_generator::generate(*obj, repo, o), std::move(obj),
std::forward<CreatorFunc>(creator) std::forward<CreatorFunc>(creator)
); );
result.value()->info_ = std::move(info); result.value()->info_ = std::move(info);

View File

@ -108,6 +108,8 @@ add_library(matador-core STATIC
utils/value.cpp utils/value.cpp
utils/version.cpp utils/version.cpp
object/object.cpp object/object.cpp
../../include/matador/object/object_generator.hpp
object/object_generator.cpp
) )
target_link_libraries(matador-core ${CMAKE_DL_LIBS}) target_link_libraries(matador-core ${CMAKE_DL_LIBS})

View File

@ -0,0 +1,60 @@
#include "matador/object/object_generator.hpp"
#include "matador/object/repository.hpp"
namespace matador::object {
object_generator::object_generator( const repository& repo, std::unique_ptr<object>&& object )
: repo_(repo)
, object_(std::move(object)) {}
void object_generator::on_revision(const char* id, uint64_t& rev) {
on_attribute(id, rev);
}
void object_generator::create_pk_constraint(const std::string& name) const {
class constraint pk_constraint("PK_" + object_->name());
pk_constraint.options_ |= utils::constraints::PrimaryKey;
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
pk_constraint.attr_ = &*pk_attr;
}
pk_constraint.owner_ = object_.get();
object_->constraints_.emplace_back(std::move(pk_constraint));
}
void object_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const {
const auto result = repo_.basic_info(ti);
if (!result) {
return;
}
const auto *pk_attribute = result.value().get().primary_key_attribute();
class constraint pk_constraint("FK_" + object_->name() + "_" + name);
pk_constraint.options_ |= utils::constraints::ForeignKey;
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
pk_constraint.attr_ = &*pk_attr;
}
pk_constraint.owner_ = object_.get();
pk_constraint.ref_column_name_ = pk_attribute->name();
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
object_->constraints_.emplace_back(std::move(pk_constraint));
}
void object_generator::create_unique_constraint(const std::string& name) const {
class constraint pk_constraint("UK_" + object_->name() + "_" + name);
pk_constraint.options_ |= utils::constraints::Unique;
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
pk_constraint.attr_ = &*pk_attr;
}
pk_constraint.owner_ = object_.get();
}
std::vector<attribute>::iterator object_generator::find_attribute_by_name(const std::string& name) const {
return std::find_if(std::begin(object_->attributes_), std::end(object_->attributes_), [&name](const attribute& elem) {
return elem.name() == name;
});
}
void object_generator::prepare_primary_key(attribute& ref, utils::identifier &&pk) const {
object_->pk_attribute_ = &ref;
object_->pk_identifier_ = std::move(pk);
}
}

View File

@ -1,6 +1,7 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include "matador/object/object.hpp" #include "matador/object/object.hpp"
#include "matador/object/object_generator.hpp"
#include "matador/object/repository.hpp" #include "matador/object/repository.hpp"
#include "../test/models/author.hpp" #include "../test/models/author.hpp"
@ -12,7 +13,7 @@ TEST_CASE("Generate object from type", "[object][generate]") {
auto result = repo.attach<test::author>("authors"); auto result = repo.attach<test::author>("authors");
REQUIRE(result); REQUIRE(result);
const auto obj = object::object::generate<test::book>(repo, "books"); const auto obj = object::object_generator::generate<test::book>(repo, "books");
REQUIRE(obj->name() == "books"); REQUIRE(obj->name() == "books");
REQUIRE(obj->alias().empty()); REQUIRE(obj->alias().empty());
REQUIRE(obj->attributes().size() == 4); REQUIRE(obj->attributes().size() == 4);