Added functionality of EXIT, SHOW, SAVE, INSERT, and CREATE commands through parser. Added some functions to help with that.

This commit is contained in:
Alexander Huddleston 2015-09-27 23:24:02 -05:00
parent e244c6ca45
commit 851821750c
9 changed files with 249 additions and 94 deletions

View file

@ -16,6 +16,13 @@ Attribute::Attribute(string n, string t, bool k){
size = 0; size = 0;
} }
Attribute::Attribute(string n, string t, bool k, int s){
name = n;
type = t;
key = k;
size = s;
}
void Attribute::addCell(string v){ void Attribute::addCell(string v){
values.push_back(v); values.push_back(v);
size++; size++;
@ -56,7 +63,12 @@ int Attribute::getSize(){
void Attribute::display(){ void Attribute::display(){
cout << "-------------\n"; cout << "-------------\n";
cout << name << "\n" << type << "\n\n"; cout << name << "\n" << type;
if(type == "VARCHAR")
{
cout << "(" << size << ")";
}
cout << "\n\n";
vector<string>::iterator it = values.begin(); vector<string>::iterator it = values.begin();
while (it != values.end()){ while (it != values.end()){

View file

@ -14,6 +14,7 @@ class Attribute{
public: public:
Attribute(); Attribute();
Attribute(string n, string t, bool k); Attribute(string n, string t, bool k);
Attribute(string n, string t, bool k, int s);
void addCell(string v); void addCell(string v);
void removeCell(int index); void removeCell(int index);
string operator[](int i); string operator[](int i);

View file

@ -25,6 +25,17 @@ void DBEngine::createTable(Relation r){
size++; size++;
} }
void DBEngine::insertValues(string r, vector <string> v)
{
for(int i = 0; i < tables.size(); i++)
{
if (tables[i].getTableName() == r)
{
tables[i].insertTuple(v);
}
}
}
void DBEngine::storeCommands(string s){ void DBEngine::storeCommands(string s){
commands.push_back(s); commands.push_back(s);
} }
@ -51,23 +62,14 @@ Relation& DBEngine::getTableFromName(string n){
return tables[i]; return tables[i];
} }
} }
cout << "No Relation with this name." << endl;
exit(1);
} }
Relation DBEngine::selection(string attName, string s, Relation r){ Relation DBEngine::selection(string attName, string s, Relation r){
equality(attName, s, r); equality(attName, s, r);
} }
/*
Relation DBEngine::selection(string attName, string s, Relation r){
equality(attName, s, r);
}
Relation DBEngine::selection(string attName, string s, Relation r){
equality(attName, s, r);
}
*/
//assumes that all attribute titles are unique //assumes that all attribute titles are unique
Relation DBEngine::projection(vector<string> input, Relation r){ Relation DBEngine::projection(vector<string> input, Relation r){

View file

@ -15,6 +15,7 @@ public:
void createTable(string n); void createTable(string n);
void createTable(string n, vector<Attribute> a); void createTable(string n, vector<Attribute> a);
void createTable(Relation r); void createTable(Relation r);
void insertValues(string r, vector <string> v);
vector<Relation> getRelations(); vector<Relation> getRelations();
//void showTable(Relation r); //void showTable(Relation r);
Relation& getTableFromName(string n); Relation& getTableFromName(string n);

View file

@ -36,6 +36,7 @@ class PAttribute
{ {
string name; string name;
string type; string type;
bool key;
int size; int size;
public: public:
@ -43,6 +44,7 @@ class PAttribute
{ {
name = "~"; name = "~";
type = "~"; type = "~";
key = false;
size = 0; size = 0;
} }
@ -50,6 +52,7 @@ class PAttribute
{ {
name = str; name = str;
type = t; type = t;
key = false;
size = 0; size = 0;
} }
@ -57,6 +60,7 @@ class PAttribute
{ {
name = str; name = str;
type = t; type = t;
key = false;
size = s; size = s;
} }
@ -70,6 +74,11 @@ class PAttribute
type = t; type = t;
} }
void setPAttributeKey()
{
key = true;
}
void setPAttributeSize(int s) void setPAttributeSize(int s)
{ {
size = s; size = s;
@ -79,6 +88,21 @@ class PAttribute
{ {
return name; return name;
} }
string getPAttributeType()
{
return type;
}
bool getPAttributeKey()
{
return key;
}
int getPAttributeSize()
{
return size;
}
}; };
class PUnion class PUnion

View file

@ -1,5 +1,6 @@
#include <string> // std::string #include <string> // std::string
#include <iostream> // std::cout #include <iostream> // std::cout
#include <fstream> // std::ofstream
#include <sstream> // std::stringstream #include <sstream> // std::stringstream
#include <vector> #include <vector>
#include <string> #include <string>
@ -25,6 +26,7 @@ vector<string> tokenize(string ss)
{ {
cout<<output[i]<<" "; cout<<output[i]<<" ";
} }
cout << endl;
//---------------------- //----------------------
return output; return output;
@ -39,7 +41,7 @@ void displayTokenList(vector<string> input)
} }
} }
vector<string> showCMD(vector<string> input) vector<string> showCMD(vector<string> input, DBEngine &engine)
{ {
if (input.size() > 3) if (input.size() > 3)
{ {
@ -49,25 +51,58 @@ vector<string> showCMD(vector<string> input)
PRelation r(input[0]); PRelation r(input[0]);
// send show command to DBEngine // send show command to DBEngine
engine.getTableFromName(r.getName()).display();
return input; return input;
} }
vector<string> saveCMD(vector<string> input) vector<string> saveCMD(vector<string> input, DBEngine &engine)
{ {
if (input.size() > 3) if (input.size() > 2)
{ {
cout<<"Syntax error!"<<endl; cout<<"Syntax error!"<<endl;
} }
PRelation r(input[0]); //PRelation pr(input[0]);
// send save command to DBEngine // send save command to DBEngine
//engine.save();
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; return input;
} }
vector<string> closeCMD(vector<string> input) vector<string> closeCMD(vector<string> input, DBEngine &engine)
{ {
if (input.size() > 3) if (input.size() > 3)
{ {
@ -81,7 +116,7 @@ vector<string> closeCMD(vector<string> input)
return input; return input;
} }
vector<string> openCMD(vector<string> input) vector<string> openCMD(vector<string> input, DBEngine &engine)
{ {
if (input.size() > 2) if (input.size() > 2)
{ {
@ -95,28 +130,22 @@ vector<string> openCMD(vector<string> input)
return input; return input;
} }
vector<string> exitCMD(vector<string> input) vector<string> exitCMD(vector<string> input, DBEngine &engine)
{ {
if (input[1] != ";")
{
cout<<"ERROR: missing semicolon!"<<endl;
}
if (input[0] != "EXIT") { cout<<"Wrong function/syntax error!"<<endl;} exit(0);
cout<<"Passing command: "<<input[0]<<endl;
return input; return input;
} }
vector<string> createCMD(vector<string> input) vector<string> createCMD(vector<string> input, DBEngine &engine)
{ {
//relation name will be the first element of the vector of data returned by this function //relation name will be the first element of the vector of data returned by this function
if (input[0] == "CREATE" && input[1] == "TABLE") if (input[0] == "TABLE")
{ {
input.erase(input.begin()); input.erase(input.begin());
input.erase(input.begin());
PRelation r; PRelation r;
r.setPRelation(input[0]); r.setPRelation(input[0]);
@ -152,14 +181,14 @@ vector<string> createCMD(vector<string> input)
else else
{ {
temp.setPAttributeType(input[0].substr(0,input[0].find("("))); temp.setPAttributeType(input[0].substr(0,input[0].find("(")));
temp.setPAttributeSize(stoi(input[0].substr(input[0].find("("), input[0].find(")")))); temp.setPAttributeSize(stoi(input[0].substr(input[0].find("(") + 1, input[0].find(")"))));
input.erase(input.begin()); input.erase(input.begin());
} }
a.push_back(temp); a.push_back(temp);
} }
vector <PAttribute> apk; //save primary keys temp storage //vector <PAttribute> apk; //save primary keys temp storage
if(input[0] == "PRIMARY" && input[1] == "KEY") if(input[0] == "PRIMARY" && input[1] == "KEY")
{ {
@ -171,34 +200,50 @@ vector<string> createCMD(vector<string> input)
while(input[0] != ")") //inserting all values to relation while(input[0] != ")") //inserting all values to relation
{ {
PAttribute temp; //PAttribute temp;
if (input[0] == ",") if (input[0] == ",")
{ {
input.erase(input.begin()); input.erase(input.begin());
} }
temp.setPAttributeName(input[0]); //temp.setPAttributeName(input[0]);
apk.push_back(temp); //apk.push_back(temp);
for(int i = 0; i < a.size(); ++i)
{
if(input[0] == a[i].getPAttribute())
{
a[i].setPAttributeKey();
}
}
input.erase(input.begin()); 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; return input;
} }
}
}
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error! 2"<<endl;
} }
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error! 1"<<endl;
} }
vector<string> insertCMD(vector<string> input) vector<string> insertCMD(vector<string> input, DBEngine &engine)
{ {
//relation name will be the first element of the vector of data returned by this function //relation name will be the first element of the vector of data returned by this function
@ -206,7 +251,8 @@ vector<string> insertCMD(vector<string> input)
{ {
input.erase(input.begin()); input.erase(input.begin());
PRelation r(input[0]); PRelation pr(input[0]);
Relation r = engine.getTableFromName(input[0]);
input.erase(input.begin()); input.erase(input.begin());
@ -218,65 +264,115 @@ vector<string> insertCMD(vector<string> input)
input.erase(input.begin()); input.erase(input.begin());
vector <Attribute> a = r.getAttributes();
if(input[0] == "(") 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) == '\"') if(input[0].at(0) == '\"')
{ {
s.push_back(input[0].substr(1,input[0].find_last_of("\""))); cout << "Incorrect type matching. Tried to insert string." << endl;
} cout << "The values should be: ";
vector <PExpression> e; for(int x = 0; x < a.size(); ++i)
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()); cout << a[x].getType();
if(a[x].getType() == "VARCHAR")
e.push_back(input[0]); {
cout << "(" << a[x].getSize() << ")";
}
cout << endl;
}
exit(1);
}
s.push_back(input[0]);
input.erase(input.begin()); input.erase(input.begin());
} }
cout << "Inserting: "; else
while(!e.empty())
{ {
cout << e[0].getPExpression() << " "; if(input[0].at(0) == '\"')
e.erase(e.begin()); {
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);
}
} }
cout << "into " << r.getName() << ".\n";
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; return input;
} }
else if (input[0] == "RELATION") else if (input[0] == "RELATION")
{ {
input.erase(input.begin()); input.erase(input.begin());
PExpression e; //PExpression e;
while(input[0] != ";") // while(input[0] != ";")
{ // {
e.setPExpression(e.getPExpression() + input[0]); // e.setPExpression(e.getPExpression() + input[0]);
} // }
cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n"; //cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n";
cout << "Not yet implemented." << endl;
return input; return input;
} }
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error! 3"<<endl;
} }
cout<<"Syntax error!"<<endl; cout<<"Syntax error! 2"<<endl;
} }
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error! 1"<<endl;
} }
vector<string> updateCMD(vector<string> input) vector<string> updateCMD(vector<string> input, DBEngine &engine)
{ {
PRelation r(input[0]); PRelation r(input[0]);
@ -316,7 +412,7 @@ vector<string> updateCMD(vector<string> input)
else else
{ {
cout<<"Syntax error!"<<endl; cout<<"Syntax error! 2"<<endl;
} }
if(input[0] == ",") if(input[0] == ",")
@ -356,10 +452,10 @@ vector<string> updateCMD(vector<string> input)
// send update command to DBEngine // send update command to DBEngine
} }
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error! 1"<<endl;
} }
vector<string> deleteCMD(vector<string> input) vector<string> deleteCMD(vector<string> input, DBEngine &engine)
{ {
if (input[0] == "DELETE" && input[1] == "FROM") if (input[0] == "DELETE" && input[1] == "FROM")
{ {
@ -400,7 +496,7 @@ vector<string> deleteCMD(vector<string> input)
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error!"<<endl;
} }
void par_line(vector<string> input) //calls par_command() or par_query() depending on first item from token list void par_line(vector<string> input, DBEngine &engine) //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. Match the first item in the token list and determine weather this is a command or a query.
@ -420,7 +516,7 @@ void par_line(vector<string> input) //calls par_command() or par_query() dependi
{ {
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl; cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin()); input.erase(input.begin());
vector<string> insertInput = insertCMD(input); vector<string> insertInput = insertCMD(input, engine);
cout<<"arguments: "<<endl; cout<<"arguments: "<<endl;
displayTokenList(insertInput); displayTokenList(insertInput);
@ -431,7 +527,7 @@ void par_line(vector<string> input) //calls par_command() or par_query() dependi
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl; cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin()); input.erase(input.begin());
vector<string> insertInput = createCMD(input); vector<string> insertInput = createCMD(input, engine);
cout<<"arguments: "<<endl; cout<<"arguments: "<<endl;
displayTokenList(insertInput); displayTokenList(insertInput);
@ -442,7 +538,7 @@ void par_line(vector<string> input) //calls par_command() or par_query() dependi
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl; cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin()); input.erase(input.begin());
vector<string> insertInput = deleteCMD(input); vector<string> insertInput = deleteCMD(input, engine);
cout<<"arguments: "<<endl; cout<<"arguments: "<<endl;
displayTokenList(insertInput); displayTokenList(insertInput);
@ -453,7 +549,7 @@ void par_line(vector<string> input) //calls par_command() or par_query() dependi
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl; cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin()); input.erase(input.begin());
vector<string> insertInput = updateCMD(input); vector<string> insertInput = updateCMD(input, engine);
cout<<"arguments: "<<endl; cout<<"arguments: "<<endl;
displayTokenList(insertInput); displayTokenList(insertInput);
@ -461,27 +557,37 @@ void par_line(vector<string> input) //calls par_command() or par_query() dependi
if ( input[0] == "SHOW") if ( input[0] == "SHOW")
{ {
showCMD(input); cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
showCMD(input, engine);
} }
if ( input[0] == "EXIT") if ( input[0] == "EXIT")
{ {
exitCMD(input); cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
exitCMD(input, engine);
} }
if ( input[0] == "OPEN") if ( input[0] == "OPEN")
{ {
openCMD(input); cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
openCMD(input, engine);
} }
if ( input[0] == "SAVE") if ( input[0] == "SAVE")
{ {
saveCMD(input); cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
saveCMD(input, engine);
} }
if ( input[0] == "CLOSE") if ( input[0] == "CLOSE")
{ {
closeCMD(input); cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
closeCMD(input, engine);
} }
} }
} }
@ -490,7 +596,7 @@ void parse(string input, DBEngine &engine)
{ {
engine.storeCommands(input); engine.storeCommands(input);
vector<string> listOfTokens = tokenize(input); vector<string> listOfTokens = tokenize(input);
par_line(listOfTokens); par_line(listOfTokens, engine);
} }
/* /*
int main () { int main () {

View file

@ -1,3 +1,3 @@
INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ; SHOW Food ;
SHOW Dogs ; SHOW MoarFood ;
EXIT ; SAVE ;

BIN
test

Binary file not shown.

View file

@ -72,11 +72,11 @@ int main() {
*/ */
int main () { int main () {
/*
string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;"; string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;";
string ss2 = "SHOW Dogs ;"; string ss2 = "SHOW Dogs ;";
string ss3 = "EXIT ; "; string ss3 = "EXIT ; ";
*/
DBEngine engine; DBEngine engine;
// vector<string> listOfTokens = tokenize(ss); // vector<string> listOfTokens = tokenize(ss);
@ -87,11 +87,11 @@ int main () {
// par_line(listOfTokens2); // par_line(listOfTokens2);
// par_line(listOfTokens3); // par_line(listOfTokens3);
parse(ss, engine); // parse(ss, engine);
parse(ss2, engine); // parse(ss2, engine);
parse(ss3, engine); // parse(ss3, engine);
engine.save(); // engine.save();
Attribute att1("Breakfast", "VARCHAR(20)", true); Attribute att1("Breakfast", "VARCHAR(20)", true);
Attribute att2("Lunch", "VARCHAR(20)", false); Attribute att2("Lunch", "VARCHAR(20)", false);
@ -135,8 +135,17 @@ int main () {
engine.createTable("MoarFood", v2); engine.createTable("MoarFood", v2);
engine.getTableFromName("Food").display();
engine.getTableFromName("MoarFood").display(); engine.getTableFromName("MoarFood").display();
engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display(); //engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
string x;
cout << "Enter DBMS Commands: ";
while(getline(cin, x))
{
//cout << x << endl;
parse(x, engine);
cout << "Enter DBMS Commands: ";
}
} }