92 lines
2.1 KiB
C++
92 lines
2.1 KiB
C++
#include "matador/object/object.hpp"
|
|
|
|
namespace matador::object {
|
|
object::object( std::string name, std::string alias )
|
|
: name_(std::move(name))
|
|
, alias_(std::move(alias)) {}
|
|
|
|
const attribute& object::create_attribute(std::string name, const std::shared_ptr<object>& obj) {
|
|
attribute attr{std::move(name)};
|
|
attr.owner_ = obj;
|
|
return obj->attributes_.emplace_back(std::move(attr));
|
|
}
|
|
|
|
// void object::add_attribute( attribute attr ) {
|
|
// auto &ref = attributes_.emplace_back(std::move(attr));
|
|
// ref.owner_ = this;
|
|
// }
|
|
//
|
|
// void object::add_constraint( class constraint c ) {
|
|
// auto &ref = constraints_.emplace_back(std::move(c));
|
|
// ref.owner_ = this;
|
|
// }
|
|
|
|
attribute* object::primary_key_attribute() const {
|
|
return pk_attribute_;
|
|
}
|
|
|
|
const utils::identifier& object::primary_key() const {
|
|
return pk_identifier_;
|
|
}
|
|
|
|
bool object::has_primary_key() const {
|
|
return pk_attribute_ != nullptr;
|
|
}
|
|
|
|
const std::string& object::name() const {
|
|
return name_;
|
|
}
|
|
|
|
const std::string& object::alias() const {
|
|
return alias_;
|
|
}
|
|
|
|
void object::update_name(const std::string& name) {
|
|
name_ = name;
|
|
for (auto& con : constraints_) {
|
|
if (con.is_primary_key_constraint()) {
|
|
con.name_ += name;
|
|
} else if (con.is_foreign_key_constraint()) {
|
|
con.name_ = "FK_" + name + "_" + con.column_name();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool object::has_attributes() const {
|
|
return attributes_.empty();
|
|
}
|
|
|
|
size_t object::attribute_count() const {
|
|
return attributes_.size();
|
|
}
|
|
|
|
const std::list<attribute>& object::attributes() const {
|
|
return attributes_;
|
|
}
|
|
|
|
bool object::has_constraints() const {
|
|
return constraints_.empty();
|
|
}
|
|
|
|
size_t object::constraint_count() const {
|
|
return constraints_.size();
|
|
}
|
|
|
|
const std::list<class restriction>& object::constraints() const {
|
|
return constraints_;
|
|
}
|
|
|
|
std::ostream & operator<<(std::ostream &os, const object &obj) {
|
|
os << "Object " << obj.name_ << "\nAttributes:\n";
|
|
for (const auto &attr : obj.attributes_) {
|
|
os << " " << attr << "\n";
|
|
}
|
|
os << "Constraints:\n";
|
|
for (const auto &con : obj.constraints_) {
|
|
os << " " << con << "\n";
|
|
}
|
|
os << "\n";
|
|
return os;
|
|
}
|
|
}
|