Update db_engine.cpp

This commit is contained in:
Brandon Jackson 2015-09-15 02:57:09 -05:00
parent 7c252e3746
commit a966090eea

View file

@ -1,12 +1,50 @@
#include "db_engine.h" #include "db_engine.h"
using namespace std;
db_engine::db_engine(){ db_engine::db_engine(){
// //
} }
//create a new table in memory //create a new table in memory
db_engine::createCmd(){ //creates a vector
//// //DONE
void db_engine::createCmd(string tableName, vector<string> attributes, vector<string> pkeys){
string output = "";
output += "CREATE TABLE " + tableName + " (";
vector<string>::iterator it = attributes.begin();
while (it != attributes.end()){
if (it == attributes.begin())
if ((it + 1) != attributes.end())
output += *it + ",";
else
output += *it;
else
output+= " " + *it;
++it;
}
output += ") PRIMARY KEY (";
vector<string>::iterator it2 = pkeys.begin();
while (it2 != pkeys.end()){
if (it2 == pkeys.begin())
if ((it2 + 1) != pkeys.end())
output += *it2 + ",";
else
output += *it2;
else
output+= " " + *it2;
++it2;
}
output += ");";
cout << output;
//cmdList.push_back(output);
} }
//open a txt file, parse SQL script, load data in table //open a txt file, parse SQL script, load data in table
@ -14,42 +52,92 @@ db_engine::createCmd(){
// //
//} //}
//save all the commands to the db text file //should write cmdList to a .txt file
//DONE
void db_engine::saveCmd(){ void db_engine::saveCmd(){
// ofstream dbCmdFile;
dbCmdFile.open("dbCmds.txt", ios_base::app);
vector<string>::iterator it = cmdList.begin();
while (it != cmdList.end()){
dbCmdFile << *it << "\n";
++it;
}
cmdList.clear();
} }
//display the table currently stored in memory //display the database
void db_engine::showCmd(){ //DONE
// void db_engine::showCmd(string tableName){
cmdList.push_back("SHOW " + tableName + "");
} }
//add a tuple to a table in the memory //add a tuple to a table in the memory
//BECCA
void db_engine::insertQuery(){ void db_engine::insertQuery(){
// //table.push_back();
} }
//remove a tuple from a table in the memory //remove a tuple from a table in the memory
//BECCA
void db_engine::deleteQuery(){ void db_engine::deleteQuery(){
// //
} }
//search and return one more tuples from a table in the memory //search and return one more tuples from a table in the memory
//WILLIAM
void db_engine::selectQuery(){ void db_engine::selectQuery(){
// //
} }
//return a subset of attributes (columns) //return a subset of attributes (columns)
void db_engine::projectQuery(){ //BRANDON
// vector<string> db_engine::projectQuery(vector< vector<string> > table, vector<string> attributes, string query){
/*
So basically this is what's going on:
- Take the table to iterate through, this is the 2D vector (input 1)
- Take the vector of attributes that pertains to the respective table (input 2)
- Take a string that will be used for the program to search for in the attributes (input 3)
- In the attributes vector, 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
*/
int counter = 0; //counter for iterating through the attributes, initialized at 0 for reasons
vector<string> result;
//Loop through the vector until we have found a string in it that matches the query
for(int i=0; i < attributes.size(); i++){
counter++;
if(attributes[i] == query)
break;
else
continue;
}
//So let's say we have a 2-D Vector (the table) that contains all of the rows, we iterate through each row
std::vector< std::vector<int> >::const_iterator row;
std::vector<int>::const_iterator col;
for (row = table.begin(); row != table.end(); ++row) {
//Instead of col != row->end() we have it counter, in order for the program to access the correct data under the correct attribute
for (col = row->begin(); col != counter; col++) {
result.push_back(col); //Now we store that row's attribute into the result vector
}
}
return result;
} }
//each row in the first table is paired with all the rows in the second table //each row in the first table is paired with all the rows in the second table
void db_engine::productQuery(){ //BRANDON
vector< vector<string> > db_engine::productQuery(vector< vector<string> > table_1, vector< vector<string> > table_2){
// //
} }
//true if relations have the same # of attributes and each attribute must be from the same domain //true if relations have the same # of attributes and each attribute must be from the same domain
//WILLIAM
bool db_engine::unionComp(){ bool db_engine::unionComp(){
return false; return false;
} }