From 080487c961b424c9dd822d38ec8c2c4d79aff24b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 14 May 2018 20:13:24 +0200 Subject: Only download nodes that we are missing --- src/sql/SqlExec.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/sql/SqlExec.cpp (limited to 'src/sql/SqlExec.cpp') diff --git a/src/sql/SqlExec.cpp b/src/sql/SqlExec.cpp new file mode 100644 index 0000000..732b2f1 --- /dev/null +++ b/src/sql/SqlExec.cpp @@ -0,0 +1,72 @@ +#include "../../include/odhtdb/sql/SqlExec.hpp" +#include + +namespace odhtdb +{ + SqlExec::SqlExec(sqlite3 *_db, const char *sql) : + db(_db), + stmt(nullptr) + { + int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr); + if(rc != SQLITE_OK) + { + std::string errMsg = "Failed to prepare sqlite statement, error: "; + errMsg += sqlite3_errmsg(db); + sqlite3_exec(db, "ROLLBACK", 0, 0, 0); + throw SqlExecException(errMsg); + } + } + + SqlExec::~SqlExec() + { + sqlite3_finalize(stmt); + } + + void SqlExec::execWithArgs(std::initializer_list args) + { + std::lock_guard lock(mutex); + + sqlite3_reset(stmt); + sqlite3_clear_bindings(stmt); + + int numParams = sqlite3_bind_parameter_count(stmt); + if(args.size() != numParams) + { + std::string errMsg = "Failed to prepare sqlite statement, error: Sql has "; + errMsg += std::to_string(numParams); + errMsg += " parameters, got "; + errMsg += std::to_string(args.size()); + errMsg += " arguments"; + sqlite3_exec(db, "ROLLBACK", 0, 0, 0); + throw SqlExecException(errMsg); + } + + int paramIndex = 1; + for(const SqlArg &arg : args) + { + int rc = arg.bind(stmt, paramIndex); + if(rc != SQLITE_OK) + { + std::string errMsg = "Failed to bind param, error code: "; + errMsg += std::to_string(rc); + sqlite3_exec(db, "ROLLBACK", 0, 0, 0); + throw SqlExecException(errMsg); + } + ++paramIndex; + } + + int rc = sqlite3_step(stmt); + if(rc != SQLITE_DONE) + { + std::string errMsg = "Failed to perform sql exec, error: "; + errMsg += sqlite3_errmsg(db); + sqlite3_exec(db, "ROLLBACK", 0, 0, 0); + throw SqlExecException(errMsg); + } + } + + void SqlExec::exec() + { + execWithArgs({}); + } +} -- cgit v1.2.3