update works
This commit is contained in:
parent
ea2b612129
commit
dd4453c9aa
7 changed files with 126 additions and 153 deletions
66
DBEngine.cpp
66
DBEngine.cpp
|
@ -151,8 +151,7 @@ Relation DBEngine::projection(vector<string> input, Relation r){
|
|||
}
|
||||
|
||||
//test error matching
|
||||
Relation DBEngine::rename(vector<string> newnames, Relation &r)
|
||||
{
|
||||
Relation DBEngine::rename(vector<string> newnames, Relation &r){
|
||||
vector<string> temp;
|
||||
if (r.getSize() != newnames.size()) {
|
||||
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;
|
||||
}
|
||||
|
||||
void DBEngine::updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere){
|
||||
for(int i = 0; i < tables.size(); i++){
|
||||
if (tables[i].getTableName() == relationName){
|
||||
tables[i].updateCmd(attNameSet, attSet, attNameWhere, attWhere);
|
||||
return;
|
||||
void DBEngine::updateFromRelationCmd(Relation r, Relation temp, vector<string> attName, vector<string> att){
|
||||
string rName = r.getTableName();
|
||||
int index = -1;
|
||||
|
||||
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){
|
||||
for(int i = 0; i < tables.size(); i++){
|
||||
if (tables[i].getTableName() == relationName){
|
||||
tables[i].deleteCmd(attName, att);
|
||||
return;
|
||||
void DBEngine::deleteFromRelationCmd(Relation r, Relation temp){
|
||||
string rName = r.getTableName();
|
||||
int index = -1;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,6 @@ public:
|
|||
Relation setUnion(Relation r1, Relation r2);
|
||||
Relation setDiff(Relation r1, Relation r2);
|
||||
Relation crossProduct(Relation r1, Relation r2);
|
||||
void updateCmd(string relationName, string attNameSet, string attSet, string attNameWhere, string attWhere);
|
||||
void deleteCmd(string relationName, string attName, string att);
|
||||
void updateFromRelationCmd(Relation r, Relation temp, vector<string> attName, vector<string> att);
|
||||
void deleteFromRelationCmd(Relation r, Relation temp);
|
||||
};
|
||||
|
|
146
Parser.cpp
146
Parser.cpp
|
@ -287,11 +287,11 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine){
|
|||
Relation rtemp2 = condition(input, r, engine);
|
||||
rfinal = engine.setUnion(rtemp, rtemp2);
|
||||
}
|
||||
|
||||
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");
|
||||
tuple<vector<string>, Relation> t(input, rfinal);
|
||||
if(input[0] == "select")
|
||||
|
@ -557,8 +557,7 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
|
|||
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] != "*"))
|
||||
{
|
||||
|
@ -575,8 +574,7 @@ vector<string> showCMD(vector<string> input, DBEngine &engine)
|
|||
return input;
|
||||
}
|
||||
|
||||
vector<string> saveCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
vector<string> saveCMD(vector<string> input, DBEngine &engine){
|
||||
if (input.size() > 2)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
|
@ -587,8 +585,7 @@ vector<string> saveCMD(vector<string> input, DBEngine &engine)
|
|||
return input;
|
||||
}
|
||||
|
||||
vector<string> closeCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
vector<string> closeCMD(vector<string> input, DBEngine &engine){
|
||||
if (input.size() > 3)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
|
@ -599,8 +596,7 @@ vector<string> closeCMD(vector<string> input, DBEngine &engine)
|
|||
return input;
|
||||
}
|
||||
|
||||
vector<string> openCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
vector<string> openCMD(vector<string> input, DBEngine &engine){
|
||||
if (input.size() > 2)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
|
@ -618,15 +614,12 @@ vector<string> openCMD(vector<string> input, DBEngine &engine)
|
|||
return input;
|
||||
}
|
||||
|
||||
vector<string> exitCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
vector<string> exitCMD(vector<string> input, DBEngine &engine){
|
||||
exit(0);
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
vector<string> createCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
vector<string> createCMD(vector<string> input, DBEngine &engine){
|
||||
if (input[0] == "TABLE")
|
||||
{
|
||||
input.erase(input.begin());
|
||||
|
@ -717,8 +710,7 @@ vector<string> createCMD(vector<string> input, DBEngine &engine)
|
|||
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
|
||||
|
||||
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 rcond("TEMP");
|
||||
Relation rfinal("TEMP");
|
||||
Relation temp("TEMP");
|
||||
vector <string> a;
|
||||
vector <string> c;
|
||||
vector <string> s;
|
||||
|
||||
input.erase(input.begin());
|
||||
|
||||
if(input[0] == "SET")
|
||||
{
|
||||
input.erase(input.begin());
|
||||
vector <Attribute> a = r.getAttributes();
|
||||
|
||||
|
||||
//parse out ( and send everything until ) into an Expression vector
|
||||
if(input[0] == "(")
|
||||
{
|
||||
|
||||
vector <PAttribute> a;
|
||||
PAttribute temp;
|
||||
|
||||
vector <string> s;
|
||||
|
||||
//vector <PExpression> e;
|
||||
|
||||
input.erase(input.begin());
|
||||
|
||||
while(input[0] != ")")
|
||||
{
|
||||
temp.setPAttributeName(input[0]);
|
||||
a.push_back(input[0]);
|
||||
|
||||
input.erase(input.begin());
|
||||
|
||||
|
@ -885,16 +868,18 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
|
|||
{
|
||||
input.erase(input.begin());
|
||||
|
||||
s.push_back(input[0]);
|
||||
//s.push_back(input[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
|
||||
{
|
||||
s.push_back(input[0]);
|
||||
c.push_back(input[0]);
|
||||
}
|
||||
|
||||
input.erase(input.begin());
|
||||
}
|
||||
|
||||
else
|
||||
|
@ -906,50 +891,10 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
|
|||
{
|
||||
input.erase(input.begin());
|
||||
}
|
||||
|
||||
a.push_back(temp);
|
||||
temp.setPAttributeName("~");
|
||||
}
|
||||
|
||||
input.erase(input.begin());
|
||||
}
|
||||
|
||||
if(input[0] == "WHERE")
|
||||
{
|
||||
vector<string> s;
|
||||
if(input[0] == "(")
|
||||
{
|
||||
input.erase(input.begin());
|
||||
while(input[0] != ")")
|
||||
{
|
||||
s.push_back(input[0]);
|
||||
input.erase(input.begin());
|
||||
}
|
||||
rcond = condition(s, r, engine);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// send update command to DBEngine
|
||||
}
|
||||
|
||||
else cout<<"Syntax error! 1"<<endl;
|
||||
}
|
||||
|
||||
vector<string> deleteCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
if (input[0] == "DELETE" && input[1] == "FROM")
|
||||
{
|
||||
input.erase(input.begin());
|
||||
input.erase(input.begin());
|
||||
|
||||
Relation rcond = engine.getTableFromName(input[0]);
|
||||
Relation rfinal = engine.getTableFromName(input[0]);
|
||||
|
||||
input.erase(input.begin());
|
||||
|
||||
vector<string> s;
|
||||
|
||||
if(input[0] == "WHERE")
|
||||
{
|
||||
input.erase(input.begin());
|
||||
|
@ -961,14 +906,53 @@ vector<string> deleteCMD(vector<string> input, DBEngine &engine)
|
|||
s.push_back(input[0]);
|
||||
input.erase(input.begin());
|
||||
}
|
||||
|
||||
r.display();
|
||||
|
||||
for (int i = 0; i < s.size(); ++i){
|
||||
cout << "s[i]: " << s[i] << "\n";
|
||||
}
|
||||
|
||||
temp = condition(s, r, engine);
|
||||
}
|
||||
}
|
||||
|
||||
//rcond = condition();
|
||||
// send delete command to DBEngine
|
||||
|
||||
temp.display();
|
||||
engine.updateFromRelationCmd(r, temp, a, c);
|
||||
}
|
||||
else cout<<"Syntax error!"<<endl;
|
||||
|
||||
else cout<<"Syntax error! 1"<<endl;
|
||||
}
|
||||
|
||||
vector<string> deleteCMD(vector<string> input, DBEngine &engine)
|
||||
{
|
||||
if (input[0] == "FROM"){
|
||||
input.erase(input.begin());
|
||||
|
||||
Relation r = engine.getTableFromName(input[0]);
|
||||
Relation temp("TEMP");
|
||||
input.erase(input.begin());
|
||||
|
||||
vector<string> s;
|
||||
|
||||
if(input[0] == "WHERE"){
|
||||
input.erase(input.begin());
|
||||
if(input[0] == "("){
|
||||
input.erase(input.begin());
|
||||
while(input[0] != ")"){
|
||||
s.push_back(input[0]);
|
||||
input.erase(input.begin());
|
||||
}
|
||||
}
|
||||
temp = condition(s, r, engine);
|
||||
}
|
||||
|
||||
engine.deleteFromRelationCmd(r, temp);
|
||||
}
|
||||
|
||||
else cout<<"Syntax error!"<<endl; //make specific
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
vector<string> query(vector<string> input, DBEngine &engine)
|
||||
|
@ -988,7 +972,7 @@ vector<string> query(vector<string> input, DBEngine &engine)
|
|||
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.
|
||||
|
|
49
Relation.cpp
49
Relation.cpp
|
@ -176,51 +176,6 @@ void Relation::removeFromTuple(int rindex, int aindex)
|
|||
}
|
||||
}
|
||||
|
||||
void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere){
|
||||
vector<int> inds;
|
||||
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;
|
||||
}
|
||||
void Relation::updateInAttribute(string val, int attIndex, int index){
|
||||
att[attIndex].setValue(val, index);
|
||||
}
|
|
@ -29,6 +29,5 @@ public:
|
|||
void insertFromRelation(Relation r);
|
||||
void removeTuple(int index);
|
||||
void removeFromTuple(int rindex, int aindex);
|
||||
void updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere);
|
||||
void deleteCmd(string attName, string s);
|
||||
void updateInAttribute(string val, int attIndex, int index);
|
||||
};
|
BIN
a.out
BIN
a.out
Binary file not shown.
11
test.cpp
11
test.cpp
|
@ -33,15 +33,8 @@ int main () {
|
|||
v.push_back(att3);
|
||||
|
||||
engine.createTable("Food", v);
|
||||
//engine.updateCmd("Food", "Dinner", "SUCCESS", "Breakfast", "Pancakes");
|
||||
engine.getTableFromName("Food").display();
|
||||
//engine.deleteCmd("Food", "Breakfast", "Pancakes");
|
||||
//engine.getTableFromName("Food").display();
|
||||
|
||||
vector<string> tuple;
|
||||
tuple.push_back("A pancake");
|
||||
tuple.push_back("A turkey sandwich");
|
||||
tuple.push_back("A steak");
|
||||
|
||||
engine.getTableFromName("Food").updateTuple(tuple, 0);
|
||||
parse("UPDATE Food SET ( Dinner = \"SUCCESS\" ) WHERE ( Breakfast == \"Pancakes\" ) ;", engine);
|
||||
engine.getTableFromName("Food").display();
|
||||
}
|
||||
|
|
Reference in a new issue