diff --git a/Attribute.cpp b/Attribute.cpp index 5c2a0d6..a87ee9e 100755 --- a/Attribute.cpp +++ b/Attribute.cpp @@ -16,6 +16,13 @@ Attribute::Attribute(string n, string t, bool k){ size = 0; } +Attribute::Attribute(string n, string t, bool k, int s){ + name = n; + type = t; + key = k; + size = s; +} + void Attribute::addCell(string v){ values.push_back(v); size++; @@ -56,7 +63,12 @@ int Attribute::getSize(){ void Attribute::display(){ cout << "-------------\n"; - cout << name << "\n" << type << "\n\n"; + cout << name << "\n" << type; + if(type == "VARCHAR") + { + cout << "(" << size << ")"; + } + cout << "\n\n"; vector::iterator it = values.begin(); while (it != values.end()){ diff --git a/Attribute.h b/Attribute.h index 9bb84fd..19881ce 100755 --- a/Attribute.h +++ b/Attribute.h @@ -14,6 +14,7 @@ class Attribute{ public: Attribute(); Attribute(string n, string t, bool k); + Attribute(string n, string t, bool k, int s); void addCell(string v); void removeCell(int index); string operator[](int i); diff --git a/DBEngine.cpp b/DBEngine.cpp index 3435d56..951ab45 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -25,6 +25,17 @@ void DBEngine::createTable(Relation r){ size++; } +void DBEngine::insertValues(string r, vector v) +{ + for(int i = 0; i < tables.size(); i++) + { + if (tables[i].getTableName() == r) + { + tables[i].insertTuple(v); + } + } +} + void DBEngine::storeCommands(string s){ commands.push_back(s); } @@ -51,23 +62,14 @@ Relation& DBEngine::getTableFromName(string n){ return tables[i]; } } + cout << "No Relation with this name." << endl; + exit(1); } Relation DBEngine::selection(string attName, string s, Relation r){ equality(attName, s, r); } -/* -Relation DBEngine::selection(string attName, string s, Relation r){ - equality(attName, s, r); -} - - -Relation DBEngine::selection(string attName, string s, Relation r){ - equality(attName, s, r); -} -*/ - //assumes that all attribute titles are unique Relation DBEngine::projection(vector input, Relation r){ diff --git a/DBEngine.h b/DBEngine.h index 11531a6..114ccad 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -15,6 +15,7 @@ public: void createTable(string n); void createTable(string n, vector a); void createTable(Relation r); + void insertValues(string r, vector v); vector getRelations(); //void showTable(Relation r); Relation& getTableFromName(string n); diff --git a/Parserv3.h b/Parserv3.h index c6d52fa..e6422ca 100644 --- a/Parserv3.h +++ b/Parserv3.h @@ -36,6 +36,7 @@ class PAttribute { string name; string type; + bool key; int size; public: @@ -43,6 +44,7 @@ class PAttribute { name = "~"; type = "~"; + key = false; size = 0; } @@ -50,6 +52,7 @@ class PAttribute { name = str; type = t; + key = false; size = 0; } @@ -57,6 +60,7 @@ class PAttribute { name = str; type = t; + key = false; size = s; } @@ -70,6 +74,11 @@ class PAttribute type = t; } + void setPAttributeKey() + { + key = true; + } + void setPAttributeSize(int s) { size = s; @@ -79,6 +88,21 @@ class PAttribute { return name; } + + string getPAttributeType() + { + return type; + } + + bool getPAttributeKey() + { + return key; + } + + int getPAttributeSize() + { + return size; + } }; class PUnion diff --git a/Parserv4.cpp b/Parserv4.cpp index c70802a..38571d7 100644 --- a/Parserv4.cpp +++ b/Parserv4.cpp @@ -1,5 +1,6 @@ #include // std::string #include // std::cout +#include // std::ofstream #include // std::stringstream #include #include @@ -25,6 +26,7 @@ vector tokenize(string ss) { cout< input) } } -vector showCMD(vector input) +vector showCMD(vector input, DBEngine &engine) { if (input.size() > 3) { @@ -49,25 +51,58 @@ vector showCMD(vector input) PRelation r(input[0]); // send show command to DBEngine + engine.getTableFromName(r.getName()).display(); return input; } -vector saveCMD(vector input) +vector saveCMD(vector input, DBEngine &engine) { - if (input.size() > 3) + 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) +vector closeCMD(vector input, DBEngine &engine) { if (input.size() > 3) { @@ -81,7 +116,7 @@ vector closeCMD(vector input) return input; } -vector openCMD(vector input) +vector openCMD(vector input, DBEngine &engine) { if (input.size() > 2) { @@ -95,28 +130,22 @@ vector openCMD(vector input) return input; } -vector exitCMD(vector input) +vector exitCMD(vector input, DBEngine &engine) { - if (input[1] != ";") - { - cout<<"ERROR: missing semicolon!"< createCMD(vector 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] == "CREATE" && input[1] == "TABLE") + if (input[0] == "TABLE") { input.erase(input.begin()); - input.erase(input.begin()); PRelation r; r.setPRelation(input[0]); @@ -152,14 +181,14 @@ vector createCMD(vector input) else { temp.setPAttributeType(input[0].substr(0,input[0].find("("))); - temp.setPAttributeSize(stoi(input[0].substr(input[0].find("("), 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 + //vector apk; //save primary keys temp storage if(input[0] == "PRIMARY" && input[1] == "KEY") { @@ -171,34 +200,50 @@ vector createCMD(vector input) while(input[0] != ")") //inserting all values to relation { - PAttribute temp; + //PAttribute temp; if (input[0] == ",") { input.erase(input.begin()); } - temp.setPAttributeName(input[0]); + //temp.setPAttributeName(input[0]); - apk.push_back(temp); + //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()); } - - - - return input; } } + + 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!"< insertCMD(vector input) +vector insertCMD(vector input, DBEngine &engine) { //relation name will be the first element of the vector of data returned by this function @@ -206,7 +251,8 @@ vector insertCMD(vector input) { input.erase(input.begin()); - PRelation r(input[0]); + PRelation pr(input[0]); + Relation r = engine.getTableFromName(input[0]); input.erase(input.begin()); @@ -218,65 +264,115 @@ vector insertCMD(vector input) input.erase(input.begin()); + vector a = r.getAttributes(); + 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) + for(int i = 0; i < a.size(); ++i) { - if (input[0] == ",") input.erase(input.begin()); + 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()); + } - e.push_back(input[0]); + 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); + } + } - input.erase(input.begin()); + if (input[0] == ",") + { + input.erase(input.begin()); + } } - - cout << "Inserting: "; - while(!e.empty()) + if(input[0] != ")") { - cout << e[0].getPExpression() << " "; - e.erase(e.begin()); + 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); } - cout << "into " << r.getName() << ".\n"; - + engine.insertValues(r.getTableName(), s); + input.erase(input.begin()); return input; - } else if (input[0] == "RELATION") { input.erase(input.begin()); - PExpression e; + //PExpression e; - while(input[0] != ";") - { - e.setPExpression(e.getPExpression() + input[0]); - } + // while(input[0] != ";") + // { + // e.setPExpression(e.getPExpression() + input[0]); + // } - cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n"; + //cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n"; + + cout << "Not yet implemented." << endl; return input; } - else cout<<"Syntax error!"< updateCMD(vector input) +vector updateCMD(vector input, DBEngine &engine) { PRelation r(input[0]); @@ -316,7 +412,7 @@ vector updateCMD(vector input) else { - cout<<"Syntax error!"< updateCMD(vector input) // send update command to DBEngine } - else cout<<"Syntax error!"< deleteCMD(vector input) +vector deleteCMD(vector input, DBEngine &engine) { if (input[0] == "DELETE" && input[1] == "FROM") { @@ -400,7 +496,7 @@ vector deleteCMD(vector input) else cout<<"Syntax error!"< input) //calls par_command() or par_query() depending on first item from token list +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. @@ -420,7 +516,7 @@ void par_line(vector input) //calls par_command() or par_query() dependi { cout<<"\nPassing the following arguments to dbEngine: \nCommand: "< insertInput = insertCMD(input); + vector insertInput = insertCMD(input, engine); cout<<"arguments: "< input) //calls par_command() or par_query() dependi cout<<"\nPassing the following arguments to dbEngine: \nCommand: "< insertInput = createCMD(input); + vector insertInput = createCMD(input, engine); cout<<"arguments: "< input) //calls par_command() or par_query() dependi cout<<"\nPassing the following arguments to dbEngine: \nCommand: "< insertInput = deleteCMD(input); + vector insertInput = deleteCMD(input, engine); cout<<"arguments: "< input) //calls par_command() or par_query() dependi cout<<"\nPassing the following arguments to dbEngine: \nCommand: "< insertInput = updateCMD(input); + vector insertInput = updateCMD(input, engine); cout<<"arguments: "< input) //calls par_command() or par_query() dependi if ( input[0] == "SHOW") { - showCMD(input); + cout<<"\nPassing the following arguments to dbEngine: \nCommand: "< listOfTokens = tokenize(input); - par_line(listOfTokens); + par_line(listOfTokens, engine); } /* int main () { diff --git a/savefile.txt b/savefile.txt index 948b094..4a00946 100644 --- a/savefile.txt +++ b/savefile.txt @@ -1,3 +1,3 @@ -INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ; -SHOW Dogs ; -EXIT ; +SHOW Food ; +SHOW MoarFood ; +SAVE ; diff --git a/test b/test index a3e72f0..3161c6b 100755 Binary files a/test and b/test differ diff --git a/test.cpp b/test.cpp index 25c217c..25a26ab 100755 --- a/test.cpp +++ b/test.cpp @@ -72,11 +72,11 @@ int main() { */ int main () { - +/* string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;"; string ss2 = "SHOW Dogs ;"; string ss3 = "EXIT ; "; - + */ DBEngine engine; // vector listOfTokens = tokenize(ss); @@ -87,11 +87,11 @@ int main () { // par_line(listOfTokens2); // par_line(listOfTokens3); - parse(ss, engine); - parse(ss2, engine); - parse(ss3, engine); + // parse(ss, engine); + // parse(ss2, engine); + // parse(ss3, engine); - engine.save(); + // engine.save(); Attribute att1("Breakfast", "VARCHAR(20)", true); Attribute att2("Lunch", "VARCHAR(20)", false); @@ -135,8 +135,17 @@ int main () { engine.createTable("MoarFood", v2); + engine.getTableFromName("Food").display(); engine.getTableFromName("MoarFood").display(); - engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); - + //engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); + + string x; + cout << "Enter DBMS Commands: "; + while(getline(cin, x)) + { + //cout << x << endl; + parse(x, engine); + cout << "Enter DBMS Commands: "; + } }