diff --git a/DBEngine.cpp b/DBEngine.cpp index 32a06ef..417c7bf 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -94,7 +94,7 @@ void DBEngine::save(string n){ } } - //file.close(); + file.close(); } vector DBEngine::getRelations(){ @@ -167,18 +167,6 @@ Relation DBEngine::rename(vector newnames, Relation &r) } return r; } - /* - else if (oldnames != r.getAttributeNames()) { - cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n"; - return; - } - - else { - for(int i = 0; i < oldnames.size(); ++i){ - r.renameAttribute(oldnames[i], newnames[i]); - } - } - */ } @@ -229,83 +217,38 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){ vector temp; //bool duplicate = false; - /* - for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) - { - new_r.removeTuple(0); - } - */ int size = 0; - //cout << "test1" << endl; for(int x = 0; x < r2.getAttributes().size(); ++x) { - //cout << "test2" << endl; for (int i = 0; i < r2.getAttributes()[x].getSize(); ++i) { - //cout << "test3" << endl; temp = r2.getTuple(i); for(int y = 0; y < new_r.getAttributes().size(); ++y) { size = new_r.getAttributes()[y].getSize(); - //cout << "test4" << endl; new_r.getAttributes()[y].getSize(); for (int j = 0; j < size; ++j) { - //cout << "test5" << endl; for(int a = 0; a < temp.size(); ++a) { - //cout << "test6" << endl; for(int b = 0; b < new_r.getTuple(j).size(); ++b) { - //cout << "test7" << endl; - //cout << temp[a] << " " << new_r.getTuple(j)[b] << " " << b << endl; if (temp[a] == new_r.getTuple(j)[b]) { new_r.removeFromTuple(b, j); - //cout << "test" << endl; - //duplicate = true; - //break; } } } size = new_r.getAttributes()[y].getSize(); } - /* - 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; } } diff --git a/DBEngine.h b/DBEngine.h index 81009d3..ddbdd13 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -18,7 +18,6 @@ public: void insertValues(string r, vector v); vector getRelations(); bool isRelation(string n); - //void showTable(Relation r); Relation& getTableFromName(string n); void saveToFile(vector cmds); Relation selection(string attName, string s, Relation r); diff --git a/Parser.cpp b/Parser.cpp index d665816..1c19bfa 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -45,197 +45,167 @@ void displayTokenList(vector input) Relation condition(vector input, Relation &r, DBEngine &engine) { Relation rfinal = r; - - //UNTESTED - //testing to see if there is an && or || present - /* - if (input.size() > 3) { - //test print - for (int i = 0; i < input.size(); ++i) - cout << "input[i]: " << input[i] << "\n"; - - - //this is probably wrong, added a cout for testing - string bin_op = input[3]; - cout << "bin_op, should be && or ||: " << bin_op << "\n"; - - //making each half a new vector, probably a better way to do this - vector firsthalf; - vector secondhalf; - - //again, all untested. it's weird to not test this stuff - for (int i = 0; i < input.size(); ++i){ - if (i < 3) - firsthalf.push_back(input[i]); - else - secondhalf.push_back(input[i]); - + + Attribute a1 = r.getAttributeByName(input[0]); + input.erase(input.begin()); + string op = input[0]; + input.erase(input.begin()); + Attribute a2; + string c; + if(r.isAttribute(input[0])) + { + a2 = r.getAttributeByName(input[0]); + input.erase(input.begin()); + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + int offset = 0; + if(op == "==") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x]) + { + rfinal.removeTuple(x - offset); + offset += 1; + } + } + } + else if(op == "!=") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]) + { + rfinal.removeTuple(x - offset); + offset += 1; + } + } + } + else if(op == ">=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x - offset); + offset += 1; + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x - offset); + offset += 1; + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == ">") + { + + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x - offset); + offset += 1; + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x - offset); + offset += 1; + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + } } - - //calling condition again with each half - Relation r1 = condition(firsthalf, rfinal); - Relation r2 = condition(secondhalf, rfinal); - - //NO IDEA IF THIS ACTUALLY WORKS - //HOORAY - //HOW THE FUCK DO PEOPLE CODE LIKE THIS - if (bin_op == "&&"){ - //with engine as the DBEngine object that maybe exists - return engine.crossProduct(r1, r2); - } - - else if (bin_op == "||"){ - //with engine as the DBEngine object that maybe exists - return engine.setUnion(r1, r2); - } - - else { - cout << "Something strange happened. bin_op: " << bin_op << "\n"; - cout << "Returning the initial relation."; - return rfinal; - } - } - - else { - */ - Attribute a1 = r.getAttributeByName(input[0]); + else if(input[0].at(0) == '\"') + { + c = input[0].substr(1, input[0].find_last_of("\"") - 1); input.erase(input.begin()); - string op = input[0]; - input.erase(input.begin()); - Attribute a2; - string c; - //cout << input[0] << endl; - for(int x = 0; x < input.size(); ++x) + int offset = 0; + //input.erase(input.begin()); + if(op == "==") { - cout << input[x] << endl; - } - if(r.isAttribute(input[0])) - { - a2 = r.getAttributeByName(input[0]); - input.erase(input.begin()); for(int i = 0; i < r.getAttributes().size(); ++i) { if(r.getAttributes()[i].getName() == a1.getName()) { - /* - for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size; ++j) + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) { - if(r.getAttributeByName(a1.getName()).getValues()[j] == ) + if(r.getAttributeByName(a1.getName()).getValues()[j] != c) { - rfinal.insertTuple(r.getTuple(j)); - } - }*/ - int offset = 0; - if(op == "==") - { - for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) - { - if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x]) - { - rfinal.removeTuple(x - offset); - offset += 1; - } - } - } - else if(op == "!=") - { - for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) - { - if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]) - { - rfinal.removeTuple(x - offset); - offset += 1; - } - } - } - else if(op == ">=") - { - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") - { - for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])) - { - rfinal.removeTuple(x - offset); - offset += 1; - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } - } - else if(op == "<=") - { - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") - { - for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])) - { - rfinal.removeTuple(x - offset); - offset += 1; - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } - } - else if(op == ">") - { - - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") - { - for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) - { - rfinal.removeTuple(x - offset); - offset += 1; - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } - } - else if(op == "<") - { - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") - { - for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) - { - rfinal.removeTuple(x - offset); - offset += 1; - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); + rfinal.removeTuple(j - offset); + offset += 1; } } } } } - else if(input[0].at(0) == '\"') + else if(op == "!=") { - c = input[0].substr(1, input[0].find_last_of("\"") - 1); - input.erase(input.begin()); - int offset = 0; - //input.erase(input.begin()); - if(op == "==") + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(r.getAttributeByName(a1.getName()).getValues()[j] == c) + { + rfinal.removeTuple(j - offset); + offset += 1; + } + } + } + } + } + else if(op == ">=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") { for(int i = 0; i < r.getAttributes().size(); ++i) { @@ -243,7 +213,7 @@ Relation condition(vector input, Relation &r, DBEngine &engine) { for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) { - if(r.getAttributeByName(a1.getName()).getValues()[j] != c) + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c)) { rfinal.removeTuple(j - offset); offset += 1; @@ -252,7 +222,15 @@ Relation condition(vector input, Relation &r, DBEngine &engine) } } } - else if(op == "!=") + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") { for(int i = 0; i < r.getAttributes().size(); ++i) { @@ -260,7 +238,7 @@ Relation condition(vector input, Relation &r, DBEngine &engine) { for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) { - if(r.getAttributeByName(a1.getName()).getValues()[j] == c) + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c)) { rfinal.removeTuple(j - offset); offset += 1; @@ -269,124 +247,79 @@ Relation condition(vector input, Relation &r, DBEngine &engine) } } } - else if(op == ">=") + else { - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") - { - for(int i = 0; i < r.getAttributes().size(); ++i) - { - if(r.getAttributes()[i].getName() == a1.getName()) - { - for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c)) - { - rfinal.removeTuple(j - offset); - offset += 1; - } - } - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } - } - else if(op == "<=") - { - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") - { - for(int i = 0; i < r.getAttributes().size(); ++i) - { - if(r.getAttributes()[i].getName() == a1.getName()) - { - for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c)) - { - rfinal.removeTuple(j - offset); - offset += 1; - } - } - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } - } - else if(op == ">") - { - - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") - { - for(int i = 0; i < r.getAttributes().size(); ++i) - { - if(r.getAttributes()[i].getName() == a1.getName()) - { - for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) <= stoi(c)) - { - rfinal.removeTuple(j - offset); - offset += 1; - } - } - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } - } - else if(op == "<") - { - - if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") - { - for(int i = 0; i < r.getAttributes().size(); ++i) - { - if(r.getAttributes()[i].getName() == a1.getName()) - { - for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) - { - if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) >= stoi(c)) - { - rfinal.removeTuple(j - offset); - offset += 1; - } - } - } - } - } - else - { - cout << "Attribute type is not an INTEGER." << endl; - exit(1); - } + cout << "Attribute type is not an INTEGER." << endl; + exit(1); } } - if(input[0] == "&&") + else if(op == ">") { - input.erase(input.begin()); - Relation rtemp = rfinal; - rfinal = condition(input, rtemp, engine); + + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) <= stoi(c)) + { + rfinal.removeTuple(j - offset); + offset += 1; + } + } + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } } - else if(input[0] == "||") + else if(op == "<") { - input.erase(input.begin()); - Relation rtemp = rfinal; - Relation rtemp2 = condition(input, r, engine); - rfinal = engine.setUnion(rtemp, rtemp2); + + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) >= stoi(c)) + { + rfinal.removeTuple(j - offset); + offset += 1; + } + } + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } } - return rfinal; - //} + } + if(input[0] == "&&") + { + input.erase(input.begin()); + Relation rtemp = rfinal; + rfinal = condition(input, rtemp, engine); + } + else if(input[0] == "||") + { + input.erase(input.begin()); + Relation rtemp = rfinal; + Relation rtemp2 = condition(input, r, engine); + rfinal = engine.setUnion(rtemp, rtemp2); + } + return rfinal; } tuple, Relation> expression(vector input, DBEngine &engine) @@ -395,10 +328,6 @@ tuple, Relation> expression(vector input, DBEngine &engin tuple, Relation> t(input, rfinal); if(input[0] == "select") { - /* - cout << "Not yet implemented." << endl; - exit(1); - */ input.erase(input.begin()); vector s; Relation r("TEMP"); @@ -448,16 +377,13 @@ tuple, Relation> expression(vector input, DBEngine &engin input.erase(input.begin()); get<0>(t) = input; get<1>(t) = rfinal; - //cout << rfinal.getSize() << endl; return t; } else if(input[0] == "(") { t = expression(input, engine); rfinal = engine.projection(temp, get<1>(t)); - //get<0>(t) = input; get<1>(t) = rfinal; - //cout << rfinal.getSize() << endl; return t; } } @@ -665,12 +591,6 @@ tuple, Relation> expression(vector input, DBEngine &engin vector showCMD(vector input, DBEngine &engine) { - /* - if (input.size() > 3) - { - cout<<"Syntax error!"< saveCMD(vector input, DBEngine &engine) { cout<<"Syntax error!"< att = r.getAttributes(); - for (int i = 0; i < att.size(); ++i) - { - output << "-------------\n"; - output << att[i].getName() << "\n" << att[i].getType(); - if(att[i].getType() == "VARCHAR") - { - output << "(" << att[i].getSize() << ")"; - } - output << "\n\n"; - - vector values = att[i].getValues(); - vector::iterator it = values.begin(); - while (it != values.end()) - { - output << *it << "\n"; - it++; - } - output << "-------------\n"; - } - output << "\n--------------------------"; - */ input.erase(input.begin()); return input; @@ -739,10 +625,6 @@ vector closeCMD(vector input, DBEngine &engine) { cout<<"Syntax error!"< openCMD(vector input, DBEngine &engine) { cout<<"Syntax error!"< exitCMD(vector input, DBEngine &engine) vector createCMD(vector input, DBEngine &engine) { - //relation name will be the first element of the vector of data returned by this function if (input[0] == "TABLE") { @@ -798,8 +675,6 @@ vector createCMD(vector input, DBEngine &engine) { input.erase(input.begin()); - //vector e1; - vector a; while(input[0] != ")") //inserting all values to relation @@ -830,8 +705,6 @@ vector createCMD(vector input, DBEngine &engine) a.push_back(temp); } - //vector apk; //save primary keys temp storage - if(input[0] == "PRIMARY" && input[1] == "KEY") { input.erase(input.begin()); @@ -842,17 +715,12 @@ vector createCMD(vector input, DBEngine &engine) while(input[0] != ")") //inserting all values to relation { - //PAttribute temp; if (input[0] == ",") { input.erase(input.begin()); } - //temp.setPAttributeName(input[0]); - - //apk.push_back(temp); - for(int i = 0; i < a.size(); ++i) { if(input[0] == a[i].getPAttribute()) @@ -940,7 +808,6 @@ vector insertCMD(vector input, DBEngine &engine) if(input[0].at(0) == '\"') { s.push_back(input[0].substr(1,input[0].find_last_of("\"") - 1 )); - //engine.getTableFromName(pr.getName()).getAttributes()[i].display(); input.erase(input.begin()); } else @@ -1087,27 +954,6 @@ vector updateCMD(vector input, DBEngine &engine) vector s; if(input[0] == "(") { - //PCondition c; - /* - PComparison c; - POperand oops1; - POperand oops2; - POp op; - - while(input[0] != ")") - { - oops1.setPOperand(input[0]); - input.erase(input.begin()); - - oops2.setPOperand(input[0]); - input.erase(input.begin()); - - op.setPOp(input[0]); - input.erase(input.begin()); - - c.setPComparison(oops1.getPOperand(), op.getPOp(), oops2.getPOperand()); - } - */ input.erase(input.begin()); while(input[0] != ")") { @@ -1176,7 +1022,6 @@ vector query(vector input, DBEngine &engine) tuple, Relation> t = expression(input, engine); Relation r = get<1>(t); r.setTableName(pr.getName()); - //r.display(); input = get<0>(t); engine.createTable(r); @@ -1205,8 +1050,6 @@ void par_line(vector input, DBEngine &engine) //calls par_command() or p cout<<"\nPassing the following arguments to dbEngine: \nCommand: "< insertInput = insertCMD(input, engine); - //cout<<"arguments: "< input, DBEngine &engine) //calls par_command() or p input.erase(input.begin()); vector insertInput = createCMD(input, engine); - //cout<<"arguments: "< input, DBEngine &engine) //calls par_command() or p input.erase(input.begin()); vector insertInput = deleteCMD(input, engine); - //cout<<"arguments: "< input, DBEngine &engine) //calls par_command() or p input.erase(input.begin()); vector insertInput = updateCMD(input, engine); - //cout<<"arguments: "< listOfTokens = tokenize(input); par_line(listOfTokens, engine); } -/* -int main () { - - - string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;"; - string ss2 = "SHOW Dogs ;"; - string ss3 = "EXIT ; "; - - vector listOfTokens = tokenize(ss); - vector listOfTokens2 = tokenize(ss2); - vector listOfTokens3 = tokenize(ss3); - - par_line(listOfTokens); - par_line(listOfTokens2); - par_line(listOfTokens3); - - -} -*/ diff --git a/Parser.h b/Parser.h index e6422ca..08f37da 100644 --- a/Parser.h +++ b/Parser.h @@ -416,493 +416,4 @@ class PExpression } }; -/* -vector tokenize(string ss) -{ - - string tempString; - stringstream lineStream(ss); - vector output; - - while (lineStream >> tempString) - { - output.push_back(tempString); - } - - //testing--------------- - cout<<"TokenList: "; - - for (int i = 0; i input) -{ - cout<<"TokenList: "< showCMD(vector input) -{ - if (input.size() > 3) - { - cout<<"Syntax error!"< saveCMD(vector input) -{ - if (input.size() > 3) - { - cout<<"Syntax error!"< closeCMD(vector input) -{ - if (input.size() > 3) - { - cout<<"Syntax error!"< openCMD(vector input) -{ - if (input.size() > 2) - { - cout<<"Syntax error!"< exitCMD(vector input) -{ - if (input[1] != ";") - { - cout<<"ERROR: missing semicolon!"< createCMD(vector input) -{ - //relation name will be the first element of the vector of data returned by this function - - if (input[0] == "CREATE" && input[1] == "TABLE") - { - input.erase(input.begin()); - input.erase(input.begin()); - - PRelation r; - r.setPRelation(input[0]); - - input.erase(input.begin()); - - if(input[0] == "(") - { - input.erase(input.begin()); - - //vector e1; - - vector a; - - while(input[0] != ")") //inserting all values to relation - { - PAttribute temp; - - if (input[0] == ",") - { - input.erase(input.begin()); - } - - temp.setPAttributeName(input[0]); - - input.erase(input.begin()); - - if(input[0] == "INTEGER") - { - temp.setPAttributeType(input[0]); - input.erase(input.begin()); - } - else - { - temp.setPAttributeType(input[0].substr(0,input[0].find("("))); - temp.setPAttributeSize(stoi(input[0].substr(input[0].find("("), input[0].find(")")))); - input.erase(input.begin()); - } - - a.push_back(temp); - } - - vector apk; //save primary keys temp storage - - if(input[0] == "PRIMARY" && input[1] == "KEY") - { - input.erase(input.begin()); - input.erase(input.begin()); - - if(input[0] == "(") - { - - while(input[0] != ")") //inserting all values to relation - { - PAttribute temp; - - if (input[0] == ",") - { - input.erase(input.begin()); - } - - temp.setPAttributeName(input[0]); - - apk.push_back(temp); - - input.erase(input.begin()); - } - - - - return input; - } - } - } - - else cout<<"Syntax error!"< insertCMD(vector input) -{ - //relation name will be the first element of the vector of data returned by this function - - if (input[0] == "INTO") - { - input.erase(input.begin()); - - PRelation r(input[0]); - - input.erase(input.begin()); - - vector s; - - if (input[0] == "VALUES" && input[1] == "FROM") - { - input.erase(input.begin()); - input.erase(input.begin()); - - - if(input[0] == "(") - { - if(input[0].at(0) == '\"') - { - s.push_back(input[0].substr(1,input[0].find_last_of("\""))); - } - vector e; - input.erase(input.begin()); - - while(input[0] != ")") //inserting all values to relation - //for (int i = 0; i < 2; ++i) - { - if (input[0] == ",") input.erase(input.begin()); - - e.push_back(input[0]); - - input.erase(input.begin()); - } - - cout << "Inserting: "; - while(!e.empty()) - { - cout << e[0].getPExpression() << " "; - e.erase(e.begin()); - } - cout << "into " << r.getName() << ".\n"; - - return input; - - } - - else if (input[0] == "RELATION") - { - input.erase(input.begin()); - - PExpression e; - - while(input[0] != ";") - { - e.setPExpression(e.getPExpression() + input[0]); - } - - cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n"; - - return input; - } - - else cout<<"Syntax error!"< updateCMD(vector input) -{ - PRelation r(input[0]); - - input.erase(input.begin()); - - if(input[0] == "SET") - { - input.erase(input.begin()); - - //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(temp); - temp.setPAttributeName("~"); - - input.erase(input.begin()); - - if(input[0] == "=") - { - input.erase(input.begin()); - - s.push_back(input[0]); - } - - else - { - cout<<"Syntax error!"< deleteCMD(vector input) -{ - if (input[0] == "DELETE" && input[1] == "FROM") - { - input.erase(input.begin()); - input.erase(input.begin()); - - PRelation r(input[0]); - - if(input[0] == "WHERE") - { - if(input[0] == "(") - { - //PCondition c; - - PComparison c; - POperand oops1; - POperand oops2; - POp op; - - while(input[0] != ")") - { - oops1.setPOperand(input[0]); - input.erase(input.begin()); - - oops2.setPOperand(input[0]); - input.erase(input.begin()); - - op.setPOp(input[0]); - input.erase(input.begin()); - - c.setPComparison(oops1.getPOperand(), op.getPOp(), oops2.getPOperand()); - } - } - } - - // send delete command to DBEngine - } - else cout<<"Syntax error!"< input) //calls par_command() or par_query() depending on first item from token list -{ -/* -• Match the first item in the token list and determine weather this is a command or a query. -• Call functions par_command() or par_query(); -• After either par_command() or par_query() returns, make sure the line ends properly with “;” token - - string tempChar = input.back(); - - if (tempChar != ";") - { - cout<<"ERROR! missing semicolon "< insertInput = insertCMD(input); - cout<<"arguments: "< insertInput = createCMD(input); - cout<<"arguments: "< insertInput = deleteCMD(input); - cout<<"arguments: "< insertInput = updateCMD(input); - cout<<"arguments: "< listOfTokens = tokenize(input); - par_line(listOfTokens); -} -*/ - void parse(string s, DBEngine &e); \ No newline at end of file diff --git a/test b/test index 8833e9b..c4326f6 100755 Binary files a/test and b/test differ diff --git a/test.cpp b/test.cpp index 1f34d4c..ff4d2cf 100755 --- a/test.cpp +++ b/test.cpp @@ -8,46 +8,6 @@ using namespace std; int main () { DBEngine engine; - - 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("Dessert", "VARCHAR(20)", true); - Attribute att5("MidnightSnack", "VARCHAR(20)", false); - - att4.addCell("IceCreamSundae"); - att4.addCell("ChocolateBar"); - att5.addCell("HummusandCarrots"); - att5.addCell("PotatoChips"); - - vector v2; - v2.push_back(att4); - v2.push_back(att5); - - engine.createTable("MoarFood", v2); - - //engine.getTableFromName("Food").display(); - //engine.getTableFromName("MoarFood").display(); - - engine.crossProduct(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); string x; cout << "Enter DBMS Commands: "; while(getline(cin, x))