diff --git a/DBEngine.cpp b/DBEngine.cpp index 3f1082f..08becc2 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -101,6 +101,7 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){ else { //currently all returned relations are called TEMP Relation new_r = r1; + new_r.setTableName("TEMP"); vector temp; bool duplicate = false; @@ -122,32 +123,76 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){ } return new_r; - - - - - - /*vector r1_atts = r1.getAttributes(); - vector r2_atts = r2.getAttributes(); - vector new_atts = r1_atts; - - for (int i = 0; i < r2_atts.size(); ++i) { - for (int j = 0; j < r2_atts[i].getSize(); ++j){ - new_atts[i].addCell(r2_atts[i][j]); - } - } - - for (int i = 0; i < new_atts.size(); ++i) { - for (int j = 0; j < new_atts.size(); ++j){ - if (new_atts[i] == new_atts[j]){ - new_atts.erase(new_atts.begin() + i); - continue; - } - } - } - - //currently all returned relations are called TEMP - Relation new_r("TEMP", new_atts); - return new_r;*/ } } + +Relation DBEngine::setDiff(Relation r1, Relation r2){ + if (r1.getAttributeNames() != r2.getAttributeNames()){ + cout << "Failure to diff: the relations are not union-compatible.\nreturning the first relation.\n"; + return r1; + } + + else { + //currently all returned relations are called TEMP + Relation new_r = r1; + new_r.setTableName("TEMP"); + vector temp; + bool duplicate = false; + + for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){ + new_r.removeTuple(i); + } + + for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) { + temp = r1.getTuple(i); + + for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){ + if (temp == r2.getTuple(j)){ + duplicate = true; + break; + } + } + + if (!duplicate){ + new_r.insertTuple(temp); + } + + duplicate = false; + } + + //make this one for loop later! + + for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) { + temp = r2.getTuple(i); + + for (int j = 0; j < r1.getAttributes()[0].getSize(); ++j){ + if (temp == r1.getTuple(j)){ + duplicate = true; + break; + } + } + + if (!duplicate){ + new_r.insertTuple(temp); + } + + duplicate = false; + } + + return new_r; + } +} + +Relation DBEngine::crossProduct(Relation r1, Relation r2){ + vector new_atts = r1.getAttributes(); + + for (int i = 0; i < r2.getAttributes().size(); ++i) { + new_atts.push_back(r2.getAttributes()[i]); + } + + //currently all returned relations are called TEMP + Relation new_r("TEMP", new_atts); + + + return new_r; +} \ No newline at end of file diff --git a/DBEngine.h b/DBEngine.h index 878a42e..53aa9c2 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -22,6 +22,6 @@ public: Relation projection(vector input, Relation r); void rename(Relation& r, vector oldnames, vector newnames); Relation setUnion(Relation r1, Relation r2); - //void setDiff(); - //void crossProduct(); + Relation setDiff(Relation r1, Relation r2); + Relation crossProduct(Relation r1, Relation r2); }; diff --git a/Relation.cpp b/Relation.cpp index 86a6dad..18d4728 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -23,6 +23,10 @@ string Relation::getTableName(){ return name; } +void Relation::setTableName(string s){ + name = s; +} + Attribute Relation::operator[](int i){ return att[i]; } diff --git a/Relation.h b/Relation.h index 2a8846c..172627f 100755 --- a/Relation.h +++ b/Relation.h @@ -13,6 +13,7 @@ public: Relation(string n, vector a); void insertAttributes(vector a); string getTableName(); + void setTableName(string s); Attribute operator[](int i); vector getTuple(int index); vector getAttributes(); diff --git a/a.out b/a.out index 54f580c..fdcea51 100755 Binary files a/a.out and b/a.out differ diff --git a/test.cpp b/test.cpp index bf7b4c3..1d6a304 100755 --- a/test.cpp +++ b/test.cpp @@ -29,26 +29,19 @@ int main() { engine.createTable("Food", v); - Attribute att4("Breakfast", "VARCHAR(20)", true); - Attribute att5("Lunch", "VARCHAR(20)", false); - Attribute att6("Dinner", "VARCHAR(20)", false); + Attribute att4("Dessert", "VARCHAR(20)", true); + Attribute att5("Midnight Snack", "VARCHAR(20)", false); - att4.addCell("Pancakes"); - att4.addCell("Bacon"); - att4.addCell("Eggs"); - att5.addCell("Turkey Sandwich"); - att5.addCell("Pasta Salad"); - att5.addCell("Taco"); - att6.addCell("Steak"); - att6.addCell("Fajitas"); - att6.addCell("Spaghetti"); + att4.addCell("Ice Cream Sundae"); + att4.addCell("Chocolate Bar"); + att5.addCell("Hummus and Carrots"); + att5.addCell("Potato Chips"); vector v2; v2.push_back(att4); v2.push_back(att5); - v2.push_back(att6); engine.createTable("MoarFood", v2); - engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); + engine.crossProduct(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); }