Merge branch 'master' into alexdev
This commit is contained in:
commit
091ac01645
7 changed files with 402 additions and 132 deletions
87
Attribute.h
87
Attribute.h
|
@ -1,37 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//Funtional, might need more functionality
|
||||
|
||||
template<typename T>
|
||||
class Attribute {
|
||||
//a named column of a relation
|
||||
string name;
|
||||
vector<T> values;
|
||||
bool isKey;
|
||||
int size;
|
||||
public:
|
||||
Attribute(){ }
|
||||
|
||||
Attribute(vector<T> v){
|
||||
this.values = v;
|
||||
}
|
||||
|
||||
Attribute(const Attribute<T>& a){
|
||||
this.values = a.getAll();
|
||||
}
|
||||
|
||||
Attribute& operator=(const Attribute<T>& a){
|
||||
this.values = a.getAll();
|
||||
}
|
||||
|
||||
string getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
vector<T> getAll(){
|
||||
return this.values;
|
||||
}
|
||||
};
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//Funtional, might need more functionality
|
||||
|
||||
//template<typename T>
|
||||
class Attribute {
|
||||
vector<string> values;
|
||||
string name;
|
||||
string type;
|
||||
bool key;
|
||||
int size;
|
||||
|
||||
public:
|
||||
Attribute(string n, string t, bool k){
|
||||
name = n;
|
||||
type = t;
|
||||
key = k;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
void addRow(string v) {
|
||||
values.push_back(v);
|
||||
size++;
|
||||
}
|
||||
|
||||
string getElementAt(int pos)
|
||||
{
|
||||
return values[pos];
|
||||
}
|
||||
|
||||
vector<string> getValues() { return values; }
|
||||
string getName(){ return name; }
|
||||
string getType(){ return type; }
|
||||
bool isKey(){ return key; }
|
||||
int getSize(){ return size; }
|
||||
|
||||
void display()
|
||||
{
|
||||
cout<<"Attribute name:\t"<< name <<"\n";
|
||||
cout<<"Elements: ";
|
||||
|
||||
for (int i = 0; i < values.size(); ++i)
|
||||
{
|
||||
cout<<values[i]<<" ";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
57
DBEngine.cpp
57
DBEngine.cpp
|
@ -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;
|
||||
}
|
89
DBEngine.h
89
DBEngine.h
|
@ -3,23 +3,78 @@
|
|||
#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();
|
||||
DBEngine(){
|
||||
size = 0;
|
||||
}
|
||||
|
||||
void createTable(string n, vector<Attribute> a) {
|
||||
Relation r(n, a);
|
||||
tables.push_back(r);
|
||||
}
|
||||
|
||||
void createTable(Relation r) { tables.push_back(r); }
|
||||
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
68
OUTPUT.txt
Executable 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
|
137
Relation.h
137
Relation.h
|
@ -1,16 +1,121 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Attribute.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//NOT DONE
|
||||
class Relation {
|
||||
//a table with rows and columns
|
||||
string name;
|
||||
vector< Attribute<T> > att;
|
||||
public:
|
||||
Relation();
|
||||
Relation(vector< Attribute<T> > a) { att = a; }
|
||||
void addTuple(vector< Attribute<T> > tuple);
|
||||
};
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Attribute.h"
|
||||
|
||||
//Functional
|
||||
class Relation {
|
||||
string name; //The title the user gives it
|
||||
vector<Attribute> att; //A vector of the columns
|
||||
int size;
|
||||
|
||||
public:
|
||||
Relation(string n, vector<Attribute> a) {
|
||||
name = n;
|
||||
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
BIN
a.out
Executable file
Binary file not shown.
96
test.cpp
96
test.cpp
|
@ -1,10 +1,96 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "DBEngine.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
//still in progress
|
||||
int main() {
|
||||
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")));
|
||||
}
|
||||
|
|
Reference in a new issue