summaryrefslogtreecommitdiff
path: root/src/database/sqlite3_statement.hpp
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2017-12-16 16:32:32 +0100
committerlouiz’ <louiz@louiz.org>2017-12-16 16:32:32 +0100
commit2c4016a4898a050c7f6ebd1843b265e209da1704 (patch)
treeb6c57d4fa446fe588cac6999c498dc1d7587dd9e /src/database/sqlite3_statement.hpp
parentaaa9de8fe2c67b67310b7ba78c607bddbf4b25bf (diff)
parentb1f850b6395610c738a8e58abcdf2abfca3edd4e (diff)
downloadbiboumi-2c4016a4898a050c7f6ebd1843b265e209da1704.tar.gz
biboumi-2c4016a4898a050c7f6ebd1843b265e209da1704.tar.bz2
biboumi-2c4016a4898a050c7f6ebd1843b265e209da1704.tar.xz
biboumi-2c4016a4898a050c7f6ebd1843b265e209da1704.zip
Merge branch 'postgresql' into 'master'
Add postgresql support Closes #3237 See merge request louiz/biboumi!18
Diffstat (limited to 'src/database/sqlite3_statement.hpp')
-rw-r--r--src/database/sqlite3_statement.hpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/database/sqlite3_statement.hpp b/src/database/sqlite3_statement.hpp
new file mode 100644
index 0000000..42a5220
--- /dev/null
+++ b/src/database/sqlite3_statement.hpp
@@ -0,0 +1,93 @@
+#pragma once
+
+#include <database/statement.hpp>
+
+#include <logger/logger.hpp>
+
+#include <sqlite3.h>
+
+class Sqlite3Statement: public Statement
+{
+ public:
+ Sqlite3Statement(sqlite3_stmt* stmt):
+ stmt(stmt) {}
+ ~Sqlite3Statement()
+ {
+ sqlite3_finalize(this->stmt);
+ }
+
+ StepResult step() override final
+ {
+ auto res = sqlite3_step(this->get());
+ log_debug("step: ", res);
+ if (res == SQLITE_ROW)
+ return StepResult::Row;
+ else if (res == SQLITE_DONE)
+ return StepResult::Done;
+ else
+ return StepResult::Error;
+ }
+
+ void bind(std::vector<std::string> params) override
+ {
+ int i = 1;
+ for (const std::string& param: params)
+ {
+ if (sqlite3_bind_text(this->get(), i, param.data(), static_cast<int>(param.size()), SQLITE_TRANSIENT) != SQLITE_OK)
+ log_error("Failed to bind ", param, " to param ", i);
+ i++;
+ }
+ }
+
+ int64_t get_column_int64(const int col) override
+ {
+ return sqlite3_column_int64(this->get(), col);
+ }
+
+ std::string get_column_text(const int col) override
+ {
+ const auto size = sqlite3_column_bytes(this->get(), col);
+ const unsigned char* str = sqlite3_column_text(this->get(), col);
+ std::string result(reinterpret_cast<const char*>(str), static_cast<std::size_t>(size));
+ return result;
+ }
+
+ bool bind_text(const int pos, const std::string& data) override
+ {
+ return sqlite3_bind_text(this->get(), pos, data.data(), static_cast<int>(data.size()), SQLITE_TRANSIENT) == SQLITE_OK;
+ }
+ bool bind_int64(const int pos, const std::int64_t value) override
+ {
+ return sqlite3_bind_int64(this->get(), pos, static_cast<sqlite3_int64>(value)) == SQLITE_OK;
+ }
+ bool bind_null(const int pos) override
+ {
+ return sqlite3_bind_null(this->get(), pos) == SQLITE_OK;
+ }
+ int get_column_int(const int col) override
+ {
+ return sqlite3_column_int(this->get(), col);
+ }
+
+ Sqlite3Statement(const Sqlite3Statement&) = delete;
+ Sqlite3Statement& operator=(const Sqlite3Statement&) = delete;
+ Sqlite3Statement(Sqlite3Statement&& other):
+ stmt(other.stmt)
+ {
+ other.stmt = nullptr;
+ }
+ Sqlite3Statement& operator=(Sqlite3Statement&& other)
+ {
+ this->stmt = other.stmt;
+ other.stmt = nullptr;
+ return *this;
+ }
+ sqlite3_stmt* get()
+ {
+ return this->stmt;
+ }
+
+ private:
+ sqlite3_stmt* stmt;
+ int last_step_result{SQLITE_OK};
+};