2015-09-23 02:47:08 -05:00
|
|
|
#include <string> // std::string
|
|
|
|
#include <iostream> // std::cout
|
2015-09-27 23:24:02 -05:00
|
|
|
#include <fstream> // std::ofstream
|
2015-09-23 02:47:08 -05:00
|
|
|
#include <sstream> // std::stringstream
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2015-09-28 19:11:26 -05:00
|
|
|
#include "Parser.h"
|
2015-09-29 12:36:45 -05:00
|
|
|
#include <tuple>
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
vector<string> tokenize(string ss)
|
|
|
|
{
|
|
|
|
string tempString;
|
|
|
|
stringstream lineStream(ss);
|
|
|
|
vector<string> output;
|
|
|
|
|
|
|
|
while (lineStream >> tempString)
|
|
|
|
{
|
|
|
|
output.push_back(tempString);
|
|
|
|
}
|
|
|
|
|
|
|
|
//testing---------------
|
2015-09-28 19:23:10 -05:00
|
|
|
//cout<<"TokenList: ";
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-28 19:23:10 -05:00
|
|
|
// for (int i = 0; i <output.size(); ++i)
|
|
|
|
// {
|
|
|
|
// cout<<output[i]<<" ";
|
|
|
|
// }
|
|
|
|
// cout << endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
//----------------------
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void displayTokenList(vector<string> input)
|
|
|
|
{
|
|
|
|
cout<<"TokenList: "<<endl;
|
|
|
|
for (int i = 0; i < input.size(); ++i)
|
|
|
|
{
|
|
|
|
cout<<input[i]<<endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 12:36:45 -05:00
|
|
|
tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engine)
|
|
|
|
{
|
|
|
|
Relation rfinal("TEMP");
|
|
|
|
tuple<vector<string>, Relation> t(input, rfinal);
|
|
|
|
if(input[0] == "select")
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "project")
|
|
|
|
{
|
2015-09-29 13:28:24 -05:00
|
|
|
input.erase(input.begin());
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
vector<string> temp;
|
|
|
|
while(input[0] != ")")
|
|
|
|
{
|
|
|
|
temp.push_back(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
input.erase(input.begin());
|
|
|
|
Relation rfinal("TEMP");
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
rfinal = engine.projection(temp, engine.getTableFromName(input[0]));
|
|
|
|
input.erase(input.begin());
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
t = expression(input, engine);
|
|
|
|
rfinal = engine.projection(temp, get<1>(t));
|
|
|
|
//get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Projection syntax incorrect." << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-09-29 12:36:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "rename")
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r1(engine.getTableFromName(input[0]));
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "+")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r2(engine.getTableFromName(input[0]));
|
|
|
|
rfinal = engine.setUnion(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r2 = get<1>(t);
|
|
|
|
rfinal = engine.setUnion(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "-")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r2(engine.getTableFromName(input[0]));
|
|
|
|
rfinal = engine.setDiff(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r2 = get<1>(t);
|
|
|
|
rfinal = engine.setDiff(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "*")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r2(engine.getTableFromName(input[0]));
|
|
|
|
rfinal = engine.crossProduct(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r2 = get<1>(t);
|
|
|
|
rfinal = engine.crossProduct(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
Relation r1(engine.getTableFromName(input[0]));
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "+")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r2(engine.getTableFromName(input[0]));
|
|
|
|
rfinal = engine.setUnion(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r2 = get<1>(t);
|
|
|
|
rfinal = engine.setUnion(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "-")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r2(engine.getTableFromName(input[0]));
|
|
|
|
rfinal = engine.setDiff(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r2 = get<1>(t);
|
|
|
|
rfinal = engine.setDiff(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(input[0] == "*")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
Relation r2(engine.getTableFromName(input[0]));
|
|
|
|
rfinal = engine.crossProduct(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
else if(input[0] == "(")
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r2 = get<1>(t);
|
|
|
|
rfinal = engine.crossProduct(r1, r2);
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get<0>(t) = input;
|
|
|
|
get<1>(t) = rfinal;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> showCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-29 13:28:24 -05:00
|
|
|
/*
|
2015-09-23 02:47:08 -05:00
|
|
|
if (input.size() > 3)
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
2015-09-29 13:28:24 -05:00
|
|
|
*/
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-29 13:28:24 -05:00
|
|
|
if(engine.isRelation(input[0]))
|
|
|
|
{
|
|
|
|
engine.getTableFromName(input[0]).display();
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
|
2015-09-29 13:28:24 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
get<1>(t).display();
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> saveCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
if (input.size() > 2)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
//PRelation pr(input[0]);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
// send save command to DBEngine
|
2015-09-28 16:18:49 -05:00
|
|
|
engine.save(input[0]);
|
|
|
|
/*
|
2015-09-27 23:24:02 -05:00
|
|
|
Relation r = engine.getTableFromName(input[0]);
|
|
|
|
ofstream output;
|
|
|
|
string name = r.getTableName() + ".txt";
|
|
|
|
output.open(name);
|
|
|
|
|
|
|
|
output << "--------------------------\n";
|
|
|
|
output << r.getTableName() << "\n\n";
|
|
|
|
vector <Attribute> att = r.getAttributes();
|
|
|
|
for (int i = 0; i < att.size(); ++i)
|
|
|
|
{
|
|
|
|
output << "-------------\n";
|
|
|
|
output << att[i].getName() << "\n" << att[i].getType();
|
|
|
|
if(att[i].getType() == "VARCHAR")
|
|
|
|
{
|
|
|
|
output << "(" << att[i].getSize() << ")";
|
|
|
|
}
|
|
|
|
output << "\n\n";
|
|
|
|
|
|
|
|
vector<string> values = att[i].getValues();
|
|
|
|
vector<string>::iterator it = values.begin();
|
|
|
|
while (it != values.end())
|
|
|
|
{
|
|
|
|
output << *it << "\n";
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
output << "-------------\n";
|
|
|
|
}
|
|
|
|
output << "\n--------------------------";
|
2015-09-28 16:18:49 -05:00
|
|
|
*/
|
2015-09-27 23:24:02 -05:00
|
|
|
input.erase(input.begin());
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> closeCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
if (input.size() > 3)
|
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
|
2015-09-28 18:47:43 -05:00
|
|
|
//PRelation r(input[0]);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
// send close command to DBEngine
|
2015-09-28 18:47:43 -05:00
|
|
|
engine.deleteRelation(input[0]);
|
|
|
|
input.erase(input.begin());
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> openCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
if (input.size() > 2)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
cout<<"Syntax error!"<<endl;
|
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
|
2015-09-28 18:47:43 -05:00
|
|
|
//PRelation r(input[0]);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
// send open command to DBEngine
|
2015-09-28 18:47:43 -05:00
|
|
|
string name = input[0] + ".txt";
|
|
|
|
ifstream file;
|
|
|
|
file.open(name);
|
|
|
|
string line;
|
|
|
|
while(getline(file, line))
|
|
|
|
{
|
|
|
|
parse(line, engine);
|
|
|
|
}
|
|
|
|
file.close();
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> exitCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
exit(0);
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> createCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
//relation name will be the first element of the vector of data returned by this function
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
if (input[0] == "TABLE")
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-23 17:34:36 -05:00
|
|
|
PRelation r;
|
|
|
|
r.setPRelation(input[0]);
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-24 19:39:07 -05:00
|
|
|
//vector <PExpression> e1;
|
|
|
|
|
2015-09-23 17:34:36 -05:00
|
|
|
vector <PAttribute> a;
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
while(input[0] != ")") //inserting all values to relation
|
|
|
|
{
|
2015-09-23 17:34:36 -05:00
|
|
|
PAttribute temp;
|
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
if (input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:34:36 -05:00
|
|
|
temp.setPAttributeName(input[0]);
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
input.erase(input.begin());
|
2015-09-23 17:34:36 -05:00
|
|
|
|
|
|
|
if(input[0] == "INTEGER")
|
|
|
|
{
|
|
|
|
temp.setPAttributeType(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp.setPAttributeType(input[0].substr(0,input[0].find("(")));
|
2015-09-27 23:24:02 -05:00
|
|
|
temp.setPAttributeSize(stoi(input[0].substr(input[0].find("(") + 1, input[0].find(")"))));
|
2015-09-23 17:34:36 -05:00
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
a.push_back(temp);
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
//vector <PAttribute> apk; //save primary keys temp storage
|
2015-09-24 19:39:07 -05:00
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
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
|
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
//PAttribute temp;
|
2015-09-23 17:34:36 -05:00
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
if (input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
//temp.setPAttributeName(input[0]);
|
2015-09-24 19:39:07 -05:00
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
//apk.push_back(temp);
|
|
|
|
|
|
|
|
for(int i = 0; i < a.size(); ++i)
|
|
|
|
{
|
|
|
|
if(input[0] == a[i].getPAttribute())
|
|
|
|
{
|
|
|
|
a[i].setPAttributeKey();
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-27 23:24:02 -05:00
|
|
|
|
|
|
|
vector <Attribute> dba;
|
|
|
|
|
|
|
|
for(int x = 0; x < a.size(); ++x)
|
|
|
|
{
|
|
|
|
Attribute temp(a[x].getPAttribute(), a[x].getPAttributeType(), a[x].getPAttributeKey(), a[x].getPAttributeSize());
|
|
|
|
dba.push_back(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.createTable(r.getName(), dba);
|
|
|
|
|
|
|
|
return input;
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
else cout<<"Syntax error! 2"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
2015-09-27 23:24:02 -05:00
|
|
|
else cout<<"Syntax error! 1"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> insertCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
//relation name will be the first element of the vector of data returned by this function
|
|
|
|
|
|
|
|
if (input[0] == "INTO")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
PRelation pr(input[0]);
|
|
|
|
Relation r = engine.getTableFromName(input[0]);
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-23 17:34:36 -05:00
|
|
|
vector <string> s;
|
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
if (input[0] == "VALUES" && input[1] == "FROM")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector <Attribute> a = r.getAttributes();
|
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
for(int i = 0; i < a.size(); ++i)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
if(a[i].getType() == "INTEGER")
|
|
|
|
{
|
|
|
|
if(input[0].at(0) == '\"')
|
|
|
|
{
|
|
|
|
cout << "Incorrect type matching. Tried to insert string." << endl;
|
|
|
|
cout << "The values should be: ";
|
|
|
|
for(int x = 0; x < a.size(); ++i)
|
|
|
|
{
|
|
|
|
cout << a[x].getType();
|
|
|
|
if(a[x].getType() == "VARCHAR")
|
|
|
|
{
|
|
|
|
cout << "(" << a[x].getSize() << ")";
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
s.push_back(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(input[0].at(0) == '\"')
|
|
|
|
{
|
|
|
|
s.push_back(input[0].substr(1,input[0].find_last_of("\"") - 1 ));
|
|
|
|
//engine.getTableFromName(pr.getName()).getAttributes()[i].display();
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Incorrect type matching. Tried to insert integer." << endl;
|
|
|
|
cout << "The values should be: ";
|
|
|
|
for(int x = 0; x < a.size(); ++x)
|
|
|
|
{
|
|
|
|
cout << a[x].getType();
|
|
|
|
if(a[x].getType() == "VARCHAR")
|
|
|
|
{
|
|
|
|
cout << "(" << a[x].getSize() << ")";
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
if (input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
2015-09-27 23:24:02 -05:00
|
|
|
if(input[0] != ")")
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout << "Too many values trying to be inserted. Only insert " << a.size() << " values.";
|
|
|
|
cout << "The values should be: ";
|
|
|
|
for(int x = 0; x < a.size(); ++x)
|
|
|
|
{
|
|
|
|
cout << a[x].getType();
|
|
|
|
if(a[x].getType() == "VARCHAR")
|
|
|
|
{
|
|
|
|
cout << "(" << a[x].getSize() << ")";
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
exit(1);
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
2015-09-27 23:24:02 -05:00
|
|
|
engine.insertValues(r.getTableName(), s);
|
|
|
|
input.erase(input.begin());
|
2015-09-23 02:47:08 -05:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (input[0] == "RELATION")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-29 12:36:45 -05:00
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-29 12:36:45 -05:00
|
|
|
engine.createTable(get<1>(t));
|
2015-09-27 23:24:02 -05:00
|
|
|
|
2015-09-29 12:36:45 -05:00
|
|
|
input = get<0>(t);
|
2015-09-24 19:39:07 -05:00
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
else cout<<"Syntax error! 3"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"Syntax error! 2"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
else cout<<"Syntax error! 1"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> updateCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-24 19:39:07 -05:00
|
|
|
PRelation r(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
2015-09-23 02:47:08 -05:00
|
|
|
|
|
|
|
if(input[0] == "SET")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
//parse out ( and send everything until ) into an Expression vector
|
|
|
|
if(input[0] == "(")
|
|
|
|
{
|
|
|
|
|
2015-09-24 19:39:07 -05:00
|
|
|
vector <PAttribute> a;
|
2015-09-24 20:40:50 -05:00
|
|
|
PAttribute temp;
|
|
|
|
|
2015-09-24 19:39:07 -05:00
|
|
|
vector <string> s;
|
|
|
|
|
2015-09-24 20:40:50 -05:00
|
|
|
//vector <PExpression> e;
|
2015-09-24 19:39:07 -05:00
|
|
|
|
2015-09-23 02:47:08 -05:00
|
|
|
input.erase(input.begin());
|
|
|
|
|
|
|
|
while(input[0] != ")")
|
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
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
|
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"Syntax error! 2"<<endl;
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(input[0] == ",")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input[0] == "WHERE")
|
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
if(input[0] == "(")
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
//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());
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
// send update command to DBEngine
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
else cout<<"Syntax error! 1"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> deleteCMD(vector<string> input, DBEngine &engine)
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
if (input[0] == "DELETE" && input[1] == "FROM")
|
|
|
|
{
|
|
|
|
input.erase(input.begin());
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-23 17:34:36 -05:00
|
|
|
PRelation r(input[0]);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
if(input[0] == "WHERE")
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
if(input[0] == "(")
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
//PCondition c;
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-24 20:40:50 -05:00
|
|
|
PComparison c;
|
|
|
|
POperand oops1;
|
|
|
|
POperand oops2;
|
|
|
|
POp op;
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-24 20:40:50 -05:00
|
|
|
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());
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
// send delete command to DBEngine
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
2015-09-24 20:40:50 -05:00
|
|
|
else cout<<"Syntax error!"<<endl;
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
|
2015-09-29 12:00:37 -05:00
|
|
|
vector<string> query(vector<string> input, DBEngine &engine)
|
2015-09-28 19:52:50 -05:00
|
|
|
{
|
|
|
|
PRelation pr(input[0]);
|
|
|
|
|
|
|
|
input.erase(input.begin());
|
2015-09-29 12:00:37 -05:00
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-29 12:36:45 -05:00
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
|
|
Relation r = get<1>(t);
|
2015-09-29 12:00:37 -05:00
|
|
|
r.setTableName(pr.getName());
|
|
|
|
//r.display();
|
2015-09-29 12:36:45 -05:00
|
|
|
input = get<0>(t);
|
2015-09-29 12:00:37 -05:00
|
|
|
|
|
|
|
engine.createTable(r);
|
|
|
|
|
|
|
|
return input;
|
2015-09-28 19:52:50 -05:00
|
|
|
}
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
void par_line(vector<string> input, DBEngine &engine) //calls par_command() or par_query() depending on first item from token list
|
2015-09-23 02:47:08 -05:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
• 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
|
|
|
|
*/
|
|
|
|
string tempChar = input.back();
|
|
|
|
|
|
|
|
if (tempChar != ";")
|
|
|
|
{
|
|
|
|
cout<<"ERROR! missing semicolon "<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2015-09-24 20:40:50 -05:00
|
|
|
if ( input[0] == "INSERT")
|
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> insertInput = insertCMD(input, engine);
|
2015-09-28 19:23:10 -05:00
|
|
|
//cout<<"arguments: "<<endl;
|
|
|
|
//displayTokenList(insertInput);
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "CREATE")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
2015-09-23 02:47:08 -05:00
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> insertInput = createCMD(input, engine);
|
2015-09-28 19:23:10 -05:00
|
|
|
//cout<<"arguments: "<<endl;
|
|
|
|
//displayTokenList(insertInput);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "DELETE")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> insertInput = deleteCMD(input, engine);
|
2015-09-28 19:23:10 -05:00
|
|
|
//cout<<"arguments: "<<endl;
|
|
|
|
//displayTokenList(insertInput);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "UPDATE")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
|
2015-09-27 23:24:02 -05:00
|
|
|
vector<string> insertInput = updateCMD(input, engine);
|
2015-09-28 19:23:10 -05:00
|
|
|
//cout<<"arguments: "<<endl;
|
|
|
|
//displayTokenList(insertInput);
|
2015-09-24 20:40:50 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "SHOW")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
showCMD(input, engine);
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "EXIT")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
exitCMD(input, engine);
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "OPEN")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
openCMD(input, engine);
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "SAVE")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
saveCMD(input, engine);
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 19:52:50 -05:00
|
|
|
else if ( input[0] == "CLOSE")
|
2015-09-24 20:40:50 -05:00
|
|
|
{
|
2015-09-27 23:24:02 -05:00
|
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
|
|
input.erase(input.begin());
|
|
|
|
closeCMD(input, engine);
|
2015-09-24 20:40:50 -05:00
|
|
|
}
|
2015-09-28 19:52:50 -05:00
|
|
|
|
|
|
|
else if( input[1] == "<-")
|
|
|
|
{
|
|
|
|
cout<<"Passing query to dbEngine."<<endl;
|
|
|
|
query(input, engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout<<"Not a valid command or query."<<endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-09-23 02:47:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 00:58:19 -05:00
|
|
|
void parse(string input, DBEngine &engine)
|
|
|
|
{
|
|
|
|
engine.storeCommands(input);
|
|
|
|
vector<string> listOfTokens = tokenize(input);
|
2015-09-27 23:24:02 -05:00
|
|
|
par_line(listOfTokens, engine);
|
2015-09-27 00:58:19 -05:00
|
|
|
}
|
|
|
|
/*
|
2015-09-23 02:47:08 -05:00
|
|
|
int main () {
|
|
|
|
|
|
|
|
|
|
|
|
string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;";
|
|
|
|
string ss2 = "SHOW Dogs ;";
|
|
|
|
string ss3 = "EXIT ; ";
|
|
|
|
|
|
|
|
vector<string> listOfTokens = tokenize(ss);
|
|
|
|
vector<string> listOfTokens2 = tokenize(ss2);
|
|
|
|
vector<string> listOfTokens3 = tokenize(ss3);
|
|
|
|
|
|
|
|
par_line(listOfTokens);
|
|
|
|
par_line(listOfTokens2);
|
|
|
|
par_line(listOfTokens3);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2015-09-27 00:58:19 -05:00
|
|
|
*/
|