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 inline void checkRow() const
{ {

View File

@ -244,34 +244,38 @@ void Statement::bind(const char* apName)
// Execute a step of the query to fetch one row of results // Execute a step of the query to fetch one row of results
bool Statement::executeStep() bool Statement::executeStep()
{ {
if (false == mbDone) const int ret = tryExecuteStep();
if ((SQLITE_ROW != ret) && (SQLITE_DONE != ret)) // on row or no (more) row ready, else it's a problem
{ {
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;
throw SQLite::Exception(mStmtPtr, ret); throw SQLite::Exception(mStmtPtr, ret);
} }
}
else
{
throw SQLite::Exception("Statement needs to be reseted.");
}
return mbOk; // true only if one row is accessible by getColumn(N) 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 int Statement::tryExecuteStep() noexcept
{
if (false == mbDone)
{ {
const int ret = sqlite3_step(mStmtPtr); const int ret = sqlite3_step(mStmtPtr);
if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it
@ -291,39 +295,13 @@ int Statement::tryExecuteStep() noexcept
return ret; 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 else
{ {
mbOk = false; // Statement needs to be reseted !
mbDone = false; return SQLITE_MISUSE;
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 // Return a copy of the column data specified by its index starting at 0
// (use the Column copy-constructor) // (use the Column copy-constructor)