diff --git a/Condition.cpp b/Condition.cpp new file mode 100755 index 0000000..80e74b7 --- /dev/null +++ b/Condition.cpp @@ -0,0 +1,59 @@ +#include +#include "Attribute.h" +#include "Relation.h" + +using namespace std; + +Relation equality(string attName, string s, Relation r){ + Attribute att = r.getAttributeByName(attName); + vector r_atts = r.getAttributes(); + vector new_atts = r_atts; + + for (int i = 0; i < new_atts.size(); ++i) { + new_atts[i].clearAllValues(); + } + + for (int i = 0; i < att.getSize(); ++i) { + if (att[i] == s){ + for (int j = 0; j < r_atts.size(); ++j){ + new_atts[j].addCell(r_atts[j][i]); + } + } + } + + //currently all returned relations are called TEMP + Relation new_r("TEMP", new_atts); + return new_r; +} + +/* +vector equality(Attribute a, int i){ + for (int i = 0; i < a.getSize(); ++i) { + // + } +} + +vector gt(Attribute a, int i){ + for (int i = 0; i < a.getSize(); ++i) { + // + } +} + +vector lt(Attribute a, int i){ + for (int i = 0; i < a.getSize(); ++i) { + // + } +} + +vector gte(Attribute a, int i){ + for (int i = 0; i < a.getSize(); ++i) { + // + } +} + +vector lte(Attribute a, int i){ + for (int i = 0; i < a.getSize(); ++i) { + // + } +} +*/ diff --git a/DBEngine.cpp b/DBEngine.cpp index 21ba967..c2dcf20 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -45,8 +45,11 @@ vector DBEngine::getRelations(){ return tables; } +<<<<<<< HEAD <<<<<<< HEAD ======= +======= +>>>>>>> master >>>>>>> master Relation& DBEngine::getTableFromName(string n){ @@ -62,6 +65,11 @@ Relation DBEngine::selection(string attName, string s, Relation 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); } @@ -83,6 +91,7 @@ Relation DBEngine::projection(vector input, Relation r){ Relation temp(new_name, v); return temp; <<<<<<< HEAD +<<<<<<< HEAD } Relation DBEngine::product(string new_name, Relation r1, Relation r2){ @@ -107,6 +116,8 @@ Relation DBEngine::product(string new_name, Relation r1, Relation r2){ return temp; ======= >>>>>>> master +======= +>>>>>>> master } //test error matching @@ -129,6 +140,7 @@ void DBEngine::rename(Relation& r, vector oldnames, vector newna } <<<<<<< HEAD +<<<<<<< HEAD /*Relation DBEngine::setUnion(Relation r1, Relation r2){ @@ -140,6 +152,8 @@ void DBEngine::rename(Relation& r, vector oldnames, vector newna else { vector r1_atts = r1.getAttributes(); ======= +======= +>>>>>>> master Relation DBEngine::setUnion(Relation r1, Relation r2){ if (r1.getAttributeNames() != r2.getAttributeNames()){ cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n"; @@ -176,6 +190,9 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){ /*vector r1_atts = r1.getAttributes(); +<<<<<<< HEAD +>>>>>>> master +======= >>>>>>> master vector r2_atts = r2.getAttributes(); vector new_atts = r1_atts; @@ -197,6 +214,7 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){ //currently all returned relations are called TEMP Relation new_r("TEMP", new_atts); +<<<<<<< HEAD <<<<<<< HEAD return new_r; } @@ -206,3 +224,8 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){ } } >>>>>>> master +======= + return new_r;*/ + } +} +>>>>>>> master diff --git a/DBEngine.h b/DBEngine.h index ce1702c..acdbe4e 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -23,10 +23,14 @@ public: Relation projection(vector input, Relation r); Relation product(string s1, Relation r1, Relation r2); void rename(Relation& r, vector oldnames, vector newnames); +<<<<<<< HEAD <<<<<<< HEAD void save(); void storeCommands(string s); //Relation setUnion(Relation r1, Relation r2); +======= + Relation setUnion(Relation r1, Relation r2); +>>>>>>> master ======= Relation setUnion(Relation r1, Relation r2); >>>>>>> master diff --git a/DevelopmentLog.pdf b/DevelopmentLog.pdf new file mode 100755 index 0000000..6d6db27 Binary files /dev/null and b/DevelopmentLog.pdf differ diff --git a/Parserv3.h b/Parserv3.h index 0ddb5a7..cd5c580 100644 --- a/Parserv3.h +++ b/Parserv3.h @@ -10,6 +10,9 @@ using namespace std; <<<<<<< HEAD ======= +<<<<<<< HEAD +>>>>>>> master +======= >>>>>>> master class PRelation { @@ -266,6 +269,7 @@ class PComparison public: PComparison() +<<<<<<< HEAD <<<<<<< HEAD { op.setPOp("~"); @@ -275,6 +279,8 @@ class PComparison PComparison(string str1, string str2, string str3) ======= +======= +>>>>>>> master { op.setPOp("~"); operand1.setPOperand("~"); @@ -289,6 +295,9 @@ class PComparison } void setPComparison(string str1, string str2, string str3) +<<<<<<< HEAD +>>>>>>> master +======= >>>>>>> master { operand1.setPOperand(str1); @@ -296,6 +305,7 @@ class PComparison operand2.setPOperand(str3); } +<<<<<<< HEAD <<<<<<< HEAD void setPComparison(string str1, string str2, string str3) { @@ -306,6 +316,10 @@ class PComparison string getPComparison() { +======= + string getPComparison() + { +>>>>>>> master ======= string getPComparison() { @@ -418,6 +432,7 @@ class PExpression } }; <<<<<<< HEAD +<<<<<<< HEAD vector tokenize(string ss) @@ -909,3 +924,5 @@ void parse(string input, DBEngine &engine) ======= >>>>>>> master +======= +>>>>>>> master diff --git a/Parserv4.cpp b/Parserv4.cpp new file mode 100644 index 0000000..0732896 --- /dev/null +++ b/Parserv4.cpp @@ -0,0 +1,505 @@ +#include // std::string +#include // std::cout +#include // std::stringstream +#include +#include +#include "Parserv3.h" + +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: "< 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(ss); + vector listOfTokens2 = tokenize(ss2); + vector listOfTokens3 = tokenize(ss3); + + par_line(listOfTokens); + par_line(listOfTokens2); + par_line(listOfTokens3); + + +} diff --git a/test b/test new file mode 100755 index 0000000..bb0df00 Binary files /dev/null and b/test differ diff --git a/test.cpp b/test.cpp index 8ff77f2..42bbc52 100755 --- a/test.cpp +++ b/test.cpp @@ -1,10 +1,14 @@ #include #include <<<<<<< HEAD +<<<<<<< HEAD #include "Parserv3.h" ======= #include "Condition.h" >>>>>>> master +======= +#include "Condition.h" +>>>>>>> master #include "DBEngine.h" using namespace std; @@ -32,6 +36,7 @@ int main() { v.push_back(att2); v.push_back(att3); +<<<<<<< HEAD <<<<<<< HEAD Relation r("Food", v); //r.renameAttribute("Breakfast", "BFST"); @@ -100,6 +105,9 @@ int main () { ======= engine.createTable("Food", v); +======= + engine.createTable("Food", v); +>>>>>>> master Attribute att4("Breakfast", "VARCHAR(20)", true); Attribute att5("Lunch", "VARCHAR(20)", false); @@ -123,5 +131,8 @@ int main () { engine.createTable("MoarFood", v2); engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); +<<<<<<< HEAD +>>>>>>> master +======= >>>>>>> master }