Conditions are fully functional, as well as select.

This commit is contained in:
Alexander Huddleston 2015-09-29 23:20:12 -05:00
parent 18ba9c1112
commit c77ce1b581
4 changed files with 55 additions and 34 deletions

View file

@ -42,12 +42,13 @@ void displayTokenList(vector<string> input)
} }
} }
Relation condition(vector<string> input, Relation &r) Relation condition(vector<string> input, Relation &r, DBEngine &engine)
{ {
Relation rfinal = r; Relation rfinal = r;
//UNTESTED //UNTESTED
//testing to see if there is an && or || present //testing to see if there is an && or || present
/*
if (input.size() > 3) { if (input.size() > 3) {
//test print //test print
for (int i = 0; i < input.size(); ++i) for (int i = 0; i < input.size(); ++i)
@ -97,7 +98,7 @@ Relation condition(vector<string> input, Relation &r)
} }
else { else {
*/
Attribute a1 = r.getAttributeByName(input[0]); Attribute a1 = r.getAttributeByName(input[0]);
input.erase(input.begin()); input.erase(input.begin());
string op = input[0]; string op = input[0];
@ -105,10 +106,9 @@ Relation condition(vector<string> input, Relation &r)
Attribute a2; Attribute a2;
string c; string c;
//cout << input[0] << endl; //cout << input[0] << endl;
if(input[0].at(0) == '\"') for(int x = 0; x < input.size(); ++x)
{ {
c = input[0].substr(1, input[0].find_last_of("\"") - 1); cout << input[x] << endl;
//cout << c << endl;
} }
if(r.isAttribute(input[0])) if(r.isAttribute(input[0]))
{ {
@ -126,14 +126,15 @@ Relation condition(vector<string> input, Relation &r)
rfinal.insertTuple(r.getTuple(j)); rfinal.insertTuple(r.getTuple(j));
} }
}*/ }*/
int offset = 0;
if(op == "==") if(op == "==")
{ {
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x) for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{ {
if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x]) if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x])
{ {
rfinal.removeTuple(x); rfinal.removeTuple(x - offset);
//rfinal.insertAttributes(a); offset += 1;
} }
} }
} }
@ -143,8 +144,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]) if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x])
{ {
rfinal.removeTuple(x); rfinal.removeTuple(x - offset);
//rfinal.insertAttributes(a); offset += 1;
} }
} }
} }
@ -156,8 +157,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])) if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{ {
rfinal.removeTuple(x); rfinal.removeTuple(x - offset);
//rfinal.insertAttributes(a); offset += 1;
} }
} }
} }
@ -175,8 +176,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])) if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{ {
rfinal.removeTuple(x); rfinal.removeTuple(x - offset);
//rfinal.insertAttributes(a); offset += 1;
} }
} }
} }
@ -195,8 +196,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{ {
rfinal.removeTuple(x); rfinal.removeTuple(x - offset);
//rfinal.insertAttributes(a); offset += 1;
} }
} }
} }
@ -214,8 +215,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{ {
rfinal.removeTuple(x); rfinal.removeTuple(x - offset);
//rfinal.insertAttributes(a); offset += 1;
} }
} }
} }
@ -228,9 +229,12 @@ Relation condition(vector<string> input, Relation &r)
} }
} }
} }
else else if(input[0].at(0) == '\"')
{ {
c = input[0].substr(1, input[0].find_last_of("\"") - 1);
input.erase(input.begin()); input.erase(input.begin());
int offset = 0;
//input.erase(input.begin());
if(op == "==") if(op == "==")
{ {
for(int i = 0; i < r.getAttributes().size(); ++i) for(int i = 0; i < r.getAttributes().size(); ++i)
@ -241,7 +245,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(r.getAttributeByName(a1.getName()).getValues()[j] != c) if(r.getAttributeByName(a1.getName()).getValues()[j] != c)
{ {
rfinal.removeTuple(j); rfinal.removeTuple(j - offset);
offset += 1;
} }
} }
} }
@ -257,7 +262,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(r.getAttributeByName(a1.getName()).getValues()[j] == c) if(r.getAttributeByName(a1.getName()).getValues()[j] == c)
{ {
rfinal.removeTuple(j); rfinal.removeTuple(j - offset);
offset += 1;
} }
} }
} }
@ -275,7 +281,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c)) if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c))
{ {
rfinal.removeTuple(j); rfinal.removeTuple(j - offset);
offset += 1;
} }
} }
} }
@ -299,7 +306,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c)) if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c))
{ {
rfinal.removeTuple(j); rfinal.removeTuple(j - offset);
offset += 1;
} }
} }
} }
@ -324,7 +332,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) <= stoi(c)) if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) <= stoi(c))
{ {
rfinal.removeTuple(j); rfinal.removeTuple(j - offset);
offset += 1;
} }
} }
} }
@ -349,7 +358,8 @@ Relation condition(vector<string> input, Relation &r)
{ {
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) >= stoi(c)) if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) >= stoi(c))
{ {
rfinal.removeTuple(j); rfinal.removeTuple(j - offset);
offset += 1;
} }
} }
} }
@ -362,8 +372,21 @@ Relation condition(vector<string> input, Relation &r)
} }
} }
} }
return rfinal; if(input[0] == "&&")
{
input.erase(input.begin());
Relation rtemp = rfinal;
rfinal = condition(input, rtemp, engine);
} }
else if(input[0] == "||")
{
input.erase(input.begin());
Relation rtemp = rfinal;
Relation rtemp2 = condition(input, r, engine);
rfinal = engine.setUnion(rtemp, rtemp2);
}
return rfinal;
//}
} }
tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engine) tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engine)
@ -390,13 +413,13 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
input.erase(input.begin()); input.erase(input.begin());
if(engine.isRelation(input[0])) if(engine.isRelation(input[0]))
{ {
r = condition(s, engine.getTableFromName(input[0])); r = condition(s, engine.getTableFromName(input[0]), engine);
input.erase(input.begin()); input.erase(input.begin());
} }
else if(input[0] == "(") else if(input[0] == "(")
{ {
tuple<vector<string>, Relation> t = expression(input, engine); tuple<vector<string>, Relation> t = expression(input, engine);
r = condition(s, get<1>(t)); r = condition(s, get<1>(t), engine);
input.erase(input.begin()); input.erase(input.begin());
} }
} }

View file

@ -92,8 +92,6 @@ void Relation::display(){
} }
void Relation::insertTuple(vector<string> tuple){ void Relation::insertTuple(vector<string> tuple){
cout << name << " " << size << endl;
cout << tuple.size() << endl;
if (tuple.size() != this->size) { if (tuple.size() != this->size) {
cout << "Failure to insert: the sizes do not match."; cout << "Failure to insert: the sizes do not match.";
} }

BIN
test

Binary file not shown.