Conditions work withou && or || arguments. Select is implemented.

This commit is contained in:
Alexander Huddleston 2015-09-29 18:35:12 -05:00
parent 8d86aec4e0
commit 2c80d78773
4 changed files with 312 additions and 1 deletions

View file

@ -42,13 +42,313 @@ void displayTokenList(vector<string> input)
}
}
Relation condition(vector<string> 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<vector<string>, Relation> expression(vector<string> input, DBEngine &engine)
{
Relation rfinal("TEMP");
tuple<vector<string>, Relation> t(input, rfinal);
if(input[0] == "select")
{
/*
cout << "Not yet implemented." << endl;
exit(1);
*/
input.erase(input.begin());
vector<string> 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<vector<string>, 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")

View file

@ -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);
}

View file

@ -19,6 +19,7 @@ public:
vector<Attribute> getAttributes();
vector<string> getAttributeNames();
Attribute& getAttributeByName(string s);
bool isAttribute(string s);
void renameAttribute(string oldstr, string newstr);
int getSize();
void display();

BIN
test

Binary file not shown.