2015-09-17 18:30:45 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include "DBEngine.h"
|
|
|
|
|
|
|
|
DBEngine::DBEngine(){
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DBEngine::createTable(string n){
|
|
|
|
Relation r(n);
|
|
|
|
tables.push_back(r);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DBEngine::createTable(string n, vector<Attribute> a){
|
|
|
|
Relation r(n, a);
|
|
|
|
tables.push_back(r);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DBEngine::createTable(Relation r){
|
|
|
|
tables.push_back(r);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Relation> DBEngine::getRelations(){
|
|
|
|
return tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
Relation DBEngine::getTableFromName(string n){
|
|
|
|
//will return first occurence
|
|
|
|
for(int i = 0; i < tables.size(); i++){
|
|
|
|
if (tables[i].getTableName() == n){
|
|
|
|
return tables[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DBEngine::saveToFile(vector<string> cmds){
|
|
|
|
//writes nothing meaningful
|
|
|
|
ofstream file;
|
|
|
|
file.open("savefile.db");
|
|
|
|
|
|
|
|
for(int i = 0; i < cmds.size(); ++i){
|
|
|
|
file << cmds[i] << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
//assumes that all attribute titles are unique
|
|
|
|
Relation DBEngine::projection(vector<string> input, Relation r){
|
|
|
|
|
2015-09-22 00:03:53 -05:00
|
|
|
vector<Attribute> v;
|
|
|
|
string new_name = r.getTableName() + " Projection";
|
|
|
|
|
2015-09-21 22:00:02 -05:00
|
|
|
for(int i = 0; i < input.size(); ++i) {
|
2015-09-17 18:30:45 -05:00
|
|
|
|
2015-09-21 22:00:02 -05:00
|
|
|
for(int j = 0; j < r.getSize(); ++j) {
|
|
|
|
if((r.getAttributes())[j].getName() == input[i])
|
2015-09-22 00:03:53 -05:00
|
|
|
v.push_back((r.getAttributes())[j]);
|
2015-09-21 22:00:02 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 00:03:53 -05:00
|
|
|
|
|
|
|
Relation temp(new_name, v);
|
|
|
|
return temp;
|
2015-09-17 18:30:45 -05:00
|
|
|
}
|
2015-09-21 16:27:23 -05:00
|
|
|
|
|
|
|
//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]);
|
2015-09-17 18:30:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 16:27:23 -05:00
|
|
|
|