object generator progress
This commit is contained in:
parent
e67c0f39a3
commit
ede5a8c636
|
|
@ -19,6 +19,7 @@ struct attribute_options {
|
||||||
int index{-1};
|
int index{-1};
|
||||||
};
|
};
|
||||||
class object;
|
class object;
|
||||||
|
class attribute_generator;
|
||||||
|
|
||||||
class attribute {
|
class attribute {
|
||||||
public:
|
public:
|
||||||
|
|
@ -87,6 +88,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class object;
|
friend class object;
|
||||||
|
friend class attribute_generator;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
object *owner_{nullptr};
|
object *owner_{nullptr};
|
||||||
|
|
|
||||||
|
|
@ -54,25 +54,25 @@ private:
|
||||||
|
|
||||||
class attribute_generator final {
|
class attribute_generator final {
|
||||||
private:
|
private:
|
||||||
attribute_generator(std::vector<attribute> &columns, const repository &repo);
|
attribute_generator(std::vector<attribute> &columns, const repository &repo, object &obj);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~attribute_generator() = default;
|
~attribute_generator() = default;
|
||||||
|
|
||||||
template < class Type >
|
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;
|
std::vector<attribute> columns;
|
||||||
attribute_generator gen(columns, repo);
|
attribute_generator gen(columns, repo, obj);
|
||||||
Type obj;
|
Type t;
|
||||||
access::process(gen, obj);
|
access::process(gen, t);
|
||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < class Type >
|
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;
|
std::vector<attribute> columns;
|
||||||
attribute_generator gen(columns, repo);
|
attribute_generator gen(columns, repo, obj);
|
||||||
access::process(gen, obj);
|
access::process(gen, t);
|
||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,6 +127,7 @@ private:
|
||||||
size_t index_ = 0;
|
size_t index_ = 0;
|
||||||
std::vector<attribute> &columns_;
|
std::vector<attribute> &columns_;
|
||||||
const repository &repo_;
|
const repository &repo_;
|
||||||
|
object &obj_;
|
||||||
|
|
||||||
fk_attribute_generator fk_column_generator_;
|
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>
|
template<typename Type>
|
||||||
void attribute_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
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>
|
template<typename Type>
|
||||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr)
|
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);
|
||||||
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 attribute;
|
||||||
class constraint_builder;
|
class constraint_builder;
|
||||||
|
class constraint_generator;
|
||||||
class object;
|
class object;
|
||||||
|
|
||||||
class constraint {
|
class constraint {
|
||||||
|
|
@ -20,6 +21,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class constraint_builder;
|
friend class constraint_builder;
|
||||||
|
friend class constraints_generator;
|
||||||
friend class object;
|
friend class object;
|
||||||
|
|
||||||
std::string name_;
|
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/error_code.hpp"
|
||||||
#include "matador/object/foreign_node_completer.hpp"
|
#include "matador/object/foreign_node_completer.hpp"
|
||||||
|
#include "matador/object/object_info.hpp"
|
||||||
#include "matador/object/relation_completer.hpp"
|
#include "matador/object/relation_completer.hpp"
|
||||||
#include "matador/object/repository_node.hpp"
|
#include "matador/object/repository_node.hpp"
|
||||||
#include "matador/object/repository_node_iterator.hpp"
|
#include "matador/object/repository_node_iterator.hpp"
|
||||||
|
|
|
||||||
|
|
@ -2,6 +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_info.hpp"
|
#include "matador/object/object_info.hpp"
|
||||||
#include "matador/object/primary_key_resolver.hpp"
|
#include "matador/object/primary_key_resolver.hpp"
|
||||||
|
|
||||||
|
|
@ -22,8 +23,9 @@ 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 ref_column = determine_reference_column(typeid(Type), name, pk_info, repo);
|
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>>(
|
auto info = std::make_unique<object_info<Type>>(
|
||||||
node,
|
node,
|
||||||
attributes,
|
attributes,
|
||||||
|
|
@ -45,9 +47,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
auto obj = creator();
|
auto obj = creator();
|
||||||
|
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),
|
attribute_generator::generate(*obj, repo, o),
|
||||||
std::forward<CreatorFunc>(creator)
|
std::forward<CreatorFunc>(creator)
|
||||||
);
|
);
|
||||||
result.value()->info_ = std::move(info);
|
result.value()->info_ = std::move(info);
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,6 @@ public:
|
||||||
|
|
||||||
executable_query table(const table &tab, std::initializer_list<object::attribute> columns);
|
executable_query table(const table &tab, std::initializer_list<object::attribute> columns);
|
||||||
executable_query table(const query::table &tab, const std::vector<object::attribute> &columns);
|
executable_query table(const query::table &tab, const std::vector<object::attribute> &columns);
|
||||||
template<class Type>
|
|
||||||
executable_query table(const matador::query::table &tab, const object::repository &schema) {
|
|
||||||
return this->table(tab, object::attribute_generator::generate<Type>(schema));
|
|
||||||
}
|
|
||||||
executable_query schema(const std::string &schema_name);
|
executable_query schema(const std::string &schema_name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ public:
|
||||||
primary_key_attribute(primary_key_attribute &&) = default;
|
primary_key_attribute(primary_key_attribute &&) = default;
|
||||||
primary_key_attribute &operator=(const primary_key_attribute &) = default;
|
primary_key_attribute &operator=(const primary_key_attribute &) = default;
|
||||||
primary_key_attribute &operator=(primary_key_attribute &&) = default;
|
primary_key_attribute &operator=(primary_key_attribute &&) = default;
|
||||||
|
|
||||||
primary_key_attribute& operator=(size_t size);
|
primary_key_attribute& operator=(size_t size);
|
||||||
primary_key_attribute& operator=(generator_type generator);
|
primary_key_attribute& operator=(generator_type generator);
|
||||||
|
|
||||||
|
|
@ -37,7 +36,6 @@ public:
|
||||||
* @return Size of the field
|
* @return Size of the field
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] size_t size() const;
|
[[nodiscard]] size_t size() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the generator type of the field
|
* Returns the generator type of the field
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,12 @@ add_library(matador-core STATIC
|
||||||
../../include/matador/object/attribute_generator.hpp
|
../../include/matador/object/attribute_generator.hpp
|
||||||
../../include/matador/object/basic_object_info.hpp
|
../../include/matador/object/basic_object_info.hpp
|
||||||
../../include/matador/object/constraint.hpp
|
../../include/matador/object/constraint.hpp
|
||||||
|
../../include/matador/object/constraints_generator.hpp
|
||||||
../../include/matador/object/error_code.hpp
|
../../include/matador/object/error_code.hpp
|
||||||
../../include/matador/object/foreign_node_completer.hpp
|
../../include/matador/object/foreign_node_completer.hpp
|
||||||
../../include/matador/object/internal/shadow_repository.hpp
|
../../include/matador/object/internal/shadow_repository.hpp
|
||||||
../../include/matador/object/many_to_many_relation.hpp
|
../../include/matador/object/many_to_many_relation.hpp
|
||||||
../../include/matador/object/object_generator.hpp
|
../../include/matador/object/object.hpp
|
||||||
../../include/matador/object/object_info.hpp
|
../../include/matador/object/object_info.hpp
|
||||||
../../include/matador/object/object_proxy.hpp
|
../../include/matador/object/object_proxy.hpp
|
||||||
../../include/matador/object/object_ptr.hpp
|
../../include/matador/object/object_ptr.hpp
|
||||||
|
|
@ -80,6 +81,7 @@ add_library(matador-core STATIC
|
||||||
object/attribute_generator.cpp
|
object/attribute_generator.cpp
|
||||||
object/basic_object_info.cpp
|
object/basic_object_info.cpp
|
||||||
object/constraint.cpp
|
object/constraint.cpp
|
||||||
|
object/constraints_generator.cpp
|
||||||
object/error_code.cpp
|
object/error_code.cpp
|
||||||
object/foreign_node_completer.cpp
|
object/foreign_node_completer.cpp
|
||||||
object/internal/shadow_repository.cpp
|
object/internal/shadow_repository.cpp
|
||||||
|
|
@ -105,6 +107,7 @@ add_library(matador-core STATIC
|
||||||
utils/uuid.cpp
|
utils/uuid.cpp
|
||||||
utils/value.cpp
|
utils/value.cpp
|
||||||
utils/version.cpp
|
utils/version.cpp
|
||||||
|
object/object.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,10 @@
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
|
|
||||||
attribute_generator::attribute_generator(std::vector<attribute> &columns, const repository &repo)
|
attribute_generator::attribute_generator(std::vector<attribute> &columns, const repository &repo, object &obj)
|
||||||
: columns_(columns)
|
: columns_(columns)
|
||||||
, repo_(repo)
|
, repo_(repo)
|
||||||
|
, obj_(obj)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void attribute_generator::on_revision(const char *id, uint64_t &rev) {
|
void attribute_generator::on_revision(const char *id, uint64_t &rev) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
#include "matador/object/constraints_generator.hpp"
|
||||||
|
#include "matador/object/object.hpp"
|
||||||
|
#include "matador/object/repository.hpp"
|
||||||
|
|
||||||
|
namespace matador::object {
|
||||||
|
constraints_generator::constraints_generator(std::vector<class constraint> &constraints, const repository& repo, object &obj)
|
||||||
|
: constraints_(constraints)
|
||||||
|
, repo_(repo)
|
||||||
|
, obj_(obj) {}
|
||||||
|
|
||||||
|
void constraints_generator::create_pk_constraint(const std::string& name) const {
|
||||||
|
class constraint pk_constraint("PK_" + obj_.name());
|
||||||
|
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
||||||
|
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||||
|
pk_constraint.attr_ = &*pk_attr;
|
||||||
|
}
|
||||||
|
pk_constraint.owner_ = &obj_;
|
||||||
|
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.reference_column();
|
||||||
|
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_)) {
|
||||||
|
pk_constraint.attr_ = &*pk_attr;
|
||||||
|
}
|
||||||
|
pk_constraint.owner_ = &obj_;
|
||||||
|
pk_constraint.ref_column_name_ = pk_attribute->name();
|
||||||
|
pk_constraint.ref_table_name_ = pk_attribute->table_name();
|
||||||
|
constraints_.emplace_back(std::move(pk_constraint));
|
||||||
|
}
|
||||||
|
|
||||||
|
void constraints_generator::create_unique_constraint(const std::string& name) const {
|
||||||
|
class constraint pk_constraint("UK_" + obj_.name() + "_" + name);
|
||||||
|
pk_constraint.options_ |= utils::constraints::Unique;
|
||||||
|
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||||
|
pk_constraint.attr_ = &*pk_attr;
|
||||||
|
}
|
||||||
|
pk_constraint.owner_ = &obj_;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<attribute>::iterator constraints_generator::find_attribute_by_name(const std::string& name) const {
|
||||||
|
return std::find_if(std::begin(obj_.attributes_), std::end(obj_.attributes_), [&name](const attribute& elem) {
|
||||||
|
return elem.name() == name;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
#include "matador/object/object.hpp"
|
||||||
|
|
||||||
|
namespace matador::object {
|
||||||
|
object::object( std::string name, std::string alias )
|
||||||
|
: name_(std::move(name))
|
||||||
|
, alias_(std::move(alias)) {}
|
||||||
|
|
||||||
|
const attribute& object::create_attribute( std::string name, object& obj ) {
|
||||||
|
attribute attr{std::move(name)};
|
||||||
|
attr.owner_ = &obj;
|
||||||
|
return obj.attributes_.emplace_back(std::move(attr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void object::add_attribute( attribute attr ) {
|
||||||
|
auto &ref = attributes_.emplace_back(std::move(attr));
|
||||||
|
ref.owner_ = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void object::add_constraint( class constraint c ) {
|
||||||
|
auto &ref = constraints_.emplace_back(std::move(c));
|
||||||
|
ref.owner_ = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& object::name() const {
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& object::alias() const {
|
||||||
|
return alias_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool object::has_attributes() const {
|
||||||
|
return attributes_.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t object::attribute_count() const {
|
||||||
|
return attributes_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<attribute>& object::attributes() const {
|
||||||
|
return attributes_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool object::has_constraints() const {
|
||||||
|
return constraints_.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t object::constraint_count() const {
|
||||||
|
return constraints_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<class constraint>& object::constraints() const {
|
||||||
|
return constraints_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,22 +3,23 @@ CPMAddPackage("gh:catchorg/Catch2@3.7.1")
|
||||||
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
||||||
|
|
||||||
add_executable(CoreTests
|
add_executable(CoreTests
|
||||||
|
../backends/SchemaFixture.hpp
|
||||||
logger/LoggerTest.cpp
|
logger/LoggerTest.cpp
|
||||||
|
object/AttributeGeneratorTest.cpp
|
||||||
|
object/ObjectTest.cpp
|
||||||
|
object/PrimaryKeyResolverTest.cpp
|
||||||
|
object/SchemaTest.cpp
|
||||||
utils/BasicTypeToVisitorTest.cpp
|
utils/BasicTypeToVisitorTest.cpp
|
||||||
utils/ConvertTest.cpp
|
utils/ConvertTest.cpp
|
||||||
utils/DefaultTypeTraitsTest.cpp
|
utils/DefaultTypeTraitsTest.cpp
|
||||||
utils/DependencyInjectionTest.cpp
|
utils/DependencyInjectionTest.cpp
|
||||||
utils/IdentifierTest.cpp
|
|
||||||
utils/ResultTest.cpp
|
|
||||||
utils/FieldAttributeTest.cpp
|
utils/FieldAttributeTest.cpp
|
||||||
utils/VersionTest.cpp
|
utils/IdentifierTest.cpp
|
||||||
utils/ThreadPoolTest.cpp
|
|
||||||
utils/StringTest.cpp
|
|
||||||
object/AttributeDefinitionGeneratorTest.cpp
|
|
||||||
object/PrimaryKeyResolverTest.cpp
|
|
||||||
object/SchemaTest.cpp
|
|
||||||
utils/MessageBusTest.cpp
|
utils/MessageBusTest.cpp
|
||||||
../backends/SchemaFixture.hpp
|
utils/ResultTest.cpp
|
||||||
|
utils/StringTest.cpp
|
||||||
|
utils/ThreadPoolTest.cpp
|
||||||
|
utils/VersionTest.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(CoreTests matador-core Catch2::Catch2WithMain)
|
target_link_libraries(CoreTests matador-core Catch2::Catch2WithMain)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
#include "matador/object/attribute_generator.hpp"
|
#include "matador/object/attribute_generator.hpp"
|
||||||
|
#include "matador/object/object.hpp"
|
||||||
#include "matador/object/repository.hpp"
|
#include "matador/object/repository.hpp"
|
||||||
|
|
||||||
#include "../test/models/product.hpp"
|
#include "../test/models/product.hpp"
|
||||||
|
|
@ -17,7 +18,8 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
|
||||||
.and_then([&repo] { return repo.attach<matador::test::category>("categories"); });
|
.and_then([&repo] { return repo.attach<matador::test::category>("categories"); });
|
||||||
REQUIRE(result);
|
REQUIRE(result);
|
||||||
|
|
||||||
auto columns = attribute_generator::generate<matador::test::product>(repo);
|
object obj("products");
|
||||||
|
auto columns = attribute_generator::generate<matador::test::product>(repo, obj);
|
||||||
|
|
||||||
const std::vector expected_columns = {
|
const std::vector expected_columns = {
|
||||||
attribute{"product_name", basic_type::type_varchar, constraints::PrimaryKey, null_option_type::NOT_NULL },
|
attribute{"product_name", basic_type::type_varchar, constraints::PrimaryKey, null_option_type::NOT_NULL },
|
||||||
|
|
@ -43,7 +45,8 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
|
||||||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
||||||
repository repo("main");
|
repository repo("main");
|
||||||
|
|
||||||
auto columns = attribute_generator::generate<matador::test::optional>(repo);
|
object obj("optionals");
|
||||||
|
auto columns = attribute_generator::generate<matador::test::optional>(repo, obj);
|
||||||
|
|
||||||
const std::vector expected_columns = {
|
const std::vector expected_columns = {
|
||||||
attribute{"id", basic_type::type_uint32, constraints::PrimaryKey, null_option_type::NOT_NULL },
|
attribute{"id", basic_type::type_uint32, constraints::PrimaryKey, null_option_type::NOT_NULL },
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "matador/object/object.hpp"
|
||||||
|
#include "matador/object/repository.hpp"
|
||||||
|
|
||||||
|
#include "../test/models/author.hpp"
|
||||||
|
#include "../test/models/book.hpp"
|
||||||
|
|
||||||
|
TEST_CASE("Generate object from type", "[object][generate]") {
|
||||||
|
using namespace matador;
|
||||||
|
object::repository repo;
|
||||||
|
auto result = repo.attach<test::author>("authors");
|
||||||
|
REQUIRE(result);
|
||||||
|
|
||||||
|
const auto obj = object::object::generate<test::book>(repo, "books");
|
||||||
|
REQUIRE(obj->name() == "books");
|
||||||
|
REQUIRE(obj->alias().empty());
|
||||||
|
REQUIRE(obj->attributes().size() == 4);
|
||||||
|
REQUIRE(obj->constraints().size() == 2);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue