Skip to content

Commit c14d884

Browse files
committed
Mutualize code into tryExecuteStep() from PR #142 using SQLITE_MISUSE when statement needs to be reseted
1 parent 94c7897 commit c14d884

File tree

2 files changed

+28
-50
lines changed

2 files changed

+28
-50
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ class Statement
628628
}
629629

630630
/**
631-
* @brief Check if there is a row of result returnes by executeStep(), else throw a SQLite::Exception.
631+
* @brief Check if there is a row of result returned by executeStep(), else throw a SQLite::Exception.
632632
*/
633633
inline void checkRow() const
634634
{

src/Statement.cpp

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -244,87 +244,65 @@ void Statement::bind(const char* apName)
244244
// Execute a step of the query to fetch one row of results
245245
bool Statement::executeStep()
246246
{
247-
if (false == mbDone)
247+
const int ret = tryExecuteStep();
248+
if ((SQLITE_ROW != ret) && (SQLITE_DONE != ret)) // on row or no (more) row ready, else it's a problem
248249
{
249-
const int ret = sqlite3_step(mStmtPtr);
250-
if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it
251-
{
252-
mbOk = true;
253-
}
254-
else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing
255-
{
256-
mbOk = false;
257-
mbDone = true;
258-
}
259-
else
260-
{
261-
mbOk = false;
262-
mbDone = false;
263-
throw SQLite::Exception(mStmtPtr, ret);
264-
}
265-
}
266-
else
267-
{
268-
throw SQLite::Exception("Statement needs to be reseted.");
250+
throw SQLite::Exception(mStmtPtr, ret);
269251
}
270252

271253
return mbOk; // true only if one row is accessible by getColumn(N)
272254
}
273255

274-
int Statement::tryExecuteStep() noexcept
256+
// Execute a one-step query with no expected result
257+
int Statement::exec()
275258
{
276-
const int ret = sqlite3_step(mStmtPtr);
277-
if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it
278-
{
279-
mbOk = true;
280-
}
281-
else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing
282-
{
283-
mbOk = false;
284-
mbDone = true;
285-
}
286-
else
259+
const int ret = tryExecuteStep();
260+
if (SQLITE_DONE != ret) // the statement has finished executing successfully
287261
{
288-
mbOk = false;
289-
mbDone = false;
262+
if (SQLITE_ROW == ret)
263+
{
264+
throw SQLite::Exception("exec() does not expect results. Use executeStep.");
265+
}
266+
else
267+
{
268+
throw SQLite::Exception(mStmtPtr, ret);
269+
}
290270
}
291271

292-
return ret;
272+
// Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE)
273+
return sqlite3_changes(mStmtPtr);
293274
}
294275

295-
// Execute a one-step query with no expected result
296-
int Statement::exec()
276+
int Statement::tryExecuteStep() noexcept
297277
{
298278
if (false == mbDone)
299279
{
300280
const int ret = sqlite3_step(mStmtPtr);
301-
if (SQLITE_DONE == ret) // the statement has finished executing successfully
281+
if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it
302282
{
303-
mbOk = false;
304-
mbDone = true;
283+
mbOk = true;
305284
}
306-
else if (SQLITE_ROW == ret)
285+
else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing
307286
{
308287
mbOk = false;
309-
mbDone = false;
310-
throw SQLite::Exception("exec() does not expect results. Use executeStep.");
288+
mbDone = true;
311289
}
312290
else
313291
{
314292
mbOk = false;
315293
mbDone = false;
316-
throw SQLite::Exception(mStmtPtr, ret);
317294
}
295+
296+
return ret;
318297
}
319298
else
320299
{
321-
throw SQLite::Exception("Statement need to be reseted.");
300+
// Statement needs to be reseted !
301+
return SQLITE_MISUSE;
322302
}
323-
324-
// Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE)
325-
return sqlite3_changes(mStmtPtr);
326303
}
327304

305+
328306
// Return a copy of the column data specified by its index starting at 0
329307
// (use the Column copy-constructor)
330308
Column Statement::getColumn(const int aIndex)

0 commit comments

Comments
 (0)