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
This commit is contained in:
Sébastien Rombauts 2021-07-25 09:36:17 +02:00
parent 0c46d86e0d
commit ca7c0e5f1a
4 changed files with 25 additions and 7 deletions

View File

@ -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 <filesystem>
#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;

View File

@ -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;
////////////////////////////////////////////////////////////////////////////

View File

@ -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
{

View File

@ -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);