diff --git a/Attribute.h b/Attribute.h index 2c1bd3d..50949a1 100755 --- a/Attribute.h +++ b/Attribute.h @@ -1,37 +1,50 @@ -#include -#include - -using namespace std; - -//Funtional, might need more functionality - -template -class Attribute { - //a named column of a relation - string name; - vector values; - bool isKey; - int size; -public: - Attribute(){ } - - Attribute(vector v){ - this.values = v; - } - - Attribute(const Attribute& a){ - this.values = a.getAll(); - } - - Attribute& operator=(const Attribute& a){ - this.values = a.getAll(); - } - - string getName(){ - return name; - } - - vector getAll(){ - return this.values; - } -}; \ No newline at end of file +#include +#include + +using namespace std; + +//Funtional, might need more functionality + +//template +class Attribute { + vector 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 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< #include "Relation.h" -using namespace std; - -class DBEngine { - //member variables - //NOT DONE - //vector tables; +//still in progress +class DBEngine { + vector 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 a) { + Relation r(n, a); + tables.push_back(r); + } + + void createTable(Relation r) { tables.push_back(r); } + vector 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 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 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 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 table1_stuff; + + + return temp; + } + */ + + bool unionComp(Relation r1, Relation r2) { + return ((r1.getSize() == r2.getSize()) && (r1.getDomains() == r2.getDomains())); + } }; diff --git a/OUTPUT.txt b/OUTPUT.txt new file mode 100755 index 0000000..c79684b --- /dev/null +++ b/OUTPUT.txt @@ -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 \ No newline at end of file diff --git a/Relation.h b/Relation.h index 222b8a5..995e3ea 100755 --- a/Relation.h +++ b/Relation.h @@ -1,16 +1,121 @@ -#include -#include -#include "Attribute.h" - -using namespace std; - -//NOT DONE -class Relation { - //a table with rows and columns - string name; - vector< Attribute > att; -public: - Relation(); - Relation(vector< Attribute > a) { att = a; } - void addTuple(vector< Attribute > tuple); -}; \ No newline at end of file +#include +#include +#include "Attribute.h" + +//Functional +class Relation { + string name; //The title the user gives it + vector att; //A vector of the columns + int size; + +public: + Relation(string n, vector a) { + name = n; + att = a; + size = a.size(); + } + + int getSize() { return size; } + + /*void addTuple(vector 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"< findTuple(string attributeType, string type) + { + vector 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 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--------------------------------"< getDomains() { + vector ds; + + for (int i = 0; i < size; ++i) + { + ds.push_back(att[i].getType()); + } + + return ds; + } +}; diff --git a/a.out b/a.out new file mode 100755 index 0000000..e1e1ddd Binary files /dev/null and b/a.out differ diff --git a/test.cpp b/test.cpp index a419a4a..897d3cf 100755 --- a/test.cpp +++ b/test.cpp @@ -1,10 +1,96 @@ #include +#include #include "DBEngine.h" - + using namespace std; - -int main() { + +//still in progress +int main() { DBEngine engine; - Relation r; - Attribute 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 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 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 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 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 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"))); }