Conditions are fully functional, as well as select.
This commit is contained in:
parent
18ba9c1112
commit
c77ce1b581
4 changed files with 55 additions and 34 deletions
77
Parser.cpp
77
Parser.cpp
|
@ -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;
|
||||
|
||||
//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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<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());
|
||||
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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<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());
|
||||
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<vector<string>, Relation> t = expression(input, engine);
|
||||
r = condition(s, get<1>(t));
|
||||
r = condition(s, get<1>(t), engine);
|
||||
input.erase(input.begin());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,8 +92,6 @@ void Relation::display(){
|
|||
}
|
||||
|
||||
void Relation::insertTuple(vector<string> tuple){
|
||||
cout << name << " " << size << endl;
|
||||
cout << tuple.size() << endl;
|
||||
if (tuple.size() != this->size) {
|
||||
cout << "Failure to insert: the sizes do not match.";
|
||||
}
|
||||
|
|
BIN
test
BIN
test
Binary file not shown.
10
test.cpp
10
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<Attribute> v2;
|
||||
v2.push_back(att4);
|
||||
|
|
Reference in a new issue