removed string from restriction
This commit is contained in:
parent
c4f8731262
commit
9b7b74524f
|
|
@ -39,7 +39,7 @@ public:
|
|||
[[nodiscard]] utils::field_attributes& attributes();
|
||||
[[nodiscard]] bool is_nullable() const;
|
||||
[[nodiscard]] utils::basic_type type() const;
|
||||
[[nodiscard]] object* owner() const;
|
||||
[[nodiscard]] std::shared_ptr<object> owner() const;
|
||||
void change_type(utils::basic_type type, const utils::field_attributes &attr = utils::null_attributes);
|
||||
template < typename Type >
|
||||
void change_type(const utils::field_attributes &attr = utils::null_attributes) {
|
||||
|
|
@ -69,78 +69,12 @@ private:
|
|||
friend class object_generator;
|
||||
|
||||
std::string name_;
|
||||
object *owner_{nullptr};
|
||||
std::shared_ptr<object> owner_;
|
||||
utils::basic_type type_{utils::basic_type::Null};
|
||||
utils::field_attributes options_{};
|
||||
null_option_type null_option_{null_option_type::NotNull};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* User defined literal to have a shortcut creating a column object
|
||||
* @param name Name of the column
|
||||
* @param type
|
||||
* @param attr Length of the column name
|
||||
* @param null_opt
|
||||
* @return A column object with a given name
|
||||
*/
|
||||
// attribute make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL);
|
||||
//
|
||||
// template < typename Type >
|
||||
// attribute make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL)
|
||||
// {
|
||||
// return make_column(name, utils::data_type_traits<Type>::type(0), attr, null_opt);
|
||||
// }
|
||||
// template <>
|
||||
// attribute make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option_type null_opt);
|
||||
//
|
||||
// template < typename Type >
|
||||
// attribute make_pk_column(const std::string &name, size_t size = 0)
|
||||
// {
|
||||
// return make_column<Type>(name, { size, utils::constraints::PrimaryKey });
|
||||
// }
|
||||
//
|
||||
// template <>
|
||||
// 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::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::ForeignKey }, null_option_type::NOT_NULL};
|
||||
// }
|
||||
//
|
||||
// template <>
|
||||
// [[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute> &ref_column);
|
||||
//
|
||||
// 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::ForeignKey),
|
||||
// { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// 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::ForeignKey}),
|
||||
// { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// template <>
|
||||
// [[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
//
|
||||
// template <>
|
||||
// [[maybe_unused]] attribute make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name);
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_COLUMN_DEFINITION_HPP
|
||||
|
|
|
|||
|
|
@ -55,19 +55,19 @@ private:
|
|||
|
||||
class attribute_generator final {
|
||||
private:
|
||||
attribute_generator(std::vector<attribute> &columns, const repository &repo, object &obj);
|
||||
attribute_generator(std::vector<attribute> &columns, const repository &repo, const std::shared_ptr<object>& obj);
|
||||
|
||||
public:
|
||||
~attribute_generator() = default;
|
||||
|
||||
template < class Type >
|
||||
static std::vector<attribute> generate(const repository &repo, object &obj) {
|
||||
static std::vector<attribute> generate(const repository &repo, const std::shared_ptr<object> &obj) {
|
||||
Type t;
|
||||
return generate(t, repo, obj);
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
static std::vector<attribute> generate(const Type& t, const repository &repo, object &obj) {
|
||||
static std::vector<attribute> generate(const Type& t, const repository &repo, const std::shared_ptr<object> &obj) {
|
||||
std::vector<attribute> columns;
|
||||
attribute_generator gen(columns, repo, obj);
|
||||
access::process(gen, t);
|
||||
|
|
@ -122,7 +122,7 @@ private:
|
|||
template<typename ValueType>
|
||||
attribute &emplace_attribute(const char *id, const utils::field_attributes& attr, null_option_type null_option) {
|
||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<ValueType>::type(attr.size()), attr, null_option);
|
||||
ref.owner_ = &obj_;
|
||||
ref.owner_ = obj_;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ private:
|
|||
size_t index_ = 0;
|
||||
std::vector<attribute> &columns_;
|
||||
const repository &repo_;
|
||||
object &obj_;
|
||||
const std::shared_ptr<object> obj_;
|
||||
|
||||
fk_attribute_generator fk_column_generator_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -17,10 +17,7 @@ class object {
|
|||
public:
|
||||
explicit object(std::string name, std::string alias = "");
|
||||
|
||||
static const attribute& create_attribute(std::string name, object& obj);
|
||||
|
||||
// void add_attribute(attribute attr);
|
||||
// void add_constraint(class constraint c);
|
||||
static const attribute& create_attribute(std::string name, const std::shared_ptr<object>& obj);
|
||||
|
||||
[[nodiscard]] attribute* primary_key_attribute() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ public:
|
|||
void on_foreign_key(const char *id, Pointer &/*x*/) {
|
||||
const auto type = pk_type_determinator::determine<typename Pointer::value_type>();
|
||||
auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NotNull);
|
||||
ref.owner_ = object_.get();
|
||||
ref.owner_ = object_;
|
||||
}
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
|
@ -101,7 +101,7 @@ private:
|
|||
template<typename ValueType>
|
||||
attribute &emplace_attribute(const char *id, const utils::field_attributes& attr, null_option_type null_option) {
|
||||
auto &ref = object_->attributes_.emplace_back(id, utils::data_type_traits<ValueType>::type(attr.size()), attr, null_option);
|
||||
ref.owner_ = object_.get();
|
||||
ref.owner_ = object_;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
|
@ -113,7 +113,7 @@ private:
|
|||
|
||||
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
||||
|
||||
std::shared_ptr<class object> fk_object(const std::type_index& ti) const;
|
||||
[[nodiscard]] std::shared_ptr<class object> fk_object(const std::type_index& ti) const;
|
||||
|
||||
private:
|
||||
repository &repo_;
|
||||
|
|
|
|||
|
|
@ -43,28 +43,24 @@ private:
|
|||
std::shared_ptr<object> owner_;
|
||||
std::shared_ptr<object> reference_;
|
||||
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 restriction() 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);
|
||||
// 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 restriction() const;
|
||||
//
|
||||
// private:
|
||||
// std::string constraint_name;
|
||||
// utils::constraints options_{utils::constraints::None};
|
||||
// std::string column_name;
|
||||
// };
|
||||
//
|
||||
// constraint_builder constraint(std::string name);
|
||||
|
||||
}
|
||||
#endif //MATADOR_CONSTRAINT_HPP
|
||||
|
|
@ -64,9 +64,9 @@ using Boolean = typed_data_type<bool>;
|
|||
using Varchar = sized_typed_data_type<std::string>;
|
||||
using Blob = sized_typed_data_type<std::vector<std::byte>>;
|
||||
|
||||
class restriction {
|
||||
class constraint {
|
||||
public:
|
||||
explicit restriction(std::string name)
|
||||
explicit constraint(std::string name)
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
|
|
@ -126,7 +126,7 @@ public:
|
|||
constraint_builder& references(std::string table, std::string column);
|
||||
|
||||
// ReSharper disable once CppNonExplicitConversionOperator
|
||||
operator object::restriction() const; // NOLINT(*-explicit-constructor)
|
||||
operator object::constraint() const; // NOLINT(*-explicit-constructor)
|
||||
|
||||
private:
|
||||
std::string constraint_name;
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ utils::basic_type attribute::type() const {
|
|||
return type_;
|
||||
}
|
||||
|
||||
object* attribute::owner() const {
|
||||
std::shared_ptr<object> attribute::owner() const {
|
||||
return owner_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,14 +4,14 @@
|
|||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_generator::attribute_generator(std::vector<attribute> &columns, const repository &repo, object &obj)
|
||||
attribute_generator::attribute_generator(std::vector<attribute> &columns, const repository &repo, const std::shared_ptr<object> &obj)
|
||||
: columns_(columns)
|
||||
, repo_(repo)
|
||||
, obj_(obj)
|
||||
{}
|
||||
|
||||
void attribute_generator::on_revision(const char *id, uint64_t &rev) {
|
||||
on_attribute(id, rev);
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
utils::result<attribute*, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
|
||||
|
|
@ -23,7 +23,7 @@ void attribute_generator::insert_missing_reference_column(const std::type_index&
|
|||
}
|
||||
|
||||
void attribute_generator::prepare_primary_key(attribute& ref, utils::identifier &&pk) const {
|
||||
obj_.pk_attribute_ = &ref;
|
||||
obj_.pk_identifier_ = std::move(pk);
|
||||
obj_->pk_attribute_ = &ref;
|
||||
obj_->pk_identifier_ = std::move(pk);
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ constraints_generator::constraints_generator(std::list<class restriction> &const
|
|||
, obj_(obj) {}
|
||||
|
||||
void constraints_generator::create_pk_constraint(const std::string& name) const {
|
||||
class restriction pk_constraint("PK_" + obj_->name());
|
||||
restriction 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;
|
||||
|
|
@ -25,15 +25,12 @@ void constraints_generator::create_fk_constraint(const std::type_index& ti, cons
|
|||
if (!result) {
|
||||
return;
|
||||
}
|
||||
const auto *pk_attribute = result.value().get().primary_key_attribute();
|
||||
class restriction pk_constraint("FK_" + obj_->name() + "_" + name);
|
||||
restriction 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->owner() ? pk_attribute->owner()->name() : "";
|
||||
constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ 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 ) {
|
||||
const attribute& object::create_attribute(std::string name, const std::shared_ptr<object>& obj) {
|
||||
attribute attr{std::move(name)};
|
||||
attr.owner_ = &obj;
|
||||
return obj.attributes_.emplace_back(std::move(attr));
|
||||
attr.owner_ = obj;
|
||||
return obj->attributes_.emplace_back(std::move(attr));
|
||||
}
|
||||
|
||||
// void object::add_attribute( attribute attr ) {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ void object_generator::on_revision(const char* id, uint64_t& rev) {
|
|||
}
|
||||
|
||||
void object_generator::create_pk_constraint(const std::string& name) const {
|
||||
class restriction pk_constraint("PK_" + object_->name());
|
||||
restriction pk_constraint("PK_" + object_->name());
|
||||
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
|
|
@ -25,23 +25,18 @@ void object_generator::create_pk_constraint(const std::string& name) const {
|
|||
|
||||
void object_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const {
|
||||
const auto obj = fk_object(ti);
|
||||
const auto *pk_attribute = obj->primary_key_attribute();
|
||||
class restriction pk_constraint("FK_" + object_->name() + "_" + name);
|
||||
restriction pk_constraint("FK_" + object_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_;
|
||||
pk_constraint.reference_ = obj;
|
||||
if (pk_attribute) {
|
||||
pk_constraint.ref_column_name_ = pk_attribute->name();
|
||||
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
|
||||
}
|
||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void object_generator::create_unique_constraint(const std::string& name) const {
|
||||
class restriction pk_constraint("UK_" + object_->name() + "_" + name);
|
||||
restriction pk_constraint("UK_" + object_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::Unique;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "matador/object/restriction.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/object.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
restriction::restriction(std::string name)
|
||||
|
|
@ -46,11 +47,11 @@ bool restriction::is_unique_constraint() const {
|
|||
}
|
||||
|
||||
const std::string& restriction::ref_table_name() const {
|
||||
return ref_table_name_;
|
||||
return reference_->name();
|
||||
}
|
||||
|
||||
const std::string& restriction::ref_column_name() const {
|
||||
return ref_column_name_;
|
||||
return reference_->primary_key_attribute()->name();
|
||||
}
|
||||
|
||||
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
||||
|
|
@ -58,42 +59,42 @@ std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
|||
return os;
|
||||
}
|
||||
|
||||
constraint_builder & constraint_builder::constraint(std::string name) {
|
||||
constraint_name = std::move(name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder & constraint_builder::primary_key(std::string name) {
|
||||
column_name = std::move(name);
|
||||
options_ |= utils::constraints::PrimaryKey;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder & constraint_builder::foreign_key(std::string name) {
|
||||
column_name = std::move(name);
|
||||
options_ |= utils::constraints::ForeignKey;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder & constraint_builder::references(std::string table, std::string column) {
|
||||
ref_table_name = std::move(table);
|
||||
ref_column_name = std::move(column);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder::operator class restriction() const {
|
||||
class restriction c;
|
||||
c.name_ = constraint_name;
|
||||
c.attr_ = column_name;
|
||||
c.options_ = options_;
|
||||
c.ref_column_name_ = ref_column_name;
|
||||
c.ref_table_name_ = ref_table_name;
|
||||
return c;
|
||||
}
|
||||
|
||||
constraint_builder constraint(std::string name) {
|
||||
constraint_builder builder;
|
||||
return builder.constraint(std::move(name));
|
||||
}
|
||||
// constraint_builder & constraint_builder::constraint(std::string name) {
|
||||
// constraint_name = std::move(name);
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder & constraint_builder::primary_key(std::string name) {
|
||||
// column_name = std::move(name);
|
||||
// options_ |= utils::constraints::PrimaryKey;
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder & constraint_builder::foreign_key(std::string name) {
|
||||
// column_name = std::move(name);
|
||||
// options_ |= utils::constraints::ForeignKey;
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder & constraint_builder::references(std::string table, std::string column) {
|
||||
// ref_table_name = std::move(table);
|
||||
// ref_column_name = std::move(column);
|
||||
//
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// constraint_builder::operator class restriction() const {
|
||||
// class restriction c;
|
||||
// c.name_ = constraint_name;
|
||||
// c.attr_ = column_name;
|
||||
// c.options_ = options_;
|
||||
// c.ref_column_name_ = ref_column_name;
|
||||
// c.ref_table_name_ = ref_table_name;
|
||||
// return c;
|
||||
// }
|
||||
//
|
||||
// constraint_builder constraint(std::string name) {
|
||||
// constraint_builder builder;
|
||||
// return builder.constraint(std::move(name));
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
|
|||
.and_then([&repo] { return repo.attach<matador::test::category>("categories"); });
|
||||
REQUIRE(result);
|
||||
|
||||
object obj("products");
|
||||
auto obj = std::make_shared<object>("products");
|
||||
auto columns = attribute_generator::generate<matador::test::product>(repo, obj);
|
||||
|
||||
const std::vector expected_columns = {
|
||||
|
|
@ -45,7 +45,7 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener
|
|||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
||||
repository repo("main");
|
||||
|
||||
object obj("optionals");
|
||||
auto obj = std::make_shared<object>("optionals");
|
||||
auto columns = attribute_generator::generate<matador::test::optional>(repo, obj);
|
||||
|
||||
const std::vector expected_columns = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue