diff --git a/DBEngine.cpp b/DBEngine.cpp index d028aea..a10368c 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -27,6 +27,16 @@ void DBEngine::createTable(Relation r){ vector DBEngine::getRelations(){ return tables; } + +//To check if a relation name is already used. +bool DBEngine::isRelation(string n){ + for(int i = 0; i < tables.size(); i++){ + if (tables[i].getTableName() == n){ + return true; + } + } + return false; +} Relation& DBEngine::getTableFromName(string n){ for(int i = 0; i < tables.size(); i++){ diff --git a/Parserv2.cpp b/Parserv2.cpp deleted file mode 100755 index cb848f3..0000000 --- a/Parserv2.cpp +++ /dev/null @@ -1,196 +0,0 @@ -#include -#include -#include -#include -#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: "< insertCMD(vector input) -{ - //relation name will be the first element of the vector of data returned by this function - vector output; - - if (input[0] == "INTO") - { - input.erase(input.begin()); - - output.push_back(input[0]); //pushing relation name - - input.erase(input.begin()); - - if (input[0] == "VALUES" && input[1] == "FROM") - { - input.erase(input.begin()); - input.erase(input.begin()); - - - if(input[0] == "(") - { - 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()); - - output.push_back(input[0]); - - input.erase(input.begin()); - } - - return output; - - } - - else cout<<"Syntax error!"< showCMD(vector input) -{ - if (input.size() > 3) - { - cout<<"Syntax error!"< exitCMD(vector input) -{ - if (input[1] != ";") - { - cout<<"ERROR: missing semicolon!"< createCMD(vector input) -{ - if (input[0] != "CREATE") { - cout << "Error: create keyword is missing." < openCMD(vector input){ - if (input[0] != "OPEN") { - cout << "Error: open keyword is missing." < closeCMD(vector input){ - if (input[0] != "CLOSE") { - cout << "Error: close keyword is missing." < saveCMD(vector input){ - if (input[0] != "SAVE") { - cout << "Error: save keyword is missing." < updateCMD(vector input){ - //UPDATE relation-name SET attribute-name = literal { , attribute-name = literal } WHERE condition -} - -vector deleteCMD(vector input){ - //DELETE FROM relation-name WHERE condition - if (input[0] != "DELETE") { - cout << "Error: save keyword is missing." < listOfTokens = tokenize(s); - deleteCMD(listOfTokens); -} \ No newline at end of file diff --git a/Parserv3.h b/Parserv3.h index d3f7812..0064a9b 100644 --- a/Parserv3.h +++ b/Parserv3.h @@ -7,22 +7,22 @@ using namespace std; -class Relation +class PRelation { string name; public: - Relation() + PRelation() { - name = NULL; + name = "~"; } - Relation(string str) + PRelation(string str) { name = str; } - void setRelation(string str) + void setPRelation(string str) { name = str; } @@ -32,117 +32,140 @@ class Relation } }; -class Attribute +class PAttribute { string name; + string type; + string size; public: - Attribute() + PAttribute() { - name = NULL; + name = "~"; + type = "~"; + size = "~"; } - Attribute(string str) + PAttribute(string str, string t) + { + name = str; + type = t; + size = "~"; + } + + PAttribute(string str, string t, string s) + { + name = str; + type = t; + size = s; + } + + void setPAttributeName(string str) { name = str; } - void setAttribute(string str) + void setPAttributeType(string t) { - name = str; + type = t; } - string getAttribute() + void setPAttributeSize(string s) + { + size = s; + } + + string getPAttribute() { return name; } }; -class Union +class PUnion { string Un1; string Un2; public: - Union() + PUnion() { - Un1 = NULL; - Un2 = NULL; + Un1 = "~"; + Un2 = "~"; } - Union (string s1, string s2) + PUnion (string s1, string s2) { Un1 = s1; Un2 = s2; } - string getUnion() + string getPUnion() { return "Union of " + Un1 + " and " + Un2; } }; -class Product +class PProduct { string Pr1; string Pr2; public: - Product() + PProduct() { - Pr1 = NULL; - Pr2 = NULL; + Pr1 = "~"; + Pr2 = "~"; } - Product(string s1, string s2) + PProduct(string s1, string s2) { Pr1 = s1; Pr2 = s2; } - string getProduct() + string getPProduct() { return "Product of " + Pr1 + " and " + Pr2; } }; -class Difference +class PDifference { string D1; string D2; public: - Difference() + PDifference() { - D1 = NULL; - D2 = NULL; + D1 = "~"; + D2 = "~"; } - Difference(string s1, string s2) + PDifference(string s1, string s2) { D1 = s1; D2 = s2; } - string getDifference() + string getPDifference() { return "Difference of " + D1 + " and " + D2; } }; -class Renaming +class PRenaming { string newName; string oldName; public: - Renaming() + PRenaming() { - newName = NULL; - oldName = NULL; + newName = "~"; + oldName = "~"; } - Renaming(string s1, string s2) + PRenaming(string s1, string s2) { newName = s1; oldName = s2; @@ -154,211 +177,217 @@ class Renaming } }; -class Projection +class PProjection { string newName; string oldName; public: - Projection() + PProjection() { - newName = NULL; - oldName = NULL; + newName = "~"; + oldName = "~"; } - Projection(string s1, string s2) + PProjection(string s1, string s2) { newName = s1; oldName = s2; } - string doProjection() + string doPProjection() { return "Projecting " + oldName + " onto " + newName; } }; -class Operand +class POperand { string op; public: - Operand() + POperand() { - op = NULL; + 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 = NULL; + 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() + PComparison() { - op = NULL; - operand1 = NULL; - operand2 = NULL; + op.setPOp("~"); + operand1.setPOperand("~"); + operand2.setPOperand("~"); } - Comparison(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); } - void setComparison(string str1, string str2, string str3) + void setPComparison(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() + string getPComparison() { - return operand1.getOperand() + " " + op.getOp() + " " + operand2.getOperand(); + return operand1.getPOperand() + " " + op.getPOp() + " " + operand2.getPOperand(); } }; -class Conjunction +class PConjunction { string conj; public: - Conjunction() + PConjunction() { - conj = NULL; + conj = "~"; } - Conjunction(string str) + 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() + PCondition() { - cond = NULL; + cond = "~"; } - Condition(string str) + 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() + PSelection() { - select = NULL; + select = "~"; } - Selection(string str) + PSelection(string str) { select = str; } - string getSelection() + string getPSelection() { return select; } }; -class Expression +class PExpression { - string exp; + PRelation rel; + PSelection sel; + PProjection proj; + PRenaming ren; + PUnion un; + PDifference diff; + PProduct prod; + string temp; public: - Expression() + PExpression() { - exp = NULL; } - Expression(string str) + PExpression(string str) { - exp = str; + temp = str; } - void setExpression(string str) + void setPExpression(string str) { - exp = str; + temp = str; } - string getExpression() + string getPExpression() { - return exp; + return temp; } }; diff --git a/Parserv4.cpp b/Parserv4.cpp index 8d5f17b..f6c0bb4 100644 --- a/Parserv4.cpp +++ b/Parserv4.cpp @@ -118,8 +118,8 @@ vector createCMD(vector input) input.erase(input.begin()); input.erase(input.begin()); - Relation r; - r.setRelation(input[0]); + PRelation r; + r.setPRelation(input[0]); input.erase(input.begin()); @@ -127,21 +127,39 @@ vector createCMD(vector input) { input.erase(input.begin()); - vector e1; + //vector e1; + + vector a; while(input[0] != ")") //inserting all values to relation { + PAttribute temp; + if (input[0] == ",") { input.erase(input.begin()); } - e1.push_back(input[0]); + 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(input[0].substr(input[0].find("("), input[0].find(")"))); + input.erase(input.begin()); + } + + a.push_back(temp); } - vector e2; + vector apk; //save primary keys temp storage if(input[0] == "PRIMARY" && input[1] == "KEY") { @@ -153,23 +171,21 @@ vector createCMD(vector input) while(input[0] != ")") //inserting all values to relation { + PAttribute temp; + if (input[0] == ",") { input.erase(input.begin()); } - e2.push_back(input[0]); + temp.setPAttributeName(input[0]); + + apk.push_back(temp); input.erase(input.begin()); } - cout << "Creating a table: " << r.getName() << "\n"; - cout << "Primary key: "; - while(!e2.empty()) - { - cout << e2[0].getExpression() << " "; - e2.erase(e2.begin()); - } + return input; } @@ -190,10 +206,12 @@ vector insertCMD(vector input) { input.erase(input.begin()); - Relation r(input[0]); + PRelation r(input[0]); input.erase(input.begin()); + vector s; + if (input[0] == "VALUES" && input[1] == "FROM") { input.erase(input.begin()); @@ -202,7 +220,11 @@ vector insertCMD(vector input) if(input[0] == "(") { - vector e; + 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 @@ -218,7 +240,7 @@ vector insertCMD(vector input) cout << "Inserting: "; while(!e.empty()) { - cout << e[0].getExpression() << " "; + cout << e[0].getPExpression() << " "; e.erase(e.begin()); } cout << "into " << r.getName() << ".\n"; @@ -231,14 +253,14 @@ vector insertCMD(vector input) { input.erase(input.begin()); - Expression e; + PExpression e; while(input[0] != ";") { - e.setExpression(e.getExpression() + input[0]); + e.setPExpression(e.getPExpression() + input[0]); } - cout << "Inserting: " << e.getExpression() << " into " << r.getName() << ".\n"; + cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n"; return input; } @@ -263,7 +285,7 @@ vector updateCMD(vector input) { input.erase(input.begin()); - Relation r(input[0]); + PRelation r(input[0]); input.erase(input.begin()); @@ -271,7 +293,7 @@ vector updateCMD(vector input) if(input[0] == "(") { - vector e; + vector e; input.erase(input.begin()); while(input[0] != ")") @@ -282,10 +304,10 @@ vector updateCMD(vector input) if(input[0] == "WHERE") { - Condition c; + PCondition c; while(input[0] != ";") { - c.setCondition(/*c.getCondition +*/ input[0]); + c.setPCondition(/*c.getCondition +*/ input[0]); } } } @@ -301,10 +323,10 @@ vector deleteCMD(vector input) input.erase(input.begin()); input.erase(input.begin()); - Relation r(input[0]); + PRelation r(input[0]); if(input[0] == "(") { - vector e; + vector e; input.erase(input.begin()); while(input[0] != ")") @@ -319,7 +341,7 @@ vector deleteCMD(vector input) cout << "Deleting: "; while(!e.empty()) { - cout << e[0].getExpression() << " "; + cout << e[0].getPExpression() << " "; e.erase(e.begin()); } cout << "from " << r.getName() << ".\n"; diff --git a/a.out b/a.out index 6a77a3d..a09701b 100755 Binary files a/a.out and b/a.out differ