Merge branch 'master' into alexdev

Conflicts:
	DBEngine.h
	test
This commit is contained in:
Alexander Huddleston 2015-09-28 16:21:18 -05:00
commit b5093be722
8 changed files with 118 additions and 251 deletions

View file

@ -75,8 +75,9 @@ Relation& DBEngine::getTableFromName(string n){
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){
@ -101,28 +102,6 @@ Relation DBEngine::projection(vector<string> input, Relation r){
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
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
if (oldnames.size() != newnames.size()) {
@ -142,17 +121,6 @@ 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){
if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n";
@ -162,6 +130,7 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){
else {
//currently all returned relations are called TEMP
Relation new_r = r1;
new_r.setTableName("TEMP");
vector<string> temp;
bool duplicate = false;
@ -183,32 +152,98 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){
}
return new_r;
/*
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]);
}
}
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);
}
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
temp = r1.getTuple(i);
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
if (temp == r2.getTuple(j)){
duplicate = true;
break;
}
}
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);
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) {
for (int j = 0; j < new_atts.size(); ++j){
if (new_atts[i] == new_atts[j]){
new_atts.erase(new_atts.begin() + i);
continue;
}
}
new_atts[i].clearAllValues();
}
//currently all returned relations are called TEMP
Relation new_r("TEMP", new_atts);
return new_r;*/
vector<string> r1_tuple;
vector<string> r2_tuple;
vector<string> temp;
for (int i = 0; i < r1_attsize; ++i) {
r1_tuple = r1.getTuple(i);
for (int j = 0; j < r2_attsize; ++j) {
r2_tuple = r2.getTuple(j);
temp = r1_tuple;
temp.insert(temp.end(), r2_tuple.begin(), r2_tuple.end());
new_r.insertTuple(temp);
}
}
return new_r;
}

View file

@ -22,6 +22,7 @@ public:
void saveToFile(vector<string> cmds);
Relation selection(string attName, string s, Relation r);
Relation projection(vector<string> input, Relation r);
<<<<<<< HEAD
Relation product(string s1, Relation r1, Relation r2);
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
void save();
@ -30,4 +31,12 @@ public:
Relation setUnion(Relation r1, Relation r2);
//void setDiff();
//void crossProduct();
=======
void save();
void storeCommands(string s);
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
Relation setUnion(Relation r1, Relation r2);
Relation setDiff(Relation r1, Relation r2);
Relation crossProduct(Relation r1, Relation r2);
>>>>>>> master
};

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;
}
void Relation::setTableName(string s){
name = s;
}
Attribute Relation::operator[](int i){
return att[i];
}

View file

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

BIN
a.out Executable file

Binary file not shown.

118
test.cpp
View file

@ -6,93 +6,9 @@
using namespace std;
//still in progress
/*
int main() {
DBEngine engine;
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);
Relation r("Food", v);
//r.renameAttribute("Breakfast", "BFST");
//r.display();
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);
engine.getTableFromName("Food").display();
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);
// vector<string> listOfTokens2 = tokenize(ss2);
// vector<string> listOfTokens3 = tokenize(ss3);
// 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);
@ -114,38 +30,22 @@ int main () {
engine.createTable("Food", v);
Attribute att4("Breakfast", "VARCHAR(20)", true);
Attribute att5("Lunch", "VARCHAR(20)", false);
Attribute att6("Dinner", "VARCHAR(20)", false);
Attribute att4("Dessert", "VARCHAR(20)", true);
Attribute att5("Midnight Snack", "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");
att4.addCell("Ice Cream Sundae");
att4.addCell("Chocolate Bar");
att5.addCell("Hummus and Carrots");
att5.addCell("Potato Chips");
vector<Attribute> v2;
v2.push_back(att4);
v2.push_back(att5);
v2.push_back(att6);
engine.createTable("MoarFood", v2);
engine.getTableFromName("Food").display();
engine.getTableFromName("MoarFood").display();
//engine.getTableFromName("Food").display();
//engine.getTableFromName("MoarFood").display();
//engine.setUnion(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: ";
}
engine.crossProduct(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
}