Merge pull request #4 from scho4077/master

merging me and master
This commit is contained in:
scho4077 2015-09-29 21:15:34 -05:00
commit f3a378672d
12 changed files with 1414 additions and 659 deletions

View file

@ -7,6 +7,7 @@ Attribute::Attribute(){
type = "";
key = 0;
size = 0;
limit = 0;
}
Attribute::Attribute(string n, string t, bool k){
@ -14,13 +15,15 @@ Attribute::Attribute(string n, string t, bool k){
type = t;
key = k;
size = 0;
limit = 20;
}
Attribute::Attribute(string n, string t, bool k, int s){
name = n;
type = t;
key = k;
size = s;
size = 0;
limit = s;
}
void Attribute::addCell(string v){
@ -66,7 +69,7 @@ void Attribute::display(){
cout << name << "\n" << type;
if(type == "VARCHAR")
{
cout << "(" << size << ")";
cout << "(" << limit << ")";
}
cout << "\n\n";

View file

@ -10,6 +10,7 @@ class Attribute{
string type;
bool key;
int size;
int limit;
public:
Attribute();

View file

@ -3,7 +3,7 @@
#include <sstream> // std::stringstream
#include <vector>
#include <string>
#include "Parserv3.h"
#include "Parser.h"
#include "DBEngine.h"
using namespace std;

View file

@ -25,7 +25,7 @@ void DBEngine::createTable(Relation r){
size++;
}
void DBEngine::insertValues(string r, vector <string> v)
void DBEngine::insertValues(string r, vector<string> v)
{
for(int i = 0; i < tables.size(); i++)
{
@ -52,11 +52,69 @@ void DBEngine::save(){
file.close();
}
void DBEngine::deleteRelation(string n)
{
// to conserve memory after closing a file.
for(int i = 0; i < tables.size(); i++)
{
if (tables[i].getTableName() == n)
{
tables.erase(tables.begin() + i);
}
}
}
void DBEngine::save(string n){
ofstream file;
string name = n + ".txt";
file.open(name);
for(int i = 0; i < commands.size() - 1; ++i)
{
if(commands[i].substr(0, 4) == "SAVE")
{
continue;
}
if(commands[i].substr(0, 4) == "SHOW")
{
continue;
}
if(commands[i].substr(0, 4) == "OPEN")
{
continue;
}
if(commands[i].substr(0, 5) == "CLOSE")
{
continue;
}
else
{
file << commands[i] << endl;
}
}
//file.close();
}
vector<Relation> DBEngine::getRelations(){
return tables;
}
Relation& DBEngine::getTableFromName(string n){
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)
{
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return tables[i];
@ -68,7 +126,7 @@ Relation& DBEngine::getTableFromName(string n){
}
Relation DBEngine::selection(string attName, string s, Relation r){
equality(attName, s, r);
return equality(attName, s, r);
}
//assumes that all attribute titles are unique
@ -77,11 +135,14 @@ Relation DBEngine::projection(vector<string> input, Relation r){
vector<Attribute> v;
string new_name = r.getTableName() + " Projection";
for(int i = 0; i < input.size(); ++i) {
for(int j = 0; j < r.getSize(); ++j) {
for(int i = 0; i < input.size(); ++i)
{
for(int j = 0; j < r.getSize(); ++j)
{
if((r.getAttributes())[j].getName() == input[i])
{
v.push_back((r.getAttributes())[j]);
}
}
}
@ -90,12 +151,23 @@ Relation DBEngine::projection(vector<string> input, Relation r){
}
//test error matching
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
if (oldnames.size() != newnames.size()) {
Relation DBEngine::rename(vector<string> newnames, Relation &r)
{
vector<string> temp;
if (r.getSize() != newnames.size()) {
cout << "FAILURE TO RENAME: number of attributes do not match.\n";
return;
exit(1);
}
else
{
temp = r.getAttributeNames();
for(int i = 0; i < temp.size(); ++i)
{
r.renameAttribute(temp[i], newnames[i]);
}
return r;
}
/*
else if (oldnames != r.getAttributeNames()) {
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
return;
@ -106,6 +178,8 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
r.renameAttribute(oldnames[i], newnames[i]);
}
}
*/
}
Relation DBEngine::setUnion(Relation r1, Relation r2){
@ -153,31 +227,67 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
Relation new_r = r1;
new_r.setTableName("TEMP");
vector<string> temp;
bool duplicate = false;
//bool duplicate = false;
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){
new_r.removeTuple(i);
/*
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i)
{
new_r.removeTuple(0);
}
*/
int size = 0;
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
temp = r1.getTuple(i);
//cout << "test1" << endl;
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
if (temp == r2.getTuple(j)){
duplicate = true;
break;
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;
}
}
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);
@ -194,6 +304,7 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
duplicate = false;
}
*/
return new_r;
}

View file

@ -17,14 +17,18 @@ public:
void createTable(Relation r);
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);
Relation projection(vector<string> input, Relation r);
Relation product(string s1, Relation r1, Relation r2);
void deleteRelation(string n);
void save();
void save(string n);
void storeCommands(string s);
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
Relation rename(vector<string> newnames, Relation &r);
Relation setUnion(Relation r1, Relation r2);
Relation setDiff(Relation r1, Relation r2);
Relation crossProduct(Relation r1, Relation r2);

1217
Parser.cpp Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,619 +0,0 @@
#include <string> // std::string
#include <iostream> // std::cout
#include <fstream> // std::ofstream
#include <sstream> // std::stringstream
#include <vector>
#include <string>
#include "Parserv3.h"
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]<<" ";
}
cout << endl;
//----------------------
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, DBEngine &engine)
{
if (input.size() > 3)
{
cout<<"Syntax error!"<<endl;
}
PRelation r(input[0]);
// send show command to DBEngine
engine.getTableFromName(r.getName()).display();
return input;
}
vector<string> saveCMD(vector<string> input, DBEngine &engine)
{
if (input.size() > 2)
{
cout<<"Syntax error!"<<endl;
}
//PRelation pr(input[0]);
// 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;
}
vector<string> closeCMD(vector<string> input, DBEngine &engine)
{
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, DBEngine &engine)
{
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, DBEngine &engine)
{
exit(0);
return 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
if (input[0] == "TABLE")
{
input.erase(input.begin());
PRelation r;
r.setPRelation(input[0]);
input.erase(input.begin());
if(input[0] == "(")
{
input.erase(input.begin());
//vector <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("(") + 1, 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);
for(int i = 0; i < a.size(); ++i)
{
if(input[0] == a[i].getPAttribute())
{
a[i].setPAttributeKey();
}
}
input.erase(input.begin());
}
}
}
vector <Attribute> dba;
for(int x = 0; x < a.size(); ++x)
{
Attribute temp(a[x].getPAttribute(), a[x].getPAttributeType(), a[x].getPAttributeKey(), a[x].getPAttributeSize());
dba.push_back(temp);
}
engine.createTable(r.getName(), dba);
return input;
}
else cout<<"Syntax error! 2"<<endl;
}
else cout<<"Syntax error! 1"<<endl;
}
vector<string> insertCMD(vector<string> input, DBEngine &engine)
{
//relation name will be the first element of the vector of data returned by this function
if (input[0] == "INTO")
{
input.erase(input.begin());
PRelation pr(input[0]);
Relation r = engine.getTableFromName(input[0]);
input.erase(input.begin());
vector <string> s;
if (input[0] == "VALUES" && input[1] == "FROM")
{
input.erase(input.begin());
input.erase(input.begin());
vector <Attribute> a = r.getAttributes();
if(input[0] == "(")
{
input.erase(input.begin());
for(int i = 0; i < a.size(); ++i)
{
if(a[i].getType() == "INTEGER")
{
if(input[0].at(0) == '\"')
{
cout << "Incorrect type matching. Tried to insert string." << endl;
cout << "The values should be: ";
for(int x = 0; x < a.size(); ++i)
{
cout << a[x].getType();
if(a[x].getType() == "VARCHAR")
{
cout << "(" << a[x].getSize() << ")";
}
cout << endl;
}
exit(1);
}
s.push_back(input[0]);
input.erase(input.begin());
}
else
{
if(input[0].at(0) == '\"')
{
s.push_back(input[0].substr(1,input[0].find_last_of("\"") - 1 ));
//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);
}
}
if (input[0] == ",")
{
input.erase(input.begin());
}
}
if(input[0] != ")")
{
cout << "Too many values trying to be inserted. Only insert " << a.size() << " values.";
cout << "The values should be: ";
for(int x = 0; x < a.size(); ++x)
{
cout << a[x].getType();
if(a[x].getType() == "VARCHAR")
{
cout << "(" << a[x].getSize() << ")";
}
cout << endl;
}
exit(1);
}
engine.insertValues(r.getTableName(), s);
input.erase(input.begin());
return input;
}
else if (input[0] == "RELATION")
{
input.erase(input.begin());
//PExpression e;
// while(input[0] != ";")
// {
// e.setPExpression(e.getPExpression() + input[0]);
// }
//cout << "Inserting: " << e.getPExpression() << " into " << r.getName() << ".\n";
cout << "Not yet implemented." << endl;
return input;
}
else cout<<"Syntax error! 3"<<endl;
}
cout<<"Syntax error! 2"<<endl;
}
else cout<<"Syntax error! 1"<<endl;
}
vector<string> updateCMD(vector<string> input, DBEngine &engine)
{
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! 2"<<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! 1"<<endl;
}
vector<string> deleteCMD(vector<string> input, DBEngine &engine)
{
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, 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.
Call functions par_command() or par_query();
After either par_command() or par_query() returns, make sure the line ends properly with ; token
*/
string tempChar = input.back();
if (tempChar != ";")
{
cout<<"ERROR! missing semicolon "<<endl;
}
else
{
if ( input[0] == "INSERT")
{
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
vector<string> insertInput = insertCMD(input, engine);
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, engine);
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, engine);
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, engine);
cout<<"arguments: "<<endl;
displayTokenList(insertInput);
}
if ( input[0] == "SHOW")
{
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
showCMD(input, engine);
}
if ( input[0] == "EXIT")
{
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
exitCMD(input, engine);
}
if ( input[0] == "OPEN")
{
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
openCMD(input, engine);
}
if ( input[0] == "SAVE")
{
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
saveCMD(input, engine);
}
if ( input[0] == "CLOSE")
{
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
input.erase(input.begin());
closeCMD(input, engine);
}
}
}
void parse(string input, DBEngine &engine)
{
engine.storeCommands(input);
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);
}
*/

View file

@ -63,6 +63,16 @@ Attribute& Relation::getAttributeByName(string s) {
cout << "Failure to return: the requested attribute does not exist.";
}
bool Relation::isAttribute(string s) {
for(int i = 0; i < size; ++i){
if (att[i].getName() == s) {
return true;
}
}
return false;
//cout << "Failure to return: the requested attribute does not exist.";
}
void Relation::renameAttribute(string oldstr, string newstr){
this->getAttributeByName(oldstr).setName(newstr);
}
@ -82,6 +92,8 @@ 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.";
}
@ -116,13 +128,29 @@ void Relation::insertFromRelation(Relation r){
}
void Relation::removeTuple(int index){
if (index >= this->size) {
cout << "Failure to delete: the requested index is out of bounds.";
if (index >= this->size)
{
cout << "Failure to delete: the requested index is out of bounds." << endl;
}
else {
for (int i = 0; i < att.size(); ++i) {
else
{
for (int i = 0; i < att.size(); ++i)
{
att[i].removeCell(index);
}
}
}
void Relation::removeFromTuple(int rindex, int aindex)
{
if (rindex >= this->size)
{
cout << "Failure to delete: the requested index is out of bounds." << endl;
}
else
{
att[rindex].removeCell(aindex);
}
}

View file

@ -19,10 +19,12 @@ public:
vector<Attribute> getAttributes();
vector<string> getAttributeNames();
Attribute& getAttributeByName(string s);
bool isAttribute(string s);
void renameAttribute(string oldstr, string newstr);
int getSize();
void display();
void insertTuple(vector<string> tuple); //assuming they are in order
void insertFromRelation(Relation r);
void removeTuple(int index);
void removeFromTuple(int rindex, int aindex);
};

BIN
test Executable file

Binary file not shown.

View file

@ -1,6 +1,6 @@
#include <iostream>
#include <vector>
#include "Parserv3.h"
#include "Parser.h"
//#include "Condition.h"
#include "DBEngine.h"
@ -48,4 +48,12 @@ int main () {
//engine.getTableFromName("MoarFood").display();
engine.crossProduct(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: ";
}
}