#include "matador/object/constraints_generator.hpp" #include "matador/object/object.hpp" #include "matador/object/repository.hpp" #include namespace matador::object { constraints_generator::constraints_generator(std::vector &constraints, const repository& repo, object &obj) : constraints_(constraints) , repo_(repo) , obj_(obj) {} void constraints_generator::create_pk_constraint(const std::string& name) const { class constraint pk_constraint("PK_" + obj_.name()); pk_constraint.options_ |= utils::constraints::PrimaryKey; if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) { pk_constraint.attr_ = &*pk_attr; } pk_constraint.owner_ = &obj_; constraints_.emplace_back(std::move(pk_constraint)); } void constraints_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const { const auto result = repo_.basic_info(ti); if (!result) { return; } const auto *pk_attribute = result.value().get().primary_key_attribute(); class constraint pk_constraint("FK_" + obj_.name() + "_" + name); pk_constraint.options_ |= utils::constraints::ForeignKey; if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) { pk_constraint.attr_ = &*pk_attr; } pk_constraint.owner_ = &obj_; pk_constraint.ref_column_name_ = pk_attribute->name(); pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : ""; constraints_.emplace_back(std::move(pk_constraint)); } void constraints_generator::create_unique_constraint(const std::string& name) const { class constraint pk_constraint("UK_" + obj_.name() + "_" + name); pk_constraint.options_ |= utils::constraints::Unique; if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) { pk_constraint.attr_ = &*pk_attr; } pk_constraint.owner_ = &obj_; } std::vector::iterator constraints_generator::find_attribute_by_name(const std::string& name) const { return std::find_if(std::begin(obj_.attributes_), std::end(obj_.attributes_), [&name](const attribute& elem) { return elem.name() == name; }); } }