diff --git a/Parserv3.h b/Parserv3.h index 1fb63d7..dea692c 100644 --- a/Parserv3.h +++ b/Parserv3.h @@ -3,246 +3,880 @@ #include // std::stringstream #include #include +#include "DBEngine.h" using namespace std; - -class Relation +class PRelation { string name; - Relation(string str) - { - name = str; - } - - void setRelation(string str) - { - name = str; - } - string getName() - { - return name; - } + public: + PRelation() + { + name = "~"; + } + + PRelation(string str) + { + name = str; + } + + void setPRelation(string str) + { + name = str; + } + string getName() + { + return name; + } }; +class PAttribute +{ + string name; + string type; + int size; + + public: + PAttribute() + { + name = "~"; + type = "~"; + size = 0; + } + + PAttribute(string str, string t) + { + name = str; + type = t; + size = 0; + } + + PAttribute(string str, string t, int s) + { + name = str; + type = t; + size = s; + } + + void setPAttributeName(string str) + { + name = str; + } + + void setPAttributeType(string t) + { + type = t; + } + + void setPAttributeSize(int s) + { + size = s; + } + + string getPAttribute() + { + return name; + } +}; - -class Union +class PUnion { string Un1; string Un2; - Union (string s1, string s2) - { - Un1 = s1; - Un2 = s2; - } - - string getUnion() - { - return "Union of " + Un1 + " and " + Un2; - } + public: + PUnion() + { + Un1 = "~"; + Un2 = "~"; + } + + PUnion (string s1, string s2) + { + Un1 = s1; + Un2 = s2; + } + + string getPUnion() + { + return "Union of " + Un1 + " and " + Un2; + } }; -class Product +class PProduct { string Pr1; string Pr2; - Product(string s1, string s2) - { - Pr1 = s1; - Pr2 = s2; - } - - string getProduct() - { - return "Product of " + Pr1 + " and " + Pr2; - } + public: + PProduct() + { + Pr1 = "~"; + Pr2 = "~"; + } + + PProduct(string s1, string s2) + { + Pr1 = s1; + Pr2 = s2; + } + + string getPProduct() + { + return "Product of " + Pr1 + " and " + Pr2; + } }; -class Difference +class PDifference { string D1; string D2; - Difference(string s1, string s2) - { - D1 = s1; - D2 = s2; - } - - string getDifference() - { - return "Difference of " + D1 + " and " + D2; - } + public: + PDifference() + { + D1 = "~"; + D2 = "~"; + } + + PDifference(string s1, string s2) + { + D1 = s1; + D2 = s2; + } + + string getPDifference() + { + return "Difference of " + D1 + " and " + D2; + } }; -class Renaming +class PRenaming { string newName; string oldName; - Renaming(string s1, string s2) - { - newName = s1; - oldName = s2; - } - - string doRename() - { - return "Renaming " + oldName + " to " + newName; - } + public: + PRenaming() + { + newName = "~"; + oldName = "~"; + } + + PRenaming(string s1, string s2) + { + newName = s1; + oldName = s2; + } + + string doRename() + { + return "Renaming " + oldName + " to " + newName; + } }; -class Projection +class PProjection { string newName; string oldName; - Projection(string s1, string s2) - { - newName = s1; - oldName = s2; - } - - string doProjection() - { - return "Projecting " + oldName + " onto " + newName; - } + public: + PProjection() + { + newName = "~"; + oldName = "~"; + } + + PProjection(string s1, string s2) + { + newName = s1; + oldName = s2; + } + + string doPProjection() + { + return "Projecting " + oldName + " onto " + newName; + } }; -class Operand +class POperand { string op; public: - Operand() + POperand() { + op = "~"; } - Operand(string str) + POperand(string str) { op = str; } - void setOperand(string str) + void setPOperand(string str) { op = str; } - string getOperand() + string getPOperand() { return op; } }; -class Op +class POp { string op; public: - Op() + POp() { + op = "~"; } - Op(string str) + POp(string str) { op = str; } - void setOp(string str) + void setPOp(string str) { op = str; } - string getOp() + string getPOp() { return op; } }; -class Comparison +class PComparison { - Op op; - Operand operand1; - Operand operand2; + POp op; + POperand operand1; + POperand operand2; public: - - Comparison(string str1, string str2, string str3) + PComparison() { - operand1.setOperand(str1); - op.setOp(str2); - operand2.setOperand(str3); + op.setPOp("~"); + operand1.setPOperand("~"); + operand2.setPOperand("~"); } - void setComparison(string str1, string str2, string str3) + PComparison(string str1, string str2, string str3) { - operand1.setOperand(str1); - op.setOp(str2); - operand2.setOperand(str3); + operand1.setPOperand(str1); + op.setPOp(str2); + operand2.setPOperand(str3); } - string getComparison() + void setPComparison(string str1, string str2, string str3) { - return operand1.getOperand() + " " + op.getOp() + " " + operand2.getOperand(); + operand1.setPOperand(str1); + op.setPOp(str2); + operand2.setPOperand(str3); + } + + string getPComparison() + { + return operand1.getPOperand() + " " + op.getPOp() + " " + operand2.getPOperand(); } }; -class Conjunction +class PConjunction { string conj; public: - Conjunction(string str) + PConjunction() + { + conj = "~"; + } + + PConjunction(string str) { conj = str; } - void setConjunction(string str) + void setPConjunction(string str) { conj = str; } - string getConjunction() + string getPConjunction() { return conj; } }; -class Condition +class PCondition { string cond; public: - Condition(string str) + + PCondition() + { + cond = "~"; + } + + PCondition(string str) { cond = str; } - void setCondition(string str) + void setPCondition(string str) { cond = str; } - string getCondition() + string getPCondition() { return cond; } }; -class Selection +class PSelection { string select; public: - Selection(string str) + PSelection() + { + select = "~"; + } + + PSelection(string str) { select = str; } - string getSelection() + string getPSelection() { return select; } }; + +class PExpression +{ + PRelation rel; + PSelection sel; + PProjection proj; + PRenaming ren; + PUnion un; + PDifference diff; + PProduct prod; + string temp; + + public: + + PExpression() + { + } + + PExpression(string str) + { + temp = str; + } + + void setPExpression(string str) + { + temp = str; + } + + string getPExpression() + { + return temp; + } +}; + + +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); +} +