1071 lines
24 KiB
C++
Executable file
1071 lines
24 KiB
C++
Executable file
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <tuple>
|
|
#include <vector>
|
|
#include "Parser.h"
|
|
#include "PHelpers.h"
|
|
|
|
using namespace std;
|
|
|
|
vector<string> tokenize(string ss){
|
|
string tempString;
|
|
stringstream lineStream(ss);
|
|
vector<string> output;
|
|
|
|
while (lineStream >> tempString)
|
|
{
|
|
output.push_back(tempString);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
void displayTokenList(vector<string> input){
|
|
cout<<"TokenList: "<<endl;
|
|
for (int i = 0; i < input.size(); ++i)
|
|
{
|
|
cout<<input[i]<<endl;
|
|
}
|
|
}
|
|
|
|
Relation condition(vector<string> input, Relation &r, DBEngine &engine){
|
|
Relation rfinal = r;
|
|
|
|
Attribute a1 = r.getAttributeByName(input[0]);
|
|
input.erase(input.begin());
|
|
string op = input[0];
|
|
input.erase(input.begin());
|
|
Attribute a2;
|
|
string c;
|
|
if(r.isAttribute(input[0])){
|
|
a2 = r.getAttributeByName(input[0]);
|
|
input.erase(input.begin());
|
|
|
|
for(int i = 0; i < r.getAttributes().size(); ++i){
|
|
if(r.getAttributes()[i].getName() == a1.getName()){
|
|
int offset = 0;
|
|
|
|
if(op == "=="){
|
|
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
|
|
if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x])
|
|
{
|
|
rfinal.removeTuple(x - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
else if(op == "!="){
|
|
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
|
|
if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]){
|
|
rfinal.removeTuple(x - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
else if(op == ">="){
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
|
|
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
|
|
rfinal.removeTuple(x - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
else{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
else if(op == "<="){
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
|
|
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
|
|
rfinal.removeTuple(x - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
else{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
else if(op == ">"){
|
|
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
|
|
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
|
|
rfinal.removeTuple(x - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
else{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
else if(op == "<"){
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
|
|
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
|
|
rfinal.removeTuple(x - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
else{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
else if(input[0].at(0) == '\"'){
|
|
c = input[0].substr(1, input[0].find_last_of("\"") - 1);
|
|
input.erase(input.begin());
|
|
int offset = 0;
|
|
if(op == "=="){
|
|
for(int i = 0; i < r.getAttributes().size(); ++i){
|
|
if(r.getAttributes()[i].getName() == a1.getName()){
|
|
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j){
|
|
if(r.getAttributeByName(a1.getName()).getValues()[j] != c){
|
|
rfinal.removeTuple(j - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
else if(op == "!=")
|
|
{
|
|
for(int i = 0; i < r.getAttributes().size(); ++i)
|
|
{
|
|
if(r.getAttributes()[i].getName() == a1.getName())
|
|
{
|
|
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
|
|
{
|
|
if(r.getAttributeByName(a1.getName()).getValues()[j] == c)
|
|
{
|
|
rfinal.removeTuple(j - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
else if(op == ">=")
|
|
{
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER")
|
|
{
|
|
for(int i = 0; i < r.getAttributes().size(); ++i)
|
|
{
|
|
if(r.getAttributes()[i].getName() == a1.getName())
|
|
{
|
|
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
|
|
{
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c))
|
|
{
|
|
rfinal.removeTuple(j - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
else if(op == "<=")
|
|
{
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER")
|
|
{
|
|
for(int i = 0; i < r.getAttributes().size(); ++i)
|
|
{
|
|
if(r.getAttributes()[i].getName() == a1.getName())
|
|
{
|
|
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
|
|
{
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c))
|
|
{
|
|
rfinal.removeTuple(j - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
else if(op == ">")
|
|
{
|
|
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER")
|
|
{
|
|
for(int i = 0; i < r.getAttributes().size(); ++i)
|
|
{
|
|
if(r.getAttributes()[i].getName() == a1.getName())
|
|
{
|
|
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
|
|
{
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) <= stoi(c))
|
|
{
|
|
rfinal.removeTuple(j - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
else if(op == "<")
|
|
{
|
|
|
|
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER")
|
|
{
|
|
for(int i = 0; i < r.getAttributes().size(); ++i)
|
|
{
|
|
if(r.getAttributes()[i].getName() == a1.getName())
|
|
{
|
|
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
|
|
{
|
|
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) >= stoi(c))
|
|
{
|
|
rfinal.removeTuple(j - offset);
|
|
offset += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cout << "Attribute type is not an INTEGER." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
}
|
|
if(input[0] == "&&")
|
|
{
|
|
input.erase(input.begin());
|
|
Relation rtemp = rfinal;
|
|
rfinal = condition(input, rtemp, engine);
|
|
}
|
|
else if(input[0] == "||")
|
|
{
|
|
input.erase(input.begin());
|
|
Relation rtemp = rfinal;
|
|
Relation rtemp2 = condition(input, r, engine);
|
|
rfinal = engine.setUnion(rtemp, rtemp2);
|
|
}
|
|
|
|
return rfinal;
|
|
}
|
|
|
|
tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engine){
|
|
Relation rfinal("TEMP");
|
|
tuple<vector<string>, Relation> t(input, rfinal);
|
|
if(input[0] == "select")
|
|
{
|
|
input.erase(input.begin());
|
|
vector<string> s;
|
|
Relation r("TEMP");
|
|
if(input[0] == "(")
|
|
{
|
|
input.erase(input.begin());
|
|
while(input[0] != ")")
|
|
{
|
|
s.push_back(input[0]);
|
|
input.erase(input.begin());
|
|
}
|
|
input.erase(input.begin());
|
|
if(engine.isRelation(input[0]))
|
|
{
|
|
r = condition(s, engine.getTableFromName(input[0]), engine);
|
|
input.erase(input.begin());
|
|
}
|
|
else if(input[0] == "(")
|
|
{
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
r = condition(s, get<1>(t), engine);
|
|
input.erase(input.begin());
|
|
}
|
|
}
|
|
get<0>(t) = input;
|
|
get<1>(t) = r;
|
|
return t;
|
|
}
|
|
|
|
else if(input[0] == "project")
|
|
{
|
|
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<1>(t) = rfinal;
|
|
return t;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cout << "Projection syntax incorrect." << endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
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]))
|
|
{
|
|
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;
|
|
}
|
|
|
|
vector<string> showCMD(vector<string> input, DBEngine &engine){
|
|
|
|
if(engine.isRelation(input[0]) && (input[1] != "+" && input[1] != "-" && input[1] != "*"))
|
|
{
|
|
engine.getTableFromName(input[0]).display();
|
|
input.erase(input.begin());
|
|
}
|
|
|
|
else
|
|
{
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
get<1>(t).display();
|
|
}
|
|
|
|
return input;
|
|
}
|
|
|
|
vector<string> saveCMD(vector<string> input, DBEngine &engine){
|
|
if (input.size() > 2)
|
|
{
|
|
cout<<"Syntax error!"<<endl;
|
|
}
|
|
engine.save(input[0]);
|
|
input.erase(input.begin());
|
|
|
|
return input;
|
|
}
|
|
|
|
vector<string> closeCMD(vector<string> input, DBEngine &engine){
|
|
if (input.size() > 3)
|
|
{
|
|
cout<<"Syntax error!"<<endl;
|
|
}
|
|
engine.deleteRelation(input[0]);
|
|
input.erase(input.begin());
|
|
|
|
return input;
|
|
}
|
|
|
|
vector<string> openCMD(vector<string> input, DBEngine &engine){
|
|
if (input.size() > 2)
|
|
{
|
|
cout<<"Syntax error!"<<endl;
|
|
}
|
|
string name = input[0] + ".txt";
|
|
ifstream file;
|
|
file.open(name);
|
|
string line;
|
|
while(getline(file, line))
|
|
{
|
|
parse(line, engine);
|
|
}
|
|
file.close();
|
|
|
|
return input;
|
|
}
|
|
|
|
vector<string> exitCMD(vector<string> input, DBEngine &engine){
|
|
exit(0);
|
|
return input;
|
|
}
|
|
|
|
vector<string> createCMD(vector<string> input, DBEngine &engine){
|
|
if (input[0] == "TABLE")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
PRelation r;
|
|
r.setPRelation(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(input[0] == "(")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
vector <PAttribute> a;
|
|
|
|
while(input[0] != ")")
|
|
{
|
|
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("(") + 1, input[0].find(")"))));
|
|
input.erase(input.begin());
|
|
}
|
|
|
|
a.push_back(temp);
|
|
}
|
|
|
|
if(input[0] == "PRIMARY" && input[1] == "KEY")
|
|
{
|
|
input.erase(input.begin());
|
|
input.erase(input.begin());
|
|
|
|
if(input[0] == "(")
|
|
{
|
|
|
|
while(input[0] != ")")
|
|
{
|
|
|
|
if (input[0] == ",")
|
|
{
|
|
input.erase(input.begin());
|
|
}
|
|
|
|
for(int i = 0; i < a.size(); ++i)
|
|
{
|
|
if(input[0] == a[i].getPAttribute())
|
|
{
|
|
a[i].setPAttributeKey();
|
|
}
|
|
}
|
|
|
|
input.erase(input.begin());
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
else cout<<"Syntax error! 2"<<endl;
|
|
}
|
|
else cout<<"Syntax error! 1"<<endl;
|
|
}
|
|
|
|
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")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
PRelation pr(input[0]);
|
|
Relation r = engine.getTableFromName(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
vector <string> s;
|
|
|
|
if (input[0] == "VALUES" && input[1] == "FROM")
|
|
{
|
|
input.erase(input.begin());
|
|
input.erase(input.begin());
|
|
|
|
|
|
vector <Attribute> a = r.getAttributes();
|
|
|
|
if(input[0] == "(")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
for(int i = 0; i < a.size(); ++i)
|
|
{
|
|
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());
|
|
}
|
|
|
|
else
|
|
{
|
|
if(input[0].at(0) == '\"')
|
|
{
|
|
s.push_back(input[0].substr(1,input[0].find_last_of("\"") - 1 ));
|
|
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);
|
|
}
|
|
}
|
|
|
|
if (input[0] == ",")
|
|
{
|
|
input.erase(input.begin());
|
|
}
|
|
}
|
|
if(input[0] != ")")
|
|
{
|
|
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);
|
|
}
|
|
engine.insertValues(r.getTableName(), s);
|
|
input.erase(input.begin());
|
|
return input;
|
|
}
|
|
|
|
else if (input[0] == "RELATION")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
vector<Attribute> a = get<1>(t).getAttributes();
|
|
for(int y = 0; y < a[0].getSize(); ++y)
|
|
{
|
|
for(int x = 0; x < a.size(); ++x)
|
|
{
|
|
s.push_back(a[x].getValues()[y]);
|
|
engine.insertValues(r.getTableName(), s);
|
|
s.clear();
|
|
}
|
|
}
|
|
|
|
input = get<0>(t);
|
|
|
|
return input;
|
|
}
|
|
|
|
else cout<<"Syntax error! 3"<<endl;
|
|
}
|
|
|
|
cout<<"Syntax error! 2"<<endl;
|
|
|
|
}
|
|
|
|
else cout<<"Syntax error! 1"<<endl;
|
|
|
|
}
|
|
|
|
vector<string> updateCMD(vector<string> input, DBEngine &engine){
|
|
Relation r = engine.getTableFromName(input[0]);
|
|
Relation temp("TEMP");
|
|
vector <string> a;
|
|
vector <string> c;
|
|
vector <string> s;
|
|
|
|
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] == "(")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
while(input[0] != ")")
|
|
{
|
|
a.push_back(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
|
|
if(input[0] == "=")
|
|
{
|
|
input.erase(input.begin());
|
|
|
|
if(input[0].at(0) == '\"')
|
|
{
|
|
c.push_back(input[0].substr(1, input[0].find_last_of("\"") - 1));
|
|
}
|
|
else
|
|
{
|
|
c.push_back(input[0]);
|
|
}
|
|
|
|
input.erase(input.begin());
|
|
}
|
|
|
|
else
|
|
{
|
|
cout<<"Syntax error! 2"<<endl;
|
|
}
|
|
|
|
if(input[0] == ",")
|
|
{
|
|
input.erase(input.begin());
|
|
}
|
|
}
|
|
input.erase(input.begin());
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
r.display();
|
|
|
|
for (int i = 0; i < s.size(); ++i){
|
|
cout << "s[i]: " << s[i] << "\n";
|
|
}
|
|
|
|
temp = condition(s, r, engine);
|
|
}
|
|
}
|
|
|
|
temp.display();
|
|
engine.updateFromRelationCmd(r, temp, a, c);
|
|
}
|
|
|
|
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;
|
|
|
|
return input;
|
|
}
|
|
|
|
vector<string> query(vector<string> input, DBEngine &engine)
|
|
{
|
|
PRelation pr(input[0]);
|
|
|
|
input.erase(input.begin());
|
|
input.erase(input.begin());
|
|
|
|
tuple<vector<string>, Relation> t = expression(input, engine);
|
|
Relation r = get<1>(t);
|
|
r.setTableName(pr.getName());
|
|
input = get<0>(t);
|
|
|
|
engine.createTable(r);
|
|
|
|
return input;
|
|
}
|
|
|
|
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.
|
|
• 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
|
|
{
|
|
if ( input[0] == "INSERT")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
vector<string> insertInput = insertCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "CREATE")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
|
|
vector<string> insertInput = createCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "DELETE")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
|
|
vector<string> insertInput = deleteCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "UPDATE")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
|
|
vector<string> insertInput = updateCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "SHOW")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
showCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "EXIT")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
exitCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "OPEN")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
openCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "SAVE")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
saveCMD(input, engine);
|
|
}
|
|
|
|
else if ( input[0] == "CLOSE")
|
|
{
|
|
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
|
input.erase(input.begin());
|
|
closeCMD(input, engine);
|
|
}
|
|
|
|
else if( input[1] == "<-")
|
|
{
|
|
cout<<"Passing query to dbEngine."<<endl;
|
|
query(input, engine);
|
|
}
|
|
|
|
else
|
|
{
|
|
cout<<"Not a valid command or query."<<endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void parse(string input, DBEngine &engine)
|
|
{
|
|
engine.storeCommands(input);
|
|
vector<string> listOfTokens = tokenize(input);
|
|
par_line(listOfTokens, engine);
|
|
}
|