Fix #37 Last error code cannot be accessed

+ added corresponding basic unit tests
This commit is contained in:
Sébastien Rombauts 2015-03-08 14:06:57 +01:00
parent 15180a9919
commit bfdf288d3d
4 changed files with 43 additions and 10 deletions

View File

@ -239,17 +239,25 @@ public:
return sqlite3_total_changes(mpSQLite); return sqlite3_total_changes(mpSQLite);
} }
/** /// @brief Return the filename used to open the database.
* @brief Return the filename used to open the database
*/
inline const std::string& getFilename() const noexcept // nothrow inline const std::string& getFilename() const noexcept // nothrow
{ {
return mFilename; return mFilename;
} }
/** /// @brief Return the numeric result code for the most recent failed API call (if any).
* @brief Return UTF-8 encoded English language explanation of the most recent error. inline int getErrorCode() const noexcept // nothrow
*/ {
return sqlite3_errcode(mpSQLite);
}
/// @brief Return the extended numeric result code for the most recent failed API call (if any).
inline int getExtendedErrorCode() const noexcept // nothrow
{
return sqlite3_extended_errcode(mpSQLite);
}
/// Return UTF-8 encoded English language explanation of the most recent failed API call (if any).
inline const char* errmsg() const noexcept // nothrow inline const char* errmsg() const noexcept // nothrow
{ {
return sqlite3_errmsg(mpSQLite); return sqlite3_errmsg(mpSQLite);

View File

@ -326,8 +326,18 @@ public:
{ {
return mbDone; return mbDone;
} }
/// @brief Return UTF-8 encoded English language explanation of the most recent error. /// @brief Return the numeric result code for the most recent failed API call (if any).
inline const char* errmsg() const inline int getErrorCode() const noexcept // nothrow
{
return sqlite3_errcode(mStmtPtr);
}
/// @brief Return the extended numeric result code for the most recent failed API call (if any).
inline int getExtendedErrorCode() const noexcept // nothrow
{
return sqlite3_extended_errcode(mStmtPtr);
}
/// Return UTF-8 encoded English language explanation of the most recent failed API call (if any).
inline const char* errmsg() const noexcept // nothrow
{ {
return sqlite3_errmsg(mStmtPtr); return sqlite3_errmsg(mStmtPtr);
} }

View File

@ -146,15 +146,23 @@ TEST(Database, execException) {
{ {
// Create a new database // Create a new database
SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
EXPECT_EQ(SQLITE_OK, db.getExtendedErrorCode());
// exception with SQL error: "no such table" // exception with SQL error: "no such table"
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)"), SQLite::Exception); EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)"), SQLite::Exception);
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
// Create a new table // Create a new table
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT, weight INTEGER)"); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT, weight INTEGER)");
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
EXPECT_EQ(SQLITE_OK, db.getExtendedErrorCode());
// exception with SQL error: "table test has 3 columns but 2 values were supplied" // exception with SQL error: "table test has 3 columns but 2 values were supplied"
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, 3)"), SQLite::Exception); EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, 3)"), SQLite::Exception);
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
// exception with SQL error: "No row to get a column from" // exception with SQL error: "No row to get a column from"
EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"first\""), SQLite::Exception); EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"first\""), SQLite::Exception);
@ -162,7 +170,6 @@ TEST(Database, execException) {
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)")); EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)"));
// exception with SQL error: "No row to get a column from" // exception with SQL error: "No row to get a column from"
EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"second\""), SQLite::Exception); EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"second\""), SQLite::Exception);
} // Close DB test.db3 } // Close DB test.db3
remove("test.db3"); remove("test.db3");
} }

View File

@ -22,11 +22,17 @@ TEST(Statement, invalid) {
{ {
// Create a new database // Create a new database
SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
EXPECT_EQ(SQLITE_OK, db.getExtendedErrorCode());
// Compile a SQL query, but without any table in the database // Compile a SQL query, but without any table in the database
EXPECT_THROW(SQLite::Statement query(db, "SELECT * FROM test"), SQLite::Exception); EXPECT_THROW(SQLite::Statement query(db, "SELECT * FROM test"), SQLite::Exception);
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")); EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
EXPECT_EQ(SQLITE_OK, db.getExtendedErrorCode());
// Compile a SQL query with no parameter // Compile a SQL query with no parameter
SQLite::Statement query(db, "SELECT * FROM test"); SQLite::Statement query(db, "SELECT * FROM test");
@ -61,6 +67,8 @@ TEST(Statement, invalid) {
EXPECT_THROW(query.bind(2, 123), SQLite::Exception); EXPECT_THROW(query.bind(2, 123), SQLite::Exception);
EXPECT_THROW(query.bind(0, "abc"), SQLite::Exception); EXPECT_THROW(query.bind(0, "abc"), SQLite::Exception);
EXPECT_THROW(query.bind(0), SQLite::Exception); EXPECT_THROW(query.bind(0), SQLite::Exception);
EXPECT_EQ(SQLITE_RANGE, db.getErrorCode());
EXPECT_EQ(SQLITE_RANGE, db.getExtendedErrorCode());
query.exec(); query.exec();
EXPECT_THROW(query.isColumnNull(0), SQLite::Exception); EXPECT_THROW(query.isColumnNull(0), SQLite::Exception);