Revert #31 Statement now stores the status of the last operation.

- the check in the destructor was not a good idea finaly,
   and I added instead a getErrorCode() method
This commit is contained in:
Sébastien Rombauts 2015-03-08 15:12:26 +01:00
parent bfdf288d3d
commit f31a459cd8
2 changed files with 7 additions and 26 deletions

View File

@ -372,16 +372,6 @@ public:
return mpStmt; return mpStmt;
} }
int getLastStatus() const
{
return mLastStatus;
}
void setLastStatus(int status)
{
mLastStatus = status;
}
private: private:
/// @{ Unused/forbidden copy operator /// @{ Unused/forbidden copy operator
Ptr& operator=(const Ptr& aPtr); Ptr& operator=(const Ptr& aPtr);
@ -392,7 +382,6 @@ public:
sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object
unsigned int* mpRefCount; //!< Pointer to the heap allocated reference counter of the sqlite3_stmt unsigned int* mpRefCount; //!< Pointer to the heap allocated reference counter of the sqlite3_stmt
//!< (to share it with Column objects) //!< (to share it with Column objects)
int mLastStatus; //!< The return status of the last statement evaluation
}; };
private: private:

View File

@ -182,7 +182,6 @@ bool Statement::executeStep()
if (false == mbDone) if (false == mbDone)
{ {
int ret = sqlite3_step(mStmtPtr); int ret = sqlite3_step(mStmtPtr);
mStmtPtr.setLastStatus(ret);
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
{ {
mbOk = true; mbOk = true;
@ -213,7 +212,6 @@ int Statement::exec()
if (false == mbDone) if (false == mbDone)
{ {
int ret = sqlite3_step(mStmtPtr); int ret = sqlite3_step(mStmtPtr);
mStmtPtr.setLastStatus(ret);
if (SQLITE_DONE == ret) // the statement has finished executing successfully if (SQLITE_DONE == ret) // the statement has finished executing successfully
{ {
mbOk = false; mbOk = false;
@ -276,7 +274,6 @@ bool Statement::isColumnNull(const int aIndex) const
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message // Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
void Statement::check(const int aRet) void Statement::check(const int aRet)
{ {
mStmtPtr.setLastStatus(aRet);
if (SQLITE_OK != aRet) if (SQLITE_OK != aRet)
{ {
throw SQLite::Exception(sqlite3_errmsg(mStmtPtr)); throw SQLite::Exception(sqlite3_errmsg(mStmtPtr));
@ -297,11 +294,10 @@ void Statement::check(const int aRet)
Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) : Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
mpSQLite(apSQLite), mpSQLite(apSQLite),
mpStmt(NULL), mpStmt(NULL),
mpRefCount(NULL), mpRefCount(NULL)
mLastStatus(SQLITE_OK)
{ {
mLastStatus = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast<int>(aQuery.size()), &mpStmt, NULL); int ret = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast<int>(aQuery.size()), &mpStmt, NULL);
if (SQLITE_OK != mLastStatus) if (SQLITE_OK != ret)
{ {
throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
} }
@ -319,8 +315,7 @@ Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
Statement::Ptr::Ptr(const Statement::Ptr& aPtr) : Statement::Ptr::Ptr(const Statement::Ptr& aPtr) :
mpSQLite(aPtr.mpSQLite), mpSQLite(aPtr.mpSQLite),
mpStmt(aPtr.mpStmt), mpStmt(aPtr.mpStmt),
mpRefCount(aPtr.mpRefCount), mpRefCount(aPtr.mpRefCount)
mLastStatus(SQLITE_OK)
{ {
assert(NULL != mpRefCount); assert(NULL != mpRefCount);
assert(0 != *mpRefCount); assert(0 != *mpRefCount);
@ -342,12 +337,9 @@ Statement::Ptr::~Ptr() noexcept // nothrow
--(*mpRefCount); --(*mpRefCount);
if (0 == *mpRefCount) if (0 == *mpRefCount)
{ {
// If count reaches zero, finalize the sqlite3_stmt, // If count reaches zero, finalize the sqlite3_stmt, as no Statement nor Column objet use it anymore.
// as no Statement not Column objet use it anymore // No need to check the return code, as it is the same as the last statement evaluation.
int ret = sqlite3_finalize(mpStmt); sqlite3_finalize(mpStmt);
// Never throw an exception in a destructor
SQLITECPP_ASSERT((SQLITE_OK == ret || mLastStatus == ret),
sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
// and delete the reference counter // and delete the reference counter
delete mpRefCount; delete mpRefCount;