object generation

This commit is contained in:
2025-11-30 22:03:48 +01:00
parent f47b3bb87f
commit 19b7044773
8 changed files with 40 additions and 18 deletions
@@ -25,9 +25,9 @@ public:
fk_attribute_generator() = default;
template<class Type>
attribute generate(const char *id, Type &x, const std::shared_ptr<attribute> &ref_column) {
attribute generate(const char *id, Type &x) {
access::process(*this, x);
return attribute{id, type_, 0, ref_column, {utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
return attribute{id, type_, {utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
}
template<typename ValueType>
@@ -106,9 +106,9 @@ public:
}
if (x.empty()) {
typename Pointer::value_type temp_val;
columns_.push_back(fk_column_generator_.generate(id, temp_val, ref_column));
columns_.push_back(fk_column_generator_.generate(id, temp_val));
} else {
columns_.push_back(fk_column_generator_.generate(id, *x, ref_column));
columns_.push_back(fk_column_generator_.generate(id, *x));
}
}
template<class ContainerType>
@@ -7,6 +7,7 @@
#include "matador/utils/field_attributes.hpp"
#include "matador/utils/primary_key_attribute.hpp"
#include <typeindex>
#include <vector>
namespace matador::object {
@@ -31,24 +32,20 @@ public:
}
template < class Type >
void on_primary_key(const char *id, Type &/*x*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
void on_primary_key(const char *id, Type &/*x*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
create_pk_constraint(id);
}
static 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) {
void on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr = utils::null_attributes) {
if (utils::is_constraint_set(attr.options() ,utils::constraints::Unique)) {
create_unique_constraint(id);
}
}
template<class Pointer>
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
const auto result = repo_.info<typename Pointer::value_type>();
if (!result) {
return;
}
create_fk_constraint(id, *result);
void on_belongs_to(const char *id, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
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*/) {}
@@ -61,7 +58,7 @@ public:
private:
void create_pk_constraint(const std::string& name) const;
void create_fk_constraint(const std::string& name, const basic_object_info& info) 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;
+9 -1
View File
@@ -21,7 +21,15 @@ public:
auto obj = std::make_unique<object>(std::move(name), std::move(alias));
obj->attributes_ = std::move(attribute_generator::generate<Type>(repo, *obj));
obj->constraints_ = std::move(constraints_generator::generate<Type>(repo, *obj));
return std::move(obj);
return obj;
}
template<typename Type>
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));
obj->attributes_ = std::move(attribute_generator::generate<Type>(repo, *obj));
obj->constraints_ = std::move(constraints_generator::generate<Type>(repo, *obj));
return obj;
}
static const attribute& create_attribute(std::string name, object& obj);
+2
View File
@@ -3,6 +3,8 @@
#include "matador/object/basic_object_info.hpp"
#include <functional>
namespace matador::object {
class repository_node;
+2
View File
@@ -139,6 +139,8 @@ public:
return utils::ok(result.value()->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>
@@ -24,8 +24,6 @@ public:
primary_key_resolver resolver;
auto pk_info = resolver.resolve<Type>();
auto obj = object::generate<Type>(repo, name);
auto ref_column = determine_reference_column(typeid(Type), name, pk_info, repo);
const auto attributes = attribute_generator::generate<Type>(repo, *obj);
auto info = std::make_unique<object_info<Type>>(
node,
std::move(obj),