Added more getters to the query result.

This commit is contained in:
Relintai 2021-08-20 12:58:37 +02:00
parent af05bd037c
commit 1ae34a3500
4 changed files with 64 additions and 8 deletions

View File

@ -1,5 +1,7 @@
#include "query_result.h"
#include <sstream>
bool QueryResult::next_row() {
return false;
}
@ -8,6 +10,33 @@ const char *QueryResult::get_cell(const int index) {
return "";
}
const std::string QueryResult::get_cell_str(const int index) {
return std::string(get_cell(index));
}
const bool QueryResult::get_cell_bool(const int index) {
return get_cell_int(index);
}
const int QueryResult::get_cell_int(const int index) {
if (is_cell_null(index)) {
return 0;
}
//todo better way
std::stringstream ss;
ss.str(get_cell(index));
int r;
ss >> r;
return r;
}
bool QueryResult::is_cell_null(const int index) {
return true;
}
int QueryResult::get_last_insert_rowid() {
return 0;
}

View File

@ -1,11 +1,19 @@
#ifndef QUERY_RESULT_H
#define QUERY_RESULT_H
#include <string>
class QueryResult {
public:
virtual bool next_row();
virtual const char*get_cell(const int index);
virtual int get_last_insert_rowid();
virtual bool next_row();
virtual const char *get_cell(const int index);
virtual const std::string get_cell_str(const int index);
virtual const bool get_cell_bool(const int index);
virtual const int get_cell_int(const int index);
virtual bool is_cell_null(const int index);
virtual int get_last_insert_rowid();
QueryResult();
virtual ~QueryResult();

View File

@ -7,7 +7,11 @@ bool Sqlite3QueryResult::next_row() {
}
const char *Sqlite3QueryResult::get_cell(const int index) {
return rows[current_row]->cells[index].c_str();
return rows[current_row]->cells[index].data.c_str();
}
bool Sqlite3QueryResult::is_cell_null(const int index) {
return rows[current_row]->cells[index].null;
}
int Sqlite3QueryResult::get_last_insert_rowid() {
@ -31,12 +35,15 @@ int Sqlite3QueryResult::run_query_finished(void *data, int argc, char **argv, ch
Sqlite3QueryResultRow *r = new Sqlite3QueryResultRow();
for (int i = 0; i < argc; ++i) {
Cell c;
if (argv[i]) {
r->cells.push_back(argv[i]);
c.data = argv[i];
} else {
//todo mark nulls in cells
r->cells.push_back("NULL");
c.null = true;
}
r->cells.push_back(c);
}
res->rows.push_back(r);

View File

@ -12,6 +12,9 @@ class Sqlite3QueryResult : public QueryResult {
public:
bool next_row();
const char* get_cell(const int index);
bool is_cell_null(const int index);
int get_last_insert_rowid();
void query(const std::string &query, sqlite3 *conn);
@ -24,8 +27,17 @@ public:
char* err_msg;
public:
struct Cell {
bool null;
std::string data;
Cell() {
null = false;
}
};
struct Sqlite3QueryResultRow {
std::vector<std::string> cells;
std::vector<Cell> cells;
};
char **col_names;