Merge branch 'master' into alexdev

This commit is contained in:
Alexander Huddleston 2015-09-16 16:59:22 -05:00
commit 091ac01645
7 changed files with 402 additions and 132 deletions

View file

@ -5,33 +5,46 @@ using namespace std;
//Funtional, might need more functionality //Funtional, might need more functionality
template<typename T> //template<typename T>
class Attribute { class Attribute {
//a named column of a relation vector<string> values;
string name; string name;
vector<T> values; string type;
bool isKey; bool key;
int size; int size;
public: public:
Attribute(){ } Attribute(string n, string t, bool k){
name = n;
Attribute(vector<T> v){ type = t;
this.values = v; key = k;
size = 0;
} }
Attribute(const Attribute<T>& a){ void addRow(string v) {
this.values = a.getAll(); values.push_back(v);
size++;
} }
Attribute& operator=(const Attribute<T>& a){ string getElementAt(int pos)
this.values = a.getAll(); {
return values[pos];
} }
string getName(){ vector<string> getValues() { return values; }
return name; string getName(){ return name; }
} string getType(){ return type; }
bool isKey(){ return key; }
int getSize(){ return size; }
vector<T> getAll(){ void display()
return this.values; {
cout<<"Attribute name:\t"<< name <<"\n";
cout<<"Elements: ";
for (int i = 0; i < values.size(); ++i)
{
cout<<values[i]<<" ";
}
} }
}; };

View file

@ -1,57 +0,0 @@
#include "DBEngine.h"
using namespace std;
DBEngine::DBEngine(){
//
}
//create a new table in memory
void DBEngine::createCmd(){
//
}
//open a txt file, parse SQL script, load data in table
//void DBEngine::openCmd(){
//
//}
//should write cmdList to a .txt file
void DBEngine::saveCmd(){
//
}
//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)
void DBEngine::projectQuery(){
//
}
//each row in the first table is paired with all the rows in the second table
void DBEngine::productQuery(){
//
}
//true if relations have the same # of attributes and each attribute must be from the same domain
bool DBEngine::unionComp(){
return false;
}

View file

@ -3,23 +3,78 @@
#include <vector> #include <vector>
#include "Relation.h" #include "Relation.h"
using namespace std; //still in progress
class DBEngine { class DBEngine {
//member variables vector<Relation> tables;
//NOT DONE int size;
//vector<Relation> tables;
public: public:
DBEngine(); DBEngine(){
void createCmd(); size = 0;
//void openCmd(); }
void saveCmd();
void showCmd(); void createTable(string n, vector<Attribute> a) {
void insertQuery(); Relation r(n, a);
void deleteQuery(); tables.push_back(r);
void selectQuery(); }
void projectQuery();
void productQuery(); void createTable(Relation r) { tables.push_back(r); }
bool unionComp(); vector<Relation> getRelations() { return tables; }
void showTable(Relation r) { r.display(); }
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) {
ofstream file;
file.open("savefile.db");
for(int i = 0; i < cmds.size(); ++i){
file << cmds[i] << endl;
}
file.close();
}
//void insertTuple(Relation r, vector<string> t) { r.addTuple(t); }
//need to add find by name
/*void deleteTuple(Relation r, int n) {
cout << "a";
r.removeTuple(n); }*/
void selectTuples() {
//
}
void project(Relation r, string n) { r.projectQuery(n); }
/*
Relation product(string p_name, Relation table_1, Relation table_2) {
vector<Attribute> all_att;
//Insert table 1 attributes
all_att.insert(all_att.begin(), (table_1.getAttributes()).begin(), (table_1.getAttributes()).end());
//Insert table 2 attributes
all_att.insert(all_att.begin(), (table_2.getAttributes()).begin(), (table_2.getAttributes()).end());
Relation temp(p_name, all_att);
vector<string> table1_stuff;
return temp;
}
*/
bool unionComp(Relation r1, Relation r2) {
return ((r1.getSize() == r2.getSize()) && (r1.getDomains() == r2.getDomains()));
}
}; };

68
OUTPUT.txt Executable file
View file

@ -0,0 +1,68 @@
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 text 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

View file

@ -2,15 +2,120 @@
#include <vector> #include <vector>
#include "Attribute.h" #include "Attribute.h"
using namespace std; //Functional
//NOT DONE
class Relation { class Relation {
//a table with rows and columns string name; //The title the user gives it
string name; vector<Attribute> att; //A vector of the columns
vector< Attribute<T> > att; int size;
public: public:
Relation(); Relation(string n, vector<Attribute> a) {
Relation(vector< Attribute<T> > a) { att = a; } name = n;
void addTuple(vector< Attribute<T> > tuple); att = a;
size = a.size();
}
int getSize() { return size; }
/*void addTuple(vector<string> tuple) {
//Loop through the attribute columns
for(int i = 0; i < att.size(); i++) {
//Loop through the elements in the i'th column
for(int j = 0; j < att[i].getValues().size(); j++){
//In this column, at this element's spot, assign an element from the tuple vector to this spot
att[i].addRow(tuple[i]);
size++;
}
}
}
void removeTuple(int tupleNum) {
if (tupleNum > att[0].getSize() || tupleNum < 0)
{
cout<<"ERROR! index out of bound"<<endl;
}
else
{
cout << "a" + att.size();
for(int i = 0; i < att.size(); ++i) //for all the attributes
{
cout << "SSSSSSSSSSSSSSSSSSSSSSSSSS" + tupleNum;
att[i].getValues().erase(att[i].getValues().begin()+ tupleNum);
}
}
}
vector<int> findTuple(string attributeType, string type)
{
vector<int> tupleSlot; // tuples that have the attribute in question
for (int i = 0; i < att.size(); ++i)// find attribute in question
{
if(att[i].getName() == attributeType)
{
for (int j = 0; j < att[i].getSize(); ++j)//search through all the values of the attribute column
{
if (att[i].getElementAt(j) == type)
{
tupleSlot.push_back(j);
}
}
}
}
return tupleSlot;
}*/
string getTableName() {
return name;
}
void displayTableName() {
cout << "The table name is: " << name << endl;
}
vector<Attribute> getAttributes() {
return att;
}
//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<<"\nDisplay of relation--------------------------------"<<endl;
cout<<"Relation name: "<<name<<endl;
for (int i = 0; i < size; ++i)
{
cout<<"\n";
att[i].display();
}
}
//make this better
vector<string> getDomains() {
vector<string> ds;
for (int i = 0; i < size; ++i)
{
ds.push_back(att[i].getType());
}
return ds;
}
}; };

BIN
a.out Executable file

Binary file not shown.

View file

@ -1,10 +1,96 @@
#include <iostream> #include <iostream>
#include <vector>
#include "DBEngine.h" #include "DBEngine.h"
using namespace std; using namespace std;
//still in progress
int main() { int main() {
DBEngine engine; DBEngine engine;
Relation r;
Attribute<int> a; /*Attribute att1("shamWow", "VARCHAR(10)", true);
att1.addRow("rag");
att1.addRow("sponge");
att1.addRow("wooow");
att1.addRow("cloth");
Attribute att2("doom", "VARCHAR(20)", false);
att2.addRow("zombieman");
att2.addRow("revenant");
att2.addRow("imp");
att2.addRow("archvile");
vector<Attribute> vec;
vec.push_back(att1);
vec.push_back(att2);*/
Attribute att3("name", "VARCHAR(20)", true);
att3.addRow("Fry");
att3.addRow("Bender");
att3.addRow("Leela");
att3.addRow("Zoidberg");
Attribute att4("age", "INTEGER", false);
att4.addRow("22");
att4.addRow("5");
att4.addRow("22");
att4.addRow("50");
vector<Attribute> vec2;
vec2.push_back(att3);
vec2.push_back(att4);
//beginning testing of core DB functions
cout << "\a";
engine.createTable("table1", vec2);
cout << "\a";
//engine.createTable("table2", vec);
engine.showTable(engine.getTableFromName("table1"));
cout << "\n\n";
cout << "\a";
/*Attribute att5("name", "VARCHAR(20)", true);
att5.addRow("Yrf");
att5.addRow("Redneb");
att5.addRow("Aleel");
att5.addRow("Grebdoiz");
cout << "\a";
Attribute att6("age", "INTEGER", false);
att6.addRow("44");
att6.addRow("10");
att6.addRow("44");
att6.addRow("100");
vector<Attribute> vec3;
vec3.push_back(att5);
vec3.push_back(att6);
engine.createTable("table3", vec3);*/
//cout << "\n";
//cout << engine.unionComp(engine.getTableFromName("table1"), engine.getTableFromName("table2"));
//cout << engine.unionComp(engine.getTableFromName("table1"), engine.getTableFromName("table3"));
//cout << "\n";
//engine.project((engine.getTableFromName("table1")), "name");
/*vector<string> cmds;
cmds.push_back("CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);");
cmds.push_back("SHOW animals;");
engine.saveToFile(cmds);*/
vector<string> t;
t.push_back("Professor");
t.push_back("180");
//engine.insertTuple((engine.getTableFromName("table1")), t);
//engine.deleteTuple((engine.getTableFromName("table1")), 2);
cout << "\n\n";
engine.showTable((engine.getTableFromName("table1")));
} }