From 8191046ea5e36f9471f44cc27defab04ae5de727 Mon Sep 17 00:00:00 2001 From: Henrik Jakobsson Majava Date: Mon, 28 Aug 2017 11:00:17 +0200 Subject: [PATCH] Added tryExecuteStep and tryReset --- include/SQLiteCpp/Statement.h | 19 +++++++++++++++++++ src/Statement.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index b4f478a..13a122e 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -77,6 +77,10 @@ public: /// Reset the statement to make it ready for a new execution. void reset(); + + /// Reset the statement to make it ready for a new execution. Returns the sqlite result code + /// instead of throwing an exception on error. + int tryReset() noexcept; /** * @brief Clears away all the bindings of a prepared statement. @@ -335,6 +339,7 @@ public: * thru the getColumn() method * * @see exec() execute a one-step prepared statement with no expected result + * @see tryExecuteStep() try to execute a step of the prepared query to fetch one row of results, returning the sqlite result code. * @see Database::exec() is a shortcut to execute one or multiple statements without results * * @return - true (SQLITE_ROW) if there is another row ready : you can call getColumn(N) to get it @@ -345,6 +350,19 @@ public: * @throw SQLite::Exception in case of error */ bool executeStep(); + + /** + * @brief Try to execute a step of the prepared query to fetch one row of results, returning the sqlite result code. + * + * + * + * @see exec() execute a one-step prepared statement with no expected result + * @see executeStep() execute a step of the prepared query to fetch one row of results + * @see Database::exec() is a shortcut to execute one or multiple statements without results + * + * @return the sqlite result code. + */ + int tryExecuteStep() noexcept; /** * @brief Execute a one-step query with no expected result. @@ -359,6 +377,7 @@ public: * - reusing it allows for better performances (efficient for multiple insertion). * * @see executeStep() execute a step of the prepared query to fetch one row of results + * @see tryExecuteStep() try to execute a step of the prepared query to fetch one row of results, returning the sqlite result code. * @see Database::exec() is a shortcut to execute one or multiple statements without results * * @return number of row modified by this SQL statement (INSERT, UPDATE or DELETE) diff --git a/src/Statement.cpp b/src/Statement.cpp index f4f430c..11068a6 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -58,6 +58,14 @@ void Statement::reset() check(ret); } +int Statement::tryReset() noexcept +{ + mbOk = false; + mbDone = false; + const int ret = sqlite3_reset(mStmtPtr); + return ret; +} + // Clears away all the bindings of a prepared statement (can be associated with #reset() above). void Statement::clearBindings() { @@ -266,6 +274,26 @@ bool Statement::executeStep() 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() {