Skip to content

Commit 4e770eb

Browse files
committed
Statement now stores the status of the last operation so it can be checked in the pointer destructor
1 parent 55edadd commit 4e770eb

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

include/SQLiteCpp/Statement.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Statement
5050
* @brief Compile and register the SQL query for the provided SQLite Database Connection
5151
*
5252
* @param[in] aDatabase the SQLite Database Connection
53-
* @param[in] apQuery an UTF-8 encoded query string
53+
* @param[in] apQuery an UTF-8 encoded query string
5454
*
5555
* Exception is thrown in case of error, then the Statement object is NOT constructed.
5656
*/
@@ -362,6 +362,16 @@ class Statement
362362
return mpStmt;
363363
}
364364

365+
int getLastStatus () const
366+
{
367+
return mLastStatus ;
368+
}
369+
370+
void setLastStatus (int status)
371+
{
372+
mLastStatus = status ;
373+
}
374+
365375
private:
366376
/// @{ Unused/forbidden copy operator
367377
Ptr& operator=(const Ptr& aPtr);
@@ -372,6 +382,7 @@ class Statement
372382
sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object
373383
unsigned int* mpRefCount; //!< Pointer to the heap allocated reference counter of the sqlite3_stmt
374384
//!< (to share it with Column objects)
385+
int mLastStatus;//!< The return status of the last statement evaluation
375386
};
376387

377388
private:
@@ -385,7 +396,7 @@ class Statement
385396
*
386397
* @param[in] SQLite return code to test against the SQLITE_OK expected value
387398
*/
388-
void check(const int aRet) const;
399+
void check(const int aRet);
389400

390401
private:
391402
std::string mQuery; //!< UTF-8 SQL Query

src/Statement.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ bool Statement::executeStep()
181181
{
182182
if (false == mbDone)
183183
{
184-
int ret = sqlite3_step(mStmtPtr);
184+
int ret = sqlite3_step(mStmtPtr) ;
185+
mStmtPtr.setLastStatus (ret);
185186
if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it
186187
{
187188
mbOk = true;
@@ -200,7 +201,7 @@ bool Statement::executeStep()
200201
}
201202
else
202203
{
203-
throw SQLite::Exception("Statement need to be reseted");
204+
throw SQLite::Exception("Statement needs to be reset");
204205
}
205206

206207
return mbOk; // true only if one row is accessible by getColumn(N)
@@ -212,6 +213,7 @@ int Statement::exec()
212213
if (false == mbDone)
213214
{
214215
int ret = sqlite3_step(mStmtPtr);
216+
mStmtPtr.setLastStatus (ret);
215217
if (SQLITE_DONE == ret) // the statement has finished executing successfully
216218
{
217219
mbOk = false;
@@ -272,8 +274,9 @@ bool Statement::isColumnNull(const int aIndex) const
272274
}
273275

274276
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
275-
void Statement::check(const int aRet) const
277+
void Statement::check(const int aRet)
276278
{
279+
mStmtPtr.setLastStatus (aRet) ;
277280
if (SQLITE_OK != aRet)
278281
{
279282
throw SQLite::Exception(sqlite3_errmsg(mStmtPtr));
@@ -294,10 +297,11 @@ void Statement::check(const int aRet) const
294297
Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
295298
mpSQLite(apSQLite),
296299
mpStmt(NULL),
297-
mpRefCount(NULL)
300+
mpRefCount(NULL),
301+
mLastStatus(SQLITE_OK)
298302
{
299-
int ret = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast<int>(aQuery.size()), &mpStmt, NULL);
300-
if (SQLITE_OK != ret)
303+
mLastStatus = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast<int>(aQuery.size()), &mpStmt, NULL);
304+
if (SQLITE_OK != mLastStatus)
301305
{
302306
throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
303307
}
@@ -315,7 +319,8 @@ Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
315319
Statement::Ptr::Ptr(const Statement::Ptr& aPtr) :
316320
mpSQLite(aPtr.mpSQLite),
317321
mpStmt(aPtr.mpStmt),
318-
mpRefCount(aPtr.mpRefCount)
322+
mpRefCount(aPtr.mpRefCount),
323+
mLastStatus(SQLITE_OK)
319324
{
320325
assert(NULL != mpRefCount);
321326
assert(0 != *mpRefCount);
@@ -341,7 +346,7 @@ Statement::Ptr::~Ptr() noexcept // nothrow
341346
// as no Statement not Column objet use it anymore
342347
int ret = sqlite3_finalize(mpStmt);
343348
// Never throw an exception in a destructor
344-
SQLITECPP_ASSERT(SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
349+
SQLITECPP_ASSERT((SQLITE_OK == ret || mLastStatus == ret), sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
345350

346351
// and delete the reference counter
347352
delete mpRefCount;

0 commit comments

Comments
 (0)