2018-03-01 16:19:13 +00:00
|
|
|
#include "resulttable.h"
|
|
|
|
|
|
|
|
ResultTable::ResultTable(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:48:55 +00:00
|
|
|
ResultTable::ResultTable(string query){
|
|
|
|
this->originalQuery = query;
|
|
|
|
}
|
|
|
|
|
2018-03-31 10:53:31 +00:00
|
|
|
ResultTable::ResultTable(int resultCode){
|
|
|
|
this->queryCode = resultCode;
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
void ResultTable::extractData(sqlite3_stmt *stmt){
|
|
|
|
this->values.clear();
|
|
|
|
int res = sqlite3_step(stmt);
|
|
|
|
while (res == SQLITE_ROW){
|
|
|
|
processRow(stmt);
|
|
|
|
res = sqlite3_step(stmt);
|
|
|
|
}
|
2018-03-03 07:38:32 +00:00
|
|
|
this->queryCode = res;
|
2018-03-01 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResultTable::processRow(sqlite3_stmt *stmt){
|
|
|
|
int colCount = sqlite3_column_count(stmt);
|
|
|
|
vector<string> currentRow;
|
|
|
|
|
|
|
|
for (int i = 0; i < colCount; i++){
|
|
|
|
currentRow.push_back(convertToString(sqlite3_column_value(stmt, i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->values.push_back(currentRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResultTable::printData(){
|
2018-03-31 10:53:31 +00:00
|
|
|
printf("--> Result Code: [%d] <--\n", this->getReturnCode());
|
2018-03-02 10:30:16 +00:00
|
|
|
if (this->isEmpty()){
|
|
|
|
printf("Result table is empty.\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-03 07:48:55 +00:00
|
|
|
printf("Printing table: [%d x %d]\t%s\n", this->rowCount(), this->columnCount(), this->originalQuery.c_str());
|
2018-03-01 16:19:13 +00:00
|
|
|
for (unsigned int row = 0; row < this->rowCount(); row++){
|
|
|
|
for (unsigned int col = 0; col < this->columnCount(); col++){
|
2018-03-02 10:30:16 +00:00
|
|
|
printf("| %s ", this->values[row][col].c_str());
|
2018-03-01 16:19:13 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResultTable::isEmpty(){
|
|
|
|
return this->values.empty();
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:29:10 +00:00
|
|
|
string ResultTable::at(unsigned int row, unsigned int col){
|
2018-03-01 16:19:13 +00:00
|
|
|
if (isIndexValid(row, col)){
|
|
|
|
return this->values[row][col];
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Out of bounds error while trying to get value in result table at [%d, %d].\n", row, col);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:38:32 +00:00
|
|
|
int ResultTable::getReturnCode(){
|
|
|
|
return this->queryCode;
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:48:55 +00:00
|
|
|
string ResultTable::getOriginalQuery(){
|
|
|
|
return this->originalQuery;
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
unsigned int ResultTable::columnCount(){
|
|
|
|
if (this->isEmpty()){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return this->values[0].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ResultTable::rowCount(){
|
|
|
|
if (this->isEmpty()){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return this->values.size();
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:29:10 +00:00
|
|
|
vector<vector<string> > ResultTable::rows(){
|
|
|
|
return this->values;
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:19:13 +00:00
|
|
|
string ResultTable::convertToString(sqlite3_value *val){
|
|
|
|
const unsigned char* raw_text = sqlite3_value_text(val);
|
|
|
|
if (raw_text == 0){
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
string st = (const char*) raw_text;
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResultTable::isIndexValid(unsigned int row, unsigned int col){
|
|
|
|
return (row < this->values.size()) && (col < this->values[0].size());
|
|
|
|
}
|