diff --git a/DBEngine.cpp b/DBEngine.cpp index 951ab45..fa8575c 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -55,15 +55,16 @@ void DBEngine::save(){ vector DBEngine::getRelations(){ return tables; } - + Relation& DBEngine::getTableFromName(string n){ for(int i = 0; i < tables.size(); i++){ if (tables[i].getTableName() == n){ return tables[i]; } } - cout << "No Relation with this name." << endl; - exit(1); + + cout << "FAILURE TO FIND: could not locate a Relation with this name."; + return tables[0]; } Relation DBEngine::selection(string attName, string s, Relation r){ @@ -85,29 +86,7 @@ Relation DBEngine::projection(vector input, Relation r){ } Relation temp(new_name, v); - return temp; -} - -Relation DBEngine::product(string new_name, Relation r1, Relation r2){ - - Relation temp(new_name); - vector a1; - - for(int i = 0; i < r1.getAttributes().size(); ++i){ - a1.push_back(r1.getAttributes()[i]); - } - - for(int i = 0; i < r2.getAttributes().size(); ++i){ - a1.push_back(r2.getAttributes()[i]); - } - - temp.insertAttributes(a1); - - vector tuple1; - vector tuple2; - vector result_tuple; - - return temp; + return temp; } //test error matching @@ -128,18 +107,7 @@ void DBEngine::rename(Relation& r, vector oldnames, vector newna } } } - -/* -Relation DBEngine::setUnion(Relation r1, Relation r2){ - if (r1.getAttributeNames() != r2.getAttributeNames()){ - cout << "Failure to union: the relations are not union-compatible"; - return; - } - - else { - vector r1_atts = r1.getAttributes(); -*/ Relation DBEngine::setUnion(Relation r1, Relation r2){ if (r1.getAttributeNames() != r2.getAttributeNames()){ cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n"; @@ -149,6 +117,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; @@ -170,32 +139,105 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){ } 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; -/* - - 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 < r1.getAttributes()[0].getSize(); ++i){ + new_r.removeTuple(i); } - 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; + 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; } - //currently all returned relations are called TEMP - Relation new_r("TEMP", new_atts); - return new_r;*/ + //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(); + int r1_attsize = r1.getAttributes()[0].getSize(); + int r2_attsize = r2.getAttributes()[0].getSize(); + + for (int i = 0; i < r2_attsize; ++i) { + new_atts.push_back(r2.getAttributes()[i]); + } + + for (int i = 0; i < new_atts.size(); ++i) { + new_atts[i].clearAllValues(); + } + + vector r1_tuple; + vector r2_tuple; + + for (int i = 0; i < r1_attsize; ++i) { + r1_tuple = r1.getTuple(i); + + for (int a = 0; a < r1_tuple.size(); ++a) { + cout << r1_tuple[a] << " "; + } + + for (int j = 0; j < r2_attsize; ++j) { + r2_tuple = r2.getTuple(j); + + for (int b = 0; b < r2_tuple.size(); ++b){ + cout << r2_tuple[b] << " "; + } + + cout << "\n"; + } + + cout << "\n\n"; + } + + //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 114ccad..3d95a7d 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -22,11 +22,10 @@ public: void saveToFile(vector cmds); Relation selection(string attName, string s, Relation r); Relation projection(vector input, Relation r); - Relation product(string s1, Relation r1, Relation r2); - void rename(Relation& r, vector oldnames, vector newnames); void save(); - void storeCommands(string s); - Relation setUnion(Relation r1, Relation r2); - //void setDiff(); - //void crossProduct(); + void storeCommands(string s); + void rename(Relation& r, vector oldnames, vector newnames); + Relation setUnion(Relation r1, Relation r2); + Relation setDiff(Relation r1, Relation r2); + Relation crossProduct(Relation r1, Relation r2); }; diff --git a/DevelopmentLog.pdf b/DevelopmentLog.pdf deleted file mode 100755 index 6d6db27..0000000 Binary files a/DevelopmentLog.pdf and /dev/null differ diff --git a/OUTPUT.txt b/OUTPUT.txt deleted file mode 100755 index 09bc7f9..0000000 --- a/OUTPUT.txt +++ /dev/null @@ -1,82 +0,0 @@ ---------------- -Queries ---------------- - -Selection: - -correct: select ( age == 12 ) people ; -incorrect: select ( age ))))) people ; - -Projection: - -correct: project ( age ) people ; -incorrect: project age people ; - -Renaming: - -correct: rename ( age, years old ) ; -incorrect: rename age years ; - -Set Union: - -correct: union ( people + animals ) ; -incorrect: union people animals ; - -Set Difference: - -correct: difference ( people - animals ) ; -incorrect: difference people animals ; - -Cross Product: - -correct: product ( people * animals ) ; -incorrect: product people animals ; - -------------------- -Commands -------------------- - -Open: - -correct: OPEN people ; -incorrect: OPEN ; - -Close: - -correct: CLOSE people ; -incorrect: CLOSE ; - -Save: - -correct: SAVE people ; -incorrect: SAVE ; - -Exit: - -correct: EXIT ; -incorrect: EXIT people ; - -Show: - -correct: SHOW people ; -incorrect: SHOW ; - -Create: - -correct: CREATE TABLE people ( age INTEGER, name VARCHAR ( 20 ) ) PRIMARY KEY ( name ) ; -incorrect: CREATE TABLE people age name ; - -Update: - -correct: UPDATE people SET ( age = 10 ) WHERE ( name == "Bob" ) ; -incorrect: UPDATE ( age = 10 ) WHERE people ; - -Insert: - -correct: INSERT INTO people VALUES FROM ( 12, "Benny" ) ; -incorrect: INSERT INTO people 12 "Benny" ; - -Delete: - -correct: DELETE FROM people WHERE ( name == "John" ) ; -incorrect: DELETE IN people WHEN (name=John) ; diff --git a/Relation.cpp b/Relation.cpp index 0e3f95a..4a7dd9c 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/test b/test deleted file mode 100755 index 3161c6b..0000000 Binary files a/test and /dev/null differ diff --git a/test.cpp b/test.cpp index 25a26ab..2c85d22 100755 --- a/test.cpp +++ b/test.cpp @@ -1,15 +1,14 @@ #include -#include -#include "Parserv3.h" -//#include "Condition.h" +#include +#include "Parserv3.h" +//#include "Condition.h" #include "DBEngine.h" using namespace std; -//still in progress -/* -int main() { +int main () { DBEngine engine; + Attribute att1("Breakfast", "VARCHAR(20)", true); Attribute att2("Lunch", "VARCHAR(20)", false); Attribute att3("Dinner", "VARCHAR(20)", false); @@ -28,124 +27,25 @@ int main() { v.push_back(att1); v.push_back(att2); v.push_back(att3); - - Relation r("Food", v); - //r.renameAttribute("Breakfast", "BFST"); - //r.display(); engine.createTable("Food", v); - - vector tuple; - tuple.push_back("Omelette"); - tuple.push_back("Fried Rice"); - tuple.push_back("Grouper"); - - engine.getTableFromName("Food").insertTuple(tuple); - - vector old; - vector newa; - - old.push_back("Breakfast"); - old.push_back("Lunch"); - old.push_back("Dinner"); - - newa.push_back("Tsafkaerb"); - newa.push_back("Hcnul"); - newa.push_back("Rennid"); - - //Projection test - vector projectTest; - projectTest.push_back("Breakfast"); - projectTest.push_back("Dinner"); - - cout << "\n***Initiated Projection***\n" << endl; - - Relation sub_r = engine.projection(projectTest, r); - sub_r.display(); - - //engine.rename(r, o, n); - engine.rename(engine.getTableFromName("Food"), old, newa); - engine.getTableFromName("Food").display(); - cout << "finished"; -} - -*/ -int main () { -/* - string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;"; - string ss2 = "SHOW Dogs ;"; - string ss3 = "EXIT ; "; - */ - DBEngine engine; + Attribute att4("Dessert", "VARCHAR(20)", true); + Attribute att5("Midnight Snack", "VARCHAR(20)", false); - // vector listOfTokens = tokenize(ss); - // vector listOfTokens2 = tokenize(ss2); - // vector listOfTokens3 = tokenize(ss3); - - // par_line(listOfTokens); - // par_line(listOfTokens2); - // par_line(listOfTokens3); - - // parse(ss, engine); - // parse(ss2, engine); - // parse(ss3, engine); - - // engine.save(); - - Attribute att1("Breakfast", "VARCHAR(20)", true); - Attribute att2("Lunch", "VARCHAR(20)", false); - Attribute att3("Dinner", "VARCHAR(20)", false); - - att1.addCell("Pancakes"); - att1.addCell("Waffles"); - att1.addCell("Biscuits"); - att2.addCell("Turkey Sandwich"); - att2.addCell("Caesar Salad"); - att2.addCell("Pizza"); - att3.addCell("Steak"); - att3.addCell("Shrimp"); - att3.addCell("Ribs"); - - vector v; - v.push_back(att1); - v.push_back(att2); - v.push_back(att3); - - engine.createTable("Food", v); - - Attribute att4("Breakfast", "VARCHAR(20)", true); - Attribute att5("Lunch", "VARCHAR(20)", false); - Attribute att6("Dinner", "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.getTableFromName("Food").display(); - engine.getTableFromName("MoarFood").display(); + //engine.getTableFromName("Food").display(); + //engine.getTableFromName("MoarFood").display(); - //engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); - - string x; - cout << "Enter DBMS Commands: "; - while(getline(cin, x)) - { - //cout << x << endl; - parse(x, engine); - cout << "Enter DBMS Commands: "; - } + engine.crossProduct(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); }