Mutualize code into tryExecuteStep() from PR #142 using SQLITE_MISUSE when statement needs to be reseted

This commit is contained in:
Sébastien Rombauts 2017-08-28 16:04:35 +02:00
parent 94c7897d1b
commit c14d884ba5
2 changed files with 36 additions and 58 deletions

View File

@ -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
{

View File

@ -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)