From 18abb3079e57549a9855b2dd0418f4195fda2ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Sun, 25 Jul 2021 09:36:17 +0200 Subject: [PATCH] Added Database and Statement method getChanges() Fix #331 How to get the number of updated/deleted rows? --- include/SQLiteCpp/Database.h | 5 ++++- include/SQLiteCpp/Statement.h | 7 +++++-- src/Database.cpp | 8 +++++++- src/Statement.cpp | 8 +++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 1490fd83..db7e267a 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -241,7 +241,7 @@ class Database void setBusyTimeout(const int aBusyTimeoutMs); /** - * @brief Shortcut to execute one or multiple statements without results. + * @brief Shortcut to execute one or multiple statements without results. Return the number of changes. * * This is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" : * - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE" @@ -404,6 +404,9 @@ class Database */ long long getLastInsertRowid() const noexcept; + /// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table). + int getChanges() const noexcept; + /// Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection (not DROP table). int getTotalChanges() const noexcept; diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index a4074175..84b0046f 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -471,7 +471,7 @@ class Statement int tryExecuteStep() noexcept; /** - * @brief Execute a one-step query with no expected result. + * @brief Execute a one-step query with no expected result, and return the number of changes. * * This method is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" : * - Data Definition Language (DDL) statements "CREATE", "ALTER" and "DROP" @@ -488,7 +488,7 @@ class Statement * * @return number of row modified by this SQL statement (INSERT, UPDATE or DELETE) * - * @throw SQLite::Exception in case of error, or if row of results are returned ! + * @throw SQLite::Exception in case of error, or if row of results are returned while they are not expected! */ int exec(); @@ -660,6 +660,9 @@ class Statement const char * getColumnDeclaredType(const int aIndex) const; + /// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table). + int getChanges() const noexcept; + //////////////////////////////////////////////////////////////////////////// diff --git a/src/Database.cpp b/src/Database.cpp index 7b81f8b9..fc12d6f5 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -112,7 +112,7 @@ void Database::setBusyTimeout(const int aBusyTimeoutMs) check(ret); } -// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...). +// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...). Return the number of changes. int Database::exec(const char* apQueries) { const int ret = tryExec(apQueries); @@ -153,6 +153,12 @@ bool Database::tableExists(const char* apTableName) long long Database::getLastInsertRowid() const noexcept { return sqlite3_last_insert_rowid(getHandle()); +} + +// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table). +int Database::getChanges() const noexcept +{ + return sqlite3_changes(getHandle()); } // Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection. diff --git a/src/Statement.cpp b/src/Statement.cpp index 8ab68624..3876654e 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -167,7 +167,7 @@ bool Statement::executeStep() return mbHasRow; // true only if one row is accessible by getColumn(N) } -// Execute a one-step query with no expected result +// Execute a one-step query with no expected result, and return the number of changes. int Statement::exec() { const int ret = tryExecuteStep(); @@ -308,6 +308,12 @@ const char * Statement::getColumnDeclaredType(const int aIndex) const { return result; } +} + +// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table). +int Statement::getChanges() const noexcept +{ + return sqlite3_changes(mStmtPtr); } int Statement::getBindParameterCount() const noexcept