updating brandondev
This commit is contained in:
commit
b0e9ada7f9
11 changed files with 301 additions and 85 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,5 @@ public:
|
|||
string getType();
|
||||
bool isKey();
|
||||
int getSize();
|
||||
void rename(string s);
|
||||
void display();
|
||||
};
|
13
Condition.h
Executable file
13
Condition.h
Executable 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);
|
||||
};
|
30
DBEngine.cpp
30
DBEngine.cpp
|
@ -28,8 +28,7 @@ vector<Relation> 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<string> cmds){
|
||||
//writes nothing meaningful
|
||||
ofstream file;
|
||||
file.open("savefile.db");
|
||||
|
||||
|
@ -52,23 +51,16 @@ void DBEngine::saveToFile(vector<string> cmds){
|
|||
//assumes that all attribute titles are unique
|
||||
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.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<string> oldnames, vector<string> newnames){
|
||||
//test error matching
|
||||
void DBEngine::rename(Relation& r, vector<string> oldnames, vector<string> 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<string> oldnames, vector<string> 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,13 +15,11 @@ public:
|
|||
void createTable(Relation r);
|
||||
vector<Relation> getRelations();
|
||||
//void showTable(Relation r);
|
||||
Relation getTableFromName(string n);
|
||||
Relation& getTableFromName(string n);
|
||||
void saveToFile(vector<string> cmds);
|
||||
|
||||
//operations
|
||||
//Relation selection();
|
||||
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 setDiff();
|
||||
//void crossProduct();
|
||||
|
|
90
OUTPUT.txt
90
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:
|
||||
Save:
|
||||
|
||||
engine.project((engine.getTableFromName("table1")), "name");
|
||||
correct: SAVE people ;
|
||||
incorrect: SAVE ;
|
||||
|
||||
Exit:
|
||||
|
||||
This will display:
|
||||
correct: EXIT ;
|
||||
incorrect: EXIT people ;
|
||||
|
||||
-----------Initiated Query Projection---------
|
||||
Column Title: name
|
||||
Fry
|
||||
Bender
|
||||
Leela
|
||||
Zoidberg
|
||||
Show:
|
||||
|
||||
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:
|
||||
|
||||
|
||||
//insert
|
||||
|
||||
//delete
|
||||
|
||||
//select
|
||||
|
||||
//product
|
||||
correct: DELETE FROM people WHERE ( name == "John" ) ;
|
||||
incorrect: DELETE IN people WHEN (name=John) ;
|
||||
|
|
196
Parserv2.cpp
Executable file
196
Parserv2.cpp
Executable 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);
|
||||
}
|
|
@ -29,7 +29,7 @@ vector<string> 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;
|
||||
}
|
||||
|
|
|
@ -13,10 +13,11 @@ public:
|
|||
string getTableName();
|
||||
vector<Attribute> getAttributes();
|
||||
vector<string> getAttributeNames();
|
||||
Attribute getAttributeByName(string s);
|
||||
Attribute& getAttributeByName(string s);
|
||||
void renameAttribute(string oldstr, string newstr);
|
||||
int getSize();
|
||||
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 removeTuple();
|
||||
};
|
BIN
a.out
BIN
a.out
Binary file not shown.
32
test.cpp
32
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<string> 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<string> o;
|
||||
vector<string> n;
|
||||
vector<string> old;
|
||||
vector<string> 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<string> 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
|
||||
|
|
Reference in a new issue