2015-09-22 23:27:34 -05:00
|
|
|
#include <string> // std::string
|
|
|
|
#include <iostream> // std::cout
|
|
|
|
#include <sstream> // std::stringstream
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2015-09-25 00:30:09 -05:00
|
|
|
#include "DBEngine.h"
|
2015-09-22 23:27:34 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PRelation
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string name;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
public:
|
|
|
|
PRelation()
|
|
|
|
{
|
|
|
|
name = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PRelation(string str)
|
|
|
|
{
|
|
|
|
name = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPRelation(string str)
|
|
|
|
{
|
|
|
|
name = str;
|
|
|
|
}
|
|
|
|
string getName()
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
2015-09-22 23:44:51 -05:00
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PAttribute
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
string type;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PAttribute()
|
|
|
|
{
|
|
|
|
name = "~";
|
|
|
|
type = "~";
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PAttribute(string str, string t)
|
|
|
|
{
|
|
|
|
name = str;
|
|
|
|
type = t;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PAttribute(string str, string t, int s)
|
|
|
|
{
|
|
|
|
name = str;
|
|
|
|
type = t;
|
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPAttributeName(string str)
|
|
|
|
{
|
|
|
|
name = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPAttributeType(string t)
|
|
|
|
{
|
|
|
|
type = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPAttributeSize(int s)
|
|
|
|
{
|
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
string getPAttribute()
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PUnion
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string Un1;
|
|
|
|
string Un2;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
public:
|
|
|
|
PUnion()
|
|
|
|
{
|
|
|
|
Un1 = "~";
|
|
|
|
Un2 = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PUnion (string s1, string s2)
|
|
|
|
{
|
|
|
|
Un1 = s1;
|
|
|
|
Un2 = s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
string getPUnion()
|
|
|
|
{
|
|
|
|
return "Union of " + Un1 + " and " + Un2;
|
|
|
|
}
|
2015-09-22 23:44:51 -05:00
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PProduct
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string Pr1;
|
|
|
|
string Pr2;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
public:
|
|
|
|
PProduct()
|
|
|
|
{
|
|
|
|
Pr1 = "~";
|
|
|
|
Pr2 = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PProduct(string s1, string s2)
|
|
|
|
{
|
|
|
|
Pr1 = s1;
|
|
|
|
Pr2 = s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
string getPProduct()
|
|
|
|
{
|
|
|
|
return "Product of " + Pr1 + " and " + Pr2;
|
|
|
|
}
|
2015-09-22 23:44:51 -05:00
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PDifference
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string D1;
|
|
|
|
string D2;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
public:
|
|
|
|
PDifference()
|
|
|
|
{
|
|
|
|
D1 = "~";
|
|
|
|
D2 = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PDifference(string s1, string s2)
|
|
|
|
{
|
|
|
|
D1 = s1;
|
|
|
|
D2 = s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
string getPDifference()
|
|
|
|
{
|
|
|
|
return "Difference of " + D1 + " and " + D2;
|
|
|
|
}
|
2015-09-22 23:44:51 -05:00
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PRenaming
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string newName;
|
|
|
|
string oldName;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
public:
|
|
|
|
PRenaming()
|
|
|
|
{
|
|
|
|
newName = "~";
|
|
|
|
oldName = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PRenaming(string s1, string s2)
|
|
|
|
{
|
|
|
|
newName = s1;
|
|
|
|
oldName = s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
string doRename()
|
|
|
|
{
|
|
|
|
return "Renaming " + oldName + " to " + newName;
|
|
|
|
}
|
2015-09-22 23:44:51 -05:00
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PProjection
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string newName;
|
|
|
|
string oldName;
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
public:
|
|
|
|
PProjection()
|
|
|
|
{
|
|
|
|
newName = "~";
|
|
|
|
oldName = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PProjection(string s1, string s2)
|
|
|
|
{
|
|
|
|
newName = s1;
|
|
|
|
oldName = s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
string doPProjection()
|
|
|
|
{
|
|
|
|
return "Projecting " + oldName + " onto " + newName;
|
|
|
|
}
|
2015-09-22 23:44:51 -05:00
|
|
|
};
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class POperand
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string op;
|
|
|
|
|
2015-09-22 23:44:51 -05:00
|
|
|
public:
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
POperand()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
2015-09-25 00:30:09 -05:00
|
|
|
op = "~";
|
2015-09-22 23:44:51 -05:00
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
POperand(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
op = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
void setPOperand(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
op = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
string getPOperand()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class POp
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string op;
|
|
|
|
|
2015-09-22 23:44:51 -05:00
|
|
|
public:
|
2015-09-25 00:30:09 -05:00
|
|
|
POp()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
2015-09-25 00:30:09 -05:00
|
|
|
op = "~";
|
2015-09-22 23:44:51 -05:00
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
POp(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
op = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
void setPOp(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
op = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
string getPOp()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PComparison
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
2015-09-25 00:30:09 -05:00
|
|
|
POp op;
|
|
|
|
POperand operand1;
|
|
|
|
POperand operand2;
|
2015-09-22 23:27:34 -05:00
|
|
|
|
2015-09-22 23:44:51 -05:00
|
|
|
public:
|
2015-09-25 00:30:09 -05:00
|
|
|
PComparison()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
2015-09-25 00:30:09 -05:00
|
|
|
op.setPOp("~");
|
|
|
|
operand1.setPOperand("~");
|
|
|
|
operand2.setPOperand("~");
|
2015-09-22 23:44:51 -05:00
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
PComparison(string str1, string str2, string str3)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
2015-09-25 00:30:09 -05:00
|
|
|
operand1.setPOperand(str1);
|
|
|
|
op.setPOp(str2);
|
|
|
|
operand2.setPOperand(str3);
|
2015-09-22 23:44:51 -05:00
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
void setPComparison(string str1, string str2, string str3)
|
2015-09-26 23:24:24 -05:00
|
|
|
{
|
|
|
|
operand1.setPOperand(str1);
|
|
|
|
op.setPOp(str2);
|
|
|
|
operand2.setPOperand(str3);
|
|
|
|
}
|
|
|
|
|
|
|
|
string getPComparison()
|
|
|
|
{
|
2015-09-25 00:30:09 -05:00
|
|
|
return operand1.getPOperand() + " " + op.getPOp() + " " + operand2.getPOperand();
|
2015-09-22 23:44:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PConjunction
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string conj;
|
2015-09-22 23:44:51 -05:00
|
|
|
public:
|
2015-09-25 00:30:09 -05:00
|
|
|
PConjunction()
|
|
|
|
{
|
|
|
|
conj = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PConjunction(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
conj = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
void setPConjunction(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
conj = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
string getPConjunction()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
return conj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PCondition
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string cond;
|
2015-09-22 23:44:51 -05:00
|
|
|
public:
|
2015-09-25 00:30:09 -05:00
|
|
|
|
|
|
|
PCondition()
|
|
|
|
{
|
|
|
|
cond = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PCondition(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
cond = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
void setPCondition(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
cond = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
string getPCondition()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
return cond;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
class PSelection
|
2015-09-22 23:27:34 -05:00
|
|
|
{
|
|
|
|
string select;
|
2015-09-22 23:44:51 -05:00
|
|
|
public:
|
2015-09-25 00:30:09 -05:00
|
|
|
PSelection()
|
|
|
|
{
|
|
|
|
select = "~";
|
|
|
|
}
|
|
|
|
|
|
|
|
PSelection(string str)
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
select = str;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
string getPSelection()
|
2015-09-22 23:44:51 -05:00
|
|
|
{
|
|
|
|
return select;
|
|
|
|
}
|
|
|
|
};
|
2015-09-25 00:30:09 -05:00
|
|
|
|
|
|
|
class PExpression
|
|
|
|
{
|
|
|
|
PRelation rel;
|
|
|
|
PSelection sel;
|
|
|
|
PProjection proj;
|
|
|
|
PRenaming ren;
|
|
|
|
PUnion un;
|
|
|
|
PDifference diff;
|
|
|
|
PProduct prod;
|
|
|
|
string temp;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
PExpression()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PExpression(string str)
|
|
|
|
{
|
|
|
|
temp = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPExpression(string str)
|
|
|
|
{
|
|
|
|
temp = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
string getPExpression()
|
|
|
|
{
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-27 00:58:19 -05:00
|
|
|
/*
|
2015-09-25 00:30:09 -05:00
|
|
|
vector<string> tokenize(string ss)
|
|
|
|
{
|
|
|
|
|
|
|
|
string tempString;
|
|
|
|
stringstream lineStream(ss);
|
|
|
|
vector<string> output;
|
|
|
|
|
|
|
|
while (lineStream >> tempString)
|
|
|
|
{
|
|
|
|
output.push_back(tempString);
|
|
|
|
}
|
|
|
|
|
|
|
|
//testing---------------
|
|
|
|
cout<<"TokenList: ";
|
|
|
|
|
|
|
|
for (int i = 0; i <output.size(); ++i)
|
|
|
|
{
|
|
|
|
cout<<output[i]<<" ";
|
|
|
|
}
|
|
|
|
//----------------------
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void displayTokenList(vector<string> input)
|
|
|
|
{
|
|
|
|
cout<<"TokenList: "<<endl;
|
|
|
|
for (int i = 0; i < input.size(); ++i)
|
|
|
|
{
|
|
|
|
cout<<input[i]<<endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> showCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
if (input.size() > 3)
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
// send show command to DBEngine
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> saveCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
if (input.size() > 3)
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
// send save command to DBEngine
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> closeCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
if (input.size() > 3)
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
// send close command to DBEngine
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> openCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
if (input.size() > 2)
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
// send open command to DBEngine
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> exitCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
if (input[1] != ";")
|
|
|
|
{
|
|
|
|
cout<<"ERROR: missing semicolon!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[0] != "EXIT") { cout<<"Wrong function/syntax error!"<<endl;}
|
|
|
|
cout<<"Passing command: "<<input[0]<<endl;
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<string> createCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
//relation name will be the first element of the vector of data returned by this function
|
|
|
|
|
|
|
|
if (input[0] == "CREATE" && input[1] == "TABLE")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
PRelation r;
|
|
|
|
r.setPRelation(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
//vector <PExpression> e1;
|
|
|
|
|
|
|
|
vector <PAttribute> a;
|
|
|
|
|
|
|
|
while(input[0] != ")") //inserting all values to relation
|
|
|
|
{
|
|
|
|
PAttribute temp;
|
|
|
|
|
|
|
|
if (input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
temp.setPAttributeName(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "INTEGER")
|
|
|
|
{
|
|
|
|
temp.setPAttributeType(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp.setPAttributeType(input[0].substr(0,input[0].find("(")));
|
|
|
|
temp.setPAttributeSize(stoi(input[0].substr(input[0].find("("), input[0].find(")"))));
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
a.push_back(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector <PAttribute> apk; //save primary keys temp storage
|
|
|
|
|
|
|
|
if(input[0] == "PRIMARY" && input[1] == "KEY")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
|
|
|
|
while(input[0] != ")") //inserting all values to relation
|
|
|
|
{
|
|
|
|
PAttribute temp;
|
|
|
|
|
|
|
|
if (input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
temp.setPAttributeName(input[0]);
|
|
|
|
|
|
|
|
apk.push_back(temp);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
else cout<<"Syntax error!"<<endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> insertCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
//relation name will be the first element of the vector of data returned by this function
|
|
|
|
|
|
|
|
if (input[0] == "INTO")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
vector <string> s;
|
|
|
|
|
|
|
|
if (input[0] == "VALUES" && input[1] == "FROM")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
if(input[0].at(0) == '\"')
|
|
|
|
{
|
|
|
|
s.push_back(input[0].substr(1,input[0].find_last_of("\"")));
|
|
|
|
}
|
|
|
|
vector <PExpression> e;
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
while(input[0] != ")") //inserting all values to relation
|
|
|
|
//for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
if (input[0] == ",") input.erase(input.begin());
|
|
|
|
|
|
|
|
e.push_back(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Inserting: ";
|
|
|
|
while(!e.empty())
|
|
|
|
{
|
|
|
|
cout << e[0].getPExpression() << " ";
|
|
|
|
e.erase(e.begin());
|
|
|
|
}
|
|
|
|
cout << "into " << r.getName() << ".\n";
|
|
|
|
|
|
|
|
return input;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (input[0] == "RELATION")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
PExpression e;
|
|
|
|
|
|
|
|
while(input[0] != ";")
|
|
|
|
{
|
|
|
|
e.setPExpression(e.getPExpression() + input[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n";
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
else cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else cout<<"Syntax error!"<<endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> updateCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "SET")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
//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(temp);
|
|
|
|
temp.setPAttributeName("~");
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "=")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
s.push_back(input[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input[0] == "WHERE")
|
|
|
|
{
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
//PCondition c;
|
|
|
|
|
|
|
|
PComparison c;
|
|
|
|
POperand oops1;
|
|
|
|
POperand oops2;
|
|
|
|
POp op;
|
|
|
|
|
|
|
|
while(input[0] != ")")
|
|
|
|
{
|
|
|
|
oops1.setPOperand(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
oops2.setPOperand(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
op.setPOp(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
c.setPComparison(oops1.getPOperand(), op.getPOp(), oops2.getPOperand());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send update command to DBEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
else cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> deleteCMD(vector<string> input)
|
|
|
|
{
|
|
|
|
if (input[0] == "DELETE" && input[1] == "FROM")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
if(input[0] == "WHERE")
|
|
|
|
{
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
//PCondition c;
|
|
|
|
|
|
|
|
PComparison c;
|
|
|
|
POperand oops1;
|
|
|
|
POperand oops2;
|
|
|
|
POp op;
|
|
|
|
|
|
|
|
while(input[0] != ")")
|
|
|
|
{
|
|
|
|
oops1.setPOperand(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
oops2.setPOperand(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
op.setPOp(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
c.setPComparison(oops1.getPOperand(), op.getPOp(), oops2.getPOperand());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send delete command to DBEngine
|
|
|
|
}
|
|
|
|
else cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void par_line(vector<string> input) //calls par_command() or par_query() depending on first item from token list
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
• Match the first item in the token list and determine weather this is a command or a query.
|
|
|
|
• Call functions par_command() or par_query();
|
|
|
|
• After either par_command() or par_query() returns, make sure the line ends properly with “;” token
|
2015-09-27 00:58:19 -05:00
|
|
|
|
2015-09-25 00:30:09 -05:00
|
|
|
string tempChar = input.back();
|
|
|
|
|
|
|
|
if (tempChar != ";")
|
|
|
|
{
|
|
|
|
cout<<"ERROR! missing semicolon "<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( input[0] == "INSERT")
|
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
vector<string> insertInput = insertCMD(input);
|
|
|
|
cout<<"arguments: "<<endl;
|
|
|
|
displayTokenList(insertInput);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "CREATE")
|
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
vector<string> insertInput = createCMD(input);
|
|
|
|
cout<<"arguments: "<<endl;
|
|
|
|
displayTokenList(insertInput);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "DELETE")
|
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
vector<string> insertInput = deleteCMD(input);
|
|
|
|
cout<<"arguments: "<<endl;
|
|
|
|
displayTokenList(insertInput);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "UPDATE")
|
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
vector<string> insertInput = updateCMD(input);
|
|
|
|
cout<<"arguments: "<<endl;
|
|
|
|
displayTokenList(insertInput);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "SHOW")
|
|
|
|
{
|
|
|
|
showCMD(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "EXIT")
|
|
|
|
{
|
|
|
|
exitCMD(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "OPEN")
|
|
|
|
{
|
|
|
|
openCMD(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "SAVE")
|
|
|
|
{
|
|
|
|
saveCMD(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( input[0] == "CLOSE")
|
|
|
|
{
|
|
|
|
closeCMD(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse(string input, DBEngine &engine)
|
|
|
|
{
|
|
|
|
engine.storeCommands(input);
|
|
|
|
vector<string> listOfTokens = tokenize(input);
|
|
|
|
par_line(listOfTokens);
|
|
|
|
}
|
2015-09-27 00:58:19 -05:00
|
|
|
*/
|
2015-09-25 00:30:09 -05:00
|
|
|
|
2015-09-27 00:58:19 -05:00
|
|
|
void parse(string s, DBEngine &e);
|