synced with becca
This commit is contained in:
commit
61cb0d3b1f
8 changed files with 301 additions and 249 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";
|
||||
}
|
75
Attribute.h
Normal file → Executable file
75
Attribute.h
Normal file → Executable file
|
@ -3,67 +3,22 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
//Funtional, might need more functionality
|
||||
|
||||
//template<typename T>
|
||||
class Attribute {
|
||||
//a named column of a relation
|
||||
class Attribute{
|
||||
vector<string> values;
|
||||
string name;
|
||||
bool isKey;
|
||||
string type;
|
||||
bool key;
|
||||
int size;
|
||||
|
||||
public:
|
||||
|
||||
vector<string> values;
|
||||
|
||||
void initializeAttribute(string n, vector<string> a){
|
||||
|
||||
name = n;
|
||||
values = a;
|
||||
}
|
||||
|
||||
string getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
Attribute(){ }
|
||||
|
||||
void display()
|
||||
{
|
||||
//cout<<"\nAtribute name:\t"<<name<<"\n";
|
||||
//cout<<"Elements: ";
|
||||
for (int i = 0; i < values.size(); ++i)
|
||||
{
|
||||
cout<<values[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
int getSize()
|
||||
{
|
||||
return values.size();
|
||||
}
|
||||
|
||||
void pushBack(string s)
|
||||
{
|
||||
values.push_back(s);
|
||||
}
|
||||
/*
|
||||
Attribute(vector<string> v){
|
||||
this.values = v;
|
||||
}
|
||||
|
||||
Attribute(const Attribute<string>& a){
|
||||
this.values = a.getAll();
|
||||
}
|
||||
|
||||
Attribute& operator=(const Attribute<string>& a){
|
||||
this.values = a.getAll();
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<string> getAll(){
|
||||
return this.values;
|
||||
}
|
||||
*/
|
||||
};
|
||||
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();
|
||||
};
|
111
DBEngine.cpp
111
DBEngine.cpp
|
@ -1,73 +1,70 @@
|
|||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "DBEngine.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
DBEngine::DBEngine(){
|
||||
//
|
||||
size = 0;
|
||||
}
|
||||
|
||||
//create a new table in memory
|
||||
void DBEngine::createCmd(){
|
||||
//
|
||||
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++;
|
||||
}
|
||||
|
||||
//open a txt file, parse SQL script, load data in table
|
||||
//void DBEngine::openCmd(){
|
||||
//
|
||||
//}
|
||||
|
||||
//should write cmdList to a .txt file
|
||||
void DBEngine::saveCmd(){
|
||||
//
|
||||
void DBEngine::createTable(Relation r){
|
||||
tables.push_back(r);
|
||||
size++;
|
||||
}
|
||||
|
||||
//display the database
|
||||
void DBEngine::showCmd(){
|
||||
//
|
||||
}
|
||||
|
||||
//add a tuple to a table in the memory
|
||||
void DBEngine::insertQuery(){
|
||||
//
|
||||
}
|
||||
|
||||
//remove a tuple from a table in the memory
|
||||
void DBEngine::deleteQuery(){
|
||||
//
|
||||
}
|
||||
|
||||
//search and return one more tuples from a table in the memory
|
||||
void DBEngine::selectQuery(){
|
||||
//
|
||||
}
|
||||
|
||||
//return a subset of attributes (columns)
|
||||
/*
|
||||
vector<string> DBEngine::projectQuery(Relation table, string query){
|
||||
|
||||
/*
|
||||
So basically this is what's going on:
|
||||
- Take the table to iterate through, this is the relation (input 1)
|
||||
- Take a string that will be used for the program to search for in the attributes (input 2)
|
||||
- Iterate through each attribute until the attribute (string) matches the input query
|
||||
- For every iterator, have a counter that counts how many attributes are skipped over in the vector until the attribute is found
|
||||
- This counter will be used to iterate through each vector (row) in the list, skip over that many attributes, and push that into a result vector
|
||||
- Once all vector's elements at the specified attribute are pushed back, return the result vector
|
||||
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 < table.getSize(); i++) {
|
||||
|
||||
|
||||
for(int i = 0; i < cmds.size(); ++i){
|
||||
file << cmds[i] << endl;
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
//each row in the first table is paired with all the rows in the second table
|
||||
void DBEngine::productQuery(){
|
||||
//
|
||||
file.close();
|
||||
}
|
||||
|
||||
//true if relations have the same # of attributes and each attribute must be from the same domain
|
||||
bool DBEngine::unionComp(){
|
||||
return false;
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
35
DBEngine.h
35
DBEngine.h
|
@ -3,23 +3,26 @@
|
|||
#include <vector>
|
||||
#include "Relation.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class DBEngine {
|
||||
//member variables
|
||||
//NOT DONE
|
||||
//vector<Relation> tables;
|
||||
//still in progress
|
||||
class DBEngine{
|
||||
vector<Relation> tables;
|
||||
int size;
|
||||
|
||||
public:
|
||||
DBEngine();
|
||||
void createCmd();
|
||||
//void openCmd();
|
||||
void saveCmd();
|
||||
void showCmd();
|
||||
void insertQuery();
|
||||
void deleteQuery();
|
||||
void selectQuery();
|
||||
void projectQuery();
|
||||
void productQuery();
|
||||
bool unionComp();
|
||||
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);
|
||||
|
||||
//operations
|
||||
//void selection();
|
||||
Relation projection(vector<string> input, Relation r);
|
||||
//void renaming();
|
||||
//void setUnion();
|
||||
//void setDiff();
|
||||
//void crossProduct();
|
||||
};
|
||||
|
|
70
OUTPUT.txt
Executable file
70
OUTPUT.txt
Executable file
|
@ -0,0 +1,70 @@
|
|||
Assuming previously defined Attribute, compiled in a vector known as 'vec' and with subsequent numbers added
|
||||
|
||||
To create a table:
|
||||
|
||||
engine.createTable("table1", vec);
|
||||
|
||||
|
||||
This creates a Relation object with the appropriate Attribute objects. It displays nothing.
|
||||
|
||||
To display the table:
|
||||
|
||||
engine.showTables(engine.getTableFromName("table1"));
|
||||
|
||||
|
||||
This results in such output:
|
||||
|
||||
Display of relation--------------------------------
|
||||
Relation name: table1
|
||||
|
||||
Attribute name: name
|
||||
Elements: Fry Bender Leela Zoidberg
|
||||
Attribute name: age
|
||||
Elements: 22 5 22 50
|
||||
|
||||
|
||||
With table3 having an equal domain to table1, this:
|
||||
|
||||
cout << engine.unionComp(engine.getTableFromName("table1"), engine.getTableFromName("table3"));
|
||||
|
||||
|
||||
This will display:
|
||||
|
||||
1
|
||||
|
||||
|
||||
To project a table's column:
|
||||
|
||||
engine.project((engine.getTableFromName("table1")), "name");
|
||||
|
||||
|
||||
This will display:
|
||||
|
||||
-----------Initiated Query Projection---------
|
||||
Column Title: name
|
||||
Fry
|
||||
Bender
|
||||
Leela
|
||||
Zoidberg
|
||||
|
||||
|
||||
With an arbitrary vector of strings as cmds (until we are able to parse effectively):
|
||||
|
||||
engine.saveToFile(cmds);
|
||||
|
||||
|
||||
This will result in a db file with the contents:
|
||||
|
||||
CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);
|
||||
|
||||
SHOW animals;
|
||||
|
||||
|
||||
|
||||
//insert
|
||||
|
||||
//delete
|
||||
|
||||
//select
|
||||
|
||||
//product
|
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";
|
||||
}
|
75
Relation.h
Normal file → Executable file
75
Relation.h
Normal file → Executable file
|
@ -2,70 +2,17 @@
|
|||
#include <vector>
|
||||
#include "Attribute.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//NOT DONE
|
||||
class Relation {
|
||||
//a table with rows and columns
|
||||
string name; //the name of the relation (table)
|
||||
class Relation{
|
||||
string name;
|
||||
vector<Attribute> att;
|
||||
vector<string> attributeNames;
|
||||
int size;
|
||||
|
||||
public:
|
||||
//Relation();
|
||||
|
||||
void initializeRelation(string n, vector<string> attNames, vector<Attribute> a)
|
||||
{
|
||||
attributeNames = attNames;
|
||||
name = n;
|
||||
att = a;
|
||||
}
|
||||
|
||||
void addTuple(vector< string > tuple) {
|
||||
|
||||
|
||||
if(tuple.size() != att.size()){
|
||||
cout << "\n ERROR" << endl;
|
||||
}
|
||||
else {
|
||||
//Loop through the attribute columns
|
||||
for(int i = 0; i < att.size(); i++) {
|
||||
att[i].pushBack(tuple[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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].values[j] << endl;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
cout << "Attribute input not valid" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void display()
|
||||
{
|
||||
cout<<"\n\nDisplay of relation--------------------------------"<<endl;
|
||||
cout<<"Relation name: "<<name<<endl;
|
||||
for (int i = 0; i < attributeNames.size(); ++i)
|
||||
{
|
||||
|
||||
cout<<"\nAttribute name: "<<attributeNames[i]<<": ";
|
||||
|
||||
att[i].display();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
Relation(string n);
|
||||
Relation(string n, vector<Attribute> a);
|
||||
string getTableName();
|
||||
vector<Attribute> getAttributes();
|
||||
vector<string> getAttributeNames();
|
||||
int getSize();
|
||||
void display();
|
||||
};
|
83
test.cpp
83
test.cpp
|
@ -1,65 +1,44 @@
|
|||
#include <iostream>
|
||||
//#include "DBEngine.h"
|
||||
#include <vector>
|
||||
//#include "Attribute.h"
|
||||
#include "Relation.h"
|
||||
#include "DBEngine.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//still in progress
|
||||
int main() {
|
||||
|
||||
/*
|
||||
DBEngine engine;
|
||||
Relation r;
|
||||
Attribute<int> a;
|
||||
*/
|
||||
Attribute att1("Breakfast", "VARCHAR(20)", true);
|
||||
Attribute att2("Lunch", "VARCHAR(20)", false);
|
||||
Attribute att3("Dinner", "VARCHAR(20)", false);
|
||||
|
||||
vector<string> shamWow;
|
||||
shamWow.push_back("rag");
|
||||
shamWow.push_back("sponge");
|
||||
shamWow.push_back("wooow");
|
||||
shamWow.push_back("cloth");
|
||||
att1.addCell("Pancakes");
|
||||
att1.addCell("Waffles");
|
||||
att1.addCell("Biscuits");
|
||||
att2.addCell("Turkey Sandwich");
|
||||
att2.addCell("Caesar Salad");
|
||||
att2.addCell("Pizza");
|
||||
att3.addCell("Steak");
|
||||
att3.addCell("Shrimp");
|
||||
att3.addCell("Ribs");
|
||||
|
||||
Attribute atributo;
|
||||
atributo.initializeAttribute("atributo",shamWow);
|
||||
vector<Attribute> v;
|
||||
v.push_back(att1);
|
||||
v.push_back(att2);
|
||||
v.push_back(att3);
|
||||
|
||||
atributo.display();
|
||||
Relation r("AltFood", v);
|
||||
//r.display();
|
||||
|
||||
vector<string> doom;
|
||||
doom.push_back("zombieman");
|
||||
doom.push_back("revenant");
|
||||
doom.push_back("imp");
|
||||
doom.push_back("archvile");
|
||||
engine.createTable("Food", v);
|
||||
engine.createTable(r);
|
||||
engine.createTable("Sadness");
|
||||
//vector<Relation> rs = engine.getRelations();
|
||||
Relation r2 = engine.getTableFromName("Food");
|
||||
r2.display();
|
||||
|
||||
Attribute atributo2;
|
||||
atributo2.initializeAttribute("atributo2",doom);
|
||||
vector<string> v1 = r2.getAttributeNames();
|
||||
|
||||
atributo2.display();
|
||||
|
||||
vector<Attribute> setOfAttributes;
|
||||
setOfAttributes.push_back(atributo);
|
||||
setOfAttributes.push_back(atributo2);
|
||||
|
||||
vector<string> attNames;
|
||||
attNames.push_back("attName1");
|
||||
attNames.push_back("attName2");
|
||||
|
||||
Relation myLittleRelation;
|
||||
myLittleRelation.initializeRelation("randomName", attNames, setOfAttributes);
|
||||
|
||||
|
||||
//adding tuple business-----------------------
|
||||
|
||||
vector<string> tuple;
|
||||
tuple.push_back("hadoken");
|
||||
tuple.push_back("soryuken");
|
||||
|
||||
myLittleRelation.addTuple(tuple);
|
||||
|
||||
myLittleRelation.display();
|
||||
|
||||
string input_query = "atributo";
|
||||
cout << "\nThe attempted input query is: " << input_query << endl;
|
||||
|
||||
myLittleRelation.projectQuery(input_query);
|
||||
}
|
||||
for (int i = 0; i < v.size(); ++i){
|
||||
cout << v1[i];
|
||||
}
|
||||
}
|
Reference in a new issue