From 20f415026f72c175311fb7c657006d8b317ea014 Mon Sep 17 00:00:00 2001 From: mbandic Date: Thu, 31 Oct 2024 10:33:43 +0000 Subject: [PATCH] Prepare statement --- .vscode/settings.json | 48 ++++++++++++++++++++++++++++++++++++++++++- lib/sqlite3.hpp | 3 ++- src/sqlite3.cpp | 43 +++++++++++++++++++++++++++++++++++++- test/test.cpp | 2 +- 4 files changed, 92 insertions(+), 4 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 94ddb0b..de7e3a3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,52 @@ "type_traits": "cpp", "tuple": "cpp", "array": "cpp", - "utility": "cpp" + "utility": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "atomic": "cpp", + "bit": "cpp", + "chrono": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "map": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" } } \ No newline at end of file diff --git a/lib/sqlite3.hpp b/lib/sqlite3.hpp index 037fc52..18d64d8 100644 --- a/lib/sqlite3.hpp +++ b/lib/sqlite3.hpp @@ -34,7 +34,8 @@ public: SQLite3(const string path, bool _keepOpen = false); - map> query(const string sql_command); + // map> query(const string sql_command); + std::map> query(const std::string& sql_command, const std::vector& values); ~SQLite3(); diff --git a/src/sqlite3.cpp b/src/sqlite3.cpp index 3cbf82e..2707b85 100644 --- a/src/sqlite3.cpp +++ b/src/sqlite3.cpp @@ -23,7 +23,7 @@ bool SQLite3::close() { db = NULL; return operationStatus != SQLITE_OK; } - +/* map> SQLite3::query(const string sql_command) { io.lock(); if (!keepOpen) { @@ -68,7 +68,48 @@ map> SQLite3::query(const string sql_command) { io.unlock(); return _parsed; } +*/ +map> SQLite3::query(const string& sql_command, const vector& values) { + lock_guard lock(io); + map> results; + + if (!keepOpen) { + if (open()) { + throw string("[ERROR] Unable to open database "); + } + } + + sqlite3_stmt* stmt; + if (sqlite3_prepare_v2(db, sql_command.c_str(), -1, &stmt, nullptr) != SQLITE_OK) { + throw string("[ERROR] Failed to prepare statement: ") + sqlite3_errmsg(db); + } + + // Binduj parametre + for (size_t i = 0; i < values.size(); ++i) { + sqlite3_bind_text(stmt, static_cast(i + 1), values[i].c_str(), -1, SQLITE_TRANSIENT); + } + + // Dohvati rezultate + int columnCount = sqlite3_column_count(stmt); + while (sqlite3_step(stmt) == SQLITE_ROW) { + for (int i = 0; i < columnCount; ++i) { + const char* columnName = sqlite3_column_name(stmt, i); + const char* columnValue = (const char*)sqlite3_column_text(stmt, i); + results[columnName].push_back(columnValue ? columnValue : "NULL"); + } + } + + sqlite3_finalize(stmt); // Oslobodi resurse vezane uz izjavu + + if (!keepOpen) { + if (close()) { + throw string("[ERROR] Unable to close database "); + } + } + + return results; +} map> SQLite3::parse(const string& result) { string a = result; diff --git a/test/test.cpp b/test/test.cpp index f1f4541..fc55811 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -13,7 +13,7 @@ int main() { // cout << mydb.ask("Select * from Tab1"); // cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');"); - auto res = mydb.query("Select * from Tab1"); + auto res = mydb.query("Select * from Tab1 Where id > ?", {"3"}); // cout << endl << res["NAME"][1]; // cout << endl << res["MAIL"][1];