db: Migrate to DB abstraction layer in db.c

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2019-08-13 16:00:46 +02:00
committed by Rusty Russell
parent 716a3b11a5
commit c68efdfcf4
3 changed files with 64 additions and 93 deletions

View File

@@ -78,7 +78,9 @@ static bool test_empty_db_migrate(struct lightningd *ld)
static bool test_primitives(void)
{
struct db_stmt *stmt;
struct db *db = create_test_db();
db_err = NULL;
db_begin_transaction(db);
CHECK(db->in_transaction);
db_commit_transaction(db);
@@ -87,11 +89,15 @@ static bool test_primitives(void)
db_commit_transaction(db);
db_begin_transaction(db);
db_exec(__func__, db, "SELECT name FROM sqlite_master WHERE type='table';");
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");
CHECK_MSG(!db_err, "Simple correct SQL command");
tal_free(stmt);
db_exec(__func__, db, "not a valid SQL statement");
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);
db_commit_transaction(db);
CHECK(!db->in_transaction);

View File

@@ -1126,6 +1126,7 @@ static bool test_channel_config_crud(struct lightningd *ld, const tal_t *ctx)
static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx)
{
struct db_stmt *stmt;
struct htlc_in in, *hin;
struct htlc_out out, *hout;
struct preimage payment_key;
@@ -1136,9 +1137,11 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx)
struct htlc_out_map *htlcs_out = tal(ctx, struct htlc_out_map);
/* Make sure we have our references correct */
CHECK(transaction_wrap(w->db,
db_exec(__func__, w->db, "INSERT INTO channels (id) VALUES (1);")));
db_begin_transaction(w->db);
char *query = SQL("INSERT INTO channels (id) VALUES (1);");
stmt = db_prepare_v2(w->db, query);
db_exec_prepared_v2(stmt);
tal_free(stmt);
db_commit_transaction(w->db);
chan->dbid = 1;