preparations for object_generator (generating objects with attributes and constraints)
This commit is contained in:
@@ -18,6 +18,7 @@ struct attribute_options {
|
||||
null_option_type null_option{null_option_type::NOT_NULL};
|
||||
int index{-1};
|
||||
};
|
||||
class object;
|
||||
|
||||
class attribute {
|
||||
public:
|
||||
@@ -85,7 +86,10 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
friend class object;
|
||||
|
||||
std::string name_;
|
||||
object *owner_{nullptr};
|
||||
std::string table_name_;
|
||||
attribute_options options_;
|
||||
utils::basic_type type_{utils::basic_type::type_null};
|
||||
@@ -94,22 +98,6 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class object {
|
||||
public:
|
||||
using iterator = std::vector<attribute>::iterator;
|
||||
using const_iterator = std::vector<attribute>::const_iterator;
|
||||
|
||||
private:
|
||||
std::vector<attribute> columns_;
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
};
|
||||
|
||||
class constraint {
|
||||
public:
|
||||
private:
|
||||
std::shared_ptr<attribute> column_;
|
||||
};
|
||||
/**
|
||||
* User defined literal to have a shortcut creating a column object
|
||||
* @param name Name of the column
|
||||
@@ -131,7 +119,7 @@ attribute make_column<std::string>(const std::string &name, utils::field_attribu
|
||||
template < typename Type >
|
||||
attribute make_pk_column(const std::string &name, size_t size = 0)
|
||||
{
|
||||
return make_column<Type>(name, { size, utils::constraints::PRIMARY_KEY });
|
||||
return make_column<Type>(name, { size, utils::constraints::PrimaryKey });
|
||||
}
|
||||
|
||||
template <>
|
||||
@@ -139,13 +127,13 @@ attribute make_pk_column<std::string>(const std::string &name, size_t size);
|
||||
|
||||
template < typename Type >
|
||||
attribute make_fk_column(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column) {
|
||||
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }};
|
||||
return {name, utils::data_type_traits<Type>::type(size), ref_column, { size, utils::constraints::ForeignKey }};
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, const std::shared_ptr<attribute> &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
|
||||
return {name, utils::data_type_traits<Type>::type(0), 0, ref_column, { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
|
||||
}
|
||||
|
||||
template <>
|
||||
@@ -155,8 +143,8 @@ template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(size), 0,
|
||||
std::make_shared<attribute>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::FOREIGN_KEY),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
std::make_shared<attribute>(ref_column_name, ref_table_name, utils::data_type_traits<Type>::type(size), utils::constraints::ForeignKey),
|
||||
{ 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
@@ -164,8 +152,8 @@ template < typename Type >
|
||||
[[maybe_unused]] attribute make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
return {
|
||||
name, utils::data_type_traits<Type>::type(0), 0,
|
||||
std::make_shared<attribute>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL
|
||||
std::make_shared<attribute>(ref_column_name, utils::data_type_traits<Type>::type(0), ref_table_name, attribute_options{utils::constraints::ForeignKey}),
|
||||
{ 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public:
|
||||
template<class Type>
|
||||
attribute generate(const char *id, Type &x, const std::shared_ptr<attribute> &ref_column) {
|
||||
access::process(*this, x);
|
||||
return attribute{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL};
|
||||
return attribute{id, type_, 0, ref_column, {utils::constraints::ForeignKey }, null_option_type::NOT_NULL};
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
@@ -133,7 +133,7 @@ private:
|
||||
|
||||
template<typename ValueType>
|
||||
void attribute_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||
on_attribute(id, x, { attr.size(), utils::constraints::PRIMARY_KEY });
|
||||
on_attribute(id, x, { attr.size(), utils::constraints::PrimaryKey });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef MATADOR_CONSTRAINT_HPP
|
||||
#define MATADOR_CONSTRAINT_HPP
|
||||
|
||||
#include "matador/utils/constraints.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class attribute;
|
||||
class constraint_builder;
|
||||
class object;
|
||||
|
||||
class constraint {
|
||||
public:
|
||||
constraint() = default;
|
||||
explicit constraint(std::string name);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
|
||||
private:
|
||||
friend class constraint_builder;
|
||||
friend class object;
|
||||
|
||||
std::string name_;
|
||||
attribute* attr_{nullptr};
|
||||
object *owner_{nullptr};
|
||||
utils::constraints options_{utils::constraints::None};
|
||||
std::string ref_table_name_{};
|
||||
std::string ref_column_name_{};
|
||||
};
|
||||
|
||||
class constraint_builder {
|
||||
public:
|
||||
constraint_builder& constraint(std::string name);
|
||||
constraint_builder& primary_key(std::string name);
|
||||
constraint_builder& foreign_key(std::string name);
|
||||
constraint_builder& references(std::string table, std::string column);
|
||||
|
||||
operator class constraint() const;
|
||||
|
||||
private:
|
||||
std::string constraint_name;
|
||||
utils::constraints options_{utils::constraints::None};
|
||||
std::string column_name;
|
||||
std::string ref_table_name;
|
||||
std::string ref_column_name;
|
||||
};
|
||||
|
||||
constraint_builder constraint(std::string name);
|
||||
|
||||
}
|
||||
#endif //MATADOR_CONSTRAINT_HPP
|
||||
@@ -0,0 +1,67 @@
|
||||
#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
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
result.value(),
|
||||
attribute_generator::generate(*obj, repo),
|
||||
std::move(creator)
|
||||
std::forward<CreatorFunc>(creator)
|
||||
);
|
||||
result.value()->info_ = std::move(info);
|
||||
|
||||
|
||||
@@ -17,12 +17,12 @@ class field {
|
||||
public:
|
||||
explicit field(std::string name);
|
||||
template<typename Type>
|
||||
field(std::string name, Type value, const utils::constraints type = utils::constraints::NONE, const size_t size = 0, const int index = -1)
|
||||
field(std::string name, Type value, const utils::constraints type = utils::constraints::None, const size_t size = 0, const int index = -1)
|
||||
: name_(std::move(name))
|
||||
, type_(type)
|
||||
, index_(index)
|
||||
, value_(value, size) {}
|
||||
field(std::string name, utils::basic_type dt, utils::constraints type = utils::constraints::NONE, size_t size = 0, int index = -1);
|
||||
field(std::string name, utils::basic_type dt, utils::constraints type = utils::constraints::None, size_t size = 0, int index = -1);
|
||||
field(const field &x) = default;
|
||||
field& operator=(const field &x) = default;
|
||||
field(field &&x) noexcept;
|
||||
@@ -71,7 +71,7 @@ private:
|
||||
friend class record;
|
||||
|
||||
std::string name_;
|
||||
utils::constraints type_{utils::constraints::NONE};
|
||||
utils::constraints type_{utils::constraints::None};
|
||||
int index_{-1};
|
||||
|
||||
utils::value value_;
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
namespace matador::utils {
|
||||
|
||||
enum class constraints : unsigned char {
|
||||
NONE = 0,
|
||||
INDEX = 1 << 0,
|
||||
UNIQUE = 1 << 1,
|
||||
PRIMARY_KEY = 1 << 2,
|
||||
FOREIGN_KEY = 1 << 3,
|
||||
DEFAULT = 1 << 4
|
||||
None = 0,
|
||||
Index = 1 << 0,
|
||||
Unique = 1 << 1,
|
||||
PrimaryKey = 1 << 2,
|
||||
ForeignKey = 1 << 3,
|
||||
Default = 1 << 4
|
||||
};
|
||||
|
||||
inline constraints operator|(constraints a, constraints b) { return static_cast<constraints>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b)); }
|
||||
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
|
||||
private:
|
||||
size_t size_ = 0;
|
||||
constraints options_ = constraints::NONE;
|
||||
constraints options_ = constraints::None;
|
||||
};
|
||||
|
||||
const field_attributes null_attributes {};
|
||||
|
||||
Reference in New Issue
Block a user