diff --git a/DBEngine.cpp b/DBEngine.cpp index 93bc89d..c8f4a4b 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -151,8 +151,7 @@ Relation DBEngine::projection(vector input, Relation r){ } //test error matching -Relation DBEngine::rename(vector newnames, Relation &r) -{ +Relation DBEngine::rename(vector newnames, Relation &r){ vector temp; if (r.getSize() != newnames.size()) { cout << "FAILURE TO RENAME: number of attributes do not match.\n"; @@ -286,20 +285,63 @@ Relation DBEngine::crossProduct(Relation r1, Relation r2){ return new_r; } -void DBEngine::updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere){ - for(int i = 0; i < tables.size(); i++){ - if (tables[i].getTableName() == relationName){ - tables[i].updateCmd(attNameSet, attSet, attNameWhere, attWhere); - return; +void DBEngine::updateFromRelationCmd(Relation r, Relation temp, vector attName, vector att){ + string rName = r.getTableName(); + int index = -1; + + for (int i = 0; i < tables.size(); ++i){ + if (tables[i].getTableName() == rName){ + index = i; + break; + } + } + + vector tuple; + vector tempTuple; + for(int i = 0; i < temp.getAttributes()[0].getSize(); ++i){ + tuple = temp.getTuple(i); + + for (int j = 0; j < tables[index].getSize(); ++j){ + tempTuple = tables[index].getTuple(j); + if (tables[index].getTuple(j) == tuple){ + for (int k = 0; k < tables[index].getSize(); ++k){ + for (int a = 0; a < attName.size(); ++a){ + if (tables[index][k].getName() == attName[a]){ + cout << "point 7\n"; + cout << "tables[index][k][j]:" << tables[index][k][j] << "\n"; + cout << "att[a]:" << att[a] << "\n"; + tables[index].updateInAttribute(att[a], k, j); + } + } + } + } } } } -void DBEngine::deleteCmd(string relationName, string attName, string att){ - for(int i = 0; i < tables.size(); i++){ - if (tables[i].getTableName() == relationName){ - tables[i].deleteCmd(attName, att); - return; +void DBEngine::deleteFromRelationCmd(Relation r, Relation temp){ + string rName = r.getTableName(); + int index = -1; + + for (int i = 0; i < tables.size(); ++i){ + if (tables[i].getTableName() == rName){ + index = i; + break; + } + } + + vector tuple; + vector tempTuple; + + for(int i = 0; i < temp.getAttributes()[0].getSize(); ++i){ + tuple = temp.getTuple(i); + + for (int j = 0; j < tables[index].getSize(); ++j){ + tempTuple = tables[index].getTuple(j); + + if (tempTuple == tuple){ + tables[index].removeTuple(j); + } } } } diff --git a/DBEngine.h b/DBEngine.h index 06d8b2f..a35b3a7 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -31,6 +31,6 @@ public: Relation setUnion(Relation r1, Relation r2); Relation setDiff(Relation r1, Relation r2); Relation crossProduct(Relation r1, Relation r2); - void updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere); - void deleteCmd(string relationName, string attName, string att); + void updateFromRelationCmd(Relation r, Relation temp, vector attName, vector att); + void deleteFromRelationCmd(Relation r, Relation temp); }; diff --git a/Parser.cpp b/Parser.cpp index 8e36adb..6362964 100755 --- a/Parser.cpp +++ b/Parser.cpp @@ -287,11 +287,11 @@ Relation condition(vector input, Relation &r, DBEngine &engine){ Relation rtemp2 = condition(input, r, engine); rfinal = engine.setUnion(rtemp, rtemp2); } + return rfinal; } -tuple, Relation> expression(vector input, DBEngine &engine) -{ +tuple, Relation> expression(vector input, DBEngine &engine){ Relation rfinal("TEMP"); tuple, Relation> t(input, rfinal); if(input[0] == "select") @@ -557,8 +557,7 @@ tuple, Relation> expression(vector input, DBEngine &engin return t; } -vector showCMD(vector input, DBEngine &engine) -{ +vector showCMD(vector input, DBEngine &engine){ if(engine.isRelation(input[0]) && (input[1] != "+" && input[1] != "-" && input[1] != "*")) { @@ -575,8 +574,7 @@ vector showCMD(vector input, DBEngine &engine) return input; } -vector saveCMD(vector input, DBEngine &engine) -{ +vector saveCMD(vector input, DBEngine &engine){ if (input.size() > 2) { cout<<"Syntax error!"< saveCMD(vector input, DBEngine &engine) return input; } -vector closeCMD(vector input, DBEngine &engine) -{ +vector closeCMD(vector input, DBEngine &engine){ if (input.size() > 3) { cout<<"Syntax error!"< closeCMD(vector input, DBEngine &engine) return input; } -vector openCMD(vector input, DBEngine &engine) -{ +vector openCMD(vector input, DBEngine &engine){ if (input.size() > 2) { cout<<"Syntax error!"< openCMD(vector input, DBEngine &engine) return input; } -vector exitCMD(vector input, DBEngine &engine) -{ +vector exitCMD(vector input, DBEngine &engine){ exit(0); return input; } - -vector createCMD(vector input, DBEngine &engine) -{ +vector createCMD(vector input, DBEngine &engine){ if (input[0] == "TABLE") { input.erase(input.begin()); @@ -717,8 +710,7 @@ vector createCMD(vector input, DBEngine &engine) else cout<<"Syntax error! 1"< insertCMD(vector input, DBEngine &engine) -{ +vector insertCMD(vector input, DBEngine &engine){ //relation name will be the first element of the vector of data returned by this function if (input[0] == "INTO") @@ -848,36 +840,27 @@ vector insertCMD(vector input, DBEngine &engine) } -vector updateCMD(vector input, DBEngine &engine) -{ +vector updateCMD(vector input, DBEngine &engine){ Relation r = engine.getTableFromName(input[0]); - Relation rcond("TEMP"); - Relation rfinal("TEMP"); + Relation temp("TEMP"); + vector a; + vector c; + vector s; input.erase(input.begin()); if(input[0] == "SET") { input.erase(input.begin()); - vector a = r.getAttributes(); - //parse out ( and send everything until ) into an Expression vector if(input[0] == "(") { - - vector a; - PAttribute temp; - - vector s; - - //vector e; - input.erase(input.begin()); while(input[0] != ")") { - temp.setPAttributeName(input[0]); + a.push_back(input[0]); input.erase(input.begin()); @@ -885,16 +868,18 @@ vector updateCMD(vector input, DBEngine &engine) { input.erase(input.begin()); - s.push_back(input[0]); + //s.push_back(input[0]); if(input[0].at(0) == '\"') { - s.push_back(input[0].substr(1, input[0].find_last_of("\""))); + c.push_back(input[0].substr(1, input[0].find_last_of("\"") - 1)); } else { - s.push_back(input[0]); + c.push_back(input[0]); } + + input.erase(input.begin()); } else @@ -906,50 +891,10 @@ vector updateCMD(vector input, DBEngine &engine) { input.erase(input.begin()); } - - a.push_back(temp); - temp.setPAttributeName("~"); } - + input.erase(input.begin()); } - if(input[0] == "WHERE") - { - vector s; - if(input[0] == "(") - { - input.erase(input.begin()); - while(input[0] != ")") - { - s.push_back(input[0]); - input.erase(input.begin()); - } - rcond = condition(s, r, engine); - } - - } - - - // send update command to DBEngine - } - - else cout<<"Syntax error! 1"< deleteCMD(vector input, DBEngine &engine) -{ - if (input[0] == "DELETE" && input[1] == "FROM") - { - input.erase(input.begin()); - input.erase(input.begin()); - - Relation rcond = engine.getTableFromName(input[0]); - Relation rfinal = engine.getTableFromName(input[0]); - - input.erase(input.begin()); - - vector s; - if(input[0] == "WHERE") { input.erase(input.begin()); @@ -961,14 +906,53 @@ vector deleteCMD(vector input, DBEngine &engine) s.push_back(input[0]); input.erase(input.begin()); } + + r.display(); + + for (int i = 0; i < s.size(); ++i){ + cout << "s[i]: " << s[i] << "\n"; + } + + temp = condition(s, r, engine); } } - //rcond = condition(); - // send delete command to DBEngine - + temp.display(); + engine.updateFromRelationCmd(r, temp, a, c); } - else cout<<"Syntax error!"< deleteCMD(vector input, DBEngine &engine) +{ + if (input[0] == "FROM"){ + input.erase(input.begin()); + + Relation r = engine.getTableFromName(input[0]); + Relation temp("TEMP"); + input.erase(input.begin()); + + vector s; + + if(input[0] == "WHERE"){ + input.erase(input.begin()); + if(input[0] == "("){ + input.erase(input.begin()); + while(input[0] != ")"){ + s.push_back(input[0]); + input.erase(input.begin()); + } + } + temp = condition(s, r, engine); + } + + engine.deleteFromRelationCmd(r, temp); + } + + else cout<<"Syntax error!"< query(vector input, DBEngine &engine) @@ -988,7 +972,7 @@ vector query(vector input, DBEngine &engine) return input; } -void par_line(vector input, DBEngine &engine) //calls par_command() or par_query() depending on first item from token list +void par_line(vector input, DBEngine &engine) { /* • Match the first item in the token list and determine weather this is a command or a query. diff --git a/Relation.cpp b/Relation.cpp index de3078b..7ab725d 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -176,51 +176,6 @@ void Relation::removeFromTuple(int rindex, int aindex) } } -void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere){ - vector inds; - bool found = false; - - for (int i = 0; i < att.size(); ++i){ - if (att[i].getName() == attNameWhere){ - found = true; - for (int j = 0; j < att[i].getSize(); ++j){ - if (att[i][j] == attWhere){ - inds.push_back(j); - } - } - } - } - - if (!found){ - cout << "Failure to update, the requested attribute was not found." << endl; - } - - for (int i = 0; i < att.size(); ++i){ - if (att[i].getName() == attNameSet){ - for (int j = 0; j < inds.size(); ++j){ - att[i].setValue(attSet, inds[j]); - } - } - } - - return; -} - -void Relation::deleteCmd(string attName, string s){ - bool found = false; - - for (int i = 0; i < att.size(); ++i){ - if (att[i].getName() == attName){ - found = true; - for (int j = 0; j < att[i].getSize(); ++j){ - if (att[i][j] == s){ - removeTuple(j); - } - } - } - } - - if (!found){ - cout << "Failure to delete, the requested attribute was not found." << endl; - } +void Relation::updateInAttribute(string val, int attIndex, int index){ + att[attIndex].setValue(val, index); } \ No newline at end of file diff --git a/Relation.h b/Relation.h index 8d3f4d5..7bb601b 100755 --- a/Relation.h +++ b/Relation.h @@ -29,6 +29,5 @@ public: void insertFromRelation(Relation r); void removeTuple(int index); void removeFromTuple(int rindex, int aindex); - void updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere); - void deleteCmd(string attName, string s); + void updateInAttribute(string val, int attIndex, int index); }; \ No newline at end of file diff --git a/a.out b/a.out index 8c7ff57..e264b7c 100755 Binary files a/a.out and b/a.out differ diff --git a/test.cpp b/test.cpp index 24e2e88..3cc4f9b 100755 --- a/test.cpp +++ b/test.cpp @@ -33,15 +33,8 @@ int main () { v.push_back(att3); engine.createTable("Food", v); - //engine.updateCmd("Food", "Dinner", "SUCCESS", "Breakfast", "Pancakes"); - engine.getTableFromName("Food").display(); - //engine.deleteCmd("Food", "Breakfast", "Pancakes"); + //engine.getTableFromName("Food").display(); - vector tuple; - tuple.push_back("A pancake"); - tuple.push_back("A turkey sandwich"); - tuple.push_back("A steak"); - - engine.getTableFromName("Food").updateTuple(tuple, 0); + parse("UPDATE Food SET ( Dinner = \"SUCCESS\" ) WHERE ( Breakfast == \"Pancakes\" ) ;", engine); engine.getTableFromName("Food").display(); }