This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
dmspine64backup/db_engine.cpp

144 lines
4 KiB
C++
Raw Normal View History

2015-09-14 15:24:54 -05:00
#include "db_engine.h"
2015-09-14 15:22:14 -05:00
2015-09-15 02:57:09 -05:00
using namespace std;
2015-09-14 15:22:14 -05:00
db_engine::db_engine(){
//
}
//create a new table in memory
2015-09-15 02:57:09 -05:00
//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);
2015-09-14 15:22:14 -05:00
}
//open a txt file, parse SQL script, load data in table
//void db_engine::openCmd(){
//
//}
2015-09-15 02:57:09 -05:00
//should write cmdList to a .txt file
//DONE
2015-09-14 15:22:14 -05:00
void db_engine::saveCmd(){
2015-09-15 02:57:09 -05:00
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();
2015-09-14 15:22:14 -05:00
}
2015-09-15 02:57:09 -05:00
//display the database
//DONE
void db_engine::showCmd(string tableName){
cmdList.push_back("SHOW " + tableName + "");
2015-09-14 15:22:14 -05:00
}
//add a tuple to a table in the memory
2015-09-15 02:57:09 -05:00
//BECCA
2015-09-14 15:22:14 -05:00
void db_engine::insertQuery(){
2015-09-15 02:57:09 -05:00
//table.push_back();
2015-09-14 15:22:14 -05:00
}
//remove a tuple from a table in the memory
2015-09-15 02:57:09 -05:00
//BECCA
2015-09-14 15:22:14 -05:00
void db_engine::deleteQuery(){
//
}
//search and return one more tuples from a table in the memory
2015-09-15 02:57:09 -05:00
//WILLIAM
2015-09-14 15:22:14 -05:00
void db_engine::selectQuery(){
//
}
//return a subset of attributes (columns)
2015-09-15 02:57:09 -05:00
//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;
2015-09-14 15:22:14 -05:00
}
//each row in the first table is paired with all the rows in the second table
2015-09-15 02:57:09 -05:00
//BRANDON
vector< vector<string> > db_engine::productQuery(vector< vector<string> > table_1, vector< vector<string> > table_2){
2015-09-14 15:22:14 -05:00
//
}
//true if relations have the same # of attributes and each attribute must be from the same domain
2015-09-15 02:57:09 -05:00
//WILLIAM
2015-09-14 15:22:14 -05:00
bool db_engine::unionComp(){
return false;
}