fixed restriction and constraint class to use size_t

This commit is contained in:
sascha 2026-06-03 16:36:51 +02:00
parent 87bd2f8b89
commit 705d361aaa
10 changed files with 29 additions and 22 deletions

View File

@ -28,7 +28,7 @@ public:
[[nodiscard]] std::string name() const;
[[nodiscard]] std::shared_ptr<class object> object() const;
[[nodiscard]] const std::vector<attribute>& attributes() const;
[[nodiscard]] const std::vector<restriction>& constraints() const;
[[nodiscard]] const std::list<restriction>& constraints() const;
[[nodiscard]] bool has_primary_key() const;
[[nodiscard]] const utils::identifier& primary_key() const;

View File

@ -35,7 +35,7 @@ public:
[[nodiscard]] bool has_constraints() const;
[[nodiscard]] size_t constraint_count() const;
[[nodiscard]] const std::vector<restriction>& constraints() const;
[[nodiscard]] const std::list<restriction>& constraints() const;
friend std::ostream& operator<<(std::ostream& os, const object& obj);
@ -50,7 +50,7 @@ private:
utils::identifier pk_identifier_;
std::vector<attribute> attributes_;
std::vector<restriction> constraints_;
std::list<restriction> constraints_;
};
}
#endif //MATADOR_OBJECT_HPP

View File

@ -170,7 +170,7 @@ void object_generator::create_fk_constraint(const std::string& name) const {
return;
}
const auto obj = fk_object<Type>();
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;

View File

@ -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<object> 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<object> owner_;
std::weak_ptr<object> reference_;
utils::constraints options_{utils::constraints::None};

View File

@ -25,7 +25,7 @@ const std::vector<attribute>& basic_object_info::attributes() const {
return object_->attributes();
}
const std::vector<class restriction>& basic_object_info::constraints() const {
const std::list<restriction>& basic_object_info::constraints() const {
return object_->constraints();
}

View File

@ -62,7 +62,7 @@ size_t object::constraint_count() const {
return constraints_.size();
}
const std::vector<restriction>& object::constraints() const {
const std::list<restriction>& object::constraints() const {
return constraints_;
}

View File

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

View File

@ -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<object> restriction::owner() const {
return owner_.lock();
}

View File

@ -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<internal::query_add_constraint_part_by_constraint>(
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<internal::query_drop_key_constraint_part_by_constraint>(
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_};
}

View File

@ -46,11 +46,11 @@ executable_query query_create_table_columns_intermediate::constraints(const std:
std::list<table_constraint> 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<internal::query_create_table_constraints_part>(constraints));