insertions done
This commit is contained in:
parent
61cb0d3b1f
commit
b6af7f18c8
8 changed files with 104 additions and 22 deletions
|
@ -2,6 +2,13 @@
|
|||
#include <vector>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string> getValues();
|
||||
string getName();
|
||||
void setName(string s);
|
||||
string getType();
|
||||
bool isKey();
|
||||
int getSize();
|
||||
void setName(string s);
|
||||
void rename(string s);
|
||||
void display();
|
||||
};
|
26
DBEngine.cpp
26
DBEngine.cpp
|
@ -59,12 +59,26 @@ Relation DBEngine::projection(vector<string> input, Relation r){
|
|||
//if(r[i].getName == input[])
|
||||
// }
|
||||
}
|
||||
/*
|
||||
void renameAttribute(vector<Attribute> 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<string> oldnames, vector<string> 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
|
|
@ -19,9 +19,9 @@ public:
|
|||
void saveToFile(vector<string> cmds);
|
||||
|
||||
//operations
|
||||
//void selection();
|
||||
//Relation selection();
|
||||
Relation projection(vector<string> input, Relation r);
|
||||
//void renaming();
|
||||
void rename(Relation r, vector<string> oldnames, vector<string> newnames);
|
||||
//void setUnion();
|
||||
//void setDiff();
|
||||
//void crossProduct();
|
||||
|
|
44
Relation.cpp
44
Relation.cpp
|
@ -29,6 +29,16 @@ vector<string> 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<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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,10 @@ public:
|
|||
string getTableName();
|
||||
vector<Attribute> getAttributes();
|
||||
vector<string> getAttributeNames();
|
||||
Attribute getAttributeByName(string s);
|
||||
int getSize();
|
||||
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
BIN
a.out
Executable file
Binary file not shown.
31
test.cpp
31
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<Relation> rs = engine.getRelations();
|
||||
Relation r2 = engine.getTableFromName("Food");
|
||||
r2.display();
|
||||
vector<string> tuple;
|
||||
tuple.push_back("Omelette");
|
||||
tuple.push_back("Fried Rice");
|
||||
tuple.push_back("Grouper");
|
||||
|
||||
vector<string> v1 = r2.getAttributeNames();
|
||||
r.insertTuple(tuple);
|
||||
r.display();
|
||||
|
||||
for (int i = 0; i < v.size(); ++i){
|
||||
cout << v1[i];
|
||||
}
|
||||
vector<string> o;
|
||||
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);
|
||||
}
|
Reference in a new issue