Queries work (kind of). Conditions need to be implemented to finish it. Cross Product may need work.
This commit is contained in:
parent
5e58734054
commit
28facf2997
7 changed files with 259 additions and 25 deletions
|
@ -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";
|
||||||
|
|
||||||
|
|
80
DBEngine.cpp
80
DBEngine.cpp
|
@ -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;
|
||||||
|
|
||||||
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
|
cout << "test1" << endl;
|
||||||
temp = r1.getTuple(i);
|
|
||||||
|
|
||||||
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
|
for(int x = 0; x < r2.getAttributes().size(); ++x)
|
||||||
if (temp == r2.getTuple(j)){
|
{
|
||||||
duplicate = true;
|
|
||||||
break;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (!duplicate){
|
size = new_r.getAttributes()[y].getSize();
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
if (!duplicate)
|
||||||
|
{
|
||||||
new_r.insertTuple(temp);
|
new_r.insertTuple(temp);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
duplicate = false;
|
//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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
166
Parser.cpp
166
Parser.cpp
|
@ -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
|
||||||
|
|
25
Relation.cpp
25
Relation.cpp
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
BIN
test
Binary file not shown.
Reference in a new issue