Added the dealocation of the reference counter

- Suppressed the exception that can be thrown in those two destructors:
- Added documentation of those destructors
This commit is contained in:
Sébastien Rombauts 2012-04-17 07:06:41 +02:00
parent fb1b17bfff
commit cfe042dd41
5 changed files with 18 additions and 3 deletions

View File

@ -5,6 +5,9 @@ Missing features in v0.3.0:
- Blob - Blob
- getColumnByName ? std::map getRow() ? - getColumnByName ? std::map getRow() ?
Missing documentation in v0.3.0:
- Help for the new helper functions
Advanced missing features: Advanced missing features:
- batch mode managing multiple queries semicolon separated ? - batch mode managing multiple queries semicolon separated ?
- Function ? - Function ?

View File

@ -24,15 +24,21 @@ Column::Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, unsigned int* apStmtRefC
(*mpStmtRefCount)++; (*mpStmtRefCount)++;
} }
// Finalize and unregister the SQL query from the SQLite Database Connection.
Column::~Column(void) throw() // nothrow Column::~Column(void) throw() // nothrow
{ {
// Decrement and check the reference counter
(*mpStmtRefCount)--; (*mpStmtRefCount)--;
if (0 == *mpStmtRefCount) if (0 == *mpStmtRefCount)
{ {
// When count reaches zero, dealloc and finalize the statement
delete mpStmtRefCount;
int ret = sqlite3_finalize(mpStmt); int ret = sqlite3_finalize(mpStmt);
if (SQLITE_OK != ret) if (SQLITE_OK != ret)
{ {
throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); // Never throw an exception in a destructor
//std::cout << sqlite3_errmsg(mpSQLite);
} }
mpStmt = NULL; mpStmt = NULL;
} }

View File

@ -76,7 +76,7 @@ private:
private: private:
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object
unsigned int* mpStmtRefCount; //!< Pointer to the reference counter of the (shared) Statement Object unsigned int* mpStmtRefCount; //!< Pointer to the reference counter of the Statement Object (to share it with a Statement object)
int mIndex; //!< Index of the column in the row of result int mIndex; //!< Index of the column in the row of result
}; };

View File

@ -28,6 +28,7 @@ Statement::Statement(Database &aDatabase, const char* apQuery) : // throw(SQLite
int ret = sqlite3_prepare_v2(mpSQLite, mQuery.c_str(), mQuery.size(), &mpStmt, NULL); int ret = sqlite3_prepare_v2(mpSQLite, mQuery.c_str(), mQuery.size(), &mpStmt, NULL);
check(ret); check(ret);
mColumnCount = sqlite3_column_count(mpStmt); mColumnCount = sqlite3_column_count(mpStmt);
// Initialize the reference counter of the Statement Object : used to share the mpStmt with Column objects
mpStmtRefCount = new unsigned int; mpStmtRefCount = new unsigned int;
*mpStmtRefCount = 1; *mpStmtRefCount = 1;
} }
@ -35,9 +36,13 @@ Statement::Statement(Database &aDatabase, const char* apQuery) : // throw(SQLite
// Finalize and unregister the SQL query from the SQLite Database Connection. // Finalize and unregister the SQL query from the SQLite Database Connection.
Statement::~Statement(void) throw() // nothrow Statement::~Statement(void) throw() // nothrow
{ {
// Decrement and check the reference counter
(*mpStmtRefCount)--; (*mpStmtRefCount)--;
if (0 == *mpStmtRefCount) if (0 == *mpStmtRefCount)
{ {
// When count reaches zero, dealloc and finalize the statement
delete mpStmtRefCount;
int ret = sqlite3_finalize(mpStmt); int ret = sqlite3_finalize(mpStmt);
if (SQLITE_OK != ret) if (SQLITE_OK != ret)
{ {
@ -189,6 +194,7 @@ Column Statement::getColumn(const int aIndex) const // throw(SQLite::Exception)
throw SQLite::Exception("Column index out of range"); throw SQLite::Exception("Column index out of range");
} }
// Share the Statement Object handle with the new Column created
return Column(mpSQLite, mpStmt, mpStmtRefCount, aIndex); return Column(mpSQLite, mpStmt, mpStmtRefCount, aIndex);
} }

View File

@ -176,7 +176,7 @@ private:
private: private:
sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object
unsigned int* mpStmtRefCount; //!< Pointer to the reference counter of the (shared) Statement Object unsigned int* mpStmtRefCount; //!< Pointer to the reference counter of the Statement Object (to share it with Column objects)
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
std::string mQuery; //!< UTF-8 SQL Query std::string mQuery; //!< UTF-8 SQL Query
int mColumnCount; //!< Number of column in the result of the prepared statement int mColumnCount; //!< Number of column in the result of the prepared statement