updating brandondev

This commit is contained in:
Rebecca Schofield 2015-09-22 23:23:48 -05:00
commit b0e9ada7f9
11 changed files with 301 additions and 85 deletions

View file

@ -45,11 +45,6 @@ bool Attribute::isKey(){
return key; return key;
} }
void Attribute::rename(string s){
name = s;
}
//may need to change primary key implementation
int Attribute::getSize(){ int Attribute::getSize(){
return size; return size;
} }

View file

@ -21,6 +21,5 @@ public:
string getType(); string getType();
bool isKey(); bool isKey();
int getSize(); int getSize();
void rename(string s);
void display(); void display();
}; };

13
Condition.h Executable file
View file

@ -0,0 +1,13 @@
#include <iostream>
#include "Attribute.h"
using namespace std;
class Condition{
Attribute att;
public:
//currently only implemented for comparison
Condition(Attribute a);
Condition(Attribute a);
};

View file

@ -28,8 +28,7 @@ vector<Relation> DBEngine::getRelations(){
return tables; return tables;
} }
Relation DBEngine::getTableFromName(string n){ Relation& DBEngine::getTableFromName(string n){
//will return first occurence
for(int i = 0; i < tables.size(); i++){ for(int i = 0; i < tables.size(); i++){
if (tables[i].getTableName() == n){ if (tables[i].getTableName() == n){
return tables[i]; return tables[i];
@ -37,8 +36,8 @@ Relation DBEngine::getTableFromName(string n){
} }
} }
//currently writes nothing meaningful
void DBEngine::saveToFile(vector<string> cmds){ void DBEngine::saveToFile(vector<string> cmds){
//writes nothing meaningful
ofstream file; ofstream file;
file.open("savefile.db"); file.open("savefile.db");
@ -52,23 +51,16 @@ void DBEngine::saveToFile(vector<string> cmds){
//assumes that all attribute titles are unique //assumes that all attribute titles are unique
Relation DBEngine::projection(vector<string> input, Relation r){ Relation DBEngine::projection(vector<string> input, Relation r){
vector<Attribute> 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[i].getName == input[])
if((r.getAttributes())[j].getName() == input[i]) // }
v.push_back((r.getAttributes())[j]);
}
}
Relation temp(new_name, v);
return temp;
} }
//ASAP: TEST ALL OF THIS //test error matching
void rename(Relation r, vector<string> oldnames, vector<string> newnames){ void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> newnames){
if (oldnames.size() != newnames.size()) { if (oldnames.size() != newnames.size()) {
cout << "Failure to rename: number of attributes do not match."; cout << "Failure to rename: number of attributes do not match.";
return; return;
@ -80,12 +72,8 @@ void rename(Relation r, vector<string> oldnames, vector<string> newnames){
} }
else { else {
Attribute temp;
for(int i = 0; i < oldnames.size(); ++i){ for(int i = 0; i < oldnames.size(); ++i){
temp = r.getAttributeByName(oldnames[i]); r.renameAttribute(oldnames[i], newnames[i]);
temp.setName(newnames[i]);
} }
} }
} }

View file

@ -15,13 +15,11 @@ public:
void createTable(Relation r); void createTable(Relation r);
vector<Relation> getRelations(); vector<Relation> getRelations();
//void showTable(Relation r); //void showTable(Relation r);
Relation getTableFromName(string n); Relation& getTableFromName(string n);
void saveToFile(vector<string> cmds); void saveToFile(vector<string> cmds);
//operations
//Relation selection(); //Relation selection();
Relation projection(vector<string> input, Relation r); Relation projection(vector<string> input, Relation r);
void rename(Relation r, vector<string> oldnames, vector<string> newnames); void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
//void setUnion(); //void setUnion();
//void setDiff(); //void setDiff();
//void crossProduct(); //void crossProduct();

View file

@ -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-------------------------------- Set Difference:
Relation name: table1
Attribute name: name correct: difference ( people - animals ) ;
Elements: Fry Bender Leela Zoidberg incorrect: difference people animals ;
Attribute name: age
Elements: 22 5 22 50
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: Save:
engine.project((engine.getTableFromName("table1")), "name"); correct: SAVE people ;
incorrect: SAVE ;
Exit:
This will display: correct: EXIT ;
incorrect: EXIT people ;
-----------Initiated Query Projection--------- Show:
Column Title: name
Fry
Bender
Leela
Zoidberg
correct: SHOW people ;
incorrect: SHOW ;
With an arbitrary vector of strings as cmds (until we are able to parse effectively): Create:
engine.saveToFile(cmds); correct: CREATE TABLE people ( age INTEGER, name VARCHAR ( 20 ) ) PRIMARY KEY ( name ) ;
incorrect: CREATE TABLE people age name ;
Update:
This will result in a db file with the contents: correct: UPDATE people SET ( age = 10 ) WHERE ( name == "Bob" ) ;
incorrect: UPDATE ( age = 10 ) WHERE people ;
CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind); Insert:
SHOW animals; correct: INSERT INTO people VALUES FROM ( 12, "Benny" ) ;
incorrect: INSERT INTO people 12 "Benny" ;
Delete:
correct: DELETE FROM people WHERE ( name == "John" ) ;
//insert incorrect: DELETE IN people WHEN (name=John) ;
//delete
//select
//product

196
Parserv2.cpp Executable file
View file

@ -0,0 +1,196 @@
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
vector<string> tokenize(string ss)
{
string tempString;
stringstream lineStream(ss);
vector<string> output;
while (lineStream >> tempString)
{
output.push_back(tempString);
}
//testing---------------
cout<<"TokenList: ";
for (int i = 0; i <output.size(); ++i)
{
cout<<output[i]<<" ";
}
//----------------------
return output;
}
void displayTokenList(vector<string> input)
{
cout<<"TokenList: "<<endl;
for (int i = 0; i < input.size(); ++i)
{
cout<<input[i]<<endl;
}
}
vector<string> insertCMD(vector<string> input)
{
//relation name will be the first element of the vector of data returned by this function
vector<string> 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!"<<endl;
}
else cout<<"Syntax error!"<<endl;
}
else cout<<"Syntax error!"<<endl;
}
vector<string> showCMD(vector<string> input)
{
if (input.size() > 3)
{
cout<<"Syntax error!"<<endl;
}
cout<<"\nPassing the following arguments to dbEngine: "<<endl;
cout << "command :" << input[0] << endl;
cout << "relation name / expression: "<< input[1] << endl;
return input;
}
vector<string> exitCMD(vector<string> input)
{
if (input[1] != ";")
{
cout<<"ERROR: missing semicolon!"<<endl;
return input;
}
if (input[0] != "EXIT") {
cout<<"Wrong function/syntax error!"<<endl;
return input;
}
cout<<"Passing command: "<< input[0] <<endl;
return input;
}
vector<string> createCMD(vector<string> input)
{
if (input[0] != "CREATE") {
cout << "Error: create keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
// CREATE TABLE relation-name ( typed-attribute-list ) PRIMARY KEY ( attribute-list )
cout << "command :" << input[0] << endl;
cout << "argument: " << input[1] << endl;
return input;
}
vector<string> openCMD(vector<string> input){
if (input[0] != "OPEN") {
cout << "Error: open keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[1] << endl;
}
vector<string> closeCMD(vector<string> input){
if (input[0] != "CLOSE") {
cout << "Error: close keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[1] << endl;
return input;
}
vector<string> saveCMD(vector<string> input){
if (input[0] != "SAVE") {
cout << "Error: save keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[1] << endl;
return input;
}
vector<string> updateCMD(vector<string> input){
//UPDATE relation-name SET attribute-name = literal { , attribute-name = literal } WHERE condition
}
vector<string> deleteCMD(vector<string> input){
//DELETE FROM relation-name WHERE condition
if (input[0] != "DELETE") {
cout << "Error: save keyword is missing." <<endl;
return input;
}
cout << "\nPassing the following arguments to dbEngine: " << endl;
cout << "command :" << input[0] << endl;
cout << "relation: " << input[2] << endl;
cout << "condition: " << input[5] << input[6] << input[7] << endl;
return input;
}
int main () {
string s = "DELETE FROM animals WHERE ( age == 12 ) ;";
vector<string> listOfTokens = tokenize(s);
deleteCMD(listOfTokens);
}

View file

@ -29,7 +29,7 @@ vector<string> Relation::getAttributeNames(){
return temp; return temp;
} }
Attribute Relation::getAttributeByName(string s) { Attribute& Relation::getAttributeByName(string s) {
for(int i = 0; i < size; ++i){ for(int i = 0; i < size; ++i){
if (att[i].getName() == s) { if (att[i].getName() == s) {
return att[i]; return att[i];
@ -39,6 +39,10 @@ Attribute Relation::getAttributeByName(string s) {
cout << "Failure to return: the requested attribute does not exist."; 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(){ int Relation::getSize(){
return size; return size;
} }

View file

@ -13,10 +13,11 @@ public:
string getTableName(); string getTableName();
vector<Attribute> getAttributes(); vector<Attribute> getAttributes();
vector<string> getAttributeNames(); vector<string> getAttributeNames();
Attribute getAttributeByName(string s); Attribute& getAttributeByName(string s);
void renameAttribute(string oldstr, string newstr);
int getSize(); int getSize();
void display(); void display();
void insertTuple(vector<string> tuple); //we are assuming they are in order void insertTuple(vector<string> tuple); //assuming they are in order
void insertFromRelation(Relation r); void insertFromRelation(Relation r);
//void removeTuple(); //void removeTuple();
}; };

BIN
a.out

Binary file not shown.

View file

@ -26,27 +26,31 @@ int main() {
v.push_back(att2); v.push_back(att2);
v.push_back(att3); v.push_back(att3);
Relation r("Food", v); //Relation r("Food", v);
//r.renameAttribute("Breakfast", "BFST");
//r.display();
engine.createTable("Food", v);
vector<string> tuple; vector<string> tuple;
tuple.push_back("Omelette"); tuple.push_back("Omelette");
tuple.push_back("Fried Rice"); tuple.push_back("Fried Rice");
tuple.push_back("Grouper"); tuple.push_back("Grouper");
r.insertTuple(tuple); engine.getTableFromName("Food").insertTuple(tuple);
r.display();
vector<string> o; vector<string> old;
vector<string> n; vector<string> newa;
o.push_back("Breakfast"); old.push_back("Breakfast");
o.push_back("Lunch"); old.push_back("Lunch");
o.push_back("Dinner"); old.push_back("Dinner");
n.push_back("Tsafkaerb"); newa.push_back("Tsafkaerb");
n.push_back("Hcnul"); newa.push_back("Hcnul");
n.push_back("Rennid"); newa.push_back("Rennid");
<<<<<<< HEAD
//Projection test //Projection test
vector<string> projectTest; vector<string> projectTest;
projectTest.push_back("Breakfast"); projectTest.push_back("Breakfast");
@ -59,3 +63,9 @@ int main() {
//engine.rename(r, o, n); //engine.rename(r, o, n);
} }
=======
engine.rename(engine.getTableFromName("Food"), old, newa);
engine.getTableFromName("Food").display();
cout << "finished";
}
>>>>>>> master