aboutsummaryrefslogtreecommitdiff
path: root/src/sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/Sql.cpp21
-rw-r--r--src/sql/SqlExec.cpp72
-rw-r--r--src/sql/SqlQuery.cpp14
3 files changed, 93 insertions, 14 deletions
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 <sqlite3.h>
+
+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 <sqlite3.h>
+
+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<SqlArg> args)
+ {
+ std::lock_guard<std::mutex> 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<SqlArg> args) :
db(_db),
stmt(nullptr),