diff --git a/Attribute.cpp b/Attribute.cpp index 9a66043..2ecee1c 100755 --- a/Attribute.cpp +++ b/Attribute.cpp @@ -2,6 +2,13 @@ #include #include "Attribute.h" +Attribute::Attribute(){ + name = ""; + type = ""; + key = 0; + size = 0; +} + Attribute::Attribute(string n, string t, bool k){ name = n; type = t; @@ -26,6 +33,10 @@ string Attribute::getName(){ return name; } +void Attribute::setName(string s){ + name = s; +} + string Attribute::getType(){ return type; } @@ -34,7 +45,7 @@ bool Attribute::isKey(){ return key; } -void Attribute::setName(string s){ +void Attribute::rename(string s){ name = s; } diff --git a/Attribute.h b/Attribute.h index f74e650..fa98b14 100755 --- a/Attribute.h +++ b/Attribute.h @@ -11,14 +11,16 @@ class Attribute{ int size; public: + Attribute(); Attribute(string n, string t, bool k); void addCell(string v); string operator[](int i); vector getValues(); string getName(); + void setName(string s); string getType(); bool isKey(); int getSize(); - void setName(string s); + void rename(string s); void display(); }; \ No newline at end of file diff --git a/DBEngine.cpp b/DBEngine.cpp index a813c90..1e47c4a 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -59,12 +59,26 @@ Relation DBEngine::projection(vector input, Relation r){ //if(r[i].getName == input[]) // } } -/* -void renameAttribute(vector v, string o, string s){ - for(int i = 0; i < v.size(); ++i){ - if(v[i].getName() == o){ - v[i].setName(s); + +//ASAP: TEST ALL OF THIS +void rename(Relation r, vector oldnames, vector newnames){ + if (oldnames.size() != newnames.size()) { + cout << "Failure to rename: number of attributes do not match."; + return; + } + + else if (oldnames != r.getAttributeNames()) { + cout << "Failure to rename: the attributes to be renamed do not exist in the relation."; + return; + } + + else { + Attribute temp; + + for(int i = 0; i < oldnames.size(); ++i){ + temp = r.getAttributeByName(oldnames[i]); + temp.setName(newnames[i]); } } } -*/ + diff --git a/DBEngine.h b/DBEngine.h index f2e5e3f..8533656 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -19,9 +19,9 @@ public: void saveToFile(vector cmds); //operations - //void selection(); + //Relation selection(); Relation projection(vector input, Relation r); - //void renaming(); + void rename(Relation r, vector oldnames, vector newnames); //void setUnion(); //void setDiff(); //void crossProduct(); diff --git a/Relation.cpp b/Relation.cpp index 09eeaf7..300cde1 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -29,6 +29,16 @@ vector Relation::getAttributeNames(){ return temp; } +Attribute Relation::getAttributeByName(string s) { + for(int i = 0; i < size; ++i){ + if (att[i].getName() == s) { + return att[i]; + } + } + + cout << "Failure to return: the requested attribute does not exist."; +} + int Relation::getSize(){ return size; } @@ -42,3 +52,37 @@ void Relation::display(){ cout << "--------------------------\n"; } + +void Relation::insertTuple(vector tuple){ + if (tuple.size() != this->size) { + cout << "Failure to insert: the sizes do not match."; + } + + else { + for (int i = 0; i < att.size(); ++i) { + att[i].addCell(tuple[i]); + } + } +} + +void Relation::insertFromRelation(Relation r){ + if (r.size != this->size) { + cout << "Failure to insert: the sizes do not match."; + return; + } + + else if (this->getAttributeNames() != r.getAttributeNames()) { + cout << "Failure to insert: the attributes do not match."; + return; + } + + else { + vector newAtts = r.getAttributes(); + + for (int i = 0; i < newAtts.size(); ++i) { + for (int j = 0; j < newAtts[i].getSize(); ++j) { + this->att[i].addCell(newAtts[i][j]); + } + } + } +} diff --git a/Relation.h b/Relation.h index 5a25284..beea397 100755 --- a/Relation.h +++ b/Relation.h @@ -13,6 +13,10 @@ public: string getTableName(); vector getAttributes(); vector getAttributeNames(); + Attribute getAttributeByName(string s); int getSize(); void display(); + void insertTuple(vector tuple); //we are assuming they are in order + void insertFromRelation(Relation r); + //void removeTuple(); }; \ No newline at end of file diff --git a/a.out b/a.out new file mode 100755 index 0000000..0bb0543 Binary files /dev/null and b/a.out differ diff --git a/test.cpp b/test.cpp index 165d27e..1c138cb 100755 --- a/test.cpp +++ b/test.cpp @@ -26,19 +26,26 @@ int main() { v.push_back(att2); v.push_back(att3); - Relation r("AltFood", v); - //r.display(); + Relation r("Food", v); - engine.createTable("Food", v); - engine.createTable(r); - engine.createTable("Sadness"); - //vector rs = engine.getRelations(); - Relation r2 = engine.getTableFromName("Food"); - r2.display(); + vector tuple; + tuple.push_back("Omelette"); + tuple.push_back("Fried Rice"); + tuple.push_back("Grouper"); - vector v1 = r2.getAttributeNames(); + r.insertTuple(tuple); + r.display(); - for (int i = 0; i < v.size(); ++i){ - cout << v1[i]; - } + vector o; + vector n; + + o.push_back("Breakfast"); + o.push_back("Lunch"); + o.push_back("Dinner"); + + n.push_back("Tsafkaerb"); + n.push_back("Hcnul"); + n.push_back("Rennid"); + + //engine.rename(r, o, n); } \ No newline at end of file