180 lines
No EOL
3.6 KiB
C++
Executable file
180 lines
No EOL
3.6 KiB
C++
Executable file
#include <iostream>
|
|
#include <vector>
|
|
#include "Relation.h"
|
|
|
|
Relation::Relation(string n){
|
|
name = n;
|
|
size = 0;
|
|
}
|
|
|
|
Relation::Relation(string n, vector<Attribute> a){
|
|
name = n;
|
|
att = a;
|
|
size = a.size();
|
|
}
|
|
|
|
void Relation::insertAttributes(vector<Attribute> 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<string> Relation::getTuple(int index){
|
|
vector<string> temp;
|
|
|
|
for (int i = 0; i < att.size(); ++i){
|
|
temp.push_back(att[i][index]);
|
|
}
|
|
|
|
return temp;
|
|
}
|
|
|
|
vector<Attribute> Relation::getAttributes(){
|
|
return att;
|
|
}
|
|
|
|
void Relation::setAttributes(vector<Attribute> v){
|
|
size = v.size();
|
|
att = v;
|
|
}
|
|
|
|
vector<string> Relation::getAttributeNames(){
|
|
vector<string> 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;
|
|
}
|
|
|
|
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<string> 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::updateTuple(vector<string> tuple, int index){
|
|
if (tuple.size() != this->size) {
|
|
cout << "Failure to update: the sizes do not match.";
|
|
}
|
|
|
|
if (!(index < att[0].getSize())) {
|
|
cout << "Failure to update: the requested index is out of bounds.";
|
|
}
|
|
|
|
else {
|
|
for (int i = 0; i < att.size(); ++i) {
|
|
cout << "att[i][index]: " << att[i][index] << "\n";
|
|
att[i].setValue(tuple[i], index);
|
|
cout << "att[i][index]: " << att[i][index] << "\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
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<Attribute> 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);
|
|
}
|
|
}
|
|
|
|
void Relation::updateInAttribute(string val, int attIndex, int index){
|
|
att[attIndex].setValue(val, index);
|
|
} |