db: db_exec_prepared takes ownership of the statement

Technically it's the caller that'll own the statement, but it is nice
to have db_exec_prepared dispose of it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2017-10-30 16:05:00 +01:00
committed by Rusty Russell
parent f78205f30f
commit b267b24c08
2 changed files with 13 additions and 6 deletions

View File

@@ -150,17 +150,23 @@ sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query)
bool db_exec_prepared_(const char *caller, struct db *db, sqlite3_stmt *stmt)
{
if (db->in_transaction && db->err)
return false;
if (db->in_transaction && db->err) {
goto fail;
}
db_clear_error(db);
if (sqlite3_step(stmt) != SQLITE_DONE) {
db->err =
tal_fmt(db, "%s: %s", caller, sqlite3_errmsg(db->sql));
return false;
} else {
return true;
goto fail;
}
sqlite3_finalize(stmt);
return true;
fail:
sqlite3_finalize(stmt);
return false;
}
bool PRINTF_FMT(3, 4)

View File

@@ -102,7 +102,8 @@ sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query);
* all non-null variables using the `sqlite3_bind_*` functions, it can
* be executed with this function. It is a small, transaction-aware,
* wrapper around `sqlite3_step`, that also sets `db->err` if the
* execution fails.
* execution fails. This will take ownership of `stmt` and will free
* it before returning.
*
* @db: The database to execute on
* @stmt: The prepared statement to execute