Skip to content

Commit

Permalink
db: Make the db struct private and provide accessors instead
Browse files Browse the repository at this point in the history
We will soon generalize the DB, so directly reaching into the `struct db`
instance to talk to the sqlite3 connection is bad anyway. This increases
flexibility and allows us to tailor the actual implementation to the
underlying DB.

Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed Sep 5, 2019
1 parent 66a47d2 commit 803007e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lightningd/subd.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ static void destroy_subd(struct subd *sd)
sd->channel = NULL;

/* We can be freed both inside msg handling, or spontaneously. */
outer_transaction = db->in_transaction;
outer_transaction = db_in_transaction(db);
if (!outer_transaction)
db_begin_transaction(db);
if (sd->errcb)
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-find_my_abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ void db_commit_transaction(struct db *db UNNEEDED)
/* Generated stub for db_get_intvar */
s64 db_get_intvar(struct db *db UNNEEDED, char *varname UNNEEDED, s64 defval UNNEEDED)
{ fprintf(stderr, "db_get_intvar called!\n"); abort(); }
/* Generated stub for db_in_transaction */
bool db_in_transaction(struct db *db UNNEEDED)
{ fprintf(stderr, "db_in_transaction called!\n"); abort(); }
/* Generated stub for fatal */
void fatal(const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "fatal called!\n"); abort(); }
Expand Down
22 changes: 22 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ struct migration {
void (*func)(struct lightningd *ld, struct db *db);
};

struct db {
char *filename;
const char *in_transaction;
sqlite3 *sql;
const char **changes;
};

void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db);

/* Do not reorder or remove elements from this array, it is used to
Expand Down Expand Up @@ -685,6 +692,21 @@ static void db_prepare_for_changes(struct db *db)
db->changes = tal_arr(db, const char *, 0);
}

bool db_in_transaction(struct db *db)
{
return db->in_transaction;
}

u64 db_last_insert_id(struct db *db)
{
return sqlite3_last_insert_rowid(db->sql);
}

size_t db_changes(struct db *db)
{
return sqlite3_changes(db->sql);
}

void db_begin_transaction_(struct db *db, const char *location)
{
if (db->in_transaction)
Expand Down
17 changes: 11 additions & 6 deletions wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ struct lightningd;
struct log;
struct node_id;

struct db {
char *filename;
const char *in_transaction;
sqlite3 *sql;
const char **changes;
};
struct db;

/**
* Macro to annotate a named SQL query.
Expand Down Expand Up @@ -79,6 +74,16 @@ sqlite3_stmt *db_select_(const char *location, struct db *db, const char *query)
db_begin_transaction_((db), __FILE__ ":" stringify(__LINE__))
void db_begin_transaction_(struct db *db, const char *location);

bool db_in_transaction(struct db *db);

// FIXME(cdecker) Need to maybe add a pointer to the db_stmt we are referring to
// FIXME(cdecker) Comment
u64 db_last_insert_id(struct db *db);

// FIXME(cdecker) Need to maybe add a pointer to the db_stmt we are referring to
// FIXME(cdecker) Comment
size_t db_changes(struct db *db);

/**
* db_commit_transaction - Commit a running transaction
*
Expand Down
4 changes: 2 additions & 2 deletions wallet/invoices.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ bool invoices_create(struct invoices *invoices,

db_exec_prepared(invoices->db, stmt);

pinvoice->id = sqlite3_last_insert_rowid(invoices->db->sql);
pinvoice->id = db_last_insert_id(invoices->db);

/* Install expiration trigger. */
if (!invoices->expiration_timer ||
Expand Down Expand Up @@ -392,7 +392,7 @@ bool invoices_delete(struct invoices *invoices,
sqlite3_bind_int64(stmt, 1, invoice.id);
db_exec_prepared(invoices->db, stmt);

if (sqlite3_changes(invoices->db->sql) != 1)
if (db_changes(invoices->db) != 1)
return false;

/* Tell all the waiters about the fact that it was deleted. */
Expand Down
14 changes: 7 additions & 7 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ bool wallet_update_output_status(struct wallet *w,
sqlite3_bind_int(stmt, 3, outnum);
}
db_exec_prepared(w->db, stmt);
return sqlite3_changes(w->db->sql) > 0;
return db_changes(w->db) > 0;
}

struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, const enum output_status state)
Expand Down Expand Up @@ -546,7 +546,7 @@ static void wallet_shachain_init(struct wallet *wallet,
sqlite3_bind_int64(stmt, 1, chain->chain.min_index);
db_exec_prepared(wallet->db, stmt);

chain->id = sqlite3_last_insert_rowid(wallet->db->sql);
chain->id = db_last_insert_id(wallet->db);
}

/* TODO(cdecker) Stolen from shachain, move to some appropriate location */
Expand Down Expand Up @@ -1067,7 +1067,7 @@ static void wallet_channel_config_insert(struct wallet *w,

stmt = db_prepare(w->db, "INSERT INTO channel_configs DEFAULT VALUES;");
db_exec_prepared(w->db, stmt);
cc->id = sqlite3_last_insert_rowid(w->db->sql);
cc->id = db_last_insert_id(w->db);
}

static void wallet_channel_config_save(struct wallet *w,
Expand Down Expand Up @@ -1293,7 +1293,7 @@ void wallet_channel_insert(struct wallet *w, struct channel *chan)
type_to_string(tmpctx, struct wireaddr_internal, &chan->peer->addr),
-1, SQLITE_TRANSIENT);
db_exec_prepared(w->db, stmt);
chan->peer->dbid = sqlite3_last_insert_rowid(w->db->sql);
chan->peer->dbid = db_last_insert_id(w->db);
}

/* Insert a stub, that we update, unifies INSERT and UPDATE paths */
Expand Down Expand Up @@ -1512,7 +1512,7 @@ void wallet_htlc_save_in(struct wallet *wallet,
sqlite3_bind_timeabs(stmt, 11, in->received_time);

db_exec_prepared(wallet->db, stmt);
in->dbid = sqlite3_last_insert_rowid(wallet->db->sql);
in->dbid = db_last_insert_id(wallet->db);
}

void wallet_htlc_save_out(struct wallet *wallet,
Expand Down Expand Up @@ -1562,7 +1562,7 @@ void wallet_htlc_save_out(struct wallet *wallet,

db_exec_prepared(wallet->db, stmt);

out->dbid = sqlite3_last_insert_rowid(wallet->db->sql);
out->dbid = db_last_insert_id(wallet->db);
}

void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid,
Expand Down Expand Up @@ -2518,7 +2518,7 @@ wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight,

db_exec_prepared(w->db, stmt);

if (sqlite3_changes(w->db->sql) == 0) {
if (db_changes(w->db) == 0) {
return NULL;
}

Expand Down

0 comments on commit 803007e

Please sign in to comment.