From c14d884ba5b35179ec66e4603780f0c783a4c99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Mon, 28 Aug 2017 16:04:35 +0200 Subject: [PATCH] Mutualize code into tryExecuteStep() from PR #142 using SQLITE_MISUSE when statement needs to be reseted --- include/SQLiteCpp/Statement.h | 2 +- src/Statement.cpp | 92 +++++++++++++---------------------- 2 files changed, 36 insertions(+), 58 deletions(-) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 31f60cb..6309111 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -628,7 +628,7 @@ private: } /** - * @brief Check if there is a row of result returnes by executeStep(), else throw a SQLite::Exception. + * @brief Check if there is a row of result returned by executeStep(), else throw a SQLite::Exception. */ inline void checkRow() const { diff --git a/src/Statement.cpp b/src/Statement.cpp index 4b07ae1..5e03061 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -243,6 +243,37 @@ void Statement::bind(const char* apName) // Execute a step of the query to fetch one row of results bool Statement::executeStep() +{ + const int ret = tryExecuteStep(); + if ((SQLITE_ROW != ret) && (SQLITE_DONE != ret)) // on row or no (more) row ready, else it's a problem + { + throw SQLite::Exception(mStmtPtr, ret); + } + + return mbOk; // true only if one row is accessible by getColumn(N) +} + +// Execute a one-step query with no expected result +int Statement::exec() +{ + const int ret = tryExecuteStep(); + if (SQLITE_DONE != ret) // the statement has finished executing successfully + { + if (SQLITE_ROW == ret) + { + throw SQLite::Exception("exec() does not expect results. Use executeStep."); + } + else + { + throw SQLite::Exception(mStmtPtr, ret); + } + } + + // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE) + return sqlite3_changes(mStmtPtr); +} + +int Statement::tryExecuteStep() noexcept { if (false == mbDone) { @@ -260,70 +291,17 @@ bool Statement::executeStep() { mbOk = false; mbDone = false; - throw SQLite::Exception(mStmtPtr, ret); } + + return ret; } else { - throw SQLite::Exception("Statement needs to be reseted."); + // Statement needs to be reseted ! + return SQLITE_MISUSE; } - - return mbOk; // true only if one row is accessible by getColumn(N) } -int Statement::tryExecuteStep() noexcept -{ - const int ret = sqlite3_step(mStmtPtr); - if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it - { - mbOk = true; - } - else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing - { - mbOk = false; - mbDone = true; - } - else - { - mbOk = false; - mbDone = false; - } - - return ret; -} - -// Execute a one-step query with no expected result -int Statement::exec() -{ - if (false == mbDone) - { - const int ret = sqlite3_step(mStmtPtr); - if (SQLITE_DONE == ret) // the statement has finished executing successfully - { - mbOk = false; - mbDone = true; - } - else if (SQLITE_ROW == ret) - { - mbOk = false; - mbDone = false; - throw SQLite::Exception("exec() does not expect results. Use executeStep."); - } - else - { - mbOk = false; - mbDone = false; - throw SQLite::Exception(mStmtPtr, ret); - } - } - else - { - throw SQLite::Exception("Statement need to be reseted."); - } - - // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE) - return sqlite3_changes(mStmtPtr); -} // Return a copy of the column data specified by its index starting at 0 // (use the Column copy-constructor)