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/Sql.cpp | 21 +++++++++++++++ src/sql/SqlExec.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sql/SqlQuery.cpp | 14 ---------- 3 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 src/sql/Sql.cpp create mode 100644 src/sql/SqlExec.cpp (limited to 'src/sql') diff --git a/src/sql/Sql.cpp b/src/sql/Sql.cpp new file mode 100644 index 0000000..754a30d --- /dev/null +++ b/src/sql/Sql.cpp @@ -0,0 +1,21 @@ +#include "../../include/odhtdb/sql/Sql.hpp" +#include + +namespace odhtdb +{ + int SqlArg::bind(sqlite3_stmt *stmt, int paramIndex) const + { + switch(type) + { + case Type::DATA_VIEW: + return sqlite3_bind_blob(stmt, paramIndex, dataView.data, dataView.size, SQLITE_STATIC); + case Type::INT: + return sqlite3_bind_int(stmt, paramIndex, integer); + case Type::INT64: + return sqlite3_bind_int64(stmt, paramIndex, integer64); + case Type::UINT64: // TODO: Find a way to use u64 in sqlite + return sqlite3_bind_int64(stmt, paramIndex, uinteger64); + } + return SQLITE_OK; + } +} 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({}); + } +} diff --git a/src/sql/SqlQuery.cpp b/src/sql/SqlQuery.cpp index 47f1463..b99f92d 100644 --- a/src/sql/SqlQuery.cpp +++ b/src/sql/SqlQuery.cpp @@ -3,20 +3,6 @@ namespace odhtdb { - int SqlArg::bind(sqlite3_stmt *stmt, int paramIndex) const - { - switch(type) - { - case Type::DATA_VIEW: - return sqlite3_bind_blob(stmt, paramIndex, dataView.data, dataView.size, SQLITE_STATIC); - case Type::INT: - return sqlite3_bind_int(stmt, paramIndex, integer); - case Type::INT64: - return sqlite3_bind_int64(stmt, paramIndex, integer64); - } - return SQLITE_OK; - } - SqlQuery::SqlQuery(sqlite3 *_db, const char *sql, std::initializer_list args) : db(_db), stmt(nullptr), -- cgit v1.2.3