rename expression works.
This commit is contained in:
parent
fa50473ad4
commit
d56a2ee90e
4 changed files with 58 additions and 6 deletions
21
DBEngine.cpp
21
DBEngine.cpp
|
@ -151,12 +151,23 @@ Relation DBEngine::projection(vector<string> input, Relation r){
|
||||||
}
|
}
|
||||||
|
|
||||||
//test error matching
|
//test error matching
|
||||||
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
|
Relation DBEngine::rename(vector<string> newnames, Relation &r)
|
||||||
if (oldnames.size() != newnames.size()) {
|
{
|
||||||
|
vector<string> temp;
|
||||||
|
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";
|
||||||
return;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp = r.getAttributeNames();
|
||||||
|
for(int i = 0; i < temp.size(); ++i)
|
||||||
|
{
|
||||||
|
r.renameAttribute(temp[i], newnames[i]);
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
/*
|
||||||
else if (oldnames != r.getAttributeNames()) {
|
else if (oldnames != r.getAttributeNames()) {
|
||||||
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
|
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
|
||||||
return;
|
return;
|
||||||
|
@ -167,6 +178,8 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
|
||||||
r.renameAttribute(oldnames[i], newnames[i]);
|
r.renameAttribute(oldnames[i], newnames[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Relation DBEngine::setUnion(Relation r1, Relation r2){
|
Relation DBEngine::setUnion(Relation r1, Relation r2){
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
void save();
|
void save();
|
||||||
void save(string n);
|
void save(string n);
|
||||||
void storeCommands(string s);
|
void storeCommands(string s);
|
||||||
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
|
Relation rename(vector<string> newnames, Relation &r);
|
||||||
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);
|
||||||
|
|
41
Parser.cpp
41
Parser.cpp
|
@ -91,7 +91,46 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
|
||||||
|
|
||||||
else if(input[0] == "rename")
|
else if(input[0] == "rename")
|
||||||
{
|
{
|
||||||
|
input.erase(input.begin());
|
||||||
|
if(input[0] == "(")
|
||||||
|
{
|
||||||
|
input.erase(input.begin());
|
||||||
|
vector<string> temp;
|
||||||
|
while(input[0] != ")")
|
||||||
|
{
|
||||||
|
if(input[0] == ",")
|
||||||
|
{
|
||||||
|
input.erase(input.begin());
|
||||||
|
}
|
||||||
|
temp.push_back(input[0]);
|
||||||
|
input.erase(input.begin());
|
||||||
|
}
|
||||||
|
input.erase(input.begin());
|
||||||
|
Relation rfinal("TEMP");
|
||||||
|
if(engine.isRelation(input[0]))
|
||||||
|
{
|
||||||
|
rfinal = engine.rename(temp, engine.getTableFromName(input[0]));
|
||||||
|
input.erase(input.begin());
|
||||||
|
get<0>(t) = input;
|
||||||
|
get<1>(t) = rfinal;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
else if(input[0] == "(")
|
||||||
|
{
|
||||||
|
input.erase(input.begin());
|
||||||
|
t = expression(input, engine);
|
||||||
|
input.erase(input.begin());
|
||||||
|
rfinal = engine.rename(temp, get<1>(t));
|
||||||
|
get<0>(t) = input;
|
||||||
|
get<1>(t) = rfinal;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "Rename syntax incorrect." << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(engine.isRelation(input[0]))
|
else if(engine.isRelation(input[0]))
|
||||||
|
|
BIN
test
BIN
test
Binary file not shown.
Reference in a new issue