48 lines
2.0 KiB
C++
48 lines
2.0 KiB
C++
#include "matador/object/constraints_generator.hpp"
|
|
#include "matador/object/object.hpp"
|
|
#include "matador/object/repository.hpp"
|
|
|
|
namespace matador::object {
|
|
constraints_generator::constraints_generator(std::vector<class constraint> &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::string& name, const basic_object_info& info) const {
|
|
const auto pk_attribute = info.reference_column();
|
|
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->table_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<attribute>::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;
|
|
});
|
|
}
|
|
} |