removed name member from restriction and changed attribute member to a reference

This commit is contained in:
Sascha Kühl 2025-12-11 09:26:35 +01:00
parent f842efb611
commit 571cfd52ae
4 changed files with 25 additions and 50 deletions

View File

@ -17,11 +17,9 @@ class object;
class restriction { class restriction {
public: public:
restriction() = default; explicit restriction(const class attribute& attr);
explicit restriction(std::string name);
[[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::string column_name() const;
[[nodiscard]] std::shared_ptr<object> owner() const; [[nodiscard]] std::shared_ptr<object> owner() const;
[[nodiscard]] bool is_primary_key_constraint() const; [[nodiscard]] bool is_primary_key_constraint() const;
@ -39,8 +37,7 @@ private:
friend class object_generator; friend class object_generator;
friend class object; friend class object;
std::string name_; const class attribute& attr_;
std::variant<class attribute*, std::string> attr_;
std::shared_ptr<object> owner_; std::shared_ptr<object> owner_;
std::shared_ptr<object> reference_; std::shared_ptr<object> reference_;
utils::constraints options_{utils::constraints::None}; utils::constraints options_{utils::constraints::None};

View File

@ -2,9 +2,7 @@
namespace matador::object { namespace matador::object {
object::object(std::string name) object::object(std::string name)
: name_(std::move(name)) { : name_(std::move(name)) {}
int i = 9;
}
const attribute& object::create_attribute(std::string name, const std::shared_ptr<object>& obj) { const attribute& object::create_attribute(std::string name, const std::shared_ptr<object>& obj) {
attribute attr{std::move(name)}; attribute attr{std::move(name)};
@ -30,13 +28,6 @@ const std::string& object::name() const {
void object::update_name(const std::string& name) { void object::update_name(const std::string& name) {
name_ = 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 { bool object::has_attributes() const {

View File

@ -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 { void object_generator::create_pk_constraint(const std::string& name) const {
restriction pk_constraint("PK_" + object_->name()); const auto pk_attr = find_attribute_by_name(name);
pk_constraint.options_ |= utils::constraints::PrimaryKey; if (pk_attr == std::end(object_->attributes_)) {
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) { return;
pk_constraint.attr_ = &*pk_attr;
} }
restriction pk_constraint(*pk_attr);
pk_constraint.options_ |= utils::constraints::PrimaryKey;
pk_constraint.owner_ = object_; pk_constraint.owner_ = object_;
object_->constraints_.emplace_back(std::move(pk_constraint)); object_->constraints_.emplace_back(std::move(pk_constraint));
} }
void object_generator::create_fk_constraint(const std::type_index& ti, 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_attr = find_attribute_by_name(name);
restriction pk_constraint("FK_" + object_->name() + "_" + name); if (pk_attr == std::end(object_->attributes_)) {
pk_constraint.options_ |= utils::constraints::ForeignKey; return;
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
pk_constraint.attr_ = &*pk_attr;
} }
const auto obj = fk_object(ti);
restriction pk_constraint(*pk_attr);
pk_constraint.options_ |= utils::constraints::ForeignKey;
pk_constraint.owner_ = object_; pk_constraint.owner_ = object_;
pk_constraint.reference_ = obj; pk_constraint.reference_ = obj;
object_->constraints_.emplace_back(std::move(pk_constraint)); object_->constraints_.emplace_back(std::move(pk_constraint));
} }
void object_generator::create_unique_constraint(const std::string& name) const { void object_generator::create_unique_constraint(const std::string& name) const {
restriction pk_constraint("UK_" + object_->name() + "_" + name); const auto pk_attr = find_attribute_by_name(name);
pk_constraint.options_ |= utils::constraints::Unique; if (pk_attr == std::end(object_->attributes_)) {
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) { return;
pk_constraint.attr_ = &*pk_attr;
} }
restriction pk_constraint(*pk_attr);
pk_constraint.options_ |= utils::constraints::Unique;
pk_constraint.owner_ = object_; pk_constraint.owner_ = object_;
} }

View File

@ -4,32 +4,16 @@
#include "matador/object/object.hpp" #include "matador/object/object.hpp"
namespace matador::object { namespace matador::object {
restriction::restriction(std::string name) restriction::restriction(const class attribute& attr)
: name_(std::move(name)) {} : attr_(attr) {}
std::string restriction::name() const { const class attribute& restriction::attribute() const {
return type_string() + name_; return attr_;
}
const class attribute* restriction::attribute() const {
if (std::holds_alternative<class attribute*>(attr_)) {
return std::get<class attribute*>(attr_);
}
return nullptr;
} }
std::string restriction::column_name() const { std::string restriction::column_name() const {
if (std::holds_alternative<class attribute*>(attr_)) { return attr_.name();
return std::get<class attribute*>(attr_)->name();
}
if (std::holds_alternative<std::string>(attr_)) {
return std::get<std::string>(attr_);
}
return "";
} }
std::shared_ptr<object> restriction::owner() const { std::shared_ptr<object> restriction::owner() const {
return owner_; return owner_;
} }