Took out unncessary comments in code.

This commit is contained in:
Alexander Huddleston 2015-09-30 13:00:51 -05:00
parent 0504ce7fea
commit 7f0f5c4a97
6 changed files with 223 additions and 992 deletions

View file

@ -94,7 +94,7 @@ void DBEngine::save(string n){
}
}
//file.close();
file.close();
}
vector<Relation> DBEngine::getRelations(){
@ -167,18 +167,6 @@ Relation DBEngine::rename(vector<string> newnames, Relation &r)
}
return r;
}
/*
else if (oldnames != r.getAttributeNames()) {
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
return;
}
else {
for(int i = 0; i < oldnames.size(); ++i){
r.renameAttribute(oldnames[i], newnames[i]);
}
}
*/
}
@ -229,83 +217,38 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
vector<string> temp;
//bool duplicate = false;
/*
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i)
{
new_r.removeTuple(0);
}
*/
int size = 0;
//cout << "test1" << endl;
for(int x = 0; x < r2.getAttributes().size(); ++x)
{
//cout << "test2" << endl;
for (int i = 0; i < r2.getAttributes()[x].getSize(); ++i)
{
//cout << "test3" << endl;
temp = r2.getTuple(i);
for(int y = 0; y < new_r.getAttributes().size(); ++y)
{
size = new_r.getAttributes()[y].getSize();
//cout << "test4" << endl;
new_r.getAttributes()[y].getSize();
for (int j = 0; j < size; ++j)
{
//cout << "test5" << endl;
for(int a = 0; a < temp.size(); ++a)
{
//cout << "test6" << endl;
for(int b = 0; b < new_r.getTuple(j).size(); ++b)
{
//cout << "test7" << endl;
//cout << temp[a] << " " << new_r.getTuple(j)[b] << " " << b << endl;
if (temp[a] == new_r.getTuple(j)[b])
{
new_r.removeFromTuple(b, j);
//cout << "test" << endl;
//duplicate = true;
//break;
}
}
}
size = new_r.getAttributes()[y].getSize();
}
/*
if (!duplicate)
{
new_r.insertTuple(temp);
}
*/
//duplicate = false;
}
}
}
//make this one for loop later!
/*
for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
temp = r2.getTuple(i);
for (int j = 0; j < r1.getAttributes()[0].getSize(); ++j){
if (temp == r1.getTuple(j)){
duplicate = true;
break;
}
}
if (!duplicate){
new_r.insertTuple(temp);
}
duplicate = false;
}
*/
return new_r;
}
}

View file

@ -18,7 +18,6 @@ public:
void insertValues(string r, vector <string> v);
vector<Relation> getRelations();
bool isRelation(string n);
//void showTable(Relation r);
Relation& getTableFromName(string n);
void saveToFile(vector<string> cmds);
Relation selection(string attName, string s, Relation r);

View file

@ -45,197 +45,167 @@ void displayTokenList(vector<string> input)
Relation condition(vector<string> input, Relation &r, DBEngine &engine)
{
Relation rfinal = r;
//UNTESTED
//testing to see if there is an && or || present
/*
if (input.size() > 3) {
//test print
for (int i = 0; i < input.size(); ++i)
cout << "input[i]: " << input[i] << "\n";
//this is probably wrong, added a cout for testing
string bin_op = input[3];
cout << "bin_op, should be && or ||: " << bin_op << "\n";
//making each half a new vector, probably a better way to do this
vector<string> firsthalf;
vector<string> secondhalf;
//again, all untested. it's weird to not test this stuff
for (int i = 0; i < input.size(); ++i){
if (i < 3)
firsthalf.push_back(input[i]);
else
secondhalf.push_back(input[i]);
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);
}
}
}
}
//calling condition again with each half
Relation r1 = condition(firsthalf, rfinal);
Relation r2 = condition(secondhalf, rfinal);
//NO IDEA IF THIS ACTUALLY WORKS
//HOORAY
//HOW THE FUCK DO PEOPLE CODE LIKE THIS
if (bin_op == "&&"){
//with engine as the DBEngine object that maybe exists
return engine.crossProduct(r1, r2);
}
else if (bin_op == "||"){
//with engine as the DBEngine object that maybe exists
return engine.setUnion(r1, r2);
}
else {
cout << "Something strange happened. bin_op: " << bin_op << "\n";
cout << "Returning the initial relation.";
return rfinal;
}
}
else {
*/
Attribute a1 = r.getAttributeByName(input[0]);
else if(input[0].at(0) == '\"')
{
c = input[0].substr(1, input[0].find_last_of("\"") - 1);
input.erase(input.begin());
string op = input[0];
input.erase(input.begin());
Attribute a2;
string c;
//cout << input[0] << endl;
for(int x = 0; x < input.size(); ++x)
int offset = 0;
//input.erase(input.begin());
if(op == "==")
{
cout << input[x] << endl;
}
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())
{
/*
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size; ++j)
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
{
if(r.getAttributeByName(a1.getName()).getValues()[j] == )
if(r.getAttributeByName(a1.getName()).getValues()[j] != c)
{
rfinal.insertTuple(r.getTuple(j));
}
}*/
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);
rfinal.removeTuple(j - offset);
offset += 1;
}
}
}
}
}
else if(input[0].at(0) == '\"')
else if(op == "!=")
{
c = input[0].substr(1, input[0].find_last_of("\"") - 1);
input.erase(input.begin());
int offset = 0;
//input.erase(input.begin());
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)
{
@ -243,7 +213,7 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
{
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
{
if(r.getAttributeByName(a1.getName()).getValues()[j] != c)
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) < stoi(c))
{
rfinal.removeTuple(j - offset);
offset += 1;
@ -252,7 +222,15 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
}
}
}
else if(op == "!=")
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)
{
@ -260,7 +238,7 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
{
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
{
if(r.getAttributeByName(a1.getName()).getValues()[j] == c)
if(stoi(r.getAttributeByName(a1.getName()).getValues()[j]) > stoi(c))
{
rfinal.removeTuple(j - offset);
offset += 1;
@ -269,124 +247,79 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
}
}
}
else if(op == ">=")
else
{
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);
}
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
if(input[0] == "&&")
else if(op == ">")
{
input.erase(input.begin());
Relation rtemp = rfinal;
rfinal = condition(input, rtemp, engine);
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(input[0] == "||")
else if(op == "<")
{
input.erase(input.begin());
Relation rtemp = rfinal;
Relation rtemp2 = condition(input, r, engine);
rfinal = engine.setUnion(rtemp, rtemp2);
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);
}
}
return rfinal;
//}
}
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)
@ -395,10 +328,6 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
tuple<vector<string>, Relation> t(input, rfinal);
if(input[0] == "select")
{
/*
cout << "Not yet implemented." << endl;
exit(1);
*/
input.erase(input.begin());
vector<string> s;
Relation r("TEMP");
@ -448,16 +377,13 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
input.erase(input.begin());
get<0>(t) = input;
get<1>(t) = rfinal;
//cout << rfinal.getSize() << endl;
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;
//cout << rfinal.getSize() << endl;
return t;
}
}
@ -665,12 +591,6 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
vector<string> showCMD(vector<string> input, DBEngine &engine)
{
/*
if (input.size() > 3)
{
cout<<"Syntax error!"<<endl;
}
*/
if(engine.isRelation(input[0]))
{
@ -693,41 +613,7 @@ vector<string> saveCMD(vector<string> input, DBEngine &engine)
{
cout<<"Syntax error!"<<endl;
}
//PRelation pr(input[0]);
// send save command to DBEngine
engine.save(input[0]);
/*
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--------------------------";
*/
input.erase(input.begin());
return input;
@ -739,10 +625,6 @@ vector<string> closeCMD(vector<string> input, DBEngine &engine)
{
cout<<"Syntax error!"<<endl;
}
//PRelation r(input[0]);
// send close command to DBEngine
engine.deleteRelation(input[0]);
input.erase(input.begin());
@ -755,10 +637,6 @@ vector<string> openCMD(vector<string> input, DBEngine &engine)
{
cout<<"Syntax error!"<<endl;
}
//PRelation r(input[0]);
// send open command to DBEngine
string name = input[0] + ".txt";
ifstream file;
file.open(name);
@ -783,7 +661,6 @@ vector<string> exitCMD(vector<string> input, DBEngine &engine)
vector<string> createCMD(vector<string> input, DBEngine &engine)
{
//relation name will be the first element of the vector of data returned by this function
if (input[0] == "TABLE")
{
@ -798,8 +675,6 @@ vector<string> createCMD(vector<string> input, DBEngine &engine)
{
input.erase(input.begin());
//vector <PExpression> e1;
vector <PAttribute> a;
while(input[0] != ")") //inserting all values to relation
@ -830,8 +705,6 @@ vector<string> createCMD(vector<string> input, DBEngine &engine)
a.push_back(temp);
}
//vector <PAttribute> apk; //save primary keys temp storage
if(input[0] == "PRIMARY" && input[1] == "KEY")
{
input.erase(input.begin());
@ -842,17 +715,12 @@ vector<string> createCMD(vector<string> input, DBEngine &engine)
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);
for(int i = 0; i < a.size(); ++i)
{
if(input[0] == a[i].getPAttribute())
@ -940,7 +808,6 @@ vector<string> insertCMD(vector<string> input, DBEngine &engine)
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
@ -1087,27 +954,6 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
vector<string> s;
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());
}
*/
input.erase(input.begin());
while(input[0] != ")")
{
@ -1176,7 +1022,6 @@ vector<string> query(vector<string> input, DBEngine &engine)
tuple<vector<string>, Relation> t = expression(input, engine);
Relation r = get<1>(t);
r.setTableName(pr.getName());
//r.display();
input = get<0>(t);
engine.createTable(r);
@ -1205,8 +1050,6 @@ void par_line(vector<string> input, DBEngine &engine) //calls par_command() or p
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
vector<string> insertInput = insertCMD(input, engine);
//cout<<"arguments: "<<endl;
//displayTokenList(insertInput);
}
@ -1216,8 +1059,6 @@ void par_line(vector<string> input, DBEngine &engine) //calls par_command() or p
input.erase(input.begin());
vector<string> insertInput = createCMD(input, engine);
//cout<<"arguments: "<<endl;
//displayTokenList(insertInput);
}
@ -1227,8 +1068,6 @@ void par_line(vector<string> input, DBEngine &engine) //calls par_command() or p
input.erase(input.begin());
vector<string> insertInput = deleteCMD(input, engine);
//cout<<"arguments: "<<endl;
//displayTokenList(insertInput);
}
@ -1238,8 +1077,6 @@ void par_line(vector<string> input, DBEngine &engine) //calls par_command() or p
input.erase(input.begin());
vector<string> insertInput = updateCMD(input, engine);
//cout<<"arguments: "<<endl;
//displayTokenList(insertInput);
}
@ -1298,22 +1135,3 @@ void parse(string input, DBEngine &engine)
vector<string> listOfTokens = tokenize(input);
par_line(listOfTokens, engine);
}
/*
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);
}
*/

489
Parser.h
View file

@ -416,493 +416,4 @@ class PExpression
}
};
/*
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
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);
}
*/
void parse(string s, DBEngine &e);

BIN
test

Binary file not shown.

View file

@ -8,46 +8,6 @@ using namespace std;
int main () {
DBEngine engine;
Attribute att1("Breakfast", "VARCHAR(20)", true);
Attribute att2("Lunch", "VARCHAR(20)", false);
Attribute att3("Dinner", "VARCHAR(20)", false);
att1.addCell("Pancakes");
att1.addCell("Waffles");
att1.addCell("Biscuits");
att2.addCell("Turkey Sandwich");
att2.addCell("Caesar Salad");
att2.addCell("Pizza");
att3.addCell("Steak");
att3.addCell("Shrimp");
att3.addCell("Ribs");
vector<Attribute> v;
v.push_back(att1);
v.push_back(att2);
v.push_back(att3);
engine.createTable("Food", v);
Attribute att4("Dessert", "VARCHAR(20)", true);
Attribute att5("MidnightSnack", "VARCHAR(20)", false);
att4.addCell("IceCreamSundae");
att4.addCell("ChocolateBar");
att5.addCell("HummusandCarrots");
att5.addCell("PotatoChips");
vector<Attribute> v2;
v2.push_back(att4);
v2.push_back(att5);
engine.createTable("MoarFood", v2);
//engine.getTableFromName("Food").display();
//engine.getTableFromName("MoarFood").display();
engine.crossProduct(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
string x;
cout << "Enter DBMS Commands: ";
while(getline(cin, x))