This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
dmspine64backup/DBEngine.cpp

89 lines
1.9 KiB
C++
Raw Normal View History

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;
}
//To check if a relation name is already used.
bool DBEngine::isRelation(string n){
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return true;
}
}
return false;
}
2015-09-17 18:30:45 -05:00
2015-09-22 09:03:42 -05:00
Relation& DBEngine::getTableFromName(string n){
2015-09-17 18:30:45 -05:00
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return tables[i];
}
}
}
2015-09-22 18:42:50 -05:00
//currently writes nothing meaningful
2015-09-17 18:30:45 -05:00
void DBEngine::saveToFile(vector<string> cmds){
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-17 18:30:45 -05:00
// for(int i = 0; i < input.size(); i++) {
// it = find(r.getAttributes().begin(), r.getAttributes().end(), input[i])
//if(r[i].getName == input[])
// }
}
2015-09-21 16:27:23 -05:00
2015-09-22 21:17:45 -05:00
//test error matching
2015-09-22 09:03:42 -05:00
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
2015-09-21 16:27:23 -05:00
if (oldnames.size() != newnames.size()) {
2015-09-23 16:34:15 -05:00
cout << "FAILURE TO RENAME: number of attributes do not match.\n";
2015-09-21 16:27:23 -05:00
return;
}
else if (oldnames != r.getAttributeNames()) {
2015-09-23 16:34:15 -05:00
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
2015-09-21 16:27:23 -05:00
return;
}
else {
for(int i = 0; i < oldnames.size(); ++i){
2015-09-22 09:03:42 -05:00
r.renameAttribute(oldnames[i], newnames[i]);
2015-09-17 18:30:45 -05:00
}
}
2015-09-22 23:23:48 -05:00
}