64 lines
1.8 KiB
C++
64 lines
1.8 KiB
C++
#include "matador/object/restriction.hpp"
|
|
|
|
#include "matador/object/attribute.hpp"
|
|
#include "matador/object/object.hpp"
|
|
|
|
namespace matador::object {
|
|
restriction::restriction(const class attribute& attr)
|
|
: attr_(attr) {}
|
|
|
|
const class attribute& restriction::attribute() const {
|
|
return attr_;
|
|
}
|
|
|
|
std::string restriction::column_name() const {
|
|
return attr_.name();
|
|
}
|
|
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";
|
|
}
|
|
}
|