Implement sqlite result, and more things to the sqlite db backend.

This commit is contained in:
Relintai 2021-05-01 21:53:01 +02:00
parent ec8de94827
commit 06ef029280
3 changed files with 61 additions and 3 deletions

View File

@ -2,6 +2,8 @@
#include "core/database_manager.h"
#include "sqlite3_query_result.h"
Database *SQLite3Database::_creation_func() {
return new SQLite3Database();
}
@ -24,10 +26,20 @@ void SQLite3Database::connect(const std::string &connection_str) {
}
QueryResult *SQLite3Database::query(const std::string &query) {
return nullptr;
Sqlite3QueryResult *res = new Sqlite3QueryResult();
res->query(query, conn);
return res;
}
void SQLite3Database::query_run(const std::string &query) {
char *err_msg;
if (sqlite3_exec(conn, query.c_str(), NULL, NULL, &err_msg) != SQLITE_OK) {
printf("SQLite3Database::query_run error: \nQuery: %s \n Error:\n %s\n", query.c_str(), err_msg);
sqlite3_free(err_msg);
}
}
SQLite3Database::SQLite3Database() :

View File

@ -3,15 +3,43 @@
#include <cstdio>
bool Sqlite3QueryResult::next_row() {
return false;
return ++current_row < rows.size();
}
const char* Sqlite3QueryResult::get_cell(const int index) {
return nullptr;
return rows[current_row]->cells[index].c_str();
}
void Sqlite3QueryResult::query(const std::string &query, sqlite3 *conn) {
if (sqlite3_exec(conn, query.c_str(), Sqlite3QueryResult::run_query_finished, this, &err_msg) != SQLITE_OK) {
printf("SQLite3Database::query error: \nQuery: %s \n Error:\n %s\n", query.c_str(), err_msg);
sqlite3_free(err_msg);
}
}
int Sqlite3QueryResult::run_query_finished(void *data, int argc, char **argv, char **col_names) {
Sqlite3QueryResult *res = reinterpret_cast<Sqlite3QueryResult*>(data);
//res->col_names = col_names;
Sqlite3QueryResultRow *r = new Sqlite3QueryResultRow();
for (int i = 0; i < argc; ++i) {
r->cells.push_back(argv[i]);
}
res->rows.push_back(r);
return 0;
}
Sqlite3QueryResult::Sqlite3QueryResult() : QueryResult() {
err_msg = nullptr;
current_row = -1;
}
Sqlite3QueryResult::~Sqlite3QueryResult() {
for (int i = 0; i < rows.size(); ++i) {
delete rows[i];
}
}

View File

@ -1,6 +1,9 @@
#ifndef MYSQL_QUERY_RESULT_H
#define MYSQL_QUERY_RESULT_H
#include <string>
#include <vector>
#include "core/query_result.h"
#include <sqlite3.h>
@ -10,8 +13,23 @@ public:
bool next_row();
const char* get_cell(const int index);
void query(const std::string &query, sqlite3 *conn);
static int run_query_finished(void *data, int argc, char **argv, char **col_names);
Sqlite3QueryResult();
~Sqlite3QueryResult();
char* err_msg;
public:
struct Sqlite3QueryResultRow {
std::vector<std::string> cells;
};
char **col_names;
std::vector<Sqlite3QueryResultRow *> rows;
int current_row;
};
#endif