diff --git a/include/matador/object/basic_object_info.hpp b/include/matador/object/basic_object_info.hpp index 9f7deae..5f1a9ee 100644 --- a/include/matador/object/basic_object_info.hpp +++ b/include/matador/object/basic_object_info.hpp @@ -28,7 +28,7 @@ public: [[nodiscard]] std::string name() const; [[nodiscard]] std::shared_ptr object() const; [[nodiscard]] const std::vector& attributes() const; - [[nodiscard]] const std::vector& constraints() const; + [[nodiscard]] const std::list& constraints() const; [[nodiscard]] bool has_primary_key() const; [[nodiscard]] const utils::identifier& primary_key() const; diff --git a/include/matador/object/object.hpp b/include/matador/object/object.hpp index 3575b7a..448384f 100644 --- a/include/matador/object/object.hpp +++ b/include/matador/object/object.hpp @@ -35,7 +35,7 @@ public: [[nodiscard]] bool has_constraints() const; [[nodiscard]] size_t constraint_count() const; - [[nodiscard]] const std::vector& constraints() const; + [[nodiscard]] const std::list& constraints() const; friend std::ostream& operator<<(std::ostream& os, const object& obj); @@ -50,7 +50,7 @@ private: utils::identifier pk_identifier_; std::vector attributes_; - std::vector constraints_; + std::list constraints_; }; } #endif //MATADOR_OBJECT_HPP \ No newline at end of file diff --git a/include/matador/object/object_generator.hpp b/include/matador/object/object_generator.hpp index adf2774..a2b98bd 100644 --- a/include/matador/object/object_generator.hpp +++ b/include/matador/object/object_generator.hpp @@ -170,7 +170,7 @@ void object_generator::create_fk_constraint(const std::string& name) const { return; } const auto obj = fk_object(); - restriction pk_constraint(*pk_attr); + restriction pk_constraint(pk_attr->index()); pk_constraint.options_ |= utils::constraints::ForeignKey; pk_constraint.owner_ = object_; pk_constraint.reference_ = obj; diff --git a/include/matador/object/restriction.hpp b/include/matador/object/restriction.hpp index b1e2da3..492978b 100644 --- a/include/matador/object/restriction.hpp +++ b/include/matador/object/restriction.hpp @@ -17,17 +17,17 @@ class object; class restriction { public: - explicit restriction(const class attribute& attr); + explicit restriction(size_t attr_index); - [[nodiscard]] const class attribute& attribute() const; + [[nodiscard]] size_t attribute_index() const; [[nodiscard]] std::string column_name() const; + [[nodiscard]] utils::constraints options() const; [[nodiscard]] std::shared_ptr owner() const; [[nodiscard]] bool is_primary_key_constraint() const; [[nodiscard]] bool is_foreign_key_constraint() const; [[nodiscard]] bool is_unique_constraint() const; [[nodiscard]] std::string ref_table_name() const; [[nodiscard]] std::string ref_column_name() const; - friend std::ostream& operator<<(std::ostream& os, const restriction& c); [[nodiscard]] std::string type_string() const; @@ -37,7 +37,7 @@ private: friend class object_generator; friend class object; - const class attribute& attr_; + const size_t index_; std::weak_ptr owner_; std::weak_ptr reference_; utils::constraints options_{utils::constraints::None}; diff --git a/source/core/object/basic_object_info.cpp b/source/core/object/basic_object_info.cpp index 54601f8..53d8ca4 100644 --- a/source/core/object/basic_object_info.cpp +++ b/source/core/object/basic_object_info.cpp @@ -25,7 +25,7 @@ const std::vector& basic_object_info::attributes() const { return object_->attributes(); } -const std::vector& basic_object_info::constraints() const { +const std::list& basic_object_info::constraints() const { return object_->constraints(); } diff --git a/source/core/object/object.cpp b/source/core/object/object.cpp index e31ba17..2874c2d 100644 --- a/source/core/object/object.cpp +++ b/source/core/object/object.cpp @@ -62,7 +62,7 @@ size_t object::constraint_count() const { return constraints_.size(); } -const std::vector& object::constraints() const { +const std::list& object::constraints() const { return constraints_; } diff --git a/source/core/object/object_generator.cpp b/source/core/object/object_generator.cpp index 737c10f..7e9ebf6 100644 --- a/source/core/object/object_generator.cpp +++ b/source/core/object/object_generator.cpp @@ -30,7 +30,7 @@ void object_generator::create_pk_constraint(const std::string &name) const { if (pk_attr == std::end(object_->attributes_)) { return; } - restriction pk_constraint(*pk_attr); + restriction pk_constraint(pk_attr->index()); pk_constraint.options_ |= utils::constraints::PrimaryKey; pk_constraint.owner_ = object_; object_->constraints_.emplace_back(std::move(pk_constraint)); @@ -41,7 +41,7 @@ void object_generator::create_unique_constraint(const std::string &name) const { if (pk_attr == std::end(object_->attributes_)) { return; } - restriction pk_constraint(*pk_attr); + restriction pk_constraint(pk_attr->index()); pk_constraint.options_ |= utils::constraints::Unique; pk_constraint.owner_ = object_; } diff --git a/source/core/object/restriction.cpp b/source/core/object/restriction.cpp index 496bb17..f6e4cf8 100644 --- a/source/core/object/restriction.cpp +++ b/source/core/object/restriction.cpp @@ -4,16 +4,23 @@ #include "matador/object/object.hpp" namespace matador::object { -restriction::restriction(const class attribute& attr) -: attr_(attr) {} +restriction::restriction(const size_t attr_index) +: index_(attr_index) {} -const class attribute& restriction::attribute() const { - return attr_; +size_t restriction::attribute_index() const { + return index_; } std::string restriction::column_name() const { - return attr_.name(); + const auto o = owner_.lock(); + return o ? o->attributes().at(index_).name() : ""; } + +utils::constraints restriction::options() const { + const auto o = owner_.lock(); + return o ? o->attributes().at(index_).attributes().options() : utils::constraints::None; +} + std::shared_ptr restriction::owner() const { return owner_.lock(); } diff --git a/source/orm/query/intermediates/query_alter_table_intermediate.cpp b/source/orm/query/intermediates/query_alter_table_intermediate.cpp index 677b4f4..23c0dd7 100644 --- a/source/orm/query/intermediates/query_alter_table_intermediate.cpp +++ b/source/orm/query/intermediates/query_alter_table_intermediate.cpp @@ -33,7 +33,7 @@ query_add_foreign_key_constraint_intermediate query_add_key_constraint_intermedi executable_query query_alter_table_intermediate::add_constraint(const object::restriction &rest) { context_->parts.push_back(std::make_unique( - table_constraint{rest.column_name(), rest.owner()->name(), rest.attribute().attributes().options(), rest.ref_table_name(), rest.ref_column_name()} + table_constraint{rest.column_name(), rest.owner()->name(), rest.options(), rest.ref_table_name(), rest.ref_column_name()} )); return {context_}; @@ -47,7 +47,7 @@ query_add_key_constraint_intermediate query_alter_table_intermediate::add_constr executable_query query_alter_table_intermediate::drop_constraint(const object::restriction &rest) { context_->parts.push_back(std::make_unique( - table_constraint{rest.column_name(), rest.owner()->name(), rest.attribute().attributes().options(), rest.ref_table_name(), rest.ref_column_name()}) + table_constraint{rest.column_name(), rest.owner()->name(), rest.options(), rest.ref_table_name(), rest.ref_column_name()}) ); return {context_}; } diff --git a/source/orm/query/intermediates/query_create_intermediate.cpp b/source/orm/query/intermediates/query_create_intermediate.cpp index c0a6f5a..69b11a4 100644 --- a/source/orm/query/intermediates/query_create_intermediate.cpp +++ b/source/orm/query/intermediates/query_create_intermediate.cpp @@ -46,11 +46,11 @@ executable_query query_create_table_columns_intermediate::constraints(const std: std::list constraints; for (const auto& restr : restrictions) { if (restr.is_primary_key_constraint()) { - constraints.emplace_back(restr.column_name(), restr.owner()->name(), restr.attribute().attributes().options()); + constraints.emplace_back(restr.column_name(), restr.owner()->name(), restr.options()); } else if (restr.is_foreign_key_constraint()) { - constraints.emplace_back(restr.column_name(), restr.owner()->name(), restr.attribute().attributes().options(), restr.ref_table_name(), restr.ref_column_name()); + constraints.emplace_back(restr.column_name(), restr.owner()->name(), restr.options(), restr.ref_table_name(), restr.ref_column_name()); } else if (restr.is_unique_constraint()) { - constraints.emplace_back(restr.column_name(), restr.owner()->name(), restr.attribute().attributes().options()); + constraints.emplace_back(restr.column_name(), restr.owner()->name(), restr.options()); } } context_->parts.push_back(std::make_unique(constraints));