insertions done

This commit is contained in:
Rebecca Schofield 2015-09-21 16:27:23 -05:00
parent 61cb0d3b1f
commit b6af7f18c8
8 changed files with 104 additions and 22 deletions

View file

@ -2,6 +2,13 @@
#include <vector> #include <vector>
#include "Attribute.h" #include "Attribute.h"
Attribute::Attribute(){
name = "";
type = "";
key = 0;
size = 0;
}
Attribute::Attribute(string n, string t, bool k){ Attribute::Attribute(string n, string t, bool k){
name = n; name = n;
type = t; type = t;
@ -26,6 +33,10 @@ string Attribute::getName(){
return name; return name;
} }
void Attribute::setName(string s){
name = s;
}
string Attribute::getType(){ string Attribute::getType(){
return type; return type;
} }
@ -34,7 +45,7 @@ bool Attribute::isKey(){
return key; return key;
} }
void Attribute::setName(string s){ void Attribute::rename(string s){
name = s; name = s;
} }

View file

@ -11,14 +11,16 @@ class Attribute{
int size; int size;
public: public:
Attribute();
Attribute(string n, string t, bool k); Attribute(string n, string t, bool k);
void addCell(string v); void addCell(string v);
string operator[](int i); string operator[](int i);
vector<string> getValues(); vector<string> getValues();
string getName(); string getName();
void setName(string s);
string getType(); string getType();
bool isKey(); bool isKey();
int getSize(); int getSize();
void setName(string s); void rename(string s);
void display(); void display();
}; };

View file

@ -59,12 +59,26 @@ Relation DBEngine::projection(vector<string> input, Relation r){
//if(r[i].getName == input[]) //if(r[i].getName == input[])
// } // }
} }
/*
void renameAttribute(vector<Attribute> v, string o, string s){ //ASAP: TEST ALL OF THIS
for(int i = 0; i < v.size(); ++i){ void rename(Relation r, vector<string> oldnames, vector<string> newnames){
if(v[i].getName() == o){ if (oldnames.size() != newnames.size()) {
v[i].setName(s); 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]);
} }
} }
} }
*/

View file

@ -19,9 +19,9 @@ public:
void saveToFile(vector<string> cmds); void saveToFile(vector<string> cmds);
//operations //operations
//void selection(); //Relation selection();
Relation projection(vector<string> input, Relation r); Relation projection(vector<string> input, Relation r);
//void renaming(); void rename(Relation r, vector<string> oldnames, vector<string> newnames);
//void setUnion(); //void setUnion();
//void setDiff(); //void setDiff();
//void crossProduct(); //void crossProduct();

View file

@ -29,6 +29,16 @@ vector<string> Relation::getAttributeNames(){
return temp; 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(){ int Relation::getSize(){
return size; return size;
} }
@ -42,3 +52,37 @@ void Relation::display(){
cout << "--------------------------\n"; cout << "--------------------------\n";
} }
void Relation::insertTuple(vector<string> 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<Attribute> 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]);
}
}
}
}

View file

@ -13,6 +13,10 @@ public:
string getTableName(); string getTableName();
vector<Attribute> getAttributes(); vector<Attribute> getAttributes();
vector<string> getAttributeNames(); vector<string> getAttributeNames();
Attribute getAttributeByName(string s);
int getSize(); int getSize();
void display(); void display();
void insertTuple(vector<string> tuple); //we are assuming they are in order
void insertFromRelation(Relation r);
//void removeTuple();
}; };

BIN
a.out Executable file

Binary file not shown.

View file

@ -26,19 +26,26 @@ int main() {
v.push_back(att2); v.push_back(att2);
v.push_back(att3); v.push_back(att3);
Relation r("AltFood", v); Relation r("Food", v);
//r.display();
engine.createTable("Food", v); vector<string> tuple;
engine.createTable(r); tuple.push_back("Omelette");
engine.createTable("Sadness"); tuple.push_back("Fried Rice");
//vector<Relation> rs = engine.getRelations(); tuple.push_back("Grouper");
Relation r2 = engine.getTableFromName("Food");
r2.display();
vector<string> v1 = r2.getAttributeNames(); r.insertTuple(tuple);
r.display();
for (int i = 0; i < v.size(); ++i){ vector<string> o;
cout << v1[i]; vector<string> 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);
} }