Updated DBEngine functions
This commit is contained in:
parent
58e37d41c3
commit
684cf1f811
8 changed files with 213 additions and 124 deletions
57
Attribute.cpp
Executable file
57
Attribute.cpp
Executable 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";
|
||||||
|
}
|
44
Attribute.h
44
Attribute.h
|
@ -3,9 +3,6 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//Funtional, might need more functionality
|
|
||||||
|
|
||||||
//template<typename T>
|
|
||||||
class Attribute{
|
class Attribute{
|
||||||
vector<string> values;
|
vector<string> values;
|
||||||
string name;
|
string name;
|
||||||
|
@ -14,35 +11,14 @@ class Attribute{
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Attribute(string n, string t, bool k){
|
Attribute(string n, string t, bool k);
|
||||||
name = n;
|
void addCell(string v);
|
||||||
type = t;
|
string operator[](int i);
|
||||||
key = k;
|
vector<string> getValues();
|
||||||
size = 0;
|
string getName();
|
||||||
}
|
string getType();
|
||||||
|
bool isKey();
|
||||||
void addCell(string v){
|
int getSize();
|
||||||
values.push_back(v);
|
void setName(string s);
|
||||||
size++;
|
void display();
|
||||||
}
|
|
||||||
|
|
||||||
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";
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
70
DBEngine.cpp
Executable file
70
DBEngine.cpp
Executable 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
60
DBEngine.h
60
DBEngine.h
|
@ -4,53 +4,25 @@
|
||||||
#include "Relation.h"
|
#include "Relation.h"
|
||||||
|
|
||||||
//still in progress
|
//still in progress
|
||||||
class DBEngine {
|
class DBEngine{
|
||||||
vector<Relation> tables;
|
vector<Relation> tables;
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DBEngine(){
|
DBEngine();
|
||||||
size = 0;
|
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) {
|
//operations
|
||||||
Relation r(n);
|
//void selection();
|
||||||
tables.push_back(r);
|
Relation projection(vector<string> input, Relation r);
|
||||||
size++;
|
//void renaming();
|
||||||
}
|
//void setUnion();
|
||||||
|
//void setDiff();
|
||||||
void createTable(string n, vector<Attribute> a) {
|
//void crossProduct();
|
||||||
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();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
44
Relation.cpp
Executable file
44
Relation.cpp
Executable 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";
|
||||||
|
}
|
56
Relation.h
56
Relation.h
|
@ -2,53 +2,17 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Attribute.h"
|
#include "Attribute.h"
|
||||||
|
|
||||||
//Functional
|
class Relation{
|
||||||
class Relation {
|
string name;
|
||||||
string name; //The title the user gives it
|
vector<Attribute> att;
|
||||||
vector<Attribute> att; //A vector of the columns
|
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Relation(string n) {
|
Relation(string n);
|
||||||
name = n;
|
Relation(string n, vector<Attribute> a);
|
||||||
size = 0;
|
string getTableName();
|
||||||
}
|
vector<Attribute> getAttributes();
|
||||||
Relation(string n, vector<Attribute> a) {
|
vector<string> getAttributeNames();
|
||||||
name = n;
|
int getSize();
|
||||||
att = a;
|
void display();
|
||||||
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";
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
BIN
a.out
BIN
a.out
Binary file not shown.
6
test.cpp
6
test.cpp
|
@ -35,4 +35,10 @@ int main() {
|
||||||
//vector<Relation> rs = engine.getRelations();
|
//vector<Relation> rs = engine.getRelations();
|
||||||
Relation r2 = engine.getTableFromName("Food");
|
Relation r2 = engine.getTableFromName("Food");
|
||||||
r2.display();
|
r2.display();
|
||||||
|
|
||||||
|
vector<string> v1 = r2.getAttributeNames();
|
||||||
|
|
||||||
|
for (int i = 0; i < v.size(); ++i){
|
||||||
|
cout << v1[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue