Updated DBEngine functions

This commit is contained in:
Rebecca Schofield 2015-09-17 18:30:45 -05:00
parent 58e37d41c3
commit 684cf1f811
8 changed files with 213 additions and 124 deletions

57
Attribute.cpp Executable file
View file

@ -0,0 +1,57 @@
#include <iostream>
#include <vector>
#include "Attribute.h"
Attribute::Attribute(string n, string t, bool k){
name = n;
type = t;
key = k;
size = 0;
}
void Attribute::addCell(string v){
values.push_back(v);
size++;
}
string Attribute::operator[](int i){
return values[i];
}
vector<string> Attribute::getValues(){
return values;
}
string Attribute::getName(){
return name;
}
string Attribute::getType(){
return type;
}
bool Attribute::isKey(){
return key;
}
void Attribute::setName(string s){
name = s;
}
//may need to change primary key implementation
int Attribute::getSize(){
return size;
}
void Attribute::display(){
cout << "-------------\n";
cout << name << "\n" << type << "\n\n";
vector<string>::iterator it = values.begin();
while (it != values.end()){
cout << *it << "\n";
it++;
}
cout << "-------------\n";
}

View file

@ -3,9 +3,6 @@
using namespace std;
//Funtional, might need more functionality
//template<typename T>
class Attribute{
vector<string> values;
string name;
@ -14,35 +11,14 @@ class Attribute{
int size;
public:
Attribute(string n, string t, bool k){
name = n;
type = t;
key = k;
size = 0;
}
void addCell(string v){
values.push_back(v);
size++;
}
string operator[](int i){ return values[i]; }
vector<string> getValues(){ return values; }
string getName(){ return name; }
string getType(){ return type; }
bool isKey(){ return key; }
int getSize(){ return size; } //may need to change primary key implementation
void display(){
cout << "-------------\n";
cout << name << "\n" << type << "\n\n";
vector<string>::iterator it = values.begin();
while (it != values.end()){
cout << *it << "\n";
it++;
}
cout << "-------------\n";
}
Attribute(string n, string t, bool k);
void addCell(string v);
string operator[](int i);
vector<string> getValues();
string getName();
string getType();
bool isKey();
int getSize();
void setName(string s);
void display();
};

70
DBEngine.cpp Executable file
View file

@ -0,0 +1,70 @@
#include <fstream>
#include <iostream>
#include <vector>
#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++;
}
vector<Relation> DBEngine::getRelations(){
return tables;
}
Relation DBEngine::getTableFromName(string n){
//will return first occurence
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return tables[i];
}
}
}
void DBEngine::saveToFile(vector<string> cmds){
//writes nothing meaningful
ofstream file;
file.open("savefile.db");
for(int i = 0; i < cmds.size(); ++i){
file << cmds[i] << endl;
}
file.close();
}
//assumes that all attribute titles are unique
Relation DBEngine::projection(vector<string> input, Relation r){
// for(int i = 0; i < input.size(); i++) {
// it = find(r.getAttributes().begin(), r.getAttributes().end(), input[i])
//if(r[i].getName == input[])
// }
}
/*
void renameAttribute(vector<Attribute> v, string o, string s){
for(int i = 0; i < v.size(); ++i){
if(v[i].getName() == o){
v[i].setName(s);
}
}
}
*/

View file

@ -9,48 +9,20 @@ class DBEngine {
int size;
public:
DBEngine(){
size = 0;
}
DBEngine();
void createTable(string n);
void createTable(string n, vector<Attribute> a);
void createTable(Relation r);
vector<Relation> getRelations();
//void showTable(Relation r);
Relation getTableFromName(string n);
void saveToFile(vector<string> cmds);
void createTable(string n) {
Relation r(n);
tables.push_back(r);
size++;
}
void createTable(string n, vector<Attribute> a) {
Relation r(n, a);
tables.push_back(r);
size++;
}
void createTable(Relation r){
tables.push_back(r);
size++;
}
vector<Relation> getRelations(){ return tables; }
//void showTable(Relation r){}
Relation getTableFromName(string n){
//will return first occurence
for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){
return tables[i];
}
}
}
void saveToFile(vector<string> cmds){
//writes nothing meaningful
ofstream file;
file.open("savefile.db");
for(int i = 0; i < cmds.size(); ++i){
file << cmds[i] << endl;
}
file.close();
}
//operations
//void selection();
Relation projection(vector<string> input, Relation r);
//void renaming();
//void setUnion();
//void setDiff();
//void crossProduct();
};

44
Relation.cpp Executable file
View file

@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include "Relation.h"
Relation::Relation(string n){
name = n;
size = 0;
}
Relation::Relation(string n, vector<Attribute> a){
name = n;
att = a;
size = a.size();
}
string Relation::getTableName(){
return name;
}
vector<Attribute> Relation::getAttributes(){
return att;
}
vector<string> Relation::getAttributeNames(){
vector<string> temp;
for(int i = 0; i < size; ++i){
temp.push_back(att[i].getName());
}
return temp;
}
int Relation::getSize(){
return size;
}
void Relation::display(){
cout << "--------------------------\n";
cout << name << "\n";
for (int i = 0; i < att.size(); ++i){
att[i].display();
}
cout << "--------------------------\n";
}

View file

@ -2,53 +2,17 @@
#include <vector>
#include "Attribute.h"
//Functional
class Relation{
string name; //The title the user gives it
vector<Attribute> att; //A vector of the columns
string name;
vector<Attribute> att;
int size;
public:
Relation(string n) {
name = n;
size = 0;
}
Relation(string n, vector<Attribute> a) {
name = n;
att = a;
size = a.size();
}
//addAttribute
string getTableName() { return name; }
vector<Attribute> getAttributes() { return att; }
int getSize() { return size; }
//assumes that all attribute titles are unique
void projectQuery(string input) {
cout << "-----------Initiated Query Projection---------" << endl;
for(int i = 0; i < att.size(); i++) {
if(att[i].getName() == input) {
cout << "Column Title: " << input << endl;
for(int j = 0; j < att[i].getSize(); j++) {
cout << att[i].getValues()[j] << endl;
}
break;
}
else
cout << "Attribute input not valid" << endl;
}
}
void display() {
cout << "--------------------------\n";
cout << name << "\n";
for (int i = 0; i < att.size(); ++i){
att[i].display();
}
cout << "--------------------------\n";
}
Relation(string n);
Relation(string n, vector<Attribute> a);
string getTableName();
vector<Attribute> getAttributes();
vector<string> getAttributeNames();
int getSize();
void display();
};

BIN
a.out

Binary file not shown.

View file

@ -35,4 +35,10 @@ int main() {
//vector<Relation> rs = engine.getRelations();
Relation r2 = engine.getTableFromName("Food");
r2.display();
vector<string> v1 = r2.getAttributeNames();
for (int i = 0; i < v.size(); ++i){
cout << v1[i];
}
}