updating
This commit is contained in:
parent
60b95250f1
commit
1e5e31b965
5 changed files with 62 additions and 4 deletions
23
DBEngine.cpp
23
DBEngine.cpp
|
@ -85,12 +85,33 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
|
||||||
}
|
}
|
||||||
|
|
||||||
Relation DBEngine::setUnion(Relation r1, Relation r2){
|
Relation DBEngine::setUnion(Relation r1, Relation r2){
|
||||||
if (r1.getAttributes() != r2.getAttributes()){
|
if (r1.getAttributeNames() != r2.getAttributeNames()){
|
||||||
cout << "Failure to union: the relations are not union-compatible";
|
cout << "Failure to union: the relations are not union-compatible";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
|
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 j = 0; j < new_atts.size(); ++j){
|
||||||
|
if (new_atts[i] == new_atts[j]){
|
||||||
|
new_atts.erase(new_atts.begin() + i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//currently all returned relations are called TEMP
|
||||||
|
Relation new_r("TEMP", new_atts);
|
||||||
|
return new_r;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -21,7 +21,7 @@ public:
|
||||||
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);
|
||||||
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
|
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
|
||||||
Relation setUnion();
|
Relation setUnion(Relation r1, Relation r2);
|
||||||
//void setDiff();
|
//void setDiff();
|
||||||
//void crossProduct();
|
//void crossProduct();
|
||||||
};
|
};
|
||||||
|
|
10
Relation.cpp
10
Relation.cpp
|
@ -21,6 +21,16 @@ Attribute Relation::operator[](int i){
|
||||||
return att[i];
|
return att[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<string> Relation::getTuple(int index){
|
||||||
|
vector<string> temp;
|
||||||
|
|
||||||
|
for (int i = 0; i < att.size(); ++i){
|
||||||
|
temp.push_back(att[i][index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
vector<Attribute> Relation::getAttributes(){
|
vector<Attribute> Relation::getAttributes(){
|
||||||
return att;
|
return att;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ public:
|
||||||
Relation(string n, vector<Attribute> a);
|
Relation(string n, vector<Attribute> a);
|
||||||
string getTableName();
|
string getTableName();
|
||||||
Attribute operator[](int i);
|
Attribute operator[](int i);
|
||||||
|
vector<string> getTuple(int index);
|
||||||
vector<Attribute> getAttributes();
|
vector<Attribute> getAttributes();
|
||||||
vector<string> getAttributeNames();
|
vector<string> getAttributeNames();
|
||||||
Attribute& getAttributeByName(string s);
|
Attribute& getAttributeByName(string s);
|
||||||
|
|
30
test.cpp
30
test.cpp
|
@ -29,6 +29,32 @@ int main() {
|
||||||
|
|
||||||
engine.createTable("Food", v);
|
engine.createTable("Food", v);
|
||||||
|
|
||||||
Relation r = equality("Breakfast", "Pancakes", engine.getTableFromName("Food"));
|
Attribute att4("SecondBreakfast", "VARCHAR(20)", true);
|
||||||
r.display();
|
Attribute att5("SecondLunch", "VARCHAR(20)", false);
|
||||||
|
Attribute att6("SecondDinner", "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;
|
||||||
|
v2.push_back(att4);
|
||||||
|
v2.push_back(att5);
|
||||||
|
v2.push_back(att6);
|
||||||
|
|
||||||
|
engine.createTable("MoarFood", v2);
|
||||||
|
|
||||||
|
vector<string> test = engine.getTableFromName("Food").getTuple(1);
|
||||||
|
|
||||||
|
for (int i = 0; i < test.size(); ++i){
|
||||||
|
cout << test[i] << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
//engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
|
||||||
}
|
}
|
Reference in a new issue