From 06ef029280d7304cb15a222b0584635e1130f4b0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 1 May 2021 21:53:01 +0200 Subject: [PATCH] Implement sqlite result, and more things to the sqlite db backend. --- database/sqlite/sqlite3_database.cpp | 14 ++++++++++- database/sqlite/sqlite3_query_result.cpp | 32 ++++++++++++++++++++++-- database/sqlite/sqlite3_query_result.h | 18 +++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/database/sqlite/sqlite3_database.cpp b/database/sqlite/sqlite3_database.cpp index 2570fd9..27ba92e 100644 --- a/database/sqlite/sqlite3_database.cpp +++ b/database/sqlite/sqlite3_database.cpp @@ -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() : diff --git a/database/sqlite/sqlite3_query_result.cpp b/database/sqlite/sqlite3_query_result.cpp index d772199..f83403c 100644 --- a/database/sqlite/sqlite3_query_result.cpp +++ b/database/sqlite/sqlite3_query_result.cpp @@ -3,15 +3,43 @@ #include 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(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]; + } } \ No newline at end of file diff --git a/database/sqlite/sqlite3_query_result.h b/database/sqlite/sqlite3_query_result.h index c5e71ae..7703381 100644 --- a/database/sqlite/sqlite3_query_result.h +++ b/database/sqlite/sqlite3_query_result.h @@ -1,6 +1,9 @@ #ifndef MYSQL_QUERY_RESULT_H #define MYSQL_QUERY_RESULT_H +#include +#include + #include "core/query_result.h" #include @@ -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 cells; + }; + + char **col_names; + std::vector rows; + int current_row; }; #endif \ No newline at end of file