diff --git a/Attribute.cpp b/Attribute.cpp index 2ecee1c..093aace 100755 --- a/Attribute.cpp +++ b/Attribute.cpp @@ -45,10 +45,6 @@ bool Attribute::isKey(){ return key; } -void Attribute::rename(string s){ - name = s; -} - //may need to change primary key implementation int Attribute::getSize(){ return size; diff --git a/Attribute.h b/Attribute.h index fa98b14..0e92a40 100755 --- a/Attribute.h +++ b/Attribute.h @@ -21,6 +21,5 @@ public: string getType(); bool isKey(); int getSize(); - void rename(string s); void display(); }; \ No newline at end of file diff --git a/DBEngine.cpp b/DBEngine.cpp index 1e47c4a..26670b8 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -28,7 +28,7 @@ vector DBEngine::getRelations(){ return tables; } -Relation DBEngine::getTableFromName(string n){ +Relation& DBEngine::getTableFromName(string n){ //will return first occurence for(int i = 0; i < tables.size(); i++){ if (tables[i].getTableName() == n){ @@ -61,7 +61,7 @@ Relation DBEngine::projection(vector input, Relation r){ } //ASAP: TEST ALL OF THIS -void rename(Relation r, vector oldnames, vector newnames){ +void DBEngine::rename(Relation& r, vector oldnames, vector newnames){ if (oldnames.size() != newnames.size()) { cout << "Failure to rename: number of attributes do not match."; return; @@ -73,11 +73,8 @@ void rename(Relation r, vector oldnames, vector newnames){ } else { - Attribute temp; - for(int i = 0; i < oldnames.size(); ++i){ - temp = r.getAttributeByName(oldnames[i]); - temp.setName(newnames[i]); + r.renameAttribute(oldnames[i], newnames[i]); } } } diff --git a/DBEngine.h b/DBEngine.h index 8533656..20a921c 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -15,13 +15,13 @@ public: void createTable(Relation r); vector getRelations(); //void showTable(Relation r); - Relation getTableFromName(string n); + Relation& getTableFromName(string n); void saveToFile(vector cmds); //operations //Relation selection(); Relation projection(vector input, Relation r); - void rename(Relation r, vector oldnames, vector newnames); + void rename(Relation& r, vector oldnames, vector newnames); //void setUnion(); //void setDiff(); //void crossProduct(); diff --git a/Relation.cpp b/Relation.cpp index 300cde1..f81adee 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -29,7 +29,7 @@ vector Relation::getAttributeNames(){ return temp; } -Attribute Relation::getAttributeByName(string s) { +Attribute& Relation::getAttributeByName(string s) { for(int i = 0; i < size; ++i){ if (att[i].getName() == s) { return att[i]; @@ -39,6 +39,10 @@ Attribute Relation::getAttributeByName(string s) { cout << "Failure to return: the requested attribute does not exist."; } +void Relation::renameAttribute(string oldstr, string newstr){ + this->getAttributeByName(oldstr).setName(newstr); +} + int Relation::getSize(){ return size; } diff --git a/Relation.h b/Relation.h index beea397..ffeadcd 100755 --- a/Relation.h +++ b/Relation.h @@ -13,7 +13,8 @@ public: string getTableName(); vector getAttributes(); vector getAttributeNames(); - Attribute getAttributeByName(string s); + Attribute& getAttributeByName(string s); + void renameAttribute(string oldstr, string newstr); int getSize(); void display(); void insertTuple(vector tuple); //we are assuming they are in order diff --git a/a.out b/a.out index 0bb0543..7af9855 100755 Binary files a/a.out and b/a.out differ diff --git a/test.cpp b/test.cpp index 1c138cb..4174101 100755 --- a/test.cpp +++ b/test.cpp @@ -26,26 +26,31 @@ int main() { v.push_back(att2); v.push_back(att3); - Relation r("Food", v); + //Relation r("Food", v); + //r.renameAttribute("Breakfast", "BFST"); + //r.display(); + + engine.createTable("Food", v); vector tuple; tuple.push_back("Omelette"); tuple.push_back("Fried Rice"); tuple.push_back("Grouper"); - r.insertTuple(tuple); - r.display(); + engine.getTableFromName("Food").insertTuple(tuple); - vector o; - vector n; + vector old; + vector newa; - o.push_back("Breakfast"); - o.push_back("Lunch"); - o.push_back("Dinner"); + old.push_back("Breakfast"); + old.push_back("Lunch"); + old.push_back("Dinner"); - n.push_back("Tsafkaerb"); - n.push_back("Hcnul"); - n.push_back("Rennid"); + newa.push_back("Tsafkaerb"); + newa.push_back("Hcnul"); + newa.push_back("Rennid"); - //engine.rename(r, o, n); + engine.rename(engine.getTableFromName("Food"), old, newa); + engine.getTableFromName("Food").display(); + cout << "finished"; } \ No newline at end of file