Adding DBEngine functions into CreateCMD parser function.

This commit is contained in:
Alexander Huddleston 2015-09-23 17:34:36 -05:00
parent c6c5caaf02
commit cd14f55be7
5 changed files with 185 additions and 320 deletions

View file

@ -28,6 +28,16 @@ vector<Relation> DBEngine::getRelations(){
return tables; return tables;
} }
//To check if a relation name is already used.
bool DBEngine::isRelation(string n){
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return true;
}
}
return false;
}
Relation& DBEngine::getTableFromName(string n){ Relation& DBEngine::getTableFromName(string n){
for(int i = 0; i < tables.size(); i++){ for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){ if (tables[i].getTableName() == n){

View file

@ -1,196 +0,0 @@
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
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> insertCMD(vector<string> input)
{
//relation name will be the first element of the vector of data returned by this function
vector<string> output;
if (input[0] == "INTO")
{
input.erase(input.begin());
output.push_back(input[0]); //pushing relation name
input.erase(input.begin());
if (input[0] == "VALUES" && input[1] == "FROM")
{
input.erase(input.begin());
input.erase(input.begin());
if(input[0] == "(")
{
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());
output.push_back(input[0]);
input.erase(input.begin());
}
return output;
}
else cout<<"Syntax error!"<<endl;
}
else cout<<"Syntax error!"<<endl;
}
else cout<<"Syntax error!"<<endl;
}
vector<string> showCMD(vector<string> input)
{
if (input.size() > 3)
{
cout<<"Syntax error!"<<endl;
}
cout<<"\nPassing the following arguments to dbEngine: "<<endl;
cout << "command :" << input[0] << endl;
cout << "relation name / expression: "<< input[1] << endl;
return input;
}
vector<string> exitCMD(vector<string> input)
{
if (input[1] != ";")
{
cout<<"ERROR: missing semicolon!"<<endl;
return input;
}
if (input[0] != "EXIT") {
cout<<"Wrong function/syntax error!"<<endl;
return input;
}
cout<<"Passing command: "<< input[0] <<endl;
return input;
}
vector<string> createCMD(vector<string> input)
{
if (input[0] != "CREATE") {
cout << "Error: create keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
// CREATE TABLE relation-name ( typed-attribute-list ) PRIMARY KEY ( attribute-list )
cout << "command :" << input[0] << endl;
cout << "argument: " << input[1] << endl;
return input;
}
vector<string> openCMD(vector<string> input){
if (input[0] != "OPEN") {
cout << "Error: open keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[1] << endl;
}
vector<string> closeCMD(vector<string> input){
if (input[0] != "CLOSE") {
cout << "Error: close keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[1] << endl;
return input;
}
vector<string> saveCMD(vector<string> input){
if (input[0] != "SAVE") {
cout << "Error: save keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[1] << endl;
return input;
}
vector<string> updateCMD(vector<string> input){
//UPDATE relation-name SET attribute-name = literal { , attribute-name = literal } WHERE condition
}
vector<string> deleteCMD(vector<string> input){
//DELETE FROM relation-name WHERE condition
if (input[0] != "DELETE") {
cout << "Error: save keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[2] << endl;
cout << "condition: " << input[5] << input[6] << input[7] << endl;
return input;
}
int main () {
string s = "DELETE FROM animals WHERE ( age == 12 ) ;";
vector<string> listOfTokens = tokenize(s);
deleteCMD(listOfTokens);
}

View file

@ -7,22 +7,22 @@
using namespace std; using namespace std;
class Relation class PRelation
{ {
string name; string name;
public: public:
Relation() PRelation()
{ {
name = NULL; name = "~";
} }
Relation(string str) PRelation(string str)
{ {
name = str; name = str;
} }
void setRelation(string str) void setPRelation(string str)
{ {
name = str; name = str;
} }
@ -32,117 +32,140 @@ class Relation
} }
}; };
class Attribute class PAttribute
{ {
string name; string name;
string type;
string size;
public: public:
Attribute() PAttribute()
{ {
name = NULL; name = "~";
type = "~";
size = "~";
} }
Attribute(string str) PAttribute(string str, string t)
{
name = str;
type = t;
size = "~";
}
PAttribute(string str, string t, string s)
{
name = str;
type = t;
size = s;
}
void setPAttributeName(string str)
{ {
name = str; name = str;
} }
void setAttribute(string str) void setPAttributeType(string t)
{ {
name = str; type = t;
} }
string getAttribute() void setPAttributeSize(string s)
{
size = s;
}
string getPAttribute()
{ {
return name; return name;
} }
}; };
class Union class PUnion
{ {
string Un1; string Un1;
string Un2; string Un2;
public: public:
Union() PUnion()
{ {
Un1 = NULL; Un1 = "~";
Un2 = NULL; Un2 = "~";
} }
Union (string s1, string s2) PUnion (string s1, string s2)
{ {
Un1 = s1; Un1 = s1;
Un2 = s2; Un2 = s2;
} }
string getUnion() string getPUnion()
{ {
return "Union of " + Un1 + " and " + Un2; return "Union of " + Un1 + " and " + Un2;
} }
}; };
class Product class PProduct
{ {
string Pr1; string Pr1;
string Pr2; string Pr2;
public: public:
Product() PProduct()
{ {
Pr1 = NULL; Pr1 = "~";
Pr2 = NULL; Pr2 = "~";
} }
Product(string s1, string s2) PProduct(string s1, string s2)
{ {
Pr1 = s1; Pr1 = s1;
Pr2 = s2; Pr2 = s2;
} }
string getProduct() string getPProduct()
{ {
return "Product of " + Pr1 + " and " + Pr2; return "Product of " + Pr1 + " and " + Pr2;
} }
}; };
class Difference class PDifference
{ {
string D1; string D1;
string D2; string D2;
public: public:
Difference() PDifference()
{ {
D1 = NULL; D1 = "~";
D2 = NULL; D2 = "~";
} }
Difference(string s1, string s2) PDifference(string s1, string s2)
{ {
D1 = s1; D1 = s1;
D2 = s2; D2 = s2;
} }
string getDifference() string getPDifference()
{ {
return "Difference of " + D1 + " and " + D2; return "Difference of " + D1 + " and " + D2;
} }
}; };
class Renaming class PRenaming
{ {
string newName; string newName;
string oldName; string oldName;
public: public:
Renaming() PRenaming()
{ {
newName = NULL; newName = "~";
oldName = NULL; oldName = "~";
} }
Renaming(string s1, string s2) PRenaming(string s1, string s2)
{ {
newName = s1; newName = s1;
oldName = s2; oldName = s2;
@ -154,211 +177,217 @@ class Renaming
} }
}; };
class Projection class PProjection
{ {
string newName; string newName;
string oldName; string oldName;
public: public:
Projection() PProjection()
{ {
newName = NULL; newName = "~";
oldName = NULL; oldName = "~";
} }
Projection(string s1, string s2) PProjection(string s1, string s2)
{ {
newName = s1; newName = s1;
oldName = s2; oldName = s2;
} }
string doProjection() string doPProjection()
{ {
return "Projecting " + oldName + " onto " + newName; return "Projecting " + oldName + " onto " + newName;
} }
}; };
class Operand class POperand
{ {
string op; string op;
public: public:
Operand() POperand()
{ {
op = NULL; op = "~";
} }
Operand(string str) POperand(string str)
{ {
op = str; op = str;
} }
void setOperand(string str) void setPOperand(string str)
{ {
op = str; op = str;
} }
string getOperand() string getPOperand()
{ {
return op; return op;
} }
}; };
class Op class POp
{ {
string op; string op;
public: public:
Op() POp()
{ {
op = NULL; op = "~";
} }
Op(string str) POp(string str)
{ {
op = str; op = str;
} }
void setOp(string str) void setPOp(string str)
{ {
op = str; op = str;
} }
string getOp() string getPOp()
{ {
return op; return op;
} }
}; };
class Comparison class PComparison
{ {
Op op; POp op;
Operand operand1; POperand operand1;
Operand operand2; POperand operand2;
public: public:
Comparison() PComparison()
{ {
op = NULL; op.setPOp("~");
operand1 = NULL; operand1.setPOperand("~");
operand2 = NULL; operand2.setPOperand("~");
} }
Comparison(string str1, string str2, string str3) PComparison(string str1, string str2, string str3)
{ {
operand1.setOperand(str1); operand1.setPOperand(str1);
op.setOp(str2); op.setPOp(str2);
operand2.setOperand(str3); operand2.setPOperand(str3);
} }
void setComparison(string str1, string str2, string str3) void setPComparison(string str1, string str2, string str3)
{ {
operand1.setOperand(str1); operand1.setPOperand(str1);
op.setOp(str2); op.setPOp(str2);
operand2.setOperand(str3); operand2.setPOperand(str3);
} }
string getComparison() string getPComparison()
{ {
return operand1.getOperand() + " " + op.getOp() + " " + operand2.getOperand(); return operand1.getPOperand() + " " + op.getPOp() + " " + operand2.getPOperand();
} }
}; };
class Conjunction class PConjunction
{ {
string conj; string conj;
public: public:
Conjunction() PConjunction()
{ {
conj = NULL; conj = "~";
} }
Conjunction(string str) PConjunction(string str)
{ {
conj = str; conj = str;
} }
void setConjunction(string str) void setPConjunction(string str)
{ {
conj = str; conj = str;
} }
string getConjunction() string getPConjunction()
{ {
return conj; return conj;
} }
}; };
class Condition class PCondition
{ {
string cond; string cond;
public: public:
Condition() PCondition()
{ {
cond = NULL; cond = "~";
} }
Condition(string str) PCondition(string str)
{ {
cond = str; cond = str;
} }
void setCondition(string str) void setPCondition(string str)
{ {
cond = str; cond = str;
} }
string getCondition() string getPCondition()
{ {
return cond; return cond;
} }
}; };
class Selection class PSelection
{ {
string select; string select;
public: public:
Selection() PSelection()
{ {
select = NULL; select = "~";
} }
Selection(string str) PSelection(string str)
{ {
select = str; select = str;
} }
string getSelection() string getPSelection()
{ {
return select; return select;
} }
}; };
class Expression class PExpression
{ {
string exp; PRelation rel;
PSelection sel;
PProjection proj;
PRenaming ren;
PUnion un;
PDifference diff;
PProduct prod;
string temp;
public: public:
Expression() PExpression()
{ {
exp = NULL;
} }
Expression(string str) PExpression(string str)
{ {
exp = str; temp = str;
} }
void setExpression(string str) void setPExpression(string str)
{ {
exp = str; temp = str;
} }
string getExpression() string getPExpression()
{ {
return exp; return temp;
} }
}; };

View file

@ -118,8 +118,8 @@ vector<string> createCMD(vector<string> input)
input.erase(input.begin()); input.erase(input.begin());
input.erase(input.begin()); input.erase(input.begin());
Relation r; PRelation r;
r.setRelation(input[0]); r.setPRelation(input[0]);
input.erase(input.begin()); input.erase(input.begin());
@ -127,21 +127,39 @@ vector<string> createCMD(vector<string> input)
{ {
input.erase(input.begin()); input.erase(input.begin());
vector <Expression> e1; //vector <PExpression> e1;
vector <PAttribute> a;
while(input[0] != ")") //inserting all values to relation while(input[0] != ")") //inserting all values to relation
{ {
PAttribute temp;
if (input[0] == ",") if (input[0] == ",")
{ {
input.erase(input.begin()); input.erase(input.begin());
} }
e1.push_back(input[0]); temp.setPAttributeName(input[0]);
input.erase(input.begin()); 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(input[0].substr(input[0].find("("), input[0].find(")")));
input.erase(input.begin());
}
a.push_back(temp);
} }
vector <Expression> e2; vector <PAttribute> apk; //save primary keys temp storage
if(input[0] == "PRIMARY" && input[1] == "KEY") if(input[0] == "PRIMARY" && input[1] == "KEY")
{ {
@ -153,23 +171,21 @@ vector<string> createCMD(vector<string> input)
while(input[0] != ")") //inserting all values to relation while(input[0] != ")") //inserting all values to relation
{ {
PAttribute temp;
if (input[0] == ",") if (input[0] == ",")
{ {
input.erase(input.begin()); input.erase(input.begin());
} }
e2.push_back(input[0]); temp.setPAttributeName(input[0]);
apk.push_back(temp);
input.erase(input.begin()); input.erase(input.begin());
} }
cout << "Creating a table: " << r.getName() << "\n";
cout << "Primary key: ";
while(!e2.empty())
{
cout << e2[0].getExpression() << " ";
e2.erase(e2.begin());
}
return input; return input;
} }
@ -190,10 +206,12 @@ vector<string> insertCMD(vector<string> input)
{ {
input.erase(input.begin()); input.erase(input.begin());
Relation r(input[0]); PRelation r(input[0]);
input.erase(input.begin()); input.erase(input.begin());
vector <string> s;
if (input[0] == "VALUES" && input[1] == "FROM") if (input[0] == "VALUES" && input[1] == "FROM")
{ {
input.erase(input.begin()); input.erase(input.begin());
@ -202,7 +220,11 @@ vector<string> insertCMD(vector<string> input)
if(input[0] == "(") if(input[0] == "(")
{ {
vector <Expression> e; if(input[0].at(0) == '\"')
{
s.push_back(input[0].substr(1,input[0].find_last_of("\"")));
}
vector <PExpression> e;
input.erase(input.begin()); input.erase(input.begin());
while(input[0] != ")") //inserting all values to relation while(input[0] != ")") //inserting all values to relation
@ -218,7 +240,7 @@ vector<string> insertCMD(vector<string> input)
cout << "Inserting: "; cout << "Inserting: ";
while(!e.empty()) while(!e.empty())
{ {
cout << e[0].getExpression() << " "; cout << e[0].getPExpression() << " ";
e.erase(e.begin()); e.erase(e.begin());
} }
cout << "into " << r.getName() << ".\n"; cout << "into " << r.getName() << ".\n";
@ -231,14 +253,14 @@ vector<string> insertCMD(vector<string> input)
{ {
input.erase(input.begin()); input.erase(input.begin());
Expression e; PExpression e;
while(input[0] != ";") while(input[0] != ";")
{ {
e.setExpression(e.getExpression() + input[0]); e.setPExpression(e.getPExpression() + input[0]);
} }
cout << "Inserting: " << e.getExpression() << " into " << r.getName() << ".\n"; cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n";
return input; return input;
} }
@ -263,7 +285,7 @@ vector<string> updateCMD(vector<string> input)
{ {
input.erase(input.begin()); input.erase(input.begin());
Relation r(input[0]); PRelation r(input[0]);
input.erase(input.begin()); input.erase(input.begin());
@ -271,7 +293,7 @@ vector<string> updateCMD(vector<string> input)
if(input[0] == "(") if(input[0] == "(")
{ {
vector <Expression> e; vector <PExpression> e;
input.erase(input.begin()); input.erase(input.begin());
while(input[0] != ")") while(input[0] != ")")
@ -282,10 +304,10 @@ vector<string> updateCMD(vector<string> input)
if(input[0] == "WHERE") if(input[0] == "WHERE")
{ {
Condition c; PCondition c;
while(input[0] != ";") while(input[0] != ";")
{ {
c.setCondition(/*c.getCondition +*/ input[0]); c.setPCondition(/*c.getCondition +*/ input[0]);
} }
} }
} }
@ -301,10 +323,10 @@ vector<string> deleteCMD(vector<string> input)
input.erase(input.begin()); input.erase(input.begin());
input.erase(input.begin()); input.erase(input.begin());
Relation r(input[0]); PRelation r(input[0]);
if(input[0] == "(") if(input[0] == "(")
{ {
vector <Expression> e; vector <PExpression> e;
input.erase(input.begin()); input.erase(input.begin());
while(input[0] != ")") while(input[0] != ")")
@ -319,7 +341,7 @@ vector<string> deleteCMD(vector<string> input)
cout << "Deleting: "; cout << "Deleting: ";
while(!e.empty()) while(!e.empty())
{ {
cout << e[0].getExpression() << " "; cout << e[0].getPExpression() << " ";
e.erase(e.begin()); e.erase(e.begin());
} }
cout << "from " << r.getName() << ".\n"; cout << "from " << r.getName() << ".\n";

BIN
a.out

Binary file not shown.