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

295 lines
5.9 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-30 13:00:51 -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;
}
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)
{
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){
return equality(attName, s, r);
2015-09-26 23:24:24 -05:00
}
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-29 13:28:24 -05:00
for(int i = 0; i < input.size(); ++i)
{
for(int j = 0; j < r.getSize(); ++j)
{
2015-09-23 16:08:19 -05:00
if((r.getAttributes())[j].getName() == input[i])
2015-09-29 13:28:24 -05:00
{
2015-09-23 16:08:19 -05:00
v.push_back((r.getAttributes())[j]);
2015-09-29 13:28:24 -05:00
}
2015-09-23 16:08:19 -05:00
}
}
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-29 13:55:47 -05:00
Relation DBEngine::rename(vector<string> newnames, Relation &r)
{
vector<string> temp;
if (r.getSize() != newnames.size()) {
2015-09-24 21:37:27 -05:00
cout << "FAILURE TO RENAME: number of attributes do not match.\n";
2015-09-29 13:55:47 -05:00
exit(1);
2015-09-21 16:27:23 -05:00
}
2015-09-29 13:55:47 -05:00
else
{
temp = r.getAttributeNames();
for(int i = 0; i < temp.size(); ++i)
{
r.renameAttribute(temp[i], newnames[i]);
}
return r;
}
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;
int size = 0;
2015-09-30 16:54:56 -05:00
for(int x = 0; x < r2.getAttributes().size(); ++x)
{
for (int i = 0; i < r2.getAttributes()[x].getSize(); ++i)
2015-09-30 16:50:25 -05:00
{
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();
}
2015-09-26 07:09:44 -05:00
}
2015-09-24 21:37:27 -05:00
}
2015-09-30 16:50:25 -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;
2015-09-30 16:54:56 -05:00
}
//UPDATE Senator
//SET Party = Independent
//WHERE Name = Joseph Lieberman
Relation DBEngine::update(Relation r, string attNameSet, string attSet, string attNameWhere, string attWhere){
return r;
}