diff --git a/Attribute.cpp b/Attribute.cpp index 0ef939e..81efb67 100755 --- a/Attribute.cpp +++ b/Attribute.cpp @@ -32,6 +32,7 @@ void Attribute::addCell(string v){ } void Attribute::removeCell(int index){ + cout << values[index] << " " << index << endl; values.erase(values.begin() + index); size--; } @@ -69,7 +70,7 @@ void Attribute::display(){ cout << name << "\n" << type; if(type == "VARCHAR") { - cout << "(" << size << ")"; + cout << "(" << limit << ")"; } cout << "\n\n"; diff --git a/DBEngine.cpp b/DBEngine.cpp index ede10e5..2638942 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -101,7 +101,20 @@ vector DBEngine::getRelations(){ return tables; } -Relation& DBEngine::getTableFromName(string n){ +bool DBEngine::isRelation(string n) +{ + for(int i = 0; i < tables.size(); i++) + { + if (tables[i].getTableName() == n) + { + return true; + } + } + return false; +} + +Relation& DBEngine::getTableFromName(string n) +{ for(int i = 0; i < tables.size(); i++){ if (tables[i].getTableName() == n){ return tables[i]; @@ -198,31 +211,67 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){ Relation new_r = r1; new_r.setTableName("TEMP"); vector temp; - bool duplicate = false; - - for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){ - new_r.removeTuple(i); + //bool duplicate = false; + + /* + for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) + { + new_r.removeTuple(0); } + */ + int size = 0; + + cout << "test1" << endl; + + for(int x = 0; x < r2.getAttributes().size(); ++x) + { + + cout << "test2" << endl; + for (int i = 0; i < r2.getAttributes()[x].getSize(); ++i) + { + + cout << "test3" << endl; + temp = r2.getTuple(i); - for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) { - temp = r1.getTuple(i); - - for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){ - if (temp == r2.getTuple(j)){ - duplicate = true; - break; + for(int y = 0; y < new_r.getAttributes().size(); ++y) + { + size = new_r.getAttributes()[y].getSize(); + cout << "test4" << endl; + new_r.getAttributes()[y].getSize(); + for (int j = 0; j < size; ++j) + { + cout << "test5" << endl; + for(int a = 0; a < temp.size(); ++a) + { + cout << "test6" << endl; + for(int b = 0; b < new_r.getTuple(j).size(); ++b) + { + cout << "test7" << endl; + cout << temp[a] << " " << new_r.getTuple(j)[b] << " " << b << endl; + if (temp[a] == new_r.getTuple(j)[b]) + { + new_r.removeFromTuple(b, j); + cout << "test" << endl; + //duplicate = true; + //break; + } + } + } + size = new_r.getAttributes()[y].getSize(); + } + /* + if (!duplicate) + { + new_r.insertTuple(temp); + } + */ + //duplicate = false; } } - - if (!duplicate){ - new_r.insertTuple(temp); - } - - duplicate = false; } //make this one for loop later! - + /* for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) { temp = r2.getTuple(i); @@ -239,6 +288,7 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){ duplicate = false; } + */ return new_r; } diff --git a/DBEngine.h b/DBEngine.h index de714f6..e02e5c7 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -17,6 +17,7 @@ public: void createTable(Relation r); void insertValues(string r, vector v); vector getRelations(); + bool isRelation(string n); //void showTable(Relation r); Relation& getTableFromName(string n); void saveToFile(vector cmds); diff --git a/Parser.cpp b/Parser.cpp index 15873ea..2812ea0 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -507,11 +507,175 @@ vector deleteCMD(vector input, DBEngine &engine) else cout<<"Syntax error!"< input, DBEngine &engine) +Relation expression(vector input, DBEngine &engine) +{ + Relation rfinal("TEMP"); + if(input[0] == "select") + { + + } + + else if(input[0] == "project") + { + + } + + else if(input[0] == "rename") + { + + } + + else if(engine.isRelation(input[0])) + { + Relation r1(engine.getTableFromName(input[0])); + input.erase(input.begin()); + + if(input[0] == "+") + { + input.erase(input.begin()); + if(engine.isRelation(input[0])) + { + Relation r2(engine.getTableFromName(input[0])); + rfinal = engine.setUnion(r1, r2); + return rfinal; + } + else if(input[0] == "(") + { + Relation r2 = expression(input, engine); + rfinal = engine.setUnion(r1, r2); + return rfinal; + } + } + + else if(input[0] == "-") + { + input.erase(input.begin()); + if(engine.isRelation(input[0])) + { + Relation r2(engine.getTableFromName(input[0])); + rfinal = engine.setDiff(r1, r2); + return rfinal; + } + else if(input[0] == "(") + { + Relation r2 = expression(input, engine); + rfinal = engine.setDiff(r1, r2); + return rfinal; + } + } + + else if(input[0] == "*") + { + input.erase(input.begin()); + if(engine.isRelation(input[0])) + { + Relation r2(engine.getTableFromName(input[0])); + rfinal = engine.crossProduct(r1, r2); + return rfinal; + } + else if(input[0] == "(") + { + Relation r2 = expression(input, engine); + rfinal = engine.crossProduct(r1, r2); + return rfinal; + } + } + } + + else if(input[0] == "(") + { + input.erase(input.begin()); + + if(input[0] == "+") + { + + } + + else if(input[0] == "-") + { + + } + + else if(input[0] == "*") + { + + } + } + + return rfinal; +} + +vector query(vector input, DBEngine &engine) { PRelation pr(input[0]); input.erase(input.begin()); + input.erase(input.begin()); + + Relation r = expression(input, engine); + r.setTableName(pr.getName()); + //r.display(); + + //temporary fix + while(input[0] != ";") + { + input.erase(input.begin()); + } + + /* + if(input[0] == "select") + { + + } + + else if(input[0] == "project") + { + + } + + else if(input[0] == "rename") + { + + } + + else if(engine.isRelation()) + { + Relation r(engine.getTableFromName(input[0])); + input.erase(input.begin()); + + if(input[0] == "+") + { + + } + + else if(input[0] == "-") + { + + } + + else if(input[0] == "*") + { + + } + } + + else + { + cout << "Invalid query request." << endl; + } + */ + + //testing + /* + for(int x = 0; x < input.size(); ++x) + { + cout << input[x] << endl; + } + */ + + engine.createTable(r); + + return input; } void par_line(vector input, DBEngine &engine) //calls par_command() or par_query() depending on first item from token list diff --git a/Relation.cpp b/Relation.cpp index 4a7dd9c..6839ee7 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -116,13 +116,30 @@ void Relation::insertFromRelation(Relation r){ } void Relation::removeTuple(int index){ - if (index >= this->size) { - cout << "Failure to delete: the requested index is out of bounds."; + if (index >= this->size) + { + cout << "Failure to delete: the requested index is out of bounds." << endl; } - else { - for (int i = 0; i < att.size(); ++i) { + else + { + for (int i = 0; i < att.size(); ++i) + { att[i].removeCell(index); } } +} + +void Relation::removeFromTuple(int rindex, int aindex) +{ + if (rindex >= this->size) + { + cout << "Failure to delete: the requested index is out of bounds." << endl; + } + + else + { + cout << aindex << endl; + att[rindex].removeCell(aindex); + } } \ No newline at end of file diff --git a/Relation.h b/Relation.h index 172627f..431d634 100755 --- a/Relation.h +++ b/Relation.h @@ -25,4 +25,5 @@ public: void insertTuple(vector tuple); //assuming they are in order void insertFromRelation(Relation r); void removeTuple(int index); + void removeFromTuple(int rindex, int aindex); }; \ No newline at end of file diff --git a/test b/test index 58c8923..eca42fd 100755 Binary files a/test and b/test differ