From 1e5e31b965634d8f6ebf5a2a8c5589116a6e3be2 Mon Sep 17 00:00:00 2001 From: Rebecca Schofield Date: Thu, 24 Sep 2015 20:42:59 -0500 Subject: [PATCH] updating --- DBEngine.cpp | 23 ++++++++++++++++++++++- DBEngine.h | 2 +- Relation.cpp | 10 ++++++++++ Relation.h | 1 + test.cpp | 30 ++++++++++++++++++++++++++++-- 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/DBEngine.cpp b/DBEngine.cpp index 1383e6f..9b9aabe 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -85,12 +85,33 @@ void DBEngine::rename(Relation& r, vector oldnames, vector newna } Relation DBEngine::setUnion(Relation r1, Relation r2){ - if (r1.getAttributes() != r2.getAttributes()){ + if (r1.getAttributeNames() != r2.getAttributeNames()){ cout << "Failure to union: the relations are not union-compatible"; return; } else { + 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; } } \ No newline at end of file diff --git a/DBEngine.h b/DBEngine.h index 5d91084..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 setUnion(Relation r1, Relation r2); //void setDiff(); //void crossProduct(); }; diff --git a/Relation.cpp b/Relation.cpp index 613916d..932905f 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -21,6 +21,16 @@ Attribute Relation::operator[](int i){ return att[i]; } +vector Relation::getTuple(int index){ + vector temp; + + for (int i = 0; i < att.size(); ++i){ + temp.push_back(att[i][index]); + } + + return temp; +} + vector Relation::getAttributes(){ return att; } diff --git a/Relation.h b/Relation.h index 8aa2a4b..50a240a 100755 --- a/Relation.h +++ b/Relation.h @@ -13,6 +13,7 @@ public: Relation(string n, vector a); string getTableName(); Attribute operator[](int i); + vector getTuple(int index); vector getAttributes(); vector getAttributeNames(); Attribute& getAttributeByName(string s); diff --git a/test.cpp b/test.cpp index 5740ec7..4d2d945 100755 --- a/test.cpp +++ b/test.cpp @@ -29,6 +29,32 @@ int main() { engine.createTable("Food", v); - Relation r = equality("Breakfast", "Pancakes", engine.getTableFromName("Food")); - r.display(); + Attribute att4("SecondBreakfast", "VARCHAR(20)", true); + Attribute att5("SecondLunch", "VARCHAR(20)", false); + Attribute att6("SecondDinner", "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"); + + vector v2; + v2.push_back(att4); + v2.push_back(att5); + v2.push_back(att6); + + engine.createTable("MoarFood", v2); + + vector test = engine.getTableFromName("Food").getTuple(1); + + for (int i = 0; i < test.size(); ++i){ + cout << test[i] << " "; + } + + //engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); } \ No newline at end of file