diff --git a/src/SQLiteC++/Database.cpp b/src/SQLiteC++/Database.cpp index 3176473..51ffb3d 100644 --- a/src/SQLiteC++/Database.cpp +++ b/src/SQLiteC++/Database.cpp @@ -45,7 +45,8 @@ Database::~Database(void) throw() // nothrow int ret = sqlite3_close(mpSQLite); if (SQLITE_OK != ret) { - std::cout << sqlite3_errmsg(mpSQLite); + // Never throw an exception in a destructor + //std::cout << sqlite3_errmsg(mpSQLite); } } diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index f0ea437..4a192c5 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -24,10 +24,7 @@ Statement::Statement(Database &aDatabase, const char* apQuery) : // throw(SQLite mbDone(false) { int ret = sqlite3_prepare_v2(mDatabase.mpSQLite, mQuery.c_str(), mQuery.size(), &mpStmt, NULL); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); mColumnCount = sqlite3_column_count(mpStmt); mDatabase.registerStatement(*this); } @@ -38,7 +35,8 @@ Statement::~Statement(void) throw() // nothrow int ret = sqlite3_finalize(mpStmt); if (SQLITE_OK != ret) { - std::cout << sqlite3_errmsg(mDatabase.mpSQLite); + // Never throw an exception in a destructor + //std::cout << sqlite3_errmsg(mDatabase.mpSQLite); } mpStmt = NULL; mDatabase.unregisterStatement(*this); @@ -50,71 +48,49 @@ void Statement::reset(void) // throw(SQLite::Exception) mbOk = false; mbDone = false; int ret = sqlite3_reset(mpStmt); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex, const int& aValue) // throw(SQLite::Exception) { int ret = sqlite3_bind_int(mpStmt, aIndex, aValue); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex, const sqlite3_int64& aValue) // throw(SQLite::Exception) - { int ret = sqlite3_bind_int64(mpStmt, aIndex, aValue); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex, const double& aValue) // throw(SQLite::Exception) { int ret = sqlite3_bind_double(mpStmt, aIndex, aValue); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex, const std::string& aValue) // throw(SQLite::Exception) { int ret = sqlite3_bind_text(mpStmt, aIndex, aValue.c_str(), aValue.size(), SQLITE_TRANSIENT); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex, const char* apValue) // throw(SQLite::Exception) { int ret = sqlite3_bind_text(mpStmt, aIndex, apValue, -1, SQLITE_TRANSIENT); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement void Statement::bind(const int aIndex) // throw(SQLite::Exception) { int ret = sqlite3_bind_null(mpStmt, aIndex); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); - } + check(ret); } // Execute a step of the query to fetch one row of results @@ -217,4 +193,17 @@ bool Statement::isColumnNull(const int aIndex) const // throw(SQLite::Exception) return (SQLITE_NULL == sqlite3_column_type(mpStmt, aIndex)); } + +/** + * @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message + */ +void Statement::check(const int aRet) const // throw(SQLite::Exception) +{ + if (SQLITE_OK != aRet) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + + }; // namespace SQLite diff --git a/src/SQLiteC++/Statement.h b/src/SQLiteC++/Statement.h index 522673b..51beae3 100644 --- a/src/SQLiteC++/Statement.h +++ b/src/SQLiteC++/Statement.h @@ -136,6 +136,12 @@ public: return mbDone; } +private: + /** + * @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message + */ + void check(const int aRet) const; // throw(SQLite::Exception); + private: sqlite3_stmt* mpStmt; //!< Pointeur to SQLite Statement Object Database& mDatabase; //!< Reference to the SQLite Database Connection