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 {
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<object> 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<class attribute*, std::string> attr_;
const class attribute& attr_;
std::shared_ptr<object> owner_;
std::shared_ptr<object> reference_;
utils::constraints options_{utils::constraints::None};

View File

@ -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<object>& 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 {

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 {
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_;
}

View File

@ -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<class attribute*>(attr_)) {
return std::get<class attribute*>(attr_);
}
return nullptr;
const class attribute& restriction::attribute() const {
return attr_;
}
std::string restriction::column_name() const {
if (std::holds_alternative<class attribute*>(attr_)) {
return std::get<class attribute*>(attr_)->name();
}
if (std::holds_alternative<std::string>(attr_)) {
return std::get<std::string>(attr_);
}
return "";
return attr_.name();
}
std::shared_ptr<object> restriction::owner() const {
return owner_;
}