Update DBEngine.cpp
This commit is contained in:
parent
71961f6659
commit
4b1fbf077d
1 changed files with 78 additions and 2 deletions
80
DBEngine.cpp
80
DBEngine.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "Condition.h"
|
||||||
#include "DBEngine.h"
|
#include "DBEngine.h"
|
||||||
|
|
||||||
DBEngine::DBEngine(){
|
DBEngine::DBEngine(){
|
||||||
|
@ -28,6 +29,7 @@ vector<Relation> DBEngine::getRelations(){
|
||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Relation& DBEngine::getTableFromName(string n){
|
Relation& DBEngine::getTableFromName(string n){
|
||||||
for(int i = 0; i < tables.size(); i++){
|
for(int i = 0; i < tables.size(); i++){
|
||||||
if (tables[i].getTableName() == n){
|
if (tables[i].getTableName() == n){
|
||||||
|
@ -48,6 +50,11 @@ void DBEngine::saveToFile(vector<string> cmds){
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Relation DBEngine::selection(string attName, string s, Relation r){
|
||||||
|
equality(attName, s, r);
|
||||||
|
}
|
||||||
|
|
||||||
//assumes that all attribute titles are unique
|
//assumes that all attribute titles are unique
|
||||||
Relation DBEngine::projection(vector<string> input, Relation r){
|
Relation DBEngine::projection(vector<string> input, Relation r){
|
||||||
|
|
||||||
|
@ -66,15 +73,52 @@ Relation DBEngine::projection(vector<string> input, Relation r){
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
//Don't we need to find the bigger one to start first?
|
||||||
|
if(r1.getSize() >= r2.getSize())
|
||||||
|
{
|
||||||
|
//Combine tuples from relations into one, push back that tuple into the resultant relation via insertTuple
|
||||||
|
//Yeah have fun
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(r2.getSize() > r1.getSize())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
//test error matching
|
//test error matching
|
||||||
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
|
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
|
||||||
if (oldnames.size() != newnames.size()) {
|
if (oldnames.size() != newnames.size()) {
|
||||||
cout << "Failure to rename: number of attributes do not match.";
|
cout << "FAILURE TO RENAME: number of attributes do not match.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (oldnames != r.getAttributeNames()) {
|
else if (oldnames != r.getAttributeNames()) {
|
||||||
cout << "Failure to rename: the attributes to be renamed do not exist in the relation.";
|
cout << "FAILURE TO RENAME: the attributes to be renamed do not exist in the relation.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,3 +128,35 @@ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newna
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*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();
|
||||||
|
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);
|
||||||
|
return new_r;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
Reference in a new issue