diff --git a/Attribute.cpp b/Attribute.cpp index 01437da..5c2a0d6 100755 --- a/Attribute.cpp +++ b/Attribute.cpp @@ -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]; } diff --git a/Attribute.h b/Attribute.h index f721ff7..9bb84fd 100755 --- a/Attribute.h +++ b/Attribute.h @@ -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 getValues(); string getName(); diff --git a/DBEngine.cpp b/DBEngine.cpp index f646e1f..3f1082f 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -92,14 +92,42 @@ void DBEngine::rename(Relation& r, vector oldnames, vector 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 r1_atts = r1.getAttributes(); + //currently all returned relations are called TEMP + Relation new_r = r1; + vector 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 r1_atts = r1.getAttributes(); vector r2_atts = r2.getAttributes(); vector new_atts = r1_atts; @@ -120,6 +148,6 @@ void DBEngine::rename(Relation& r, vector oldnames, vector newna //currently all returned relations are called TEMP Relation new_r("TEMP", new_atts); - return new_r; + return new_r;*/ } -}*/ +} diff --git a/DBEngine.h b/DBEngine.h index 4b8a309..878a42e 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -21,7 +21,7 @@ public: Relation selection(string attName, string s, Relation r); Relation projection(vector input, Relation r); void rename(Relation& r, vector oldnames, vector newnames); - //Relation setUnion(Relation r1, Relation r2); + Relation setUnion(Relation r1, Relation r2); //void setDiff(); //void crossProduct(); }; diff --git a/Relation.cpp b/Relation.cpp index d2dca12..86a6dad 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -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); + } + } +} \ No newline at end of file diff --git a/Relation.h b/Relation.h index 7de9d11..2a8846c 100755 --- a/Relation.h +++ b/Relation.h @@ -23,5 +23,5 @@ public: void display(); void insertTuple(vector tuple); //assuming they are in order void insertFromRelation(Relation r); - //void removeTuple(); + void removeTuple(int index); }; \ No newline at end of file diff --git a/a.out b/a.out new file mode 100755 index 0000000..54f580c Binary files /dev/null and b/a.out differ diff --git a/test.cpp b/test.cpp index f21b31b..bf7b4c3 100755 --- a/test.cpp +++ b/test.cpp @@ -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(); }