cleared out the bad code
This commit is contained in:
parent
e2776d2459
commit
c8ecb606fd
6 changed files with 73 additions and 205 deletions
26
Attribute.h
26
Attribute.h
|
@ -21,30 +21,28 @@ public:
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addRow(string v) {
|
void addCell(string v){
|
||||||
values.push_back(v);
|
values.push_back(v);
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
string getElementAt(int pos)
|
string operator[](int i){ return values[i]; }
|
||||||
{
|
|
||||||
return values[pos];
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<string> getValues(){ return values; }
|
vector<string> getValues(){ return values; }
|
||||||
string getName(){ return name; }
|
string getName(){ return name; }
|
||||||
string getType(){ return type; }
|
string getType(){ return type; }
|
||||||
bool isKey(){ return key; }
|
bool isKey(){ return key; }
|
||||||
int getSize(){ return size; }
|
int getSize(){ return size; } //may need to change primary key implementation
|
||||||
|
|
||||||
void display()
|
void display(){
|
||||||
{
|
cout << "-------------\n";
|
||||||
cout<<"Attribute name:\t"<< name <<"\n";
|
cout << name << "\n" << type << "\n\n";
|
||||||
cout<<"Elements: ";
|
|
||||||
|
|
||||||
for (int i = 0; i < values.size(); ++i)
|
vector<string>::iterator it = values.begin();
|
||||||
{
|
while (it != values.end()){
|
||||||
cout<<values[i]<<" ";
|
cout << *it << "\n";
|
||||||
|
it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cout << "-------------\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
35
DBEngine.h
35
DBEngine.h
|
@ -13,14 +13,25 @@ public:
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void createTable(string n) {
|
||||||
|
Relation r(n);
|
||||||
|
tables.push_back(r);
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
void createTable(string n, vector<Attribute> a) {
|
void createTable(string n, vector<Attribute> a) {
|
||||||
Relation r(n, a);
|
Relation r(n, a);
|
||||||
tables.push_back(r);
|
tables.push_back(r);
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void createTable(Relation r){
|
||||||
|
tables.push_back(r);
|
||||||
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void createTable(Relation r) { tables.push_back(r); }
|
|
||||||
vector<Relation> getRelations(){ return tables; }
|
vector<Relation> getRelations(){ return tables; }
|
||||||
void showTable(Relation r) { r.display(); }
|
//void showTable(Relation r){}
|
||||||
|
|
||||||
Relation getTableFromName(string n){
|
Relation getTableFromName(string n){
|
||||||
//will return first occurence
|
//will return first occurence
|
||||||
|
@ -32,6 +43,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveToFile(vector<string> cmds){
|
void saveToFile(vector<string> cmds){
|
||||||
|
//writes nothing meaningful
|
||||||
ofstream file;
|
ofstream file;
|
||||||
file.open("savefile.db");
|
file.open("savefile.db");
|
||||||
|
|
||||||
|
@ -41,23 +53,4 @@ public:
|
||||||
|
|
||||||
file.close();
|
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); }
|
|
||||||
|
|
||||||
void product() { /*Brandon*/ }
|
|
||||||
|
|
||||||
bool unionComp(Relation r1, Relation r2) {
|
|
||||||
return ((r1.getSize() == r2.getSize()) && (r1.getDomains() == r2.getDomains()));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -53,12 +53,14 @@ With an arbitrary vector of strings as cmds (until we are able to parse effectiv
|
||||||
engine.saveToFile(cmds);
|
engine.saveToFile(cmds);
|
||||||
|
|
||||||
|
|
||||||
This will result in a text file with the contents:
|
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);
|
CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);
|
||||||
|
|
||||||
SHOW animals;
|
SHOW animals;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//insert
|
//insert
|
||||||
|
|
||||||
//delete
|
//delete
|
||||||
|
|
91
Relation.h
91
Relation.h
|
@ -9,77 +9,22 @@ class Relation {
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Relation(string n) {
|
||||||
|
name = n;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
Relation(string n, vector<Attribute> a) {
|
Relation(string n, vector<Attribute> a) {
|
||||||
name = n;
|
name = n;
|
||||||
att = a;
|
att = a;
|
||||||
size = a.size();
|
size = a.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//addAttribute
|
||||||
|
|
||||||
|
string getTableName() { return name; }
|
||||||
|
vector<Attribute> getAttributes() { return att; }
|
||||||
int getSize() { return 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
|
//assumes that all attribute titles are unique
|
||||||
void projectQuery(string input) {
|
void projectQuery(string input) {
|
||||||
cout << "-----------Initiated Query Projection---------" << endl;
|
cout << "-----------Initiated Query Projection---------" << endl;
|
||||||
|
@ -98,24 +43,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void display() {
|
void display() {
|
||||||
cout<<"\nDisplay of relation--------------------------------"<<endl;
|
cout << "--------------------------\n";
|
||||||
cout<<"Relation name: "<<name<<endl;
|
cout << name << "\n";
|
||||||
for (int i = 0; i < size; ++i)
|
for (int i = 0; i < att.size(); ++i){
|
||||||
{
|
|
||||||
cout<<"\n";
|
|
||||||
att[i].display();
|
att[i].display();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//make this better
|
cout << "--------------------------\n";
|
||||||
vector<string> getDomains() {
|
|
||||||
vector<string> ds;
|
|
||||||
|
|
||||||
for (int i = 0; i < size; ++i)
|
|
||||||
{
|
|
||||||
ds.push_back(att[i].getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ds;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
BIN
a.out
BIN
a.out
Binary file not shown.
106
test.cpp
106
test.cpp
|
@ -7,90 +7,32 @@ using namespace std;
|
||||||
//still in progress
|
//still in progress
|
||||||
int main() {
|
int main() {
|
||||||
DBEngine engine;
|
DBEngine engine;
|
||||||
|
Attribute att1("Breakfast", "VARCHAR(20)", true);
|
||||||
|
Attribute att2("Lunch", "VARCHAR(20)", false);
|
||||||
|
Attribute att3("Dinner", "VARCHAR(20)", false);
|
||||||
|
|
||||||
/*Attribute att1("shamWow", "VARCHAR(10)", true);
|
att1.addCell("Pancakes");
|
||||||
att1.addRow("rag");
|
att1.addCell("Waffles");
|
||||||
att1.addRow("sponge");
|
att1.addCell("Biscuits");
|
||||||
att1.addRow("wooow");
|
att2.addCell("Turkey Sandwich");
|
||||||
att1.addRow("cloth");
|
att2.addCell("Caesar Salad");
|
||||||
|
att2.addCell("Pizza");
|
||||||
|
att3.addCell("Steak");
|
||||||
|
att3.addCell("Shrimp");
|
||||||
|
att3.addCell("Ribs");
|
||||||
|
|
||||||
Attribute att2("doom", "VARCHAR(20)", false);
|
vector<Attribute> v;
|
||||||
att2.addRow("zombieman");
|
v.push_back(att1);
|
||||||
att2.addRow("revenant");
|
v.push_back(att2);
|
||||||
att2.addRow("imp");
|
v.push_back(att3);
|
||||||
att2.addRow("archvile");
|
|
||||||
|
|
||||||
vector<Attribute> vec;
|
Relation r("AltFood", v);
|
||||||
vec.push_back(att1);
|
//r.display();
|
||||||
vec.push_back(att2);*/
|
|
||||||
|
|
||||||
Attribute att3("name", "VARCHAR(20)", true);
|
engine.createTable("Food", v);
|
||||||
att3.addRow("Fry");
|
engine.createTable(r);
|
||||||
att3.addRow("Bender");
|
engine.createTable("Sadness");
|
||||||
att3.addRow("Leela");
|
//vector<Relation> rs = engine.getRelations();
|
||||||
att3.addRow("Zoidberg");
|
Relation r2 = engine.getTableFromName("Food");
|
||||||
|
r2.display();
|
||||||
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