From 268b111817a602424b9519b90a12eb4ff33a3fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Thu, 13 Mar 2014 21:59:06 +0100 Subject: [PATCH] Added a Database::getTotalChanges() method and unit test --- include/SQLiteCpp/Database.h | 10 ++++++++++ tests/Database_test.cpp | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index ed90bcf..64361d6 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -229,6 +229,16 @@ public: return sqlite3_last_insert_rowid(mpSQLite); } + /** + * @brief Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection. + * + * @return Total number of rows modified since connection to the database. DROP tables does not count. + */ + inline int getTotalChanges() const noexcept // nothrow + { + return sqlite3_total_changes(mpSQLite); + } + /** * @brief Return the filename used to open the database */ diff --git a/tests/Database_test.cpp b/tests/Database_test.cpp index a7d1da7..e4a2399 100644 --- a/tests/Database_test.cpp +++ b/tests/Database_test.cpp @@ -68,43 +68,53 @@ TEST(Database, ctorExecAndGet) { // but its return is an undefined value for "CREATE TABLE" statements. db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); EXPECT_EQ(0, db.getLastInsertRowid()); + EXPECT_EQ(0, db.getTotalChanges()); // first row : insert the "first" text value into new row of id 1 EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")")); EXPECT_EQ(1, db.getLastInsertRowid()); + EXPECT_EQ(1, db.getTotalChanges()); // second row : insert the "second" text value into new row of id 2 EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")")); EXPECT_EQ(2, db.getLastInsertRowid()); + EXPECT_EQ(2, db.getTotalChanges()); // third row : insert the "third" text value into new row of id 3 EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"third\")")); EXPECT_EQ(3, db.getLastInsertRowid()); + EXPECT_EQ(3, db.getTotalChanges()); // update the second row : update text value to "second_updated" EXPECT_EQ(1, db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'")); EXPECT_EQ(3, db.getLastInsertRowid()); // last inserted row ID is still 3 + EXPECT_EQ(4, db.getTotalChanges()); // delete the third row EXPECT_EQ(1, db.exec("DELETE FROM test WHERE id='3'")); EXPECT_EQ(3, db.getLastInsertRowid()); + EXPECT_EQ(5, db.getTotalChanges()); // drop the whole table, ie the two remaining columns // NOTE: here exec() returns 1, like the last time, as it is an undefined value for "DROP TABLE" statements db.exec("DROP TABLE IF EXISTS test"); EXPECT_FALSE(db.tableExists("test")); + EXPECT_EQ(5, db.getTotalChanges()); // Re-Create the same table // NOTE: here exec() returns 1, like the last time, as it is an undefined value for "CREATE TABLE" statements db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + EXPECT_EQ(5, db.getTotalChanges()); // insert two rows with two *different* statements => returns only 1, ie. for the second INSERT statement EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\");INSERT INTO test VALUES (NULL, \"second\");")); EXPECT_EQ(2, db.getLastInsertRowid()); + EXPECT_EQ(7, db.getTotalChanges()); // insert two rows with only one statement => returns 2 EXPECT_EQ(2, db.exec("INSERT INTO test VALUES (NULL, \"third\"), (NULL, \"fourth\");")); EXPECT_EQ(4, db.getLastInsertRowid()); + EXPECT_EQ(9, db.getTotalChanges()); } // Close DB test.db3 remove("test.db3");