From b1d5d7936bed17a684daff15b0294ef70754e8b9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 12:24:43 +0100 Subject: more more WIP --- frontend/DB.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'frontend/DB.cpp') 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(int index) { +const char * DBQueryRow::col(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(sqlite3_column_text(this->parent.stmt.get(), index)); } +template <> +const char * DBQueryRow::col(int index) { + return this->col(index, ""); +} template <> -int DBQueryRow::col(int index) { +int DBQueryRow::col(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 index) { + return this->col(index, 0); +} -- cgit v1.2.3