2015-09-15 20:17:52 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include "Attribute.h"
|
|
|
|
|
2015-09-15 21:31:19 -05:00
|
|
|
//Functional
|
2015-09-15 20:17:52 -05:00
|
|
|
class Relation {
|
2015-09-15 21:49:49 -05:00
|
|
|
//a table with rows and columns
|
|
|
|
string name; //the name of the relation (table)
|
|
|
|
vector<Attribute> att;
|
|
|
|
vector<string> attributeNames;
|
2015-09-15 22:02:16 -05:00
|
|
|
int size;
|
2015-09-15 20:17:52 -05:00
|
|
|
|
|
|
|
public:
|
2015-09-15 21:49:49 -05:00
|
|
|
//Relation();
|
2015-09-15 20:17:52 -05:00
|
|
|
|
2015-09-15 22:02:16 -05:00
|
|
|
void initializeRelation(string n, vector<string> attNames, vector<Attribute> a) {
|
2015-09-15 21:49:49 -05:00
|
|
|
attributeNames = attNames;
|
2015-09-15 20:17:52 -05:00
|
|
|
name = n;
|
|
|
|
att = a;
|
2015-09-15 22:02:16 -05:00
|
|
|
size = 0;
|
2015-09-15 20:55:21 -05:00
|
|
|
}
|
2015-09-15 20:17:52 -05:00
|
|
|
|
2015-09-15 22:02:16 -05:00
|
|
|
int getSize() { return size; }
|
|
|
|
|
|
|
|
void addTuple(vector<string> tuple) {
|
2015-09-15 21:49:49 -05:00
|
|
|
if(tuple.size() != att.size()){
|
|
|
|
cout << "\n ERROR" << endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Loop through the attribute columns
|
|
|
|
for(int i = 0; i < att.size(); i++) {
|
|
|
|
att[i].pushBack(tuple[i]);
|
2015-09-15 20:17:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 22:02:16 -05:00
|
|
|
void removeTuple(int tupleNum) {
|
2015-09-15 21:49:49 -05:00
|
|
|
if (tupleNum > att[0].getSize() || tupleNum < 0)
|
|
|
|
{
|
|
|
|
cout<<"ERROR! index out of bound"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
for(int i = 0; i < att.size(); ++i) //for all the attributes
|
|
|
|
{
|
|
|
|
att[i].erase(tupleNum);
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 20:17:52 -05:00
|
|
|
}
|
|
|
|
|
2015-09-15 22:02:16 -05:00
|
|
|
void projectQuery(string input) {
|
2015-09-15 21:49:49 -05:00
|
|
|
cout << "-----------Initiated Query Projection---------" << endl;
|
|
|
|
for(int i = 0; i < att.size(); i++) {
|
|
|
|
if(att[i].getName() == input) {
|
|
|
|
|
|
|
|
cout << "Column Title: " << input << endl;
|
|
|
|
for(int j = 0; j < att[i].getSize(); j++) {
|
|
|
|
cout << att[i].values[j] << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cout << "Attribute input not valid" << endl;
|
|
|
|
}
|
2015-09-15 20:17:52 -05:00
|
|
|
}
|
2015-09-15 21:49:49 -05:00
|
|
|
|
2015-09-15 22:02:16 -05:00
|
|
|
void display() {
|
2015-09-15 20:23:47 -05:00
|
|
|
cout<<"\n\nDisplay of relation--------------------------------"<<endl;
|
|
|
|
cout<<"Relation name: "<<name<<endl;
|
2015-09-15 21:49:49 -05:00
|
|
|
for (int i = 0; i < attributeNames.size(); ++i)
|
2015-09-15 20:23:47 -05:00
|
|
|
{
|
|
|
|
|
2015-09-15 21:49:49 -05:00
|
|
|
cout<<"\nAttribute name: "<<attributeNames[i]<<": ";
|
2015-09-15 20:23:47 -05:00
|
|
|
|
2015-09-15 20:36:33 -05:00
|
|
|
att[i].display();
|
2015-09-15 20:23:47 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 20:17:52 -05:00
|
|
|
};
|