diff --git a/src/SQLiteC++/Database.cpp b/src/SQLiteC++/Database.cpp index 9e40d72..7e63feb 100644 --- a/src/SQLiteC++/Database.cpp +++ b/src/SQLiteC++/Database.cpp @@ -47,7 +47,7 @@ int Database::exec(const char* apQueries) // throw(SQLite::Exception); int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL); check(ret); - // Return the number of changes made by those SQL statements + // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE) return sqlite3_changes(mpSQLite); } diff --git a/src/SQLiteC++/Database.h b/src/SQLiteC++/Database.h index f583956..09a53c1 100644 --- a/src/SQLiteC++/Database.h +++ b/src/SQLiteC++/Database.h @@ -62,11 +62,12 @@ public: * - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE" * - Data Control Language (DCL) statements "GRANT", "REVOKE", "COMMIT" and "ROLLBACK" * - * @see Statement class and Statement::executeStep() for handling "SELECT" queries with results + * @see Statement::exec() to handle precompiled statements (for better performances) without results + * @see Statement::executeStep() to handle "SELECT" queries with results * * @param[in] apQueries one or multiple UTF-8 encoded, semicolon-separate SQL statements * - * @return number of changes made by those SQL statements + * @return number of rows modified by those SQL statements (INSERT, UPDATE or DELETE) * * @throw SQLite::Exception in case of error */ diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index fe201a8..b24203b 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -163,17 +163,19 @@ bool Statement::executeStep(void) // throw(SQLite::Exception) if (false == mbDone) { int ret = sqlite3_step(mpStmt); - if (SQLITE_ROW == ret) + if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it { mbOk = true; } - else if (SQLITE_DONE == ret) + else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing { mbOk = false; mbDone = true; } else { + mbOk = false; + mbDone = false; throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); } } @@ -182,7 +184,40 @@ bool Statement::executeStep(void) // throw(SQLite::Exception) throw SQLite::Exception("Statement need to be reseted"); } - return mbOk; + return mbOk; // true only if one row is accessible by getColumn(N) +} + +// Execute a one-step query with no expected result +int Statement::exec(void) // throw(SQLite::Exception) +{ + if (false == mbDone) + { + int ret = sqlite3_step(mpStmt); + if (SQLITE_DONE == ret) // the statement has finished executing successfully + { + mbOk = false; + mbDone = true; + } + else if (SQLITE_ROW == ret) + { + mbOk = false; + mbDone = false; + throw SQLite::Exception("exec() does not expect results"); + } + else + { + mbOk = false; + mbDone = false; + throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); + } + } + else + { + throw SQLite::Exception("Statement need to be reseted"); + } + + // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE) + return sqlite3_changes(mpSQLite); } // Return a copy of the column data specified by its index starting at 0 diff --git a/src/SQLiteC++/Statement.h b/src/SQLiteC++/Statement.h index 4b20a49..30e57b9 100644 --- a/src/SQLiteC++/Statement.h +++ b/src/SQLiteC++/Statement.h @@ -112,8 +112,9 @@ public: //////////////////////////////////////////////////////////////////////////// /** - * @brief Execute a step of the query to fetch one row of results. + * @brief Execute a step of the prepared query to fetch one row of results. * + * @see exec() execute a one-step prepared statement with no expected result * @see Database::exec() is a shortcut to execute one or multiple statements without results * * @return - true (SQLITE_ROW) if there is another row ready : you can call getColumn(N) to get it @@ -125,6 +126,22 @@ public: */ bool executeStep(void); // throw(SQLite::Exception); + /** + * @brief Execute a one-step query with no expected result. + * + * This exec() method is to use with precompiled statement that does not fetch results (INSERT, UPDATE, DELETE...). + * It is intended for similar usage as Database::exec(), but is able to reuse the precompiled underlying SQLite statement + * for better performances. + * + * @see executeStep() execute a step of the prepared query to fetch one row of results + * @see Database::exec() is a shortcut to execute one or multiple statements without results + * + * @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 ! + */ + int exec(void); // throw(SQLite::Exception); + //////////////////////////////////////////////////////////////////////////// /**