diff --git a/Attribute.cpp b/Attribute.cpp new file mode 100755 index 0000000..9a66043 --- /dev/null +++ b/Attribute.cpp @@ -0,0 +1,57 @@ +#include +#include +#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 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::iterator it = values.begin(); + while (it != values.end()){ + cout << *it << "\n"; + it++; + } + + cout << "-------------\n"; +} \ No newline at end of file diff --git a/Attribute.h b/Attribute.h old mode 100644 new mode 100755 index 7f0df44..f74e650 --- a/Attribute.h +++ b/Attribute.h @@ -3,67 +3,22 @@ using namespace std; -//Funtional, might need more functionality - -//template -class Attribute { - //a named column of a relation +class Attribute{ + vector values; string name; - bool isKey; + string type; + bool key; int size; public: - - vector values; - - void initializeAttribute(string n, vector a){ - - name = n; - values = a; - } - - string getName(){ - return name; - } - - Attribute(){ } - - void display() - { - //cout<<"\nAtribute name:\t"< v){ - this.values = v; - } - - Attribute(const Attribute& a){ - this.values = a.getAll(); - } - - Attribute& operator=(const Attribute& a){ - this.values = a.getAll(); - } - - - - vector getAll(){ - return this.values; - } -*/ -}; + Attribute(string n, string t, bool k); + void addCell(string v); + string operator[](int i); + vector getValues(); + string getName(); + string getType(); + bool isKey(); + int getSize(); + void setName(string s); + void display(); +}; \ No newline at end of file diff --git a/DBEngine.cpp b/DBEngine.cpp index a854dec..a813c90 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -1,73 +1,70 @@ +#include +#include +#include #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 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 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 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 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 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 v, string o, string s){ + for(int i = 0; i < v.size(); ++i){ + if(v[i].getName() == o){ + v[i].setName(s); + } + } +} +*/ diff --git a/DBEngine.h b/DBEngine.h index bd807fe..f2e5e3f 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -3,23 +3,26 @@ #include #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(); + void createTable(string n); + void createTable(string n, vector a); + void createTable(Relation r); + vector getRelations(); + //void showTable(Relation r); + Relation getTableFromName(string n); + void saveToFile(vector cmds); + + //operations + //void selection(); + Relation projection(vector input, Relation r); + //void renaming(); + //void setUnion(); + //void setDiff(); + //void crossProduct(); }; diff --git a/OUTPUT.txt b/OUTPUT.txt new file mode 100755 index 0000000..2d883ab --- /dev/null +++ b/OUTPUT.txt @@ -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 \ No newline at end of file diff --git a/Relation.cpp b/Relation.cpp new file mode 100755 index 0000000..09eeaf7 --- /dev/null +++ b/Relation.cpp @@ -0,0 +1,44 @@ +#include +#include +#include "Relation.h" + +Relation::Relation(string n){ + name = n; + size = 0; +} + +Relation::Relation(string n, vector a){ + name = n; + att = a; + size = a.size(); +} + +string Relation::getTableName(){ + return name; +} + +vector Relation::getAttributes(){ + return att; +} + +vector Relation::getAttributeNames(){ + vector 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"; +} diff --git a/Relation.h b/Relation.h old mode 100644 new mode 100755 index 6850ce0..5a25284 --- a/Relation.h +++ b/Relation.h @@ -2,70 +2,17 @@ #include #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 att; - vector attributeNames; + int size; public: - //Relation(); - - void initializeRelation(string n, vector attNames, vector 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--------------------------------"< a); + string getTableName(); + vector getAttributes(); + vector getAttributeNames(); + int getSize(); + void display(); +}; \ No newline at end of file diff --git a/test.cpp b/test.cpp index 69b71bd..165d27e 100755 --- a/test.cpp +++ b/test.cpp @@ -1,65 +1,44 @@ #include -//#include "DBEngine.h" #include -//#include "Attribute.h" -#include "Relation.h" +#include "DBEngine.h" using namespace std; +//still in progress int main() { - - /* DBEngine engine; - Relation r; - Attribute a; - */ + Attribute att1("Breakfast", "VARCHAR(20)", true); + Attribute att2("Lunch", "VARCHAR(20)", false); + Attribute att3("Dinner", "VARCHAR(20)", false); - vector 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 v; + v.push_back(att1); + v.push_back(att2); + v.push_back(att3); - atributo.display(); + Relation r("AltFood", v); + //r.display(); - vector 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 rs = engine.getRelations(); + Relation r2 = engine.getTableFromName("Food"); + r2.display(); - Attribute atributo2; - atributo2.initializeAttribute("atributo2",doom); + vector v1 = r2.getAttributeNames(); - atributo2.display(); - - vector setOfAttributes; - setOfAttributes.push_back(atributo); - setOfAttributes.push_back(atributo2); - - vector attNames; - attNames.push_back("attName1"); - attNames.push_back("attName2"); - - Relation myLittleRelation; - myLittleRelation.initializeRelation("randomName", attNames, setOfAttributes); - - - //adding tuple business----------------------- - - vector 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]; + } +} \ No newline at end of file