object generator progress
This commit is contained in:
@@ -19,6 +19,7 @@ struct attribute_options {
|
||||
int index{-1};
|
||||
};
|
||||
class object;
|
||||
class attribute_generator;
|
||||
|
||||
class attribute {
|
||||
public:
|
||||
@@ -87,6 +88,7 @@ public:
|
||||
|
||||
private:
|
||||
friend class object;
|
||||
friend class attribute_generator;
|
||||
|
||||
std::string name_;
|
||||
object *owner_{nullptr};
|
||||
|
||||
@@ -54,25 +54,25 @@ private:
|
||||
|
||||
class attribute_generator final {
|
||||
private:
|
||||
attribute_generator(std::vector<attribute> &columns, const repository &repo);
|
||||
attribute_generator(std::vector<attribute> &columns, const repository &repo, object &obj);
|
||||
|
||||
public:
|
||||
~attribute_generator() = default;
|
||||
|
||||
template < class Type >
|
||||
static std::vector<attribute> generate(const repository &repo) {
|
||||
static std::vector<attribute> generate(const repository &repo, object &obj) {
|
||||
std::vector<attribute> columns;
|
||||
attribute_generator gen(columns, repo);
|
||||
Type obj;
|
||||
access::process(gen, obj);
|
||||
attribute_generator gen(columns, repo, obj);
|
||||
Type t;
|
||||
access::process(gen, t);
|
||||
return columns;
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
static std::vector<attribute> generate(const Type& obj, const repository &repo) {
|
||||
static std::vector<attribute> generate(const Type& t, const repository &repo, object &obj) {
|
||||
std::vector<attribute> columns;
|
||||
attribute_generator gen(columns, repo);
|
||||
access::process(gen, obj);
|
||||
attribute_generator gen(columns, repo, obj);
|
||||
access::process(gen, t);
|
||||
return columns;
|
||||
}
|
||||
|
||||
@@ -127,6 +127,7 @@ private:
|
||||
size_t index_ = 0;
|
||||
std::vector<attribute> &columns_;
|
||||
const repository &repo_;
|
||||
object &obj_;
|
||||
|
||||
fk_attribute_generator fk_column_generator_;
|
||||
};
|
||||
@@ -138,13 +139,14 @@ void attribute_generator::on_primary_key(const char *id, ValueType &x, const uti
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
||||
columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NOT_NULL);
|
||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NOT_NULL);
|
||||
ref.owner_ = &obj_;
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr)
|
||||
{
|
||||
columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NULLABLE);
|
||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
|
||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<Type>::type(attr.size()), attr, null_option_type::NULLABLE);
|
||||
ref.owner_ = &obj_;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace matador::object {
|
||||
|
||||
class attribute;
|
||||
class constraint_builder;
|
||||
class constraint_generator;
|
||||
class object;
|
||||
|
||||
class constraint {
|
||||
@@ -20,6 +21,7 @@ public:
|
||||
|
||||
private:
|
||||
friend class constraint_builder;
|
||||
friend class constraints_generator;
|
||||
friend class object;
|
||||
|
||||
std::string name_;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#ifndef MATADOR_CONSTRAINTS_GENERATOR_HPP
|
||||
#define MATADOR_CONSTRAINTS_GENERATOR_HPP
|
||||
|
||||
#include "matador/object/constraint.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace matador::object {
|
||||
class object;
|
||||
class basic_object_info;
|
||||
class repository;
|
||||
|
||||
class constraints_generator final {
|
||||
private:
|
||||
constraints_generator(std::vector<class constraint> &constraints, const repository &repo, object &obj);
|
||||
|
||||
public:
|
||||
constraints_generator() = delete;
|
||||
|
||||
template < typename Type >
|
||||
static std::vector<class constraint> generate(const repository &repo, object &obj) {
|
||||
std::vector<class constraint> constraints;
|
||||
constraints_generator gen(constraints, repo, obj);
|
||||
Type t;
|
||||
access::process(gen, t);
|
||||
return constraints;
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
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:
|
||||
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_unique_constraint(const std::string& name) const;
|
||||
|
||||
[[nodiscard]] std::vector<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
||||
|
||||
private:
|
||||
std::vector<class constraint> &constraints_;
|
||||
const repository &repo_;
|
||||
object &obj_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_CONSTRAINTS_GENERATOR_HPP
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
||||
#define MATADOR_OBJECT_GENERATOR_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/constraints_generator.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class repository;
|
||||
|
||||
class object {
|
||||
public:
|
||||
explicit object(std::string name, std::string alias = "");
|
||||
|
||||
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 std::move(obj);
|
||||
}
|
||||
|
||||
static const attribute& create_attribute(std::string name, object& obj);
|
||||
|
||||
void add_attribute(attribute attr);
|
||||
void add_constraint(class constraint c);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const std::string& alias() const;
|
||||
|
||||
[[nodiscard]] bool has_attributes() const;
|
||||
[[nodiscard]] size_t attribute_count() const;
|
||||
[[nodiscard]] const std::vector<attribute>& attributes() const;
|
||||
|
||||
[[nodiscard]] bool has_constraints() const;
|
||||
[[nodiscard]] size_t constraint_count() const;
|
||||
[[nodiscard]] const std::vector<class constraint>& constraints() const;
|
||||
|
||||
private:
|
||||
friend class constraints_generator;
|
||||
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
|
||||
std::vector<attribute> attributes_;
|
||||
std::vector<class constraint> constraints_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_OBJECT_GENERATOR_HPP
|
||||
@@ -1,67 +0,0 @@
|
||||
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
||||
#define MATADOR_OBJECT_GENERATOR_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class object_generator;
|
||||
|
||||
class object {
|
||||
public:
|
||||
explicit object(std::string name, std::string alias = "");
|
||||
|
||||
void add_attribute(attribute attr) {
|
||||
auto &ref = attributes_.emplace_back(std::move(attr));
|
||||
ref.owner_ = this;
|
||||
}
|
||||
|
||||
static const attribute& create_attribute(std::string name, object& obj) {
|
||||
attribute attr{std::move(name)};
|
||||
attr.owner_ = &obj;
|
||||
return obj.attributes_.emplace_back(std::move(attr));
|
||||
}
|
||||
|
||||
void add_constraint(class constraint c) {
|
||||
auto &ref = constraints_.emplace_back(std::move(c));
|
||||
ref.owner_ = this;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::string& name() const { return name_; }
|
||||
[[nodiscard]] const std::string& alias() const { return alias_; }
|
||||
|
||||
[[nodiscard]] bool has_attributes() const { return attributes_.empty(); }
|
||||
[[nodiscard]] size_t attribute_count() const { return attributes_.size(); }
|
||||
[[nodiscard]] const std::vector<attribute>& attributes() const { return attributes_; }
|
||||
|
||||
[[nodiscard]] bool has_constraints() const { return constraints_.empty(); }
|
||||
[[nodiscard]] size_t constraint_count() const { return constraints_.size(); }
|
||||
[[nodiscard]] const std::vector<class constraint>& constraints() const { return constraints_; }
|
||||
|
||||
private:
|
||||
friend class object_generator;
|
||||
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
|
||||
std::vector<attribute> attributes_;
|
||||
std::vector<class constraint> constraints_;
|
||||
};
|
||||
|
||||
class object_generator {
|
||||
public:
|
||||
template<typename Type>
|
||||
object generate(std::string name, std::string alias = "") {
|
||||
object obj{std::move(name), std::move(alias)};
|
||||
attribute_generator::generate<Type>(*this);
|
||||
Type obj2;
|
||||
access::process(gen, obj2);
|
||||
return obj;
|
||||
}
|
||||
|
||||
private:
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_OBJECT_GENERATOR_HPP
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "matador/object/error_code.hpp"
|
||||
#include "matador/object/foreign_node_completer.hpp"
|
||||
#include "matador/object/object_info.hpp"
|
||||
#include "matador/object/relation_completer.hpp"
|
||||
#include "matador/object/repository_node.hpp"
|
||||
#include "matador/object/repository_node_iterator.hpp"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define SCHEMA_NODE_HPP
|
||||
|
||||
#include "matador/object/attribute_generator.hpp"
|
||||
#include "matador/object/object.hpp"
|
||||
#include "matador/object/object_info.hpp"
|
||||
#include "matador/object/primary_key_resolver.hpp"
|
||||
|
||||
@@ -22,8 +23,9 @@ 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);
|
||||
const auto attributes = attribute_generator::generate<Type>(repo, *obj);
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
node,
|
||||
attributes,
|
||||
@@ -45,9 +47,10 @@ public:
|
||||
}
|
||||
|
||||
auto obj = creator();
|
||||
object o(name);
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
result.value(),
|
||||
attribute_generator::generate(*obj, repo),
|
||||
attribute_generator::generate(*obj, repo, o),
|
||||
std::forward<CreatorFunc>(creator)
|
||||
);
|
||||
result.value()->info_ = std::move(info);
|
||||
|
||||
Reference in New Issue
Block a user