mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-05 02:06:02 -04:00
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:
parent
792824008a
commit
560bc958df
@ -47,7 +47,7 @@ int Database::exec(const char* apQueries) // throw(SQLite::Exception);
|
|||||||
int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
|
int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
|
||||||
check(ret);
|
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);
|
return sqlite3_changes(mpSQLite);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,11 +62,12 @@ public:
|
|||||||
* - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE"
|
* - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE"
|
||||||
* - Data Control Language (DCL) statements "GRANT", "REVOKE", "COMMIT" and "ROLLBACK"
|
* - 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
|
* @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
|
* @throw SQLite::Exception in case of error
|
||||||
*/
|
*/
|
||||||
|
@ -163,17 +163,19 @@ bool Statement::executeStep(void) // throw(SQLite::Exception)
|
|||||||
if (false == mbDone)
|
if (false == mbDone)
|
||||||
{
|
{
|
||||||
int ret = sqlite3_step(mpStmt);
|
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;
|
mbOk = true;
|
||||||
}
|
}
|
||||||
else if (SQLITE_DONE == ret)
|
else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing
|
||||||
{
|
{
|
||||||
mbOk = false;
|
mbOk = false;
|
||||||
mbDone = true;
|
mbDone = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
mbOk = false;
|
||||||
|
mbDone = false;
|
||||||
throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
|
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");
|
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
|
// Return a copy of the column data specified by its index starting at 0
|
||||||
|
@ -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
|
* @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
|
* @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);
|
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);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user