#include #include #include "Relation.h" Relation::Relation(string n){ name = n; size = 0; } Relation::Relation(string n, vector a){ name = n; att = a; size = a.size(); } void Relation::insertAttributes(vector a){ for (int i = 0; i < a.size(); ++i){ att.push_back(a[i]); } } string Relation::getTableName(){ return name; } void Relation::setTableName(string s){ name = s; } Attribute Relation::operator[](int i){ return att[i]; } vector Relation::getTuple(int index){ vector temp; for (int i = 0; i < att.size(); ++i){ temp.push_back(att[i][index]); } return temp; } vector Relation::getAttributes(){ return att; } void Relation::setAttributes(vector v){ size = v.size(); att = v; } vector Relation::getAttributeNames(){ vector temp; for(int i = 0; i < size; ++i){ temp.push_back(att[i].getName()); } return temp; } Attribute& Relation::getAttributeByName(string s) { for(int i = 0; i < size; ++i){ if (att[i].getName() == s) { return att[i]; } } cout << "Failure to return: the requested attribute does not exist."; } bool Relation::isAttribute(string s) { for(int i = 0; i < size; ++i){ if (att[i].getName() == s) { return true; } } return false; //cout << "Failure to return: the requested attribute does not exist."; } void Relation::renameAttribute(string oldstr, string newstr){ this->getAttributeByName(oldstr).setName(newstr); } int Relation::getSize(){ return size; } void Relation::display(){ cout << "--------------------------\n"; cout << getTableName() << "\n"; for (int i = 0; i < att.size(); ++i){ att[i].display(); } cout << "--------------------------\n"; } void Relation::insertTuple(vector tuple){ if (tuple.size() != this->size) { cout << "Failure to insert: the sizes do not match."; } else { for (int i = 0; i < att.size(); ++i) { att[i].addCell(tuple[i]); } } } void Relation::insertFromRelation(Relation r){ if (r.size != this->size) { cout << "Failure to insert: the sizes do not match."; return; } else if (this->getAttributeNames() != r.getAttributeNames()) { cout << "Failure to insert: the attributes do not match."; return; } else { vector newAtts = r.getAttributes(); for (int i = 0; i < newAtts.size(); ++i) { for (int j = 0; j < newAtts[i].getSize(); ++j) { this->att[i].addCell(newAtts[i][j]); } } } } void Relation::removeTuple(int index){ if (index >= this->size) { cout << "Failure to delete: the requested index is out of bounds." << endl; } else { for (int i = 0; i < att.size(); ++i) { att[i].removeCell(index); } } } void Relation::removeFromTuple(int rindex, int aindex) { if (rindex >= this->size) { cout << "Failure to delete: the requested index is out of bounds." << endl; } else { att[rindex].removeCell(aindex); } }