diff --git a/include/matador/object/restriction.hpp b/include/matador/object/restriction.hpp index c8b0586..98dc8d4 100644 --- a/include/matador/object/restriction.hpp +++ b/include/matador/object/restriction.hpp @@ -17,11 +17,9 @@ class object; class restriction { public: - restriction() = default; - explicit restriction(std::string name); + explicit restriction(const class attribute& attr); - [[nodiscard]] std::string name() const; - [[nodiscard]] const class attribute* attribute() const; + [[nodiscard]] const class attribute& attribute() const; [[nodiscard]] std::string column_name() const; [[nodiscard]] std::shared_ptr owner() const; [[nodiscard]] bool is_primary_key_constraint() const; @@ -39,8 +37,7 @@ private: friend class object_generator; friend class object; - std::string name_; - std::variant attr_; + const class attribute& attr_; std::shared_ptr owner_; std::shared_ptr reference_; utils::constraints options_{utils::constraints::None}; diff --git a/source/core/object/object.cpp b/source/core/object/object.cpp index fe4bc0d..fa78a67 100644 --- a/source/core/object/object.cpp +++ b/source/core/object/object.cpp @@ -2,9 +2,7 @@ namespace matador::object { object::object(std::string name) -: name_(std::move(name)) { - int i = 9; -} +: name_(std::move(name)) {} const attribute& object::create_attribute(std::string name, const std::shared_ptr& obj) { attribute attr{std::move(name)}; @@ -30,13 +28,6 @@ const std::string& object::name() const { void object::update_name(const std::string& name) { name_ = name; - // for (auto& con : constraints_) { - // if (con.is_primary_key_constraint()) { - // con.name_ += name; - // } else if (con.is_foreign_key_constraint()) { - // con.name_ = "FK_" + name + "_" + con.column_name(); - // } - // } } bool object::has_attributes() const { diff --git a/source/core/object/object_generator.cpp b/source/core/object/object_generator.cpp index 655e892..66e58bc 100644 --- a/source/core/object/object_generator.cpp +++ b/source/core/object/object_generator.cpp @@ -25,33 +25,36 @@ void object_generator::on_revision(const char* id, uint64_t& rev) { } void object_generator::create_pk_constraint(const std::string& name) const { - 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; + const auto pk_attr = find_attribute_by_name(name); + if (pk_attr == std::end(object_->attributes_)) { + return; } + restriction pk_constraint(*pk_attr); + pk_constraint.options_ |= utils::constraints::PrimaryKey; pk_constraint.owner_ = object_; object_->constraints_.emplace_back(std::move(pk_constraint)); } void object_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const { - const auto obj = fk_object(ti); - 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; + const auto pk_attr = find_attribute_by_name(name); + if (pk_attr == std::end(object_->attributes_)) { + return; } + const auto obj = fk_object(ti); + restriction pk_constraint(*pk_attr); + pk_constraint.options_ |= utils::constraints::ForeignKey; pk_constraint.owner_ = object_; pk_constraint.reference_ = obj; object_->constraints_.emplace_back(std::move(pk_constraint)); } void object_generator::create_unique_constraint(const std::string& name) const { - 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; + const auto pk_attr = find_attribute_by_name(name); + if (pk_attr == std::end(object_->attributes_)) { + return; } + restriction pk_constraint(*pk_attr); + 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 d1d40be..b5f0678 100644 --- a/source/core/object/restriction.cpp +++ b/source/core/object/restriction.cpp @@ -4,32 +4,16 @@ #include "matador/object/object.hpp" namespace matador::object { -restriction::restriction(std::string name) -: name_(std::move(name)) {} +restriction::restriction(const class attribute& attr) +: attr_(attr) {} -std::string restriction::name() const { - return type_string() + name_; -} - -const class attribute* restriction::attribute() const { - if (std::holds_alternative(attr_)) { - return std::get(attr_); - } - - return nullptr; +const class attribute& restriction::attribute() const { + return attr_; } std::string restriction::column_name() const { - if (std::holds_alternative(attr_)) { - return std::get(attr_)->name(); - } - if (std::holds_alternative(attr_)) { - return std::get(attr_); - } - - return ""; + return attr_.name(); } - std::shared_ptr restriction::owner() const { return owner_; }