Queries work (kind of). Conditions need to be implemented to finish it. Cross Product may need work.

This commit is contained in:
Alexander Huddleston 2015-09-29 12:00:37 -05:00
parent 5e58734054
commit 28facf2997
7 changed files with 259 additions and 25 deletions

View file

@ -32,6 +32,7 @@ void Attribute::addCell(string v){
} }
void Attribute::removeCell(int index){ void Attribute::removeCell(int index){
cout << values[index] << " " << index << endl;
values.erase(values.begin() + index); values.erase(values.begin() + index);
size--; size--;
} }
@ -69,7 +70,7 @@ void Attribute::display(){
cout << name << "\n" << type; cout << name << "\n" << type;
if(type == "VARCHAR") if(type == "VARCHAR")
{ {
cout << "(" << size << ")"; cout << "(" << limit << ")";
} }
cout << "\n\n"; cout << "\n\n";

View file

@ -101,7 +101,20 @@ vector<Relation> DBEngine::getRelations(){
return tables; 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++){ for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){ if (tables[i].getTableName() == n){
return tables[i]; return tables[i];
@ -198,31 +211,67 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
Relation new_r = r1; Relation new_r = r1;
new_r.setTableName("TEMP"); new_r.setTableName("TEMP");
vector<string> 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;
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 i = 0; i < r1.getAttributes()[0].getSize(); ++i) { for(int y = 0; y < new_r.getAttributes().size(); ++y)
temp = r1.getTuple(i); {
size = new_r.getAttributes()[y].getSize();
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){ cout << "test4" << endl;
if (temp == r2.getTuple(j)){ new_r.getAttributes()[y].getSize();
duplicate = true; for (int j = 0; j < size; ++j)
break; {
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! //make this one for loop later!
/*
for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) { for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
temp = r2.getTuple(i); temp = r2.getTuple(i);
@ -239,6 +288,7 @@ Relation DBEngine::setDiff(Relation r1, Relation r2){
duplicate = false; duplicate = false;
} }
*/
return new_r; return new_r;
} }

View file

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

View file

@ -507,11 +507,175 @@ vector<string> deleteCMD(vector<string> input, DBEngine &engine)
else cout<<"Syntax error!"<<endl; else cout<<"Syntax error!"<<endl;
} }
void query(vector<string> input, DBEngine &engine) Relation expression(vector<string> input, DBEngine &engine)
{
Relation rfinal("TEMP");
if(input[0] == "select")
{
}
else if(input[0] == "project")
{
}
else if(input[0] == "rename")
{
}
else if(engine.isRelation(input[0]))
{
Relation r1(engine.getTableFromName(input[0]));
input.erase(input.begin());
if(input[0] == "+")
{
input.erase(input.begin());
if(engine.isRelation(input[0]))
{
Relation r2(engine.getTableFromName(input[0]));
rfinal = engine.setUnion(r1, r2);
return rfinal;
}
else if(input[0] == "(")
{
Relation r2 = expression(input, engine);
rfinal = engine.setUnion(r1, r2);
return rfinal;
}
}
else if(input[0] == "-")
{
input.erase(input.begin());
if(engine.isRelation(input[0]))
{
Relation r2(engine.getTableFromName(input[0]));
rfinal = engine.setDiff(r1, r2);
return rfinal;
}
else if(input[0] == "(")
{
Relation r2 = expression(input, engine);
rfinal = engine.setDiff(r1, r2);
return rfinal;
}
}
else if(input[0] == "*")
{
input.erase(input.begin());
if(engine.isRelation(input[0]))
{
Relation r2(engine.getTableFromName(input[0]));
rfinal = engine.crossProduct(r1, r2);
return rfinal;
}
else if(input[0] == "(")
{
Relation r2 = expression(input, engine);
rfinal = engine.crossProduct(r1, r2);
return rfinal;
}
}
}
else if(input[0] == "(")
{
input.erase(input.begin());
if(input[0] == "+")
{
}
else if(input[0] == "-")
{
}
else if(input[0] == "*")
{
}
}
return rfinal;
}
vector<string> query(vector<string> input, DBEngine &engine)
{ {
PRelation pr(input[0]); PRelation pr(input[0]);
input.erase(input.begin()); input.erase(input.begin());
input.erase(input.begin());
Relation r = expression(input, engine);
r.setTableName(pr.getName());
//r.display();
//temporary fix
while(input[0] != ";")
{
input.erase(input.begin());
}
/*
if(input[0] == "select")
{
}
else if(input[0] == "project")
{
}
else if(input[0] == "rename")
{
}
else if(engine.isRelation())
{
Relation r(engine.getTableFromName(input[0]));
input.erase(input.begin());
if(input[0] == "+")
{
}
else if(input[0] == "-")
{
}
else if(input[0] == "*")
{
}
}
else
{
cout << "Invalid query request." << endl;
}
*/
//testing
/*
for(int x = 0; x < input.size(); ++x)
{
cout << input[x] << endl;
}
*/
engine.createTable(r);
return input;
} }
void par_line(vector<string> input, DBEngine &engine) //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

View file

@ -116,13 +116,30 @@ void Relation::insertFromRelation(Relation r){
} }
void Relation::removeTuple(int index){ void Relation::removeTuple(int index){
if (index >= this->size) { if (index >= this->size)
cout << "Failure to delete: the requested index is out of bounds."; {
cout << "Failure to delete: the requested index is out of bounds." << endl;
} }
else { else
for (int i = 0; i < att.size(); ++i) { {
for (int i = 0; i < att.size(); ++i)
{
att[i].removeCell(index); 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
{
cout << aindex << endl;
att[rindex].removeCell(aindex);
}
} }

View file

@ -25,4 +25,5 @@ public:
void insertTuple(vector<string> tuple); //assuming they are in order void insertTuple(vector<string> tuple); //assuming they are in order
void insertFromRelation(Relation r); void insertFromRelation(Relation r);
void removeTuple(int index); void removeTuple(int index);
void removeFromTuple(int rindex, int aindex);
}; };

BIN
test

Binary file not shown.