mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-05 02:06:02 -04:00
Statement now stores the status of the last operation so it can be checked in the pointer destructor
This commit is contained in:
parent
55edadd56d
commit
4e770eb741
@ -362,6 +362,16 @@ public:
|
||||
return mpStmt;
|
||||
}
|
||||
|
||||
int getLastStatus () const
|
||||
{
|
||||
return mLastStatus ;
|
||||
}
|
||||
|
||||
void setLastStatus (int status)
|
||||
{
|
||||
mLastStatus = status ;
|
||||
}
|
||||
|
||||
private:
|
||||
/// @{ Unused/forbidden copy operator
|
||||
Ptr& operator=(const Ptr& aPtr);
|
||||
@ -372,6 +382,7 @@ public:
|
||||
sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object
|
||||
unsigned int* mpRefCount; //!< Pointer to the heap allocated reference counter of the sqlite3_stmt
|
||||
//!< (to share it with Column objects)
|
||||
int mLastStatus;//!< The return status of the last statement evaluation
|
||||
};
|
||||
|
||||
private:
|
||||
@ -385,7 +396,7 @@ private:
|
||||
*
|
||||
* @param[in] SQLite return code to test against the SQLITE_OK expected value
|
||||
*/
|
||||
void check(const int aRet) const;
|
||||
void check(const int aRet);
|
||||
|
||||
private:
|
||||
std::string mQuery; //!< UTF-8 SQL Query
|
||||
|
@ -181,7 +181,8 @@ bool Statement::executeStep()
|
||||
{
|
||||
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
|
||||
{
|
||||
mbOk = true;
|
||||
@ -200,7 +201,7 @@ bool Statement::executeStep()
|
||||
}
|
||||
else
|
||||
{
|
||||
throw SQLite::Exception("Statement need to be reseted");
|
||||
throw SQLite::Exception("Statement needs to be reset");
|
||||
}
|
||||
|
||||
return mbOk; // true only if one row is accessible by getColumn(N)
|
||||
@ -212,6 +213,7 @@ int Statement::exec()
|
||||
if (false == mbDone)
|
||||
{
|
||||
int ret = sqlite3_step(mStmtPtr);
|
||||
mStmtPtr.setLastStatus (ret);
|
||||
if (SQLITE_DONE == ret) // the statement has finished executing successfully
|
||||
{
|
||||
mbOk = false;
|
||||
@ -272,8 +274,9 @@ bool Statement::isColumnNull(const int aIndex) const
|
||||
}
|
||||
|
||||
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
|
||||
void Statement::check(const int aRet) const
|
||||
void Statement::check(const int aRet)
|
||||
{
|
||||
mStmtPtr.setLastStatus (aRet) ;
|
||||
if (SQLITE_OK != aRet)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mStmtPtr));
|
||||
@ -294,10 +297,11 @@ void Statement::check(const int aRet) const
|
||||
Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
|
||||
mpSQLite(apSQLite),
|
||||
mpStmt(NULL),
|
||||
mpRefCount(NULL)
|
||||
mpRefCount(NULL),
|
||||
mLastStatus(SQLITE_OK)
|
||||
{
|
||||
int ret = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast<int>(aQuery.size()), &mpStmt, NULL);
|
||||
if (SQLITE_OK != ret)
|
||||
mLastStatus = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast<int>(aQuery.size()), &mpStmt, NULL);
|
||||
if (SQLITE_OK != mLastStatus)
|
||||
{
|
||||
throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
|
||||
}
|
||||
@ -315,7 +319,8 @@ Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
|
||||
Statement::Ptr::Ptr(const Statement::Ptr& aPtr) :
|
||||
mpSQLite(aPtr.mpSQLite),
|
||||
mpStmt(aPtr.mpStmt),
|
||||
mpRefCount(aPtr.mpRefCount)
|
||||
mpRefCount(aPtr.mpRefCount),
|
||||
mLastStatus(SQLITE_OK)
|
||||
{
|
||||
assert(NULL != mpRefCount);
|
||||
assert(0 != *mpRefCount);
|
||||
@ -341,7 +346,7 @@ Statement::Ptr::~Ptr() noexcept // nothrow
|
||||
// as no Statement not Column objet use it anymore
|
||||
int ret = sqlite3_finalize(mpStmt);
|
||||
// Never throw an exception in a destructor
|
||||
SQLITECPP_ASSERT(SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
SQLITECPP_ASSERT((SQLITE_OK == ret || mLastStatus == ret), sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
|
||||
// and delete the reference counter
|
||||
delete mpRefCount;
|
||||
|
Loading…
x
Reference in New Issue
Block a user