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
Alexander Huddleston 2d2e056731 Fixed github issues.
2015-09-30 20:08:29 -05:00

295 lines
5.9 KiB
C++
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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::insertValues(string r, vector<string> v)
{
for(int i = 0; i < tables.size(); i++)
{
if (tables[i].getTableName() == r)
{
tables[i].insertTuple(v);
}
}
}
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();
}
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);
}
}
}
void DBEngine::save(string n){
ofstream file;
string name = n + ".txt";
file.open(name);
for(int i = 0; i < commands.size() - 1; ++i)
{
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;
}
}
file.close();
}
vector<Relation> DBEngine::getRelations(){
return tables;
}
bool DBEngine::isRelation(string n)
{
for(int i = 0; i < tables.size(); i++)
{
if (tables[i].getTableName() == n)
{
return true;
}
}
return false;
}
Relation& DBEngine::getTableFromName(string n)
{
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return tables[i];
}
}
cout << "FAILURE TO FIND: could not locate a Relation with this name.";
return tables[0];
}
Relation DBEngine::selection(string attName, string s, Relation r){
return 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;
}
//test error matching
Relation DBEngine::rename(vector<string> newnames, Relation &r)
{
vector<string> temp;
if (r.getSize() != newnames.size()) {
cout << "FAILURE TO RENAME: number of attributes do not match.\n";
exit(1);
}
else
{
temp = r.getAttributeNames();
for(int i = 0; i < temp.size(); ++i)
{
r.renameAttribute(temp[i], newnames[i]);
}
return r;
}
}
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;
new_r.setTableName("TEMP");
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;
}
}
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;
}
else {
//currently all returned relations are called TEMP
Relation new_r = r1;
new_r.setTableName("TEMP");
vector<string> temp;
//bool duplicate = false;
int size = 0;
for(int x = 0; x < r2.getAttributes().size(); ++x)
{
for (int i = 0; i < r2.getAttributes()[x].getSize(); ++i)
{
temp = r2.getTuple(i);
for(int y = 0; y < new_r.getAttributes().size(); ++y)
{
size = new_r.getAttributes()[y].getSize();
new_r.getAttributes()[y].getSize();
for (int j = 0; j < size; ++j)
{
for(int a = 0; a < temp.size(); ++a)
{
for(int b = 0; b < new_r.getTuple(j).size(); ++b)
{
if (temp[a] == new_r.getTuple(j)[b])
{
new_r.removeFromTuple(b, j);
}
}
}
size = new_r.getAttributes()[y].getSize();
}
}
}
}
return new_r;
}
}
Relation DBEngine::crossProduct(Relation r1, Relation r2){
vector<Attribute> new_atts = r1.getAttributes();
int r1_attsize = r1.getAttributes()[0].getSize();
int r2_attsize = r2.getAttributes()[0].getSize();
for (int i = 0; i < r2_attsize; ++i) {
new_atts.push_back(r2.getAttributes()[i]);
}
for (int i = 0; i < new_atts.size(); ++i) {
new_atts[i].clearAllValues();
}
//currently all returned relations are called TEMP
Relation new_r("TEMP", new_atts);
vector<string> r1_tuple;
vector<string> r2_tuple;
vector<string> temp;
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);
temp = r1_tuple;
temp.insert(temp.end(), r2_tuple.begin(), r2_tuple.end());
new_r.insertTuple(temp);
}
}
return new_r;
}
//UPDATE Senator
//SET Party = Independent
//WHERE Name = Joseph Lieberman
Relation DBEngine::update(Relation r, string attNameSet, string attSet, string attNameWhere, string attWhere){
return r;
}