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

209 lines
4.3 KiB
C++
Raw Normal View History

2015-09-17 18:30:45 -05:00
#include <fstream>
#include <iostream>
#include <vector>
2015-09-24 21:37:27 -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++;
}
2015-09-25 00:31:23 -05:00
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();
}
2015-09-17 18:30:45 -05:00
vector<Relation> DBEngine::getRelations(){
return tables;
}
2015-09-24 21:37:27 -05:00
2015-09-26 23:24:24 -05:00
<<<<<<< HEAD
=======
>>>>>>> master
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-26 23:24:24 -05:00
Relation DBEngine::selection(string attName, string s, Relation r){
equality(attName, s, r);
}
2015-09-24 21:37:27 -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){
2015-09-23 16:08:19 -05:00
vector<Attribute> v;
string new_name = r.getTableName() + " Projection";
2015-09-23 16:08:19 -05:00
for(int i = 0; i < input.size(); ++i) {
2015-09-17 18:30:45 -05:00
2015-09-23 16:08:19 -05:00
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;
2015-09-26 23:24:24 -05:00
<<<<<<< HEAD
2015-09-17 18:30:45 -05:00
}
2015-09-21 16:27:23 -05:00
2015-09-24 21:37:27 -05:00
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;
2015-09-26 23:24:24 -05:00
=======
>>>>>>> master
2015-09-24 21:37:27 -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-24 21:37:27 -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-24 21:37:27 -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-23 16:08:19 -05:00
}
2015-09-24 21:37:27 -05:00
2015-09-26 23:24:24 -05:00
<<<<<<< HEAD
2015-09-25 00:31:23 -05:00
2015-09-24 21:37:27 -05:00
/*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();
2015-09-26 23:24:24 -05:00
=======
Relation DBEngine::setUnion(Relation r1, Relation r2){
if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to union: the relations are not union-compatible.\nreturning the first relation.\n";
return r1;
}
else {
//currently all returned relations are called TEMP
Relation new_r = r1;
vector<string> temp;
bool duplicate = false;
for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
temp = r2.getTuple(i);
for (int j = 0; j < new_r.getAttributes()[0].getSize(); ++j){
if (temp == new_r.getTuple(j)){
duplicate = true;
break;
}
}
if (!duplicate) {
new_r.insertTuple(temp);
}
duplicate = false;
}
return new_r;
/*vector<Attribute> r1_atts = r1.getAttributes();
>>>>>>> master
2015-09-24 21:37:27 -05:00
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);
2015-09-26 23:24:24 -05:00
<<<<<<< HEAD
2015-09-24 21:37:27 -05:00
return new_r;
}
}*/
2015-09-26 23:24:24 -05:00
=======
return new_r;*/
}
}
>>>>>>> master