aboutsummaryrefslogtreecommitdiff
path: root/frontend/DB.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/DB.cpp')
-rw-r--r--frontend/DB.cpp24
1 files changed, 20 insertions, 4 deletions
diff --git a/frontend/DB.cpp b/frontend/DB.cpp
index 28fd164..d4527b8 100644
--- a/frontend/DB.cpp
+++ b/frontend/DB.cpp
@@ -51,7 +51,10 @@ DBStatement & DBStatement::bind(const int & number) {
return *this;
}
-DBStatement & DBStatement::unbind() {
+DBStatement & DBStatement::reset() {
+ int ret = sqlite3_reset(this->stmt.get());
+ if (ret != SQLITE_OK)
+ throw Exception("sqlite3_reset: %d", ret);
this->param_index = 1;
return *this;
}
@@ -64,7 +67,7 @@ void DBStatement::execute() {
DBQueryRow DBStatement::row() {
int ret = sqlite3_step(this->stmt.get());
- if (ret != SQLITE_ROW)
+ if (ret != SQLITE_ROW && ret != SQLITE_DONE)
throw Exception("sqlite3_step: %d", ret);
return { *this };
@@ -72,13 +75,26 @@ DBQueryRow DBStatement::row() {
DBQueryRow::DBQueryRow(DBStatement & parent) : parent(parent) { }
+
template <>
-const char * DBQueryRow::col<const char*>(int index) {
+const char * DBQueryRow::col<const char*>(int index, const char * const & default_value) {
+ int type = sqlite3_column_type(this->parent.stmt.get(), index);
+ if (type == SQLITE_NULL) return default_value;
return reinterpret_cast<const char *>(sqlite3_column_text(this->parent.stmt.get(), index));
}
+template <>
+const char * DBQueryRow::col<const char*>(int index) {
+ return this->col<const char *>(index, "");
+}
template <>
-int DBQueryRow::col<int>(int index) {
+int DBQueryRow::col<int>(int index, const int & default_value) {
+ int type = sqlite3_column_type(this->parent.stmt.get(), index);
+ if (type == SQLITE_NULL) return default_value;
return sqlite3_column_int(this->parent.stmt.get(), index);
}
+template <>
+int DBQueryRow::col<int>(int index) {
+ return this->col<int>(index, 0);
+}