151 lines
3.2 KiB
C++
Executable file
151 lines
3.2 KiB
C++
Executable file
#include <fstream>
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include "Condition.h"
|
|
#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++;
|
|
}
|
|
|
|
void DBEngine::storeCommands(string s){
|
|
commands.push_back(s);
|
|
}
|
|
|
|
void DBEngine::save(){
|
|
|
|
ofstream file;
|
|
file.open("savefile.txt");
|
|
|
|
for(int i = 0; i < commands.size(); ++i){
|
|
file << commands[i] << endl;
|
|
}
|
|
|
|
file.close();
|
|
}
|
|
|
|
vector<Relation> DBEngine::getRelations(){
|
|
return tables;
|
|
}
|
|
|
|
Relation& DBEngine::getTableFromName(string n){
|
|
for(int i = 0; i < tables.size(); i++){
|
|
if (tables[i].getTableName() == n){
|
|
return tables[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
Relation DBEngine::selection(string attName, string s, Relation r){
|
|
equality(attName, s, r);
|
|
}
|
|
|
|
//assumes that all attribute titles are unique
|
|
Relation DBEngine::projection(vector<string> input, Relation r){
|
|
|
|
vector<Attribute> v;
|
|
string new_name = r.getTableName() + " Projection";
|
|
|
|
for(int i = 0; i < input.size(); ++i) {
|
|
|
|
for(int j = 0; j < r.getSize(); ++j) {
|
|
if((r.getAttributes())[j].getName() == input[i])
|
|
v.push_back((r.getAttributes())[j]);
|
|
}
|
|
}
|
|
|
|
Relation temp(new_name, v);
|
|
return temp;
|
|
}
|
|
|
|
Relation DBEngine::product(string new_name, Relation r1, Relation r2){
|
|
|
|
Relation temp(new_name);
|
|
vector<Attribute> a1;
|
|
|
|
for(int i = 0; i < r1.getAttributes().size(); ++i){
|
|
a1.push_back(r1.getAttributes()[i]);
|
|
}
|
|
|
|
for(int i = 0; i < r2.getAttributes().size(); ++i){
|
|
a1.push_back(r2.getAttributes()[i]);
|
|
}
|
|
|
|
temp.insertAttributes(a1);
|
|
|
|
vector<string> tuple1;
|
|
vector<string> tuple2;
|
|
vector<string> result_tuple;
|
|
|
|
return temp;
|
|
}
|
|
|
|
//test error matching
|
|
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
|
|
if (oldnames.size() != newnames.size()) {
|
|
cout << "FAILURE TO RENAME: number of attributes do not match.\n";
|
|
return;
|
|
}
|
|
|
|
else if (oldnames != r.getAttributeNames()) {
|
|
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
|
|
return;
|
|
}
|
|
|
|
else {
|
|
for(int i = 0; i < oldnames.size(); ++i){
|
|
r.renameAttribute(oldnames[i], newnames[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/*Relation DBEngine::setUnion(Relation r1, Relation r2){
|
|
if (r1.getAttributeNames() != r2.getAttributeNames()){
|
|
cout << "Failure to union: the relations are not union-compatible";
|
|
return;
|
|
}
|
|
|
|
else {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
//currently all returned relations are called TEMP
|
|
Relation new_r("TEMP", new_atts);
|
|
return new_r;
|
|
}
|
|
}*/
|