243 lines
No EOL
5.1 KiB
C++
Executable file
243 lines
No EOL
5.1 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::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();
|
|
}
|
|
|
|
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];
|
|
}
|
|
}
|
|
|
|
cout << "FAILURE TO FIND: could not locate a Relation with this name.";
|
|
return tables[0];
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
//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.\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;
|
|
|
|
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){
|
|
new_r.removeTuple(i);
|
|
}
|
|
|
|
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
|
|
temp = r1.getTuple(i);
|
|
|
|
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
|
|
if (temp == r2.getTuple(j)){
|
|
duplicate = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!duplicate){
|
|
new_r.insertTuple(temp);
|
|
}
|
|
|
|
duplicate = false;
|
|
}
|
|
|
|
//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;
|
|
}
|
|
}
|
|
|
|
if (!duplicate){
|
|
new_r.insertTuple(temp);
|
|
}
|
|
|
|
duplicate = false;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
vector<string> r1_tuple;
|
|
vector<string> r2_tuple;
|
|
|
|
for (int i = 0; i < r1_attsize; ++i) {
|
|
r1_tuple = r1.getTuple(i);
|
|
|
|
for (int a = 0; a < r1_tuple.size(); ++a) {
|
|
cout << r1_tuple[a] << " ";
|
|
}
|
|
|
|
for (int j = 0; j < r2_attsize; ++j) {
|
|
r2_tuple = r2.getTuple(j);
|
|
|
|
for (int b = 0; b < r2_tuple.size(); ++b){
|
|
cout << r2_tuple[b] << " ";
|
|
}
|
|
|
|
cout << "\n";
|
|
}
|
|
|
|
cout << "\n\n";
|
|
}
|
|
|
|
//currently all returned relations are called TEMP
|
|
Relation new_r("TEMP", new_atts);
|
|
|
|
|
|
return new_r;
|
|
} |