added the error matching

This commit is contained in:
Rebecca Schofield 2015-10-06 13:25:46 -05:00
parent c444b7368f
commit bfe5e4e981
5 changed files with 14 additions and 46 deletions

View file

@ -286,35 +286,6 @@ Relation DBEngine::crossProduct(Relation r1, Relation r2){
return new_r; return new_r;
} }
/*Relation DBEngine::updateCmd(Relation r, string attNameSet, string attSet, string attNameWhere, string attWhere){
//brute forcing this, sorry universe
//add error matching!!
vector<int> inds;
for (int i = 0; i < r.getSize(); ++i){
if (r[i].getName() == attNameWhere){
for (int j = 0; j < r[i].getSize(); ++j){
if (r[i][j] == attWhere){
inds.push_back(j);
}
}
}
}
vector<Attribute> atts = r.getAttributes();
for (int i = 0; i < atts.size(); ++i){
if (atts[i].getName() == attNameSet){
for (int j = 0; j < inds.size(); ++j){
atts[i].setValue(attSet, inds[j]);
}
}
}
r.setAttributes(atts);
return r;
}*/
void DBEngine::updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere){ void DBEngine::updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere){
for(int i = 0; i < tables.size(); i++){ for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == relationName){ if (tables[i].getTableName() == relationName){
@ -324,20 +295,6 @@ void DBEngine::updateCmd(string relationName, string attNameSet, string attSet,
} }
} }
/*Relation DBEngine::deleteCmd(Relation r, string attName, string att){
for (int i = 0; i < r.getSize(); ++i){
if (r[i].getName() == attName){
for (int j = 0; j < r[i].getSize(); ++j){
if (r[i][j] == att){
r.removeTuple(j);
}
}
}
}
return r;
}*/
void DBEngine::deleteCmd(string relationName, string attName, string att){ void DBEngine::deleteCmd(string relationName, string attName, string att){
for(int i = 0; i < tables.size(); i++){ for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == relationName){ if (tables[i].getTableName() == relationName){

View file

@ -31,8 +31,6 @@ public:
Relation setUnion(Relation r1, Relation r2); Relation setUnion(Relation r1, Relation r2);
Relation setDiff(Relation r1, Relation r2); Relation setDiff(Relation r1, Relation r2);
Relation crossProduct(Relation r1, Relation r2); Relation crossProduct(Relation r1, Relation r2);
//Relation updateCmd(Relation r, string attNameSet, string attSet, string attNameWhere, string attWhere);
void updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere); void updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere);
//Relation deleteCmd(Relation r, string attName, string att);
void deleteCmd(string relationName, string attName, string att); void deleteCmd(string relationName, string attName, string att);
}; };

View file

@ -160,9 +160,11 @@ void Relation::removeFromTuple(int rindex, int aindex)
void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere){ void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere){
vector<int> inds; vector<int> inds;
bool found = false;
for (int i = 0; i < att.size(); ++i){ for (int i = 0; i < att.size(); ++i){
if (att[i].getName() == attNameWhere){ if (att[i].getName() == attNameWhere){
found = true;
for (int j = 0; j < att[i].getSize(); ++j){ for (int j = 0; j < att[i].getSize(); ++j){
if (att[i][j] == attWhere){ if (att[i][j] == attWhere){
inds.push_back(j); inds.push_back(j);
@ -171,6 +173,10 @@ void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere,
} }
} }
if (!found){
cout << "Failure to update, the requested attribute was not found." << endl;
}
for (int i = 0; i < att.size(); ++i){ for (int i = 0; i < att.size(); ++i){
if (att[i].getName() == attNameSet){ if (att[i].getName() == attNameSet){
for (int j = 0; j < inds.size(); ++j){ for (int j = 0; j < inds.size(); ++j){
@ -183,8 +189,11 @@ void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere,
} }
void Relation::deleteCmd(string attName, string s){ void Relation::deleteCmd(string attName, string s){
bool found = false;
for (int i = 0; i < att.size(); ++i){ for (int i = 0; i < att.size(); ++i){
if (att[i].getName() == attName){ if (att[i].getName() == attName){
found = true;
for (int j = 0; j < att[i].getSize(); ++j){ for (int j = 0; j < att[i].getSize(); ++j){
if (att[i][j] == s){ if (att[i][j] == s){
removeTuple(j); removeTuple(j);
@ -192,4 +201,8 @@ void Relation::deleteCmd(string attName, string s){
} }
} }
} }
if (!found){
cout << "Failure to delete, the requested attribute was not found." << endl;
}
} }

BIN
a.out

Binary file not shown.

BIN
test

Binary file not shown.