diff --git a/Parser.cpp b/Parser.cpp index 43ed941..61272d7 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -42,12 +42,13 @@ void displayTokenList(vector input) } } -Relation condition(vector input, Relation &r) +Relation condition(vector input, Relation &r, DBEngine &engine) { Relation rfinal = r; //UNTESTED //testing to see if there is an && or || present + /* if (input.size() > 3) { //test print for (int i = 0; i < input.size(); ++i) @@ -97,7 +98,7 @@ Relation condition(vector input, Relation &r) } else { - + */ Attribute a1 = r.getAttributeByName(input[0]); input.erase(input.begin()); string op = input[0]; @@ -105,10 +106,9 @@ Relation condition(vector input, Relation &r) Attribute a2; string c; //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 << c << endl; + cout << input[x] << endl; } if(r.isAttribute(input[0])) { @@ -126,14 +126,15 @@ Relation condition(vector input, Relation &r) rfinal.insertTuple(r.getTuple(j)); } }*/ + int offset = 0; 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); + rfinal.removeTuple(x - offset); + offset += 1; } } } @@ -143,8 +144,8 @@ Relation condition(vector input, Relation &r) { if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]) { - rfinal.removeTuple(x); - //rfinal.insertAttributes(a); + rfinal.removeTuple(x - offset); + offset += 1; } } } @@ -156,8 +157,8 @@ Relation condition(vector input, Relation &r) { if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])) { - rfinal.removeTuple(x); - //rfinal.insertAttributes(a); + rfinal.removeTuple(x - offset); + offset += 1; } } } @@ -175,8 +176,8 @@ Relation condition(vector input, Relation &r) { if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])) { - rfinal.removeTuple(x); - //rfinal.insertAttributes(a); + rfinal.removeTuple(x - offset); + offset += 1; } } } @@ -195,8 +196,8 @@ Relation condition(vector input, Relation &r) { if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) { - rfinal.removeTuple(x); - //rfinal.insertAttributes(a); + rfinal.removeTuple(x - offset); + offset += 1; } } } @@ -214,8 +215,8 @@ Relation condition(vector input, Relation &r) { if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])) { - rfinal.removeTuple(x); - //rfinal.insertAttributes(a); + rfinal.removeTuple(x - offset); + offset += 1; } } } @@ -228,9 +229,12 @@ Relation condition(vector 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()); + int offset = 0; + //input.erase(input.begin()); if(op == "==") { for(int i = 0; i < r.getAttributes().size(); ++i) @@ -241,7 +245,8 @@ Relation condition(vector input, Relation &r) { if(r.getAttributeByName(a1.getName()).getValues()[j] != c) { - rfinal.removeTuple(j); + rfinal.removeTuple(j - offset); + offset += 1; } } } @@ -257,7 +262,8 @@ Relation condition(vector input, Relation &r) { if(r.getAttributeByName(a1.getName()).getValues()[j] == c) { - rfinal.removeTuple(j); + rfinal.removeTuple(j - offset); + offset += 1; } } } @@ -275,7 +281,8 @@ Relation condition(vector input, Relation &r) { 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 input, Relation &r) { 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 input, Relation &r) { 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 input, Relation &r) { 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 input, Relation &r) } } } + 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, Relation> expression(vector input, DBEngine &engine) @@ -390,13 +413,13 @@ tuple, Relation> expression(vector input, DBEngine &engin input.erase(input.begin()); if(engine.isRelation(input[0])) { - r = condition(s, engine.getTableFromName(input[0])); + r = condition(s, engine.getTableFromName(input[0]), engine); input.erase(input.begin()); } else if(input[0] == "(") { tuple, Relation> t = expression(input, engine); - r = condition(s, get<1>(t)); + r = condition(s, get<1>(t), engine); input.erase(input.begin()); } } diff --git a/Relation.cpp b/Relation.cpp index bf32b20..18a1277 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -92,8 +92,6 @@ void Relation::display(){ } void Relation::insertTuple(vector tuple){ - cout << name << " " << size << endl; - cout << tuple.size() << endl; if (tuple.size() != this->size) { cout << "Failure to insert: the sizes do not match."; } diff --git a/test b/test index 9b3ff7f..e09c206 100755 Binary files a/test and b/test differ diff --git a/test.cpp b/test.cpp index 710b098..1f34d4c 100755 --- a/test.cpp +++ b/test.cpp @@ -31,12 +31,12 @@ int main () { engine.createTable("Food", v); Attribute att4("Dessert", "VARCHAR(20)", true); - Attribute att5("Midnight Snack", "VARCHAR(20)", false); + Attribute att5("MidnightSnack", "VARCHAR(20)", false); - att4.addCell("Ice Cream Sundae"); - att4.addCell("Chocolate Bar"); - att5.addCell("Hummus and Carrots"); - att5.addCell("Potato Chips"); + att4.addCell("IceCreamSundae"); + att4.addCell("ChocolateBar"); + att5.addCell("HummusandCarrots"); + att5.addCell("PotatoChips"); vector v2; v2.push_back(att4);