This commit is contained in:
Rebecca Schofield 2015-09-30 16:50:25 -05:00
commit effe7d3d27
11 changed files with 414 additions and 931 deletions

View file

@ -52,6 +52,11 @@ void Attribute::setName(string s){
name = s;
}
void Attribute::setValue(string s, int index)
{
values[index] = s;
}
string Attribute::getType(){
return type;
}

View file

@ -22,6 +22,7 @@ public:
vector<string> getValues();
string getName();
void setName(string s);
void setValue(string s, int index);
string getType();
bool isKey();
int getSize();

View file

@ -37,4 +37,4 @@ int main()
}
}
*/
*/

126
DBAppV2.cpp Normal file
View file

@ -0,0 +1,126 @@
#include <iostream>
using namespace std;
/*
void yourAccount()
{
cout<<"name: yourName"<<endl;
cout<<"other stuff related to this user\n\n"<<endl;
//somehow display user information
// int choice;
// cin>>choice;
cout<<"0. Return to main menu"<<endl;
cout<<"\n\nEnter choice:";
}
void goToBoard()
{
cout<<"Board x"<<endl;
// int choice;
// cin>>choice;
cout<<"0. Return to main menu"<<endl;
cout<<"\n\nEnter choice:";
}
void boardList()
{
cout<<"Showing all boards:"<<endl;
// int choice;
// cin>>choice;
cout<<"0. Return to main menu"<<endl;
cout<<"\n\nEnter choice:";
}
void BoardMenu()
{
int choice;
cin>>choice;
cout<<"Boards will be displayed soon"<<endl;
//do some stuff to display boards
cout<<"1. List of boards"<<endl;
cout<<"2. Go to board (enter shortcut[first few characters])"<<endl;
cout<<"0. Return to main menu"<<endl;
cout<<"\n\nEnter choice:";
switch(choice)
{
case 1: boardList();
case 2: goToBoard();
}
}
void showMessages()
{
cout<<"Messages will be displayed soon"<<endl;
//do some stuff to display messages
// int choice;
// cin>>choice;
cout<<"0. Return to main menu"<<endl;
cout<<"\n\nEnter choice:";
}
void showGroups()
{
cout<<"Groups will be displayed soon"<<endl;
//do some stuff to display groups
// int choice;
// cin>>choice;
cout<<"0. Return to main menu"<<endl;
cout<<"\n\nEnter choice:";
}
void mainMenu()
{
cout<<"***************"<<endl;
cout<<"** Main Menu **"<<endl;
cout<<"***************\n\n"<<endl;
cout<<"1. Your account"<<endl;
cout<<"2. Boards"<<endl;
cout<<"3. Messages"<<endl;
cout<<"4. Groups\n"<<endl;
cout<<"Enter choice: ";
int choice;
cin>>choice;
switch (choice)
{
case 1: yourAccount();
case 2: BoardMenu();
case 3: showMessages();
case 4: showGroups();
}
}
int main()
{
cout<<"***************************************"<<endl;
cout<<"Welcome to our BBS "<<endl;
cout<<"***************************************"<<endl;
cout<<"** Enter BBS **"<<endl;
cout<<"1. Login"<<endl;
cout<<"2. Request new account\n\n";
cout<<"Enter choice: ";
int choice;
cin>>choice;
if (choice == 1)
{
cout<<"ramera"<<endl;
mainMenu();
}
else {cout<<"oops";}
}
*/

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]);
}
}
*/
}
@ -230,10 +218,11 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
int size = 0;
<<<<<<< HEAD
for(int x = 0; x < r2.getAttributes().size(); ++x)
{
for (int i = 0; i < r2.getAttributes()[x].getSize(); ++i)
{
{
temp = r2.getTuple(i);
for(int y = 0; y < new_r.getAttributes().size(); ++y)
@ -256,8 +245,7 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
}
}
}
}
}
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

@ -19,17 +19,6 @@ vector<string> tokenize(string ss)
{
output.push_back(tempString);
}
//testing---------------
//cout<<"TokenList: ";
// for (int i = 0; i <output.size(); ++i)
// {
// cout<<output[i]<<" ";
// }
// cout << endl;
//----------------------
return output;
}
@ -42,328 +31,284 @@ void displayTokenList(vector<string> input)
}
}
Relation condition(vector<string> input, Relation &r)
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;
if(input[0].at(0) == '\"')
int offset = 0;
//input.erase(input.begin());
if(op == "==")
{
c = input[0].substr(1, input[0].find_last_of("\"") - 1);
//cout << c << 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));
}
}*/
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);
//rfinal.insertAttributes(a);
}
}
}
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);
//rfinal.insertAttributes(a);
}
}
}
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);
//rfinal.insertAttributes(a);
}
}
}
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);
//rfinal.insertAttributes(a);
}
}
}
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);
//rfinal.insertAttributes(a);
}
}
}
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);
//rfinal.insertAttributes(a);
}
}
}
else
{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
rfinal.removeTuple(j - offset);
offset += 1;
}
}
}
}
}
else
else if(op == "!=")
{
input.erase(input.begin());
if(op == "==")
for(int i = 0; i < r.getAttributes().size(); ++i)
{
for(int i = 0; i < r.getAttributes().size(); ++i)
if(r.getAttributes()[i].getName() == a1.getName())
{
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] == c)
{
if(r.getAttributeByName(a1.getName()).getValues()[j] != c)
{
rfinal.removeTuple(j);
}
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);
}
}
}
}
}
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);
}
}
}
}
}
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);
}
}
}
}
}
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);
}
}
}
}
}
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);
}
}
}
}
}
else
{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
}
return rfinal;
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)
@ -372,10 +317,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");
@ -390,13 +331,13 @@ tuple<vector<string>, Relation> expression(vector<string> input, DBEngine &engin
input.erase(input.begin());
if(engine.isRelation(input[0]))
{
r = condition(s, engine.getTableFromName(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));
r = condition(s, get<1>(t), engine);
input.erase(input.begin());
}
}
@ -425,16 +366,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;
}
}
@ -642,12 +580,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]))
{
@ -670,41 +602,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;
@ -716,10 +614,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());
@ -732,10 +626,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);
@ -760,7 +650,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")
{
@ -775,8 +664,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
@ -807,8 +694,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());
@ -819,17 +704,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())
@ -917,7 +797,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
@ -996,14 +875,18 @@ vector<string> insertCMD(vector<string> input, DBEngine &engine)
vector<string> updateCMD(vector<string> input, DBEngine &engine)
{
PRelation r(input[0]);
Relation r = engine.getTableFromName(input[0]);
Relation rcond("TEMP");
Relation rfinal("TEMP");
input.erase(input.begin());
if(input[0] == "SET")
{
input.erase(input.begin());
vector <Attribute> a = r.getAttributes();
//parse out ( and send everything until ) into an Expression vector
if(input[0] == "(")
{
@ -1020,8 +903,6 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
while(input[0] != ")")
{
temp.setPAttributeName(input[0]);
a.push_back(temp);
temp.setPAttributeName("~");
input.erase(input.begin());
@ -1030,6 +911,15 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
input.erase(input.begin());
s.push_back(input[0]);
if(input[0].at(0) == '\"')
{
s.push_back(input[0].substr(1, input[0].find_last_of("\"")));
}
else
{
s.push_back(input[0]);
}
}
else
@ -1041,34 +931,27 @@ vector<string> updateCMD(vector<string> input, DBEngine &engine)
{
input.erase(input.begin());
}
a.push_back(temp);
temp.setPAttributeName("~");
}
}
if(input[0] == "WHERE")
{
vector<string> s;
if(input[0] == "(")
{
//PCondition c;
PComparison c;
POperand oops1;
POperand oops2;
POp op;
input.erase(input.begin());
while(input[0] != ")")
{
oops1.setPOperand(input[0]);
s.push_back(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());
}
rcond = condition(s, r, engine);
}
}
// send update command to DBEngine
@ -1128,7 +1011,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);
@ -1157,8 +1039,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);
}
@ -1168,8 +1048,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);
}
@ -1179,8 +1057,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);
}
@ -1190,8 +1066,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);
}
@ -1250,22 +1124,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);

View file

@ -92,8 +92,6 @@ void Relation::display(){
}
void Relation::insertTuple(vector<string> tuple){
cout << name << " " << size << endl;
cout << tuple.size() << endl;
if (tuple.size() != this->size) {
cout << "Failure to insert: the sizes do not match.";
}

BIN
test

Binary file not shown.

View file

@ -7,7 +7,7 @@
using namespace std;
int main () {
DBEngine engine;
DBEngine engine;
Attribute att1("Breakfast", "VARCHAR(20)", true);
Attribute att2("Lunch", "VARCHAR(20)", false);
@ -32,7 +32,7 @@ int main () {
//engine.update
/*actual testing
/*actual testing
string x;
cout << "Enter DBMS Commands: ";