From ca7c0e5f1a84f14b5c0407fd5a7f23b92d2dd0bc 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? Fix cpplint warnings about line size with a NOLINT comment when better to keep oneline --- include/SQLiteCpp/Database.h | 9 ++++++--- include/SQLiteCpp/Statement.h | 7 +++++-- src/Database.cpp | 8 +++++++- src/Statement.cpp | 8 +++++++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 1490fd8..5268353 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -14,7 +14,7 @@ // c++17: MinGW GCC version > 8 // c++17: Visual Studio 2017 version 15.7 -#if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) +#if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // NOLINT #include #endif // c++17 @@ -169,7 +169,7 @@ public: // c++17: MinGW GCC version > 8 // c++17: Visual Studio 2017 version 15.7 - #if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) + #if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // NOLINT /** * @brief Open the provided database std::filesystem::path. @@ -241,7 +241,7 @@ public: 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 @@ public: */ 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 a407417..84b0046 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -471,7 +471,7 @@ public: 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 @@ public: * * @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 @@ public: 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 7b81f8b..db7dc63 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); @@ -155,6 +155,12 @@ 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. int Database::getTotalChanges() const noexcept { diff --git a/src/Statement.cpp b/src/Statement.cpp index 8ab6862..f49a6eb 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(); @@ -310,6 +310,12 @@ const char * Statement::getColumnDeclaredType(const int aIndex) const } } +// 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 { return sqlite3_bind_parameter_count(mStmtPtr);