updating crossproduct

This commit is contained in:
Rebecca Schofield 2015-09-28 15:57:57 -05:00
commit c3d39eedbd
8 changed files with 122 additions and 258 deletions

View file

@ -55,15 +55,16 @@ void DBEngine::save(){
vector<Relation> DBEngine::getRelations(){ vector<Relation> DBEngine::getRelations(){
return tables; return tables;
} }
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){
return tables[i]; return tables[i];
} }
} }
cout << "No Relation with this name." << endl;
exit(1); cout << "FAILURE TO FIND: could not locate a Relation with this name.";
return tables[0];
} }
Relation DBEngine::selection(string attName, string s, Relation r){ Relation DBEngine::selection(string attName, string s, Relation r){
@ -85,29 +86,7 @@ Relation DBEngine::projection(vector<string> input, Relation r){
} }
Relation temp(new_name, v); Relation temp(new_name, v);
return temp; return temp;
}
Relation DBEngine::product(string new_name, Relation r1, Relation r2){
Relation temp(new_name);
vector<Attribute> a1;
for(int i = 0; i < r1.getAttributes().size(); ++i){
a1.push_back(r1.getAttributes()[i]);
}
for(int i = 0; i < r2.getAttributes().size(); ++i){
a1.push_back(r2.getAttributes()[i]);
}
temp.insertAttributes(a1);
vector<string> tuple1;
vector<string> tuple2;
vector<string> result_tuple;
return temp;
} }
//test error matching //test error matching
@ -128,18 +107,7 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
} }
} }
} }
/*
Relation DBEngine::setUnion(Relation r1, Relation r2){
if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to union: the relations are not union-compatible";
return;
}
else {
vector<Attribute> r1_atts = r1.getAttributes();
*/
Relation DBEngine::setUnion(Relation r1, Relation r2){ Relation DBEngine::setUnion(Relation r1, Relation r2){
if (r1.getAttributeNames() != r2.getAttributeNames()){ if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n"; cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n";
@ -149,6 +117,7 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){
else { else {
//currently all returned relations are called TEMP //currently all returned relations are called TEMP
Relation new_r = r1; Relation new_r = r1;
new_r.setTableName("TEMP");
vector<string> temp; vector<string> temp;
bool duplicate = false; bool duplicate = false;
@ -170,32 +139,105 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){
} }
return new_r; return new_r;
}
}
Relation DBEngine::setDiff(Relation r1, Relation r2){
if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to diff: the relations are not union-compatible.\nreturning the first relation.\n";
return r1;
}
else {
//currently all returned relations are called TEMP
Relation new_r = r1;
new_r.setTableName("TEMP");
vector<string> temp;
bool duplicate = false;
/* for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){
new_r.removeTuple(i);
vector<Attribute> r1_atts = r1.getAttributes();
vector<Attribute> r2_atts = r2.getAttributes();
vector<Attribute> new_atts = r1_atts;
for (int i = 0; i < r2_atts.size(); ++i) {
for (int j = 0; j < r2_atts[i].getSize(); ++j){
new_atts[i].addCell(r2_atts[i][j]);
}
} }
for (int i = 0; i < new_atts.size(); ++i) { for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
for (int j = 0; j < new_atts.size(); ++j){ temp = r1.getTuple(i);
if (new_atts[i] == new_atts[j]){
new_atts.erase(new_atts.begin() + i); for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
continue; if (temp == r2.getTuple(j)){
duplicate = true;
break;
} }
} }
if (!duplicate){
new_r.insertTuple(temp);
}
duplicate = false;
} }
//currently all returned relations are called TEMP //make this one for loop later!
Relation new_r("TEMP", new_atts);
return new_r;*/ for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
temp = r2.getTuple(i);
for (int j = 0; j < r1.getAttributes()[0].getSize(); ++j){
if (temp == r1.getTuple(j)){
duplicate = true;
break;
}
}
if (!duplicate){
new_r.insertTuple(temp);
}
duplicate = false;
}
return new_r;
} }
} }
Relation DBEngine::crossProduct(Relation r1, Relation r2){
vector<Attribute> new_atts = r1.getAttributes();
int r1_attsize = r1.getAttributes()[0].getSize();
int r2_attsize = r2.getAttributes()[0].getSize();
for (int i = 0; i < r2_attsize; ++i) {
new_atts.push_back(r2.getAttributes()[i]);
}
for (int i = 0; i < new_atts.size(); ++i) {
new_atts[i].clearAllValues();
}
vector<string> r1_tuple;
vector<string> r2_tuple;
for (int i = 0; i < r1_attsize; ++i) {
r1_tuple = r1.getTuple(i);
for (int a = 0; a < r1_tuple.size(); ++a) {
cout << r1_tuple[a] << " ";
}
for (int j = 0; j < r2_attsize; ++j) {
r2_tuple = r2.getTuple(j);
for (int b = 0; b < r2_tuple.size(); ++b){
cout << r2_tuple[b] << " ";
}
cout << "\n";
}
cout << "\n\n";
}
//currently all returned relations are called TEMP
Relation new_r("TEMP", new_atts);
return new_r;
}

View file

@ -22,11 +22,10 @@ public:
void saveToFile(vector<string> cmds); void saveToFile(vector<string> cmds);
Relation selection(string attName, string s, Relation r); Relation selection(string attName, string s, Relation r);
Relation projection(vector<string> input, Relation r); Relation projection(vector<string> input, Relation r);
Relation product(string s1, Relation r1, Relation r2);
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
void save(); void save();
void storeCommands(string s); void storeCommands(string s);
Relation setUnion(Relation r1, Relation r2); void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
//void setDiff(); Relation setUnion(Relation r1, Relation r2);
//void crossProduct(); Relation setDiff(Relation r1, Relation r2);
Relation crossProduct(Relation r1, Relation r2);
}; };

Binary file not shown.

View file

@ -1,82 +0,0 @@
---------------
Queries
---------------
Selection:
correct: select ( age == 12 ) people ;
incorrect: select ( age ))))) people ;
Projection:
correct: project ( age ) people ;
incorrect: project age people ;
Renaming:
correct: rename ( age, years old ) ;
incorrect: rename age years ;
Set Union:
correct: union ( people + animals ) ;
incorrect: union people animals ;
Set Difference:
correct: difference ( people - animals ) ;
incorrect: difference people animals ;
Cross Product:
correct: product ( people * animals ) ;
incorrect: product people animals ;
-------------------
Commands
-------------------
Open:
correct: OPEN people ;
incorrect: OPEN ;
Close:
correct: CLOSE people ;
incorrect: CLOSE ;
Save:
correct: SAVE people ;
incorrect: SAVE ;
Exit:
correct: EXIT ;
incorrect: EXIT people ;
Show:
correct: SHOW people ;
incorrect: SHOW ;
Create:
correct: CREATE TABLE people ( age INTEGER, name VARCHAR ( 20 ) ) PRIMARY KEY ( name ) ;
incorrect: CREATE TABLE people age name ;
Update:
correct: UPDATE people SET ( age = 10 ) WHERE ( name == "Bob" ) ;
incorrect: UPDATE ( age = 10 ) WHERE people ;
Insert:
correct: INSERT INTO people VALUES FROM ( 12, "Benny" ) ;
incorrect: INSERT INTO people 12 "Benny" ;
Delete:
correct: DELETE FROM people WHERE ( name == "John" ) ;
incorrect: DELETE IN people WHEN (name=John) ;

View file

@ -23,6 +23,10 @@ string Relation::getTableName(){
return name; return name;
} }
void Relation::setTableName(string s){
name = s;
}
Attribute Relation::operator[](int i){ Attribute Relation::operator[](int i){
return att[i]; return att[i];
} }

View file

@ -13,6 +13,7 @@ public:
Relation(string n, vector<Attribute> a); Relation(string n, vector<Attribute> a);
void insertAttributes(vector<Attribute> a); void insertAttributes(vector<Attribute> a);
string getTableName(); string getTableName();
void setTableName(string s);
Attribute operator[](int i); Attribute operator[](int i);
vector<string> getTuple(int index); vector<string> getTuple(int index);
vector<Attribute> getAttributes(); vector<Attribute> getAttributes();

BIN
test

Binary file not shown.

128
test.cpp
View file

@ -1,15 +1,14 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Parserv3.h" #include "Parserv3.h"
//#include "Condition.h" //#include "Condition.h"
#include "DBEngine.h" #include "DBEngine.h"
using namespace std; using namespace std;
//still in progress int main () {
/*
int main() {
DBEngine engine; DBEngine engine;
Attribute att1("Breakfast", "VARCHAR(20)", true); Attribute att1("Breakfast", "VARCHAR(20)", true);
Attribute att2("Lunch", "VARCHAR(20)", false); Attribute att2("Lunch", "VARCHAR(20)", false);
Attribute att3("Dinner", "VARCHAR(20)", false); Attribute att3("Dinner", "VARCHAR(20)", false);
@ -28,124 +27,25 @@ int main() {
v.push_back(att1); v.push_back(att1);
v.push_back(att2); v.push_back(att2);
v.push_back(att3); v.push_back(att3);
Relation r("Food", v);
//r.renameAttribute("Breakfast", "BFST");
//r.display();
engine.createTable("Food", v); engine.createTable("Food", v);
vector<string> tuple;
tuple.push_back("Omelette");
tuple.push_back("Fried Rice");
tuple.push_back("Grouper");
engine.getTableFromName("Food").insertTuple(tuple);
vector<string> old;
vector<string> newa;
old.push_back("Breakfast");
old.push_back("Lunch");
old.push_back("Dinner");
newa.push_back("Tsafkaerb");
newa.push_back("Hcnul");
newa.push_back("Rennid");
//Projection test
vector<string> projectTest;
projectTest.push_back("Breakfast");
projectTest.push_back("Dinner");
cout << "\n***Initiated Projection***\n" << endl;
Relation sub_r = engine.projection(projectTest, r);
sub_r.display();
//engine.rename(r, o, n);
engine.rename(engine.getTableFromName("Food"), old, newa); Attribute att4("Dessert", "VARCHAR(20)", true);
engine.getTableFromName("Food").display(); Attribute att5("Midnight Snack", "VARCHAR(20)", false);
cout << "finished";
}
*/
int main () {
/*
string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;";
string ss2 = "SHOW Dogs ;";
string ss3 = "EXIT ; ";
*/
DBEngine engine;
// vector<string> listOfTokens = tokenize(ss); att4.addCell("Ice Cream Sundae");
// vector<string> listOfTokens2 = tokenize(ss2); att4.addCell("Chocolate Bar");
// vector<string> listOfTokens3 = tokenize(ss3); att5.addCell("Hummus and Carrots");
att5.addCell("Potato Chips");
// par_line(listOfTokens);
// par_line(listOfTokens2);
// par_line(listOfTokens3);
// parse(ss, engine);
// parse(ss2, engine);
// parse(ss3, engine);
// engine.save();
Attribute att1("Breakfast", "VARCHAR(20)", true);
Attribute att2("Lunch", "VARCHAR(20)", false);
Attribute att3("Dinner", "VARCHAR(20)", false);
att1.addCell("Pancakes");
att1.addCell("Waffles");
att1.addCell("Biscuits");
att2.addCell("Turkey Sandwich");
att2.addCell("Caesar Salad");
att2.addCell("Pizza");
att3.addCell("Steak");
att3.addCell("Shrimp");
att3.addCell("Ribs");
vector<Attribute> v;
v.push_back(att1);
v.push_back(att2);
v.push_back(att3);
engine.createTable("Food", v);
Attribute att4("Breakfast", "VARCHAR(20)", true);
Attribute att5("Lunch", "VARCHAR(20)", false);
Attribute att6("Dinner", "VARCHAR(20)", false);
att4.addCell("Pancakes");
att4.addCell("Bacon");
att4.addCell("Eggs");
att5.addCell("Turkey Sandwich");
att5.addCell("Pasta Salad");
att5.addCell("Taco");
att6.addCell("Steak");
att6.addCell("Fajitas");
att6.addCell("Spaghetti");
vector<Attribute> v2; vector<Attribute> v2;
v2.push_back(att4); v2.push_back(att4);
v2.push_back(att5); v2.push_back(att5);
v2.push_back(att6);
engine.createTable("MoarFood", v2); engine.createTable("MoarFood", v2);
engine.getTableFromName("Food").display(); //engine.getTableFromName("Food").display();
engine.getTableFromName("MoarFood").display(); //engine.getTableFromName("MoarFood").display();
//engine.setUnion(engine.getTableFromName("Food"), 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: ";
}
} }