diff --git a/Parser.cpp b/Parser.cpp index e7c8da1..2716df4 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -42,13 +42,313 @@ void displayTokenList(vector input) } } +Relation condition(vector input, Relation &r) +{ + Relation rfinal = r; + Attribute a1 = r.getAttributeByName(input[0]); + input.erase(input.begin()); + string op = input[0]; + input.erase(input.begin()); + Attribute a2; + string c; + //cout << input[0] << endl; + if(input[0].at(0) == '\"') + { + c = input[0].substr(1, input[0].find_last_of("\"") - 1); + //cout << c << endl; + } + if(r.isAttribute(input[0])) + { + a2 = r.getAttributeByName(input[0]); + input.erase(input.begin()); + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + /* + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size; ++j) + { + if(r.getAttributeByName(a1.getName()).getValues()[j] == ) + { + rfinal.insertTuple(r.getTuple(j)); + } + }*/ + if(op == "==") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x]) + { + rfinal.removeTuple(x); + //rfinal.insertAttributes(a); + } + } + } + else if(op == "!=") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]) + { + rfinal.removeTuple(x); + //rfinal.insertAttributes(a); + } + } + } + else if(op == ">=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x); + //rfinal.insertAttributes(a); + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x); + //rfinal.insertAttributes(a); + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == ">") + { + + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x); + //rfinal.insertAttributes(a); + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER") + { + for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) + { + rfinal.removeTuple(x); + //rfinal.insertAttributes(a); + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + } + } + } + else + { + input.erase(input.begin()); + if(op == "==") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(r.getAttributeByName(a1.getName()).getValues()[j] != c) + { + rfinal.removeTuple(j); + } + } + } + } + } + else if(op == "!=") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(r.getAttributeByName(a1.getName()).getValues()[j] == c) + { + rfinal.removeTuple(j); + } + } + } + } + } + else if(op == ">=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c)) + { + rfinal.removeTuple(j); + } + } + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<=") + { + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c)) + { + rfinal.removeTuple(j); + } + } + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == ">") + { + + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) <= stoi(c)) + { + rfinal.removeTuple(j); + } + } + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + else if(op == "<") + { + + if(r.getAttributeByName(a1.getName()).getType() == "INTEGER") + { + for(int i = 0; i < r.getAttributes().size(); ++i) + { + if(r.getAttributes()[i].getName() == a1.getName()) + { + for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j) + { + if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) >= stoi(c)) + { + rfinal.removeTuple(j); + } + } + } + } + } + else + { + cout << "Attribute type is not an INTEGER." << endl; + exit(1); + } + } + } + return rfinal; +} + tuple, Relation> expression(vector input, DBEngine &engine) { Relation rfinal("TEMP"); tuple, Relation> t(input, rfinal); if(input[0] == "select") { - + /* + cout << "Not yet implemented." << endl; + exit(1); + */ + input.erase(input.begin()); + vector s; + Relation r("TEMP"); + if(input[0] == "(") + { + input.erase(input.begin()); + while(input[0] != ")") + { + s.push_back(input[0]); + input.erase(input.begin()); + } + input.erase(input.begin()); + if(engine.isRelation(input[0])) + { + r = condition(s, engine.getTableFromName(input[0])); + input.erase(input.begin()); + } + else if(input[0] == "(") + { + tuple, Relation> t = expression(input, engine); + r = condition(s, get<1>(t)); + input.erase(input.begin()); + } + } + get<0>(t) = input; + get<1>(t) = r; + return t; } else if(input[0] == "project") diff --git a/Relation.cpp b/Relation.cpp index 3f9fb7b..bf32b20 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -63,6 +63,16 @@ Attribute& Relation::getAttributeByName(string s) { cout << "Failure to return: the requested attribute does not exist."; } +bool Relation::isAttribute(string s) { + for(int i = 0; i < size; ++i){ + if (att[i].getName() == s) { + return true; + } + } + return false; + //cout << "Failure to return: the requested attribute does not exist."; +} + void Relation::renameAttribute(string oldstr, string newstr){ this->getAttributeByName(oldstr).setName(newstr); } diff --git a/Relation.h b/Relation.h index 431d634..4c7e1e2 100755 --- a/Relation.h +++ b/Relation.h @@ -19,6 +19,7 @@ public: vector getAttributes(); vector getAttributeNames(); Attribute& getAttributeByName(string s); + bool isAttribute(string s); void renameAttribute(string oldstr, string newstr); int getSize(); void display(); diff --git a/test b/test index b0e69f8..9b3ff7f 100755 Binary files a/test and b/test differ