Skip to content

Commit ca7c0e5

Browse files
committed
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
1 parent 0c46d86 commit ca7c0e5

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

include/SQLiteCpp/Database.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// c++17: MinGW GCC version > 8
1616
// c++17: Visual Studio 2017 version 15.7
17-
#if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))
17+
#if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // NOLINT
1818
#include <filesystem>
1919
#endif // c++17
2020

@@ -169,7 +169,7 @@ class Database
169169

170170
// c++17: MinGW GCC version > 8
171171
// c++17: Visual Studio 2017 version 15.7
172-
#if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))
172+
#if ((__cplusplus >= 201703L) && ((!defined(__MINGW32__) && !defined(__MINGW64__)) || (__GNUC__ > 8))) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) // NOLINT
173173

174174
/**
175175
* @brief Open the provided database std::filesystem::path.
@@ -241,7 +241,7 @@ class Database
241241
void setBusyTimeout(const int aBusyTimeoutMs);
242242

243243
/**
244-
* @brief Shortcut to execute one or multiple statements without results.
244+
* @brief Shortcut to execute one or multiple statements without results. Return the number of changes.
245245
*
246246
* This is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" :
247247
* - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE"
@@ -404,6 +404,9 @@ class Database
404404
*/
405405
long long getLastInsertRowid() const noexcept;
406406

407+
/// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
408+
int getChanges() const noexcept;
409+
407410
/// Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection (not DROP table).
408411
int getTotalChanges() const noexcept;
409412

include/SQLiteCpp/Statement.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class Statement
471471
int tryExecuteStep() noexcept;
472472

473473
/**
474-
* @brief Execute a one-step query with no expected result.
474+
* @brief Execute a one-step query with no expected result, and return the number of changes.
475475
*
476476
* This method is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" :
477477
* - Data Definition Language (DDL) statements "CREATE", "ALTER" and "DROP"
@@ -488,7 +488,7 @@ class Statement
488488
*
489489
* @return number of row modified by this SQL statement (INSERT, UPDATE or DELETE)
490490
*
491-
* @throw SQLite::Exception in case of error, or if row of results are returned !
491+
* @throw SQLite::Exception in case of error, or if row of results are returned while they are not expected!
492492
*/
493493
int exec();
494494

@@ -660,6 +660,9 @@ class Statement
660660
const char * getColumnDeclaredType(const int aIndex) const;
661661

662662

663+
/// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
664+
int getChanges() const noexcept;
665+
663666

664667
////////////////////////////////////////////////////////////////////////////
665668

src/Database.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void Database::setBusyTimeout(const int aBusyTimeoutMs)
112112
check(ret);
113113
}
114114

115-
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...).
115+
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...). Return the number of changes.
116116
int Database::exec(const char* apQueries)
117117
{
118118
const int ret = tryExec(apQueries);
@@ -155,6 +155,12 @@ long long Database::getLastInsertRowid() const noexcept
155155
return sqlite3_last_insert_rowid(getHandle());
156156
}
157157

158+
// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
159+
int Database::getChanges() const noexcept
160+
{
161+
return sqlite3_changes(getHandle());
162+
}
163+
158164
// Get total number of rows modified by all INSERT, UPDATE or DELETE statement since connection.
159165
int Database::getTotalChanges() const noexcept
160166
{

src/Statement.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ bool Statement::executeStep()
167167
return mbHasRow; // true only if one row is accessible by getColumn(N)
168168
}
169169

170-
// Execute a one-step query with no expected result
170+
// Execute a one-step query with no expected result, and return the number of changes.
171171
int Statement::exec()
172172
{
173173
const int ret = tryExecuteStep();
@@ -310,6 +310,12 @@ const char * Statement::getColumnDeclaredType(const int aIndex) const
310310
}
311311
}
312312

313+
// Get number of rows modified by last INSERT, UPDATE or DELETE statement (not DROP table).
314+
int Statement::getChanges() const noexcept
315+
{
316+
return sqlite3_changes(mStmtPtr);
317+
}
318+
313319
int Statement::getBindParameterCount() const noexcept
314320
{
315321
return sqlite3_bind_parameter_count(mStmtPtr);

0 commit comments

Comments
 (0)