Skip to content

Commit

Permalink
db: make db_exec_prepared_v2 return void.
Browse files Browse the repository at this point in the history
It calls db_fatal() if it fails anyway, so don't expect anyone to check.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Apr 6, 2023
1 parent 9bcf28a commit df9552b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
6 changes: 2 additions & 4 deletions db/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,15 @@ void db_set_intvar(struct db *db, char *varname, s64 val)
struct db_stmt *stmt = db_prepare_v2(db, SQL("UPDATE vars SET intval=? WHERE name=?;"));
db_bind_int(stmt, 0, val);
db_bind_text(stmt, 1, varname);
if (!db_exec_prepared_v2(stmt))
db_fatal("Error executing update: %s", stmt->error);
db_exec_prepared_v2(stmt);
changes = db_count_changes(stmt);
tal_free(stmt);

if (changes == 0) {
stmt = db_prepare_v2(db, SQL("INSERT INTO vars (name, intval) VALUES (?, ?);"));
db_bind_text(stmt, 0, varname);
db_bind_int(stmt, 1, val);
if (!db_exec_prepared_v2(stmt))
db_fatal("Error executing insert: %s", stmt->error);
db_exec_prepared_v2(stmt);
tal_free(stmt);
}
}
Expand Down
4 changes: 1 addition & 3 deletions db/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bool db_step(struct db_stmt *stmt)
return ret;
}

bool db_exec_prepared_v2(struct db_stmt *stmt TAKES)
void db_exec_prepared_v2(struct db_stmt *stmt TAKES)
{
bool ret = stmt->db->config->exec_fn(stmt);

Expand All @@ -191,8 +191,6 @@ bool db_exec_prepared_v2(struct db_stmt *stmt TAKES)

if (taken(stmt))
tal_free(stmt);

return ret;
}

size_t db_count_changes(struct db_stmt *stmt)
Expand Down
2 changes: 1 addition & 1 deletion db/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool db_step(struct db_stmt *stmt);
*
* @stmt: The prepared statement to execute
*/
bool db_exec_prepared_v2(struct db_stmt *stmt TAKES);
void db_exec_prepared_v2(struct db_stmt *stmt TAKES);

/**
* db_query_prepared -- Execute a prepared query
Expand Down
20 changes: 7 additions & 13 deletions wallet/test/run-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,10 @@ static bool test_primitives(void)

db_begin_transaction(db);
stmt = db_prepare_v2(db, SQL("SELECT name FROM sqlite_master WHERE type='table';"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);

stmt = db_prepare_v2(db, SQL("not a valid SQL statement"));
CHECK_MSG(!db_exec_prepared_v2(stmt), "db_exec_prepared must fail");
CHECK_MSG(db_err, "Failing SQL command");
tal_free(stmt);
db_err = tal_free(db_err);

/* We didn't migrate the DB, so don't have the vars table. Pretend we
* didn't change anything so we don't bump the data_version. */
db->dirty = false;
Expand Down Expand Up @@ -186,35 +180,35 @@ static bool test_manip_columns(void)
" id BIGSERIAL"
", field1 INTEGER"
", PRIMARY KEY (id))"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);

stmt = db_prepare_v2(db, SQL("INSERT INTO tablea (id, field1) VALUES (0, 1);"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);

stmt = db_prepare_v2(db, SQL("CREATE TABLE tableb ("
" id REFERENCES tablea(id) ON DELETE CASCADE"
", field1 INTEGER"
", field2 INTEGER);"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);

stmt = db_prepare_v2(db, SQL("INSERT INTO tableb (id, field1, field2) VALUES (0, 1, 2);"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);

/* Needs vars table, since this changes db. */
stmt = db_prepare_v2(db, SQL("CREATE TABLE vars (name VARCHAR(32), intval);"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);
stmt = db_prepare_v2(db, SQL("INSERT INTO vars VALUES ('data_version', 0);"));
CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed");
db_exec_prepared_v2(stmt);
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);

Expand Down

0 comments on commit df9552b

Please sign in to comment.