union works

This commit is contained in:
Rebecca Schofield 2015-09-24 21:35:48 -05:00
commit 27f63bd452
8 changed files with 58 additions and 12 deletions

View file

@ -21,6 +21,11 @@ void Attribute::addCell(string v){
size++;
}
void Attribute::removeCell(int index){
values.erase(values.begin() + index);
size--;
}
string Attribute::operator[](int i){
return values[i];
}

View file

@ -15,6 +15,7 @@ public:
Attribute();
Attribute(string n, string t, bool k);
void addCell(string v);
void removeCell(int index);
string operator[](int i);
vector<string> getValues();
string getName();

View file

@ -92,14 +92,42 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
}
}
/*Relation DBEngine::setUnion(Relation r1, Relation r2){
Relation DBEngine::setUnion(Relation r1, Relation r2){
if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to union: the relations are not union-compatible";
return;
cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n";
return r1;
}
else {
vector<Attribute> r1_atts = r1.getAttributes();
//currently all returned relations are called TEMP
Relation new_r = r1;
vector<string> temp;
bool duplicate = false;
for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
temp = r2.getTuple(i);
for (int j = 0; j < new_r.getAttributes()[0].getSize(); ++j){
if (temp == new_r.getTuple(j)){
duplicate = true;
break;
}
}
if (!duplicate) {
new_r.insertTuple(temp);
}
duplicate = false;
}
return new_r;
/*vector<Attribute> r1_atts = r1.getAttributes();
vector<Attribute> r2_atts = r2.getAttributes();
vector<Attribute> new_atts = r1_atts;
@ -120,6 +148,6 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
//currently all returned relations are called TEMP
Relation new_r("TEMP", new_atts);
return new_r;
return new_r;*/
}
}*/
}

View file

@ -21,7 +21,7 @@ public:
Relation selection(string attName, string s, Relation r);
Relation projection(vector<string> input, Relation r);
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
//Relation setUnion(Relation r1, Relation r2);
Relation setUnion(Relation r1, Relation r2);
//void setDiff();
//void crossProduct();
};

View file

@ -110,3 +110,15 @@ void Relation::insertFromRelation(Relation r){
}
}
}
void Relation::removeTuple(int index){
if (index >= this->size) {
cout << "Failure to delete: the requested index is out of bounds.";
}
else {
for (int i = 0; i < att.size(); ++i) {
att[i].removeCell(index);
}
}
}

View file

@ -23,5 +23,5 @@ public:
void display();
void insertTuple(vector<string> tuple); //assuming they are in order
void insertFromRelation(Relation r);
//void removeTuple();
void removeTuple(int index);
};

BIN
a.out Executable file

Binary file not shown.

View file

@ -29,9 +29,9 @@ int main() {
engine.createTable("Food", v);
Attribute att4("SecondBreakfast", "VARCHAR(20)", true);
Attribute att5("SecondLunch", "VARCHAR(20)", false);
Attribute att6("SecondDinner", "VARCHAR(20)", false);
Attribute att4("Breakfast", "VARCHAR(20)", true);
Attribute att5("Lunch", "VARCHAR(20)", false);
Attribute att6("Dinner", "VARCHAR(20)", false);
att4.addCell("Pancakes");
att4.addCell("Bacon");
@ -50,5 +50,5 @@ int main() {
engine.createTable("MoarFood", v2);
//engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
}