2022-06-30 13:23:48 +02:00
|
|
|
#include "sqlite3_query_result.h"
|
|
|
|
|
2022-07-06 22:14:37 +02:00
|
|
|
#include "./sqlite/sqlite3.h"
|
|
|
|
#include "core/print_string.h"
|
|
|
|
#include "core/ustring.h"
|
2022-06-30 13:23:48 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
bool Sqlite3QueryResult::next_row() {
|
|
|
|
return ++current_row < rows.size();
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:14:37 +02:00
|
|
|
String Sqlite3QueryResult::get_cell(const int index) {
|
|
|
|
return rows[current_row]->cells[index].data;
|
2022-06-30 13:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sqlite3QueryResult::is_cell_null(const int index) {
|
|
|
|
return rows[current_row]->cells[index].null;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sqlite3QueryResult::get_last_insert_rowid() {
|
|
|
|
return sqlite3_last_insert_rowid(_connection);
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:14:37 +02:00
|
|
|
String Sqlite3QueryResult::get_error_message() {
|
|
|
|
return String(err_msg);
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:23:48 +02:00
|
|
|
void Sqlite3QueryResult::query(const String &query, sqlite3 *conn) {
|
|
|
|
_connection = conn;
|
|
|
|
|
2022-07-06 22:14:37 +02:00
|
|
|
CharString q = query.utf8();
|
|
|
|
|
|
|
|
if (sqlite3_exec(conn, q.get_data(), Sqlite3QueryResult::run_query_finished, this, &err_msg) != SQLITE_OK) {
|
|
|
|
ERR_PRINT("SQLite3Database::query error: ");
|
|
|
|
ERR_PRINT("Query: " + query);
|
|
|
|
ERR_PRINT("Error: " + String(err_msg));
|
2022-06-30 13:23:48 +02:00
|
|
|
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;
|
|
|
|
|
2022-07-06 22:14:37 +02:00
|
|
|
Sqlite3QueryResultRow *r = memnew(Sqlite3QueryResultRow);
|
2022-06-30 13:23:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
Cell c;
|
|
|
|
|
|
|
|
if (argv[i]) {
|
|
|
|
c.data = argv[i];
|
|
|
|
} else {
|
|
|
|
c.null = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->cells.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
res->rows.push_back(r);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:14:37 +02:00
|
|
|
Sqlite3QueryResult::Sqlite3QueryResult() {
|
2022-06-30 13:23:48 +02:00
|
|
|
err_msg = nullptr;
|
|
|
|
current_row = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sqlite3QueryResult::~Sqlite3QueryResult() {
|
|
|
|
for (int i = 0; i < rows.size(); ++i) {
|
2022-07-06 22:14:37 +02:00
|
|
|
memdelete(rows[i]);
|
2022-06-30 13:23:48 +02:00
|
|
|
}
|
2022-07-06 22:14:37 +02:00
|
|
|
}
|