#include // std::string #include // std::cout #include // std::ofstream #include // std::stringstream #include #include #include "Parser.h" #include using namespace std; 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: "<, Relation> expression(vector input, DBEngine &engine) { Relation rfinal("TEMP"); tuple, Relation> t(input, rfinal); if(input[0] == "select") { } else if(input[0] == "project") { input.erase(input.begin()); if(input[0] == "(") { input.erase(input.begin()); vector temp; while(input[0] != ")") { temp.push_back(input[0]); input.erase(input.begin()); } input.erase(input.begin()); Relation rfinal("TEMP"); if(engine.isRelation(input[0])) { rfinal = engine.projection(temp, engine.getTableFromName(input[0])); input.erase(input.begin()); get<0>(t) = input; get<1>(t) = rfinal; 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; return t; } } else { cout << "Projection syntax incorrect." << endl; exit(1); } } else if(input[0] == "rename") { input.erase(input.begin()); if(input[0] == "(") { input.erase(input.begin()); vector temp; while(input[0] != ")") { if(input[0] == ",") { input.erase(input.begin()); } temp.push_back(input[0]); input.erase(input.begin()); } input.erase(input.begin()); Relation rfinal("TEMP"); if(engine.isRelation(input[0])) { rfinal = engine.rename(temp, engine.getTableFromName(input[0])); input.erase(input.begin()); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { input.erase(input.begin()); t = expression(input, engine); input.erase(input.begin()); rfinal = engine.rename(temp, get<1>(t)); get<0>(t) = input; get<1>(t) = rfinal; return t; } } else { cout << "Rename syntax incorrect." << endl; exit(1); } } else if(engine.isRelation(input[0])) { Relation r1(engine.getTableFromName(input[0])); input.erase(input.begin()); if(input[0] == "+") { input.erase(input.begin()); if(engine.isRelation(input[0])) { Relation r2(engine.getTableFromName(input[0])); rfinal = engine.setUnion(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); Relation r2 = get<1>(t); rfinal = engine.setUnion(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } } else if(input[0] == "-") { input.erase(input.begin()); if(engine.isRelation(input[0])) { Relation r2(engine.getTableFromName(input[0])); rfinal = engine.setDiff(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); Relation r2 = get<1>(t); rfinal = engine.setDiff(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } } else if(input[0] == "*") { input.erase(input.begin()); if(engine.isRelation(input[0])) { Relation r2(engine.getTableFromName(input[0])); rfinal = engine.crossProduct(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); Relation r2 = get<1>(t); rfinal = engine.crossProduct(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } } } else if(input[0] == "(") { input.erase(input.begin()); Relation r1(engine.getTableFromName(input[0])); input.erase(input.begin()); if(input[0] == "+") { input.erase(input.begin()); if(engine.isRelation(input[0])) { Relation r2(engine.getTableFromName(input[0])); rfinal = engine.setUnion(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); Relation r2 = get<1>(t); rfinal = engine.setUnion(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } } else if(input[0] == "-") { input.erase(input.begin()); if(engine.isRelation(input[0])) { Relation r2(engine.getTableFromName(input[0])); rfinal = engine.setDiff(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); Relation r2 = get<1>(t); rfinal = engine.setDiff(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } } else if(input[0] == "*") { input.erase(input.begin()); if(engine.isRelation(input[0])) { Relation r2(engine.getTableFromName(input[0])); rfinal = engine.crossProduct(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); Relation r2 = get<1>(t); rfinal = engine.crossProduct(r1, r2); get<0>(t) = input; get<1>(t) = rfinal; return t; } } } get<0>(t) = input; get<1>(t) = rfinal; return t; } vector showCMD(vector input, DBEngine &engine) { /* if (input.size() > 3) { cout<<"Syntax error!"<, Relation> t = expression(input, engine); get<1>(t).display(); } return input; } vector saveCMD(vector input, DBEngine &engine) { if (input.size() > 2) { 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; } vector closeCMD(vector input, DBEngine &engine) { if (input.size() > 3) { cout<<"Syntax error!"< openCMD(vector input, DBEngine &engine) { if (input.size() > 2) { cout<<"Syntax error!"< exitCMD(vector input, DBEngine &engine) { exit(0); return input; } 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") { 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("(") + 1, 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); for(int i = 0; i < a.size(); ++i) { if(input[0] == a[i].getPAttribute()) { a[i].setPAttributeKey(); } } input.erase(input.begin()); } } } vector dba; for(int x = 0; x < a.size(); ++x) { Attribute temp(a[x].getPAttribute(), a[x].getPAttributeType(), a[x].getPAttributeKey(), a[x].getPAttributeSize()); dba.push_back(temp); } engine.createTable(r.getName(), dba); return input; } else cout<<"Syntax error! 2"< 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") { input.erase(input.begin()); PRelation pr(input[0]); Relation r = engine.getTableFromName(input[0]); input.erase(input.begin()); vector s; if (input[0] == "VALUES" && input[1] == "FROM") { input.erase(input.begin()); input.erase(input.begin()); vector a = r.getAttributes(); if(input[0] == "(") { input.erase(input.begin()); for(int i = 0; i < a.size(); ++i) { if(a[i].getType() == "INTEGER") { if(input[0].at(0) == '\"') { cout << "Incorrect type matching. Tried to insert string." << endl; cout << "The values should be: "; for(int x = 0; x < a.size(); ++i) { cout << a[x].getType(); if(a[x].getType() == "VARCHAR") { cout << "(" << a[x].getSize() << ")"; } cout << endl; } exit(1); } s.push_back(input[0]); input.erase(input.begin()); } else { 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 { cout << "Incorrect type matching. Tried to insert integer." << endl; cout << "The values should be: "; for(int x = 0; x < a.size(); ++x) { cout << a[x].getType(); if(a[x].getType() == "VARCHAR") { cout << "(" << a[x].getSize() << ")"; } cout << endl; } exit(1); } } if (input[0] == ",") { input.erase(input.begin()); } } if(input[0] != ")") { cout << "Too many values trying to be inserted. Only insert " << a.size() << " values."; cout << "The values should be: "; for(int x = 0; x < a.size(); ++x) { cout << a[x].getType(); if(a[x].getType() == "VARCHAR") { cout << "(" << a[x].getSize() << ")"; } cout << endl; } exit(1); } engine.insertValues(r.getTableName(), s); input.erase(input.begin()); return input; } else if (input[0] == "RELATION") { input.erase(input.begin()); tuple, Relation> t = expression(input, engine); engine.createTable(get<1>(t)); input = get<0>(t); return input; } else cout<<"Syntax error! 3"< updateCMD(vector input, DBEngine &engine) { 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! 2"< deleteCMD(vector input, DBEngine &engine) { 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!"< query(vector input, DBEngine &engine) { PRelation pr(input[0]); input.erase(input.begin()); input.erase(input.begin()); tuple, Relation> t = expression(input, engine); Relation r = get<1>(t); r.setTableName(pr.getName()); //r.display(); input = get<0>(t); engine.createTable(r); return input; } void par_line(vector input, DBEngine &engine) //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, engine); //cout<<"arguments: "< insertInput = createCMD(input, engine); //cout<<"arguments: "< insertInput = deleteCMD(input, engine); //cout<<"arguments: "< 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); } */