diff --git a/include/matador/object/attribute.hpp b/include/matador/object/attribute.hpp index a0bc573..08d08d0 100644 --- a/include/matador/object/attribute.hpp +++ b/include/matador/object/attribute.hpp @@ -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 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 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(0), attr, null_opt); -// } -// template <> -// attribute make_column(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(name, { size, utils::constraints::PrimaryKey }); -// } -// -// template <> -// attribute make_pk_column(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 &ref_column) { -// return {name, utils::data_type_traits::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 &ref_column) -// { -// return {name, utils::data_type_traits::type(0), 0, ref_column, { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL}; -// } -// -// template <> -// [[maybe_unused]] attribute make_fk_column(const std::string &name, size_t size, const std::shared_ptr &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(size), 0, -// std::make_shared(ref_column_name, ref_table_name, utils::data_type_traits::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(0), 0, -// std::make_shared(ref_column_name, utils::data_type_traits::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(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name); -// -// template <> -// [[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); - } #endif //QUERY_COLUMN_DEFINITION_HPP diff --git a/include/matador/object/attribute_generator.hpp b/include/matador/object/attribute_generator.hpp index 2e0875a..18fbb18 100644 --- a/include/matador/object/attribute_generator.hpp +++ b/include/matador/object/attribute_generator.hpp @@ -55,19 +55,19 @@ private: class attribute_generator final { private: - attribute_generator(std::vector &columns, const repository &repo, object &obj); + attribute_generator(std::vector &columns, const repository &repo, const std::shared_ptr& obj); public: ~attribute_generator() = default; template < class Type > - static std::vector generate(const repository &repo, object &obj) { + static std::vector generate(const repository &repo, const std::shared_ptr &obj) { Type t; return generate(t, repo, obj); } template < class Type > - static std::vector generate(const Type& t, const repository &repo, object &obj) { + static std::vector generate(const Type& t, const repository &repo, const std::shared_ptr &obj) { std::vector columns; attribute_generator gen(columns, repo, obj); access::process(gen, t); @@ -122,7 +122,7 @@ private: template 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::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 &columns_; const repository &repo_; - object &obj_; + const std::shared_ptr obj_; fk_attribute_generator fk_column_generator_; }; diff --git a/include/matador/object/object.hpp b/include/matador/object/object.hpp index 1f85fa0..a77a5b9 100644 --- a/include/matador/object/object.hpp +++ b/include/matador/object/object.hpp @@ -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& obj); [[nodiscard]] attribute* primary_key_attribute() const; [[nodiscard]] const utils::identifier& primary_key() const; diff --git a/include/matador/object/object_generator.hpp b/include/matador/object/object_generator.hpp index 6228ca9..e9926d6 100644 --- a/include/matador/object/object_generator.hpp +++ b/include/matador/object/object_generator.hpp @@ -88,7 +88,7 @@ public: void on_foreign_key(const char *id, Pointer &/*x*/) { const auto type = pk_type_determinator::determine(); auto &ref = object_->attributes_.emplace_back(id, type, utils::constraints::ForeignKey, null_option_type::NotNull); - ref.owner_ = object_.get(); + ref.owner_ = object_; } template static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {} @@ -101,7 +101,7 @@ private: template 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::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 fk_object(const std::type_index& ti) const; + [[nodiscard]] std::shared_ptr fk_object(const std::type_index& ti) const; private: repository &repo_; diff --git a/include/matador/object/restriction.hpp b/include/matador/object/restriction.hpp index 63f94ef..cd4d32e 100644 --- a/include/matador/object/restriction.hpp +++ b/include/matador/object/restriction.hpp @@ -43,28 +43,24 @@ private: std::shared_ptr owner_; std::shared_ptr 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 \ No newline at end of file diff --git a/include/matador/query/builder.hpp b/include/matador/query/builder.hpp index bbe0a87..3b9ccda 100644 --- a/include/matador/query/builder.hpp +++ b/include/matador/query/builder.hpp @@ -64,9 +64,9 @@ using Boolean = typed_data_type; using Varchar = sized_typed_data_type; using Blob = sized_typed_data_type>; -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; diff --git a/source/core/object/attribute.cpp b/source/core/object/attribute.cpp index ce9f2b1..f93e6d8 100644 --- a/source/core/object/attribute.cpp +++ b/source/core/object/attribute.cpp @@ -47,7 +47,7 @@ utils::basic_type attribute::type() const { return type_; } -object* attribute::owner() const { +std::shared_ptr attribute::owner() const { return owner_; } diff --git a/source/core/object/attribute_generator.cpp b/source/core/object/attribute_generator.cpp index 3543abb..1c0715e 100644 --- a/source/core/object/attribute_generator.cpp +++ b/source/core/object/attribute_generator.cpp @@ -4,14 +4,14 @@ namespace matador::object { -attribute_generator::attribute_generator(std::vector &columns, const repository &repo, object &obj) +attribute_generator::attribute_generator(std::vector &columns, const repository &repo, const std::shared_ptr &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_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); } } \ No newline at end of file diff --git a/source/core/object/constraints_generator.cpp b/source/core/object/constraints_generator.cpp index 3898b20..caf948c 100644 --- a/source/core/object/constraints_generator.cpp +++ b/source/core/object/constraints_generator.cpp @@ -11,7 +11,7 @@ constraints_generator::constraints_generator(std::list &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)); } diff --git a/source/core/object/object.cpp b/source/core/object/object.cpp index 6ab3b91..3be8977 100644 --- a/source/core/object/object.cpp +++ b/source/core/object/object.cpp @@ -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& 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 ) { diff --git a/source/core/object/object_generator.cpp b/source/core/object/object_generator.cpp index 94f3e37..6c824b1 100644 --- a/source/core/object/object_generator.cpp +++ b/source/core/object/object_generator.cpp @@ -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; diff --git a/source/core/object/restriction.cpp b/source/core/object/restriction.cpp index 2a32158..3be83d9 100644 --- a/source/core/object/restriction.cpp +++ b/source/core/object/restriction.cpp @@ -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)); +// } } diff --git a/test/core/object/AttributeGeneratorTest.cpp b/test/core/object/AttributeGeneratorTest.cpp index 9c62e5e..dbb34b9 100644 --- a/test/core/object/AttributeGeneratorTest.cpp +++ b/test/core/object/AttributeGeneratorTest.cpp @@ -18,7 +18,7 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener .and_then([&repo] { return repo.attach("categories"); }); REQUIRE(result); - object obj("products"); + auto obj = std::make_shared("products"); auto columns = attribute_generator::generate(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("optionals"); auto columns = attribute_generator::generate(repo, obj); const std::vector expected_columns = {