diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 64361d6..09924c1 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -239,17 +239,25 @@ public: 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 { return mFilename; } - /** - * @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 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 { return sqlite3_errmsg(mpSQLite); diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index f9e457a..58b53a9 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -326,8 +326,18 @@ public: { return mbDone; } - /// @brief Return UTF-8 encoded English language explanation of the most recent error. - inline const char* errmsg() const + /// @brief Return the numeric result code for the most recent failed API call (if any). + 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); } diff --git a/tests/Database_test.cpp b/tests/Database_test.cpp index 55e4768..1cb52d6 100644 --- a/tests/Database_test.cpp +++ b/tests/Database_test.cpp @@ -146,15 +146,23 @@ TEST(Database, execException) { { // Create a new database 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" 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 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" 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" 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)")); // exception with SQL error: "No row to get a column from" EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"second\""), SQLite::Exception); - } // Close DB test.db3 remove("test.db3"); } diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index 2fb6aea..4fd9c73 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -22,12 +22,18 @@ TEST(Statement, invalid) { { // Create a new database 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 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(SQLITE_OK, db.getErrorCode()); + EXPECT_EQ(SQLITE_OK, db.getExtendedErrorCode()); + // Compile a SQL query with no parameter SQLite::Statement query(db, "SELECT * FROM test"); EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str()); @@ -61,6 +67,8 @@ TEST(Statement, invalid) { EXPECT_THROW(query.bind(2, 123), SQLite::Exception); EXPECT_THROW(query.bind(0, "abc"), SQLite::Exception); EXPECT_THROW(query.bind(0), SQLite::Exception); + EXPECT_EQ(SQLITE_RANGE, db.getErrorCode()); + EXPECT_EQ(SQLITE_RANGE, db.getExtendedErrorCode()); query.exec(); EXPECT_THROW(query.isColumnNull(0), SQLite::Exception);