71 lines
2.0 KiB
C++
71 lines
2.0 KiB
C++
#include "matador/object/restriction.hpp"
|
|
|
|
#include "matador/object/attribute.hpp"
|
|
#include "matador/object/object.hpp"
|
|
|
|
namespace matador::object {
|
|
restriction::restriction(const size_t attr_index)
|
|
: index_(attr_index) {}
|
|
|
|
size_t restriction::attribute_index() const {
|
|
return index_;
|
|
}
|
|
|
|
std::string restriction::column_name() const {
|
|
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();
|
|
}
|
|
|
|
bool restriction::is_primary_key_constraint() const {
|
|
return utils::is_constraint_set(options_, utils::constraints::PrimaryKey);
|
|
}
|
|
|
|
bool restriction::is_foreign_key_constraint() const {
|
|
return utils::is_constraint_set(options_, utils::constraints::ForeignKey);
|
|
}
|
|
|
|
bool restriction::is_unique_constraint() const {
|
|
return utils::is_constraint_set(options_, utils::constraints::Unique);
|
|
}
|
|
|
|
std::string restriction::ref_table_name() const {
|
|
const auto ref = reference_.lock();
|
|
return ref ? ref->name() : "";
|
|
}
|
|
|
|
std::string restriction::ref_column_name() const {
|
|
const auto ref = reference_.lock();
|
|
return ref ? ref->primary_key_attribute()->name() : "";
|
|
}
|
|
|
|
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
|
os << "constraint " << c.type_string() << " for column " << c.column_name();
|
|
if (c.is_foreign_key_constraint()) {
|
|
os << " references " << c.ref_table_name() << "." << c.ref_column_name();
|
|
}
|
|
return os;
|
|
}
|
|
|
|
std::string restriction::type_string() const {
|
|
if (utils::is_constraint_set(options_, utils::constraints::PrimaryKey)) {
|
|
return "PrimaryKey";
|
|
}
|
|
if (utils::is_constraint_set(options_, utils::constraints::ForeignKey)) {
|
|
return "ForeignKey";
|
|
}
|
|
if (utils::is_constraint_set(options_, utils::constraints::Unique)) {
|
|
return "Unique";
|
|
}
|
|
return "Unknown";
|
|
}
|
|
}
|