From cfe042dd41a05c8eaef6126203a6f1e3763bc1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Tue, 17 Apr 2012 07:06:41 +0200 Subject: [PATCH] Added the dealocation of the reference counter - Suppressed the exception that can be thrown in those two destructors: - Added documentation of those destructors --- TODO.txt | 3 +++ src/SQLiteC++/Column.cpp | 8 +++++++- src/SQLiteC++/Column.h | 2 +- src/SQLiteC++/Statement.cpp | 6 ++++++ src/SQLiteC++/Statement.h | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/TODO.txt b/TODO.txt index 16ea28d..98ba451 100644 --- a/TODO.txt +++ b/TODO.txt @@ -5,6 +5,9 @@ Missing features in v0.3.0: - Blob - getColumnByName ? std::map getRow() ? +Missing documentation in v0.3.0: +- Help for the new helper functions + Advanced missing features: - batch mode managing multiple queries semicolon separated ? - Function ? diff --git a/src/SQLiteC++/Column.cpp b/src/SQLiteC++/Column.cpp index e672392..281fa1a 100644 --- a/src/SQLiteC++/Column.cpp +++ b/src/SQLiteC++/Column.cpp @@ -24,15 +24,21 @@ Column::Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, unsigned int* apStmtRefC (*mpStmtRefCount)++; } +// Finalize and unregister the SQL query from the SQLite Database Connection. Column::~Column(void) throw() // nothrow { + // Decrement and check the reference counter (*mpStmtRefCount)--; if (0 == *mpStmtRefCount) { + // When count reaches zero, dealloc and finalize the statement + delete mpStmtRefCount; + int ret = sqlite3_finalize(mpStmt); if (SQLITE_OK != ret) { - throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); + // Never throw an exception in a destructor + //std::cout << sqlite3_errmsg(mpSQLite); } mpStmt = NULL; } diff --git a/src/SQLiteC++/Column.h b/src/SQLiteC++/Column.h index fa87ab5..e1432f1 100644 --- a/src/SQLiteC++/Column.h +++ b/src/SQLiteC++/Column.h @@ -76,7 +76,7 @@ private: private: sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle 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 }; diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index 533b8f4..e2cc5c2 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -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); check(ret); 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 = 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. Statement::~Statement(void) throw() // nothrow { + // Decrement and check the reference counter (*mpStmtRefCount)--; if (0 == *mpStmtRefCount) { + // When count reaches zero, dealloc and finalize the statement + delete mpStmtRefCount; + int ret = sqlite3_finalize(mpStmt); 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"); } + // Share the Statement Object handle with the new Column created return Column(mpSQLite, mpStmt, mpStmtRefCount, aIndex); } diff --git a/src/SQLiteC++/Statement.h b/src/SQLiteC++/Statement.h index 757c2f4..32eb9f0 100644 --- a/src/SQLiteC++/Statement.h +++ b/src/SQLiteC++/Statement.h @@ -176,7 +176,7 @@ private: private: 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 std::string mQuery; //!< UTF-8 SQL Query int mColumnCount; //!< Number of column in the result of the prepared statement