#include #include #include #include "Condition.h" #include "DBEngine.h" DBEngine::DBEngine(){ size = 0; } void DBEngine::createTable(string n){ Relation r(n); tables.push_back(r); size++; } void DBEngine::createTable(string n, vector a){ Relation r(n, a); tables.push_back(r); size++; } void DBEngine::createTable(Relation r){ tables.push_back(r); size++; } vector DBEngine::getRelations(){ return tables; } Relation& DBEngine::getTableFromName(string n){ for(int i = 0; i < tables.size(); i++){ if (tables[i].getTableName() == n){ return tables[i]; } } } //currently writes nothing meaningful void DBEngine::saveToFile(vector cmds){ ofstream file; file.open("savefile.db"); for(int i = 0; i < cmds.size(); ++i){ file << cmds[i] << endl; } file.close(); } Relation DBEngine::selection(string attName, string s, Relation r){ equality(attName, s, r); } //assumes that all attribute titles are unique Relation DBEngine::projection(vector input, Relation r){ vector v; string new_name = r.getTableName() + " Projection"; for(int i = 0; i < input.size(); ++i) { for(int j = 0; j < r.getSize(); ++j) { if((r.getAttributes())[j].getName() == input[i]) v.push_back((r.getAttributes())[j]); } } Relation temp(new_name, v); return temp; } Relation DBEngine::product(string new_name, Relation r1, Relation r2){ Relation temp(new_name); vector a1; for(int i = 0; i < r1.getAttributes().size(); ++i){ a1.push_back(r1.getAttributes()[i]); } for(int i = 0; i < r2.getAttributes().size(); ++i){ a1.push_back(r2.getAttributes()[i]); } temp.insertAttributes(a1); vector tuple1; vector tuple2; vector result_tuple; //Don't we need to find the bigger one to start first? if(r1.getSize() >= r2.getSize()) { //Combine tuples from relations into one, push back that tuple into the resultant relation via insertTuple //Yeah have fun } else if(r2.getSize() > r1.getSize()) { // } return temp; } //test error matching void DBEngine::rename(Relation& r, vector oldnames, vector newnames){ if (oldnames.size() != newnames.size()) { cout << "FAILURE TO RENAME: number of attributes do not match.\n"; return; } else if (oldnames != r.getAttributeNames()) { cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n"; return; } else { for(int i = 0; i < oldnames.size(); ++i){ r.renameAttribute(oldnames[i], newnames[i]); } } } /*Relation DBEngine::setUnion(Relation r1, Relation r2){ if (r1.getAttributeNames() != r2.getAttributeNames()){ cout << "Failure to union: the relations are not union-compatible"; return; } else { vector r1_atts = r1.getAttributes(); vector r2_atts = r2.getAttributes(); vector new_atts = r1_atts; for (int i = 0; i < r2_atts.size(); ++i) { for (int j = 0; j < r2_atts[i].getSize(); ++j){ new_atts[i].addCell(r2_atts[i][j]); } } for (int i = 0; i < new_atts.size(); ++i) { for (int j = 0; j < new_atts.size(); ++j){ if (new_atts[i] == new_atts[j]){ new_atts.erase(new_atts.begin() + i); continue; } } } //currently all returned relations are called TEMP Relation new_r("TEMP", new_atts); return new_r; } }*/