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

282 lines
5.7 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
2015-09-28 17:02:04 -05:00
void DBEngine::insertValues(string r, vector<string> v)
{
for(int i = 0; i < tables.size(); i++)
{
if (tables[i].getTableName() == r)
{
tables[i].insertTuple(v);
}
}
}
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-28 16:18:49 -05:00
void DBEngine::deleteRelation(string n)
{
// to conserve memory after closing a file.
for(int i = 0; i < tables.size(); i++)
{
if (tables[i].getTableName() == n)
{
tables.erase(tables.begin() + i);
}
}
}
2015-09-28 16:18:49 -05:00
void DBEngine::save(string n){
ofstream file;
string name = n + ".txt";
file.open(name);
for(int i = 0; i < commands.size() - 1; ++i)
2015-09-28 17:02:04 -05:00
{
if(commands[i].substr(0, 4) == "SAVE")
{
continue;
}
if(commands[i].substr(0, 4) == "SHOW")
{
continue;
}
if(commands[i].substr(0, 4) == "OPEN")
{
continue;
}
if(commands[i].substr(0, 5) == "CLOSE")
{
continue;
}
else
{
file << commands[i] << endl;
}
2015-09-28 16:18:49 -05:00
}
2015-09-28 17:02:04 -05:00
//file.close();
2015-09-28 16:18:49 -05:00
}
2015-09-17 18:30:45 -05:00
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-28 15:57:57 -05:00
cout << "FAILURE TO FIND: could not locate a Relation with this name.";
return tables[0];
2015-09-17 18:30:45 -05:00
}
2015-09-26 23:24:24 -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);
2015-09-24 20:55:14 -05:00
return temp;
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
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;
2015-09-26 07:09:44 -05:00
new_r.setTableName("TEMP");
2015-09-26 23:24:24 -05:00
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;
2015-09-26 07:09:44 -05:00
}
}
2015-09-26 23:24:24 -05:00
2015-09-26 07:09:44 -05:00
Relation DBEngine::setDiff(Relation r1, Relation r2){
if (r1.getAttributeNames() != r2.getAttributeNames()){
cout << "Failure to diff: the relations are not union-compatible.\nreturning the first relation.\n";
return r1;
}
2015-09-26 23:24:24 -05:00
2015-09-26 07:09:44 -05:00
else {
//currently all returned relations are called TEMP
Relation new_r = r1;
new_r.setTableName("TEMP");
vector<string> temp;
bool duplicate = false;
2015-09-26 23:24:24 -05:00
2015-09-26 07:09:44 -05:00
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){
new_r.removeTuple(i);
}
2015-09-26 23:24:24 -05:00
2015-09-26 07:09:44 -05:00
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
temp = r1.getTuple(i);
2015-09-26 23:24:24 -05:00
2015-09-26 07:09:44 -05:00
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
if (temp == r2.getTuple(j)){
duplicate = true;
break;
}
2015-09-24 21:37:27 -05:00
}
2015-09-26 23:24:24 -05:00
2015-09-26 07:09:44 -05:00
if (!duplicate){
new_r.insertTuple(temp);
2015-09-24 21:37:27 -05:00
}
2015-09-26 07:09:44 -05:00
duplicate = false;
2015-09-24 21:37:27 -05:00
}
2015-09-26 07:09:44 -05:00
//make this one for loop later!
for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
temp = r2.getTuple(i);
for (int j = 0; j < r1.getAttributes()[0].getSize(); ++j){
if (temp == r1.getTuple(j)){
duplicate = true;
break;
2015-09-24 21:37:27 -05:00
}
}
2015-09-26 07:09:44 -05:00
if (!duplicate){
new_r.insertTuple(temp);
}
duplicate = false;
2015-09-24 21:37:27 -05:00
}
2015-09-26 07:09:44 -05:00
return new_r;
2015-09-24 20:02:57 -05:00
}
2015-09-24 21:00:31 -05:00
}
2015-09-26 07:09:44 -05:00
Relation DBEngine::crossProduct(Relation r1, Relation r2){
vector<Attribute> new_atts = r1.getAttributes();
2015-09-28 15:46:42 -05:00
int r1_attsize = r1.getAttributes()[0].getSize();
int r2_attsize = r2.getAttributes()[0].getSize();
2015-09-26 07:09:44 -05:00
2015-09-28 15:46:42 -05:00
for (int i = 0; i < r2_attsize; ++i) {
2015-09-26 07:09:44 -05:00
new_atts.push_back(r2.getAttributes()[i]);
}
2015-09-26 07:11:33 -05:00
for (int i = 0; i < new_atts.size(); ++i) {
new_atts[i].clearAllValues();
}
2015-09-26 07:09:44 -05:00
2015-09-28 16:03:41 -05:00
//currently all returned relations are called TEMP
Relation new_r("TEMP", new_atts);
2015-09-28 15:46:42 -05:00
vector<string> r1_tuple;
vector<string> r2_tuple;
2015-09-28 16:03:41 -05:00
vector<string> temp;
2015-09-28 15:46:42 -05:00
for (int i = 0; i < r1_attsize; ++i) {
r1_tuple = r1.getTuple(i);
for (int j = 0; j < r2_attsize; ++j) {
r2_tuple = r2.getTuple(j);
2015-09-28 16:03:41 -05:00
temp = r1_tuple;
temp.insert(temp.end(), r2_tuple.begin(), r2_tuple.end());
2015-09-28 15:46:42 -05:00
2015-09-28 16:03:41 -05:00
new_r.insertTuple(temp);
2015-09-28 15:46:42 -05:00
}
2015-09-24 20:02:57 -05:00
}
2015-09-26 07:09:44 -05:00
return new_r;
}