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

117 lines
2.6 KiB
C++
Raw Normal View History

2015-09-17 18:30:45 -05:00
#include <fstream>
#include <iostream>
#include <vector>
2015-09-24 20:02:57 -05:00
#include "Condition.h"
2015-09-17 18:30:45 -05:00
#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;
}
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();
}
2015-09-24 20:02:57 -05:00
Relation DBEngine::selection(string attName, string s, Relation r){
equality(attName, s, r);
}
2015-09-17 18:30:45 -05:00
//assumes that all attribute titles are unique
Relation DBEngine::projection(vector<string> input, Relation r){
// 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()) {
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 {
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-21 16:27:23 -05:00
2015-09-24 20:46:30 -05:00
/*Relation DBEngine::setUnion(Relation r1, Relation r2){
2015-09-24 20:42:59 -05:00
if (r1.getAttributeNames() != r2.getAttributeNames()){
2015-09-24 20:02:57 -05:00
cout << "Failure to union: the relations are not union-compatible";
return;
}
else {
2015-09-24 20:42:59 -05:00
vector<Attribute> r1_atts = r1.getAttributes();
vector<Attribute> r2_atts = r2.getAttributes();
vector<Attribute> new_atts = r1_atts;
for (int i = 0; i < r2_atts.size(); ++i) {
for (int j = 0; j < r2_atts[i].getSize(); ++j){
new_atts[i].addCell(r2_atts[i][j]);
}
}
for (int i = 0; i < new_atts.size(); ++i) {
for (int j = 0; j < new_atts.size(); ++j){
if (new_atts[i] == new_atts[j]){
new_atts.erase(new_atts.begin() + i);
continue;
}
}
}
2015-09-24 20:02:57 -05:00
2015-09-24 20:42:59 -05:00
//currently all returned relations are called TEMP
Relation new_r("TEMP", new_atts);
return new_r;
2015-09-24 20:02:57 -05:00
}
2015-09-24 20:46:30 -05:00
}*/