object generation
This commit is contained in:
parent
f47b3bb87f
commit
19b7044773
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "matador/object/basic_object_info.hpp"
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace matador::object {
|
||||
class repository_node;
|
||||
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
#include "matador/object/object.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
constraints_generator::constraints_generator(std::vector<class constraint> &constraints, const repository& repo, object &obj)
|
||||
: constraints_(constraints)
|
||||
|
|
@ -18,8 +20,12 @@ void constraints_generator::create_pk_constraint(const std::string& name) const
|
|||
constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void constraints_generator::create_fk_constraint(const std::string& name, const basic_object_info& info) const {
|
||||
const auto *pk_attribute = info.primary_key_attribute();
|
||||
void constraints_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_" + obj_.name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||
|
|
|
|||
|
|
@ -53,6 +53,15 @@ bool repository::contains( const std::type_index& index ) const {
|
|||
return nodes_by_type_.count(index) > 0;
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> repository::basic_info(const std::type_index &ti) const {
|
||||
auto result = find_node(ti);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
||||
}
|
||||
|
||||
utils::result<basic_object_info_ref, utils::error> repository::basic_info( const std::string& name ) const {
|
||||
auto result = find_node(name);
|
||||
if (!result) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue