Adding a Statement::exec() method to execute a one-step query with no expected result

- similar to Database::exec(), but to be used with a SQLite prepared statement, for improved performances
This commit is contained in:
Sébastien Rombauts 2012-12-10 16:49:36 +01:00
parent 792824008a
commit 560bc958df
4 changed files with 60 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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