Skip to content

Commit

Permalink
Added tryExecuteStep and tryReset
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyHenrik committed Aug 28, 2017
1 parent 1a46a94 commit 8191046
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class Statement

/// 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.
Expand Down Expand Up @@ -335,6 +339,7 @@ class Statement
* 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
Expand All @@ -345,6 +350,19 @@ class Statement
* @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.
Expand All @@ -359,6 +377,7 @@ class Statement
* - 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)
Expand Down
28 changes: 28 additions & 0 deletions src/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 8191046

Please sign in to comment.