mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-02-06 08:05:54 +01:00
This means indexing errors will not result in a crash due to vectors using CRASH_BAD_INDEX error macros.
96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
#include "sqlite3_query_result.h"
|
|
|
|
#include "./sqlite/sqlite3.h"
|
|
#include "core/error/error_macros.h"
|
|
#include "core/string/print_string.h"
|
|
#include "core/string/ustring.h"
|
|
#include <cstdio>
|
|
|
|
bool Sqlite3QueryResult::next_row() {
|
|
_current_cell_index = 0;
|
|
|
|
return ++current_row < rows.size();
|
|
}
|
|
|
|
int Sqlite3QueryResult::get_stored_row_count() {
|
|
return rows.size();
|
|
}
|
|
|
|
String Sqlite3QueryResult::get_cell(const int index) {
|
|
ERR_FAIL_INDEX_V(current_row, rows.size(), String());
|
|
ERR_FAIL_INDEX_V(index, rows[current_row]->cells.size(), String());
|
|
|
|
return rows[current_row]->cells[index].data;
|
|
}
|
|
|
|
bool Sqlite3QueryResult::is_cell_null(const int index) {
|
|
ERR_FAIL_INDEX_V(current_row, rows.size(), true);
|
|
ERR_FAIL_INDEX_V(index, rows[current_row]->cells.size(), true);
|
|
|
|
return rows[current_row]->cells[index].null;
|
|
}
|
|
|
|
int Sqlite3QueryResult::get_cell_count() {
|
|
if (current_row < 0 || rows.size() >= current_row) {
|
|
return 0;
|
|
}
|
|
|
|
return rows[current_row]->cells.size();
|
|
}
|
|
|
|
int Sqlite3QueryResult::get_last_insert_rowid() {
|
|
return sqlite3_last_insert_rowid(_connection);
|
|
}
|
|
|
|
String Sqlite3QueryResult::get_error_message() {
|
|
return String(err_msg);
|
|
}
|
|
|
|
void Sqlite3QueryResult::query(const String &query, sqlite3 *conn) {
|
|
_connection = conn;
|
|
|
|
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));
|
|
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 = memnew(Sqlite3QueryResultRow);
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
Cell c;
|
|
|
|
if (argv[i]) {
|
|
c.data = String::utf8(argv[i]);
|
|
} else {
|
|
c.null = true;
|
|
}
|
|
|
|
r->cells.push_back(c);
|
|
}
|
|
|
|
res->rows.push_back(r);
|
|
|
|
return 0;
|
|
}
|
|
|
|
Sqlite3QueryResult::Sqlite3QueryResult() {
|
|
err_msg = nullptr;
|
|
current_row = -1;
|
|
}
|
|
|
|
Sqlite3QueryResult::~Sqlite3QueryResult() {
|
|
for (int i = 0; i < rows.size(); ++i) {
|
|
memdelete(rows[i]);
|
|
}
|
|
}
|