update works

This commit is contained in:
Rebecca Schofield 2015-10-06 23:19:53 -05:00
parent ea2b612129
commit dd4453c9aa
7 changed files with 126 additions and 153 deletions

View file

@ -151,8 +151,7 @@ Relation DBEngine::projection(vector<string> input, Relation r){
} }
//test error matching //test error matching
Relation DBEngine::rename(vector<string> newnames, Relation &r) Relation DBEngine::rename(vector<string> newnames, Relation &r){
{
vector<string> temp; vector<string> temp;
if (r.getSize() != newnames.size()) { if (r.getSize() != newnames.size()) {
cout << "FAILURE TO RENAME: number of attributes do not match.\n"; cout << "FAILURE TO RENAME: number of attributes do not match.\n";
@ -286,20 +285,63 @@ Relation DBEngine::crossProduct(Relation r1, Relation r2){
return new_r; return new_r;
} }
void DBEngine::updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere){ void DBEngine::updateFromRelationCmd(Relation r, Relation temp, vector<string> attName, vector<string> att){
for(int i = 0; i < tables.size(); i++){ string rName = r.getTableName();
if (tables[i].getTableName() == relationName){ int index = -1;
tables[i].updateCmd(attNameSet, attSet, attNameWhere, attWhere);
return; for (int i = 0; i < tables.size(); ++i){
if (tables[i].getTableName() == rName){
index = i;
break;
}
}
vector<string> tuple;
vector<string> tempTuple;
for(int i = 0; i < temp.getAttributes()[0].getSize(); ++i){
tuple = temp.getTuple(i);
for (int j = 0; j < tables[index].getSize(); ++j){
tempTuple = tables[index].getTuple(j);
if (tables[index].getTuple(j) == tuple){
for (int k = 0; k < tables[index].getSize(); ++k){
for (int a = 0; a < attName.size(); ++a){
if (tables[index][k].getName() == attName[a]){
cout << "point 7\n";
cout << "tables[index][k][j]:" << tables[index][k][j] << "\n";
cout << "att[a]:" << att[a] << "\n";
tables[index].updateInAttribute(att[a], k, j);
}
}
}
}
} }
} }
} }
void DBEngine::deleteCmd(string relationName, string attName, string att){ void DBEngine::deleteFromRelationCmd(Relation r, Relation temp){
for(int i = 0; i < tables.size(); i++){ string rName = r.getTableName();
if (tables[i].getTableName() == relationName){ int index = -1;
tables[i].deleteCmd(attName, att);
return; for (int i = 0; i < tables.size(); ++i){
if (tables[i].getTableName() == rName){
index = i;
break;
}
}
vector<string> tuple;
vector<string> tempTuple;
for(int i = 0; i < temp.getAttributes()[0].getSize(); ++i){
tuple = temp.getTuple(i);
for (int j = 0; j < tables[index].getSize(); ++j){
tempTuple = tables[index].getTuple(j);
if (tempTuple == tuple){
tables[index].removeTuple(j);
}
} }
} }
} }

View file

@ -31,6 +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);
void updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere); void updateFromRelationCmd(Relation r, Relation temp, vector<string> attName, vector<string> att);
void deleteCmd(string relationName, string attName, string att); void deleteFromRelationCmd(Relation r, Relation temp);
}; };

View file

@ -287,11 +287,11 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine){
Relation rtemp2 = condition(input, r, engine); Relation rtemp2 = condition(input, r, engine);
rfinal = engine.setUnion(rtemp, rtemp2); rfinal = engine.setUnion(rtemp, rtemp2);
} }
return rfinal; return rfinal;
} }
tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engine) tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engine){
{
Relation rfinal("TEMP"); Relation rfinal("TEMP");
tuple<vector<string>, Relation> t(input, rfinal); tuple<vector<string>, Relation> t(input, rfinal);
if(input[0] == "select") if(input[0] == "select")
@ -557,8 +557,7 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
return t; return t;
} }
vector<string> showCMD(vector<string> input, DBEngine &engine) vector<string> showCMD(vector<string> input, DBEngine &engine){
{
if(engine.isRelation(input[0]) && (input[1] != "+" && input[1] != "-" && input[1] != "*")) if(engine.isRelation(input[0]) && (input[1] != "+" && input[1] != "-" && input[1] != "*"))
{ {
@ -575,8 +574,7 @@ vector<string> showCMD(vector<string> input, DBEngine &engine)
return input; return input;
} }
vector<string> saveCMD(vector<string> input, DBEngine &engine) vector<string> saveCMD(vector<string> input, DBEngine &engine){
{
if (input.size() > 2) if (input.size() > 2)
{ {
cout<<"Syntax error!"<<endl; cout<<"Syntax error!"<<endl;
@ -587,8 +585,7 @@ vector<string> saveCMD(vector<string> input, DBEngine &engine)
return input; return input;
} }
vector<string> closeCMD(vector<string> input, DBEngine &engine) vector<string> closeCMD(vector<string> input, DBEngine &engine){
{
if (input.size() > 3) if (input.size() > 3)
{ {
cout<<"Syntax error!"<<endl; cout<<"Syntax error!"<<endl;
@ -599,8 +596,7 @@ vector<string> closeCMD(vector<string> input, DBEngine &engine)
return input; return input;
} }
vector<string> openCMD(vector<string> input, DBEngine &engine) vector<string> openCMD(vector<string> input, DBEngine &engine){
{
if (input.size() > 2) if (input.size() > 2)
{ {
cout<<"Syntax error!"<<endl; cout<<"Syntax error!"<<endl;
@ -618,15 +614,12 @@ vector<string> openCMD(vector<string> input, DBEngine &engine)
return input; return input;
} }
vector<string> exitCMD(vector<string> input, DBEngine &engine) vector<string> exitCMD(vector<string> input, DBEngine &engine){
{
exit(0); exit(0);
return input; return input;
} }
vector<string> createCMD(vector<string> input, DBEngine &engine){
vector<string> createCMD(vector<string> input, DBEngine &engine)
{
if (input[0] == "TABLE") if (input[0] == "TABLE")
{ {
input.erase(input.begin()); input.erase(input.begin());
@ -717,8 +710,7 @@ vector<string> createCMD(vector<string> input, DBEngine &engine)
else cout<<"Syntax error! 1"<<endl; //refine else cout<<"Syntax error! 1"<<endl; //refine
} }
vector<string> insertCMD(vector<string> input, DBEngine &engine) vector<string> insertCMD(vector<string> input, DBEngine &engine){
{
//relation name will be the first element of the vector of data returned by this function //relation name will be the first element of the vector of data returned by this function
if (input[0] == "INTO") if (input[0] == "INTO")
@ -848,36 +840,27 @@ vector<string> insertCMD(vector<string> input, DBEngine &engine)
} }
vector<string> updateCMD(vector<string> input, DBEngine &engine) vector<string> updateCMD(vector<string> input, DBEngine &engine){
{
Relation r = engine.getTableFromName(input[0]); Relation r = engine.getTableFromName(input[0]);
Relation rcond("TEMP"); Relation temp("TEMP");
Relation rfinal("TEMP"); vector <string> a;
vector <string> c;
vector <string> s;
input.erase(input.begin()); input.erase(input.begin());
if(input[0] == "SET") if(input[0] == "SET")
{ {
input.erase(input.begin()); input.erase(input.begin());
vector <Attribute> a = r.getAttributes();
//parse out ( and send everything until ) into an Expression vector //parse out ( and send everything until ) into an Expression vector
if(input[0] == "(") if(input[0] == "(")
{ {
vector <PAttribute> a;
PAttribute temp;
vector <string> s;
//vector <PExpression> e;
input.erase(input.begin()); input.erase(input.begin());
while(input[0] != ")") while(input[0] != ")")
{ {
temp.setPAttributeName(input[0]); a.push_back(input[0]);
input.erase(input.begin()); input.erase(input.begin());
@ -885,16 +868,18 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
{ {
input.erase(input.begin()); input.erase(input.begin());
s.push_back(input[0]); //s.push_back(input[0]);
if(input[0].at(0) == '\"') if(input[0].at(0) == '\"')
{ {
s.push_back(input[0].substr(1, input[0].find_last_of("\""))); c.push_back(input[0].substr(1, input[0].find_last_of("\"") - 1));
} }
else else
{ {
s.push_back(input[0]); c.push_back(input[0]);
} }
input.erase(input.begin());
} }
else else
@ -906,16 +891,13 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
{ {
input.erase(input.begin()); input.erase(input.begin());
} }
a.push_back(temp);
temp.setPAttributeName("~");
} }
input.erase(input.begin());
} }
if(input[0] == "WHERE") if(input[0] == "WHERE")
{ {
vector<string> s; input.erase(input.begin());
if(input[0] == "(") if(input[0] == "(")
{ {
input.erase(input.begin()); input.erase(input.begin());
@ -924,13 +906,19 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
s.push_back(input[0]); s.push_back(input[0]);
input.erase(input.begin()); input.erase(input.begin());
} }
rcond = condition(s, r, engine);
r.display();
for (int i = 0; i < s.size(); ++i){
cout << "s[i]: " << s[i] << "\n";
} }
temp = condition(s, r, engine);
}
} }
temp.display();
// send update command to DBEngine engine.updateFromRelationCmd(r, temp, a, c);
} }
else cout<<"Syntax error! 1"<<endl; else cout<<"Syntax error! 1"<<endl;
@ -938,37 +926,33 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
vector<string> deleteCMD(vector<string> input, DBEngine &engine) vector<string> deleteCMD(vector<string> input, DBEngine &engine)
{ {
if (input[0] == "DELETE" && input[1] == "FROM") if (input[0] == "FROM"){
{
input.erase(input.begin());
input.erase(input.begin()); input.erase(input.begin());
Relation rcond = engine.getTableFromName(input[0]); Relation r = engine.getTableFromName(input[0]);
Relation rfinal = engine.getTableFromName(input[0]); Relation temp("TEMP");
input.erase(input.begin()); input.erase(input.begin());
vector<string> s; vector<string> s;
if(input[0] == "WHERE") if(input[0] == "WHERE"){
{
input.erase(input.begin()); input.erase(input.begin());
if(input[0] == "(") if(input[0] == "("){
{
input.erase(input.begin()); input.erase(input.begin());
while(input[0] != ")") while(input[0] != ")"){
{
s.push_back(input[0]); s.push_back(input[0]);
input.erase(input.begin()); input.erase(input.begin());
} }
} }
temp = condition(s, r, engine);
} }
//rcond = condition(); engine.deleteFromRelationCmd(r, temp);
// send delete command to DBEngine
} }
else cout<<"Syntax error!"<<endl;
else cout<<"Syntax error!"<<endl; //make specific
return input;
} }
vector<string> query(vector<string> input, DBEngine &engine) vector<string> query(vector<string> input, DBEngine &engine)
@ -988,7 +972,7 @@ vector<string> query(vector<string> input, DBEngine &engine)
return input; return input;
} }
void par_line(vector<string> input, DBEngine &engine) //calls par_command() or par_query() depending on first item from token list void par_line(vector<string> input, DBEngine &engine)
{ {
/* /*
Match the first item in the token list and determine weather this is a command or a query. Match the first item in the token list and determine weather this is a command or a query.

View file

@ -176,51 +176,6 @@ void Relation::removeFromTuple(int rindex, int aindex)
} }
} }
void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere){ void Relation::updateInAttribute(string val, int attIndex, int index){
vector<int> inds; att[attIndex].setValue(val, index);
bool found = false;
for (int i = 0; i < att.size(); ++i){
if (att[i].getName() == attNameWhere){
found = true;
for (int j = 0; j < att[i].getSize(); ++j){
if (att[i][j] == attWhere){
inds.push_back(j);
}
}
}
}
if (!found){
cout << "Failure to update, the requested attribute was not found." << endl;
}
for (int i = 0; i < att.size(); ++i){
if (att[i].getName() == attNameSet){
for (int j = 0; j < inds.size(); ++j){
att[i].setValue(attSet, inds[j]);
}
}
}
return;
}
void Relation::deleteCmd(string attName, string s){
bool found = false;
for (int i = 0; i < att.size(); ++i){
if (att[i].getName() == attName){
found = true;
for (int j = 0; j < att[i].getSize(); ++j){
if (att[i][j] == s){
removeTuple(j);
}
}
}
}
if (!found){
cout << "Failure to delete, the requested attribute was not found." << endl;
}
} }

View file

@ -29,6 +29,5 @@ public:
void insertFromRelation(Relation r); void insertFromRelation(Relation r);
void removeTuple(int index); void removeTuple(int index);
void removeFromTuple(int rindex, int aindex); void removeFromTuple(int rindex, int aindex);
void updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere); void updateInAttribute(string val, int attIndex, int index);
void deleteCmd(string attName, string s);
}; };

BIN
a.out

Binary file not shown.

View file

@ -33,15 +33,8 @@ int main () {
v.push_back(att3); v.push_back(att3);
engine.createTable("Food", v); engine.createTable("Food", v);
//engine.updateCmd("Food", "Dinner", "SUCCESS", "Breakfast", "Pancakes"); //engine.getTableFromName("Food").display();
engine.getTableFromName("Food").display();
//engine.deleteCmd("Food", "Breakfast", "Pancakes");
vector<string> tuple; parse("UPDATE Food SET ( Dinner = \"SUCCESS\" ) WHERE ( Breakfast == \"Pancakes\" ) ;", engine);
tuple.push_back("A pancake");
tuple.push_back("A turkey sandwich");
tuple.push_back("A steak");
engine.getTableFromName("Food").updateTuple(tuple, 0);
engine.getTableFromName("Food").display(); engine.getTableFromName("Food").display();
} }