diff --git a/Attribute.cpp b/Attribute.cpp index 2ecee1c..eb35167 100755 --- a/Attribute.cpp +++ b/Attribute.cpp @@ -45,11 +45,6 @@ bool Attribute::isKey(){ return key; } -void Attribute::rename(string s){ - name = s; -} - -//may need to change primary key implementation int Attribute::getSize(){ return size; } diff --git a/Attribute.h b/Attribute.h index fa98b14..0e92a40 100755 --- a/Attribute.h +++ b/Attribute.h @@ -21,6 +21,5 @@ public: string getType(); bool isKey(); int getSize(); - void rename(string s); void display(); }; \ No newline at end of file diff --git a/Condition.h b/Condition.h new file mode 100755 index 0000000..cc06978 --- /dev/null +++ b/Condition.h @@ -0,0 +1,13 @@ +#include +#include "Attribute.h" + +using namespace std; + +class Condition{ + Attribute att; + +public: + //currently only implemented for comparison + Condition(Attribute a); + Condition(Attribute a); +}; \ No newline at end of file diff --git a/DBEngine.cpp b/DBEngine.cpp index 16b6f7a..d028aea 100755 --- a/DBEngine.cpp +++ b/DBEngine.cpp @@ -28,8 +28,7 @@ vector DBEngine::getRelations(){ return tables; } -Relation DBEngine::getTableFromName(string n){ - //will return first occurence +Relation& DBEngine::getTableFromName(string n){ for(int i = 0; i < tables.size(); i++){ if (tables[i].getTableName() == n){ return tables[i]; @@ -37,8 +36,8 @@ Relation DBEngine::getTableFromName(string n){ } } +//currently writes nothing meaningful void DBEngine::saveToFile(vector cmds){ - //writes nothing meaningful ofstream file; file.open("savefile.db"); @@ -52,23 +51,16 @@ void DBEngine::saveToFile(vector cmds){ //assumes that all attribute titles are unique Relation DBEngine::projection(vector input, Relation r){ - vector v; - string new_name = r.getTableName() + " Projection"; - for(int i = 0; i < input.size(); ++i) { +// for(int i = 0; i < input.size(); i++) { +// it = find(r.getAttributes().begin(), r.getAttributes().end(), input[i]) - for(int j = 0; j < r.getSize(); ++j) { - if((r.getAttributes())[j].getName() == input[i]) - v.push_back((r.getAttributes())[j]); - } - } - - Relation temp(new_name, v); - return temp; + //if(r[i].getName == input[]) +// } } -//ASAP: TEST ALL OF THIS -void rename(Relation r, vector oldnames, vector newnames){ +//test error matching +void DBEngine::rename(Relation& r, vector oldnames, vector newnames){ if (oldnames.size() != newnames.size()) { cout << "Failure to rename: number of attributes do not match."; return; @@ -80,12 +72,8 @@ void rename(Relation r, vector oldnames, vector newnames){ } else { - Attribute temp; - for(int i = 0; i < oldnames.size(); ++i){ - temp = r.getAttributeByName(oldnames[i]); - temp.setName(newnames[i]); + r.renameAttribute(oldnames[i], newnames[i]); } } -} - +} \ No newline at end of file diff --git a/DBEngine.h b/DBEngine.h index 8533656..8a71981 100755 --- a/DBEngine.h +++ b/DBEngine.h @@ -15,13 +15,11 @@ public: void createTable(Relation r); vector getRelations(); //void showTable(Relation r); - Relation getTableFromName(string n); + Relation& getTableFromName(string n); void saveToFile(vector cmds); - - //operations //Relation selection(); Relation projection(vector input, Relation r); - void rename(Relation r, vector oldnames, vector newnames); + void rename(Relation& r, vector oldnames, vector newnames); //void setUnion(); //void setDiff(); //void crossProduct(); diff --git a/OUTPUT.txt b/OUTPUT.txt index 2d883ab..09bc7f9 100755 --- a/OUTPUT.txt +++ b/OUTPUT.txt @@ -1,70 +1,82 @@ -Assuming previously defined Attribute, compiled in a vector known as 'vec' and with subsequent numbers added +--------------- +Queries +--------------- -To create a table: +Selection: - engine.createTable("table1", vec); +correct: select ( age == 12 ) people ; +incorrect: select ( age ))))) people ; +Projection: -This creates a Relation object with the appropriate Attribute objects. It displays nothing. +correct: project ( age ) people ; +incorrect: project age people ; -To display the table: +Renaming: - engine.showTables(engine.getTableFromName("table1")); +correct: rename ( age, years old ) ; +incorrect: rename age years ; +Set Union: -This results in such output: +correct: union ( people + animals ) ; +incorrect: union people animals ; - Display of relation-------------------------------- - Relation name: table1 +Set Difference: - Attribute name: name - Elements: Fry Bender Leela Zoidberg - Attribute name: age - Elements: 22 5 22 50 +correct: difference ( people - animals ) ; +incorrect: difference people animals ; +Cross Product: -With table3 having an equal domain to table1, this: +correct: product ( people * animals ) ; +incorrect: product people animals ; - cout << engine.unionComp(engine.getTableFromName("table1"), engine.getTableFromName("table3")); +------------------- +Commands +------------------- +Open: -This will display: +correct: OPEN people ; +incorrect: OPEN ; - 1 +Close: +correct: CLOSE people ; +incorrect: CLOSE ; -To project a table's column: - - engine.project((engine.getTableFromName("table1")), "name"); +Save: +correct: SAVE people ; +incorrect: SAVE ; -This will display: +Exit: - -----------Initiated Query Projection--------- - Column Title: name - Fry - Bender - Leela - Zoidberg +correct: EXIT ; +incorrect: EXIT people ; +Show: -With an arbitrary vector of strings as cmds (until we are able to parse effectively): +correct: SHOW people ; +incorrect: SHOW ; - engine.saveToFile(cmds); +Create: +correct: CREATE TABLE people ( age INTEGER, name VARCHAR ( 20 ) ) PRIMARY KEY ( name ) ; +incorrect: CREATE TABLE people age name ; -This will result in a db file with the contents: +Update: - CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind); - -SHOW animals; +correct: UPDATE people SET ( age = 10 ) WHERE ( name == "Bob" ) ; +incorrect: UPDATE ( age = 10 ) WHERE people ; +Insert: +correct: INSERT INTO people VALUES FROM ( 12, "Benny" ) ; +incorrect: INSERT INTO people 12 "Benny" ; -//insert +Delete: -//delete - -//select - -//product \ No newline at end of file +correct: DELETE FROM people WHERE ( name == "John" ) ; +incorrect: DELETE IN people WHEN (name=John) ; diff --git a/Parserv2.cpp b/Parserv2.cpp new file mode 100755 index 0000000..cb848f3 --- /dev/null +++ b/Parserv2.cpp @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include + +using namespace std; + + +vector tokenize(string ss) +{ + string tempString; + stringstream lineStream(ss); + vector output; + + while (lineStream >> tempString) + { + output.push_back(tempString); + } + + //testing--------------- + cout<<"TokenList: "; + + for (int i = 0; i input) +{ + cout<<"TokenList: "< insertCMD(vector input) +{ + //relation name will be the first element of the vector of data returned by this function + vector output; + + if (input[0] == "INTO") + { + input.erase(input.begin()); + + output.push_back(input[0]); //pushing relation name + + input.erase(input.begin()); + + if (input[0] == "VALUES" && input[1] == "FROM") + { + input.erase(input.begin()); + input.erase(input.begin()); + + + if(input[0] == "(") + { + input.erase(input.begin()); + + while(input[0] != ")") //inserting all values to relation + //for (int i = 0; i < 2; ++i) + { + if (input[0] == ",") input.erase(input.begin()); + + output.push_back(input[0]); + + input.erase(input.begin()); + } + + return output; + + } + + else cout<<"Syntax error!"< showCMD(vector input) +{ + if (input.size() > 3) + { + cout<<"Syntax error!"< exitCMD(vector input) +{ + if (input[1] != ";") + { + cout<<"ERROR: missing semicolon!"< createCMD(vector input) +{ + if (input[0] != "CREATE") { + cout << "Error: create keyword is missing." < openCMD(vector input){ + if (input[0] != "OPEN") { + cout << "Error: open keyword is missing." < closeCMD(vector input){ + if (input[0] != "CLOSE") { + cout << "Error: close keyword is missing." < saveCMD(vector input){ + if (input[0] != "SAVE") { + cout << "Error: save keyword is missing." < updateCMD(vector input){ + //UPDATE relation-name SET attribute-name = literal { , attribute-name = literal } WHERE condition +} + +vector deleteCMD(vector input){ + //DELETE FROM relation-name WHERE condition + if (input[0] != "DELETE") { + cout << "Error: save keyword is missing." < listOfTokens = tokenize(s); + deleteCMD(listOfTokens); +} \ No newline at end of file diff --git a/Relation.cpp b/Relation.cpp index 300cde1..f81adee 100755 --- a/Relation.cpp +++ b/Relation.cpp @@ -29,7 +29,7 @@ vector Relation::getAttributeNames(){ return temp; } -Attribute Relation::getAttributeByName(string s) { +Attribute& Relation::getAttributeByName(string s) { for(int i = 0; i < size; ++i){ if (att[i].getName() == s) { return att[i]; @@ -39,6 +39,10 @@ Attribute Relation::getAttributeByName(string s) { cout << "Failure to return: the requested attribute does not exist."; } +void Relation::renameAttribute(string oldstr, string newstr){ + this->getAttributeByName(oldstr).setName(newstr); +} + int Relation::getSize(){ return size; } diff --git a/Relation.h b/Relation.h index beea397..faa89ea 100755 --- a/Relation.h +++ b/Relation.h @@ -13,10 +13,11 @@ public: string getTableName(); vector getAttributes(); vector getAttributeNames(); - Attribute getAttributeByName(string s); + Attribute& getAttributeByName(string s); + void renameAttribute(string oldstr, string newstr); int getSize(); void display(); - void insertTuple(vector tuple); //we are assuming they are in order + void insertTuple(vector tuple); //assuming they are in order void insertFromRelation(Relation r); //void removeTuple(); }; \ No newline at end of file diff --git a/a.out b/a.out index 0bb0543..6a77a3d 100755 Binary files a/a.out and b/a.out differ diff --git a/test.cpp b/test.cpp index e6f7261..3cc2d12 100755 --- a/test.cpp +++ b/test.cpp @@ -26,27 +26,31 @@ int main() { v.push_back(att2); v.push_back(att3); - Relation r("Food", v); + //Relation r("Food", v); + //r.renameAttribute("Breakfast", "BFST"); + //r.display(); + + engine.createTable("Food", v); vector tuple; tuple.push_back("Omelette"); tuple.push_back("Fried Rice"); tuple.push_back("Grouper"); - r.insertTuple(tuple); - r.display(); + engine.getTableFromName("Food").insertTuple(tuple); - vector o; - vector n; + vector old; + vector newa; - o.push_back("Breakfast"); - o.push_back("Lunch"); - o.push_back("Dinner"); + old.push_back("Breakfast"); + old.push_back("Lunch"); + old.push_back("Dinner"); - n.push_back("Tsafkaerb"); - n.push_back("Hcnul"); - n.push_back("Rennid"); + newa.push_back("Tsafkaerb"); + newa.push_back("Hcnul"); + newa.push_back("Rennid"); +<<<<<<< HEAD //Projection test vector projectTest; projectTest.push_back("Breakfast"); @@ -59,3 +63,9 @@ int main() { //engine.rename(r, o, n); } +======= + engine.rename(engine.getTableFromName("Food"), old, newa); + engine.getTableFromName("Food").display(); + cout << "finished"; +} +>>>>>>> master