mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
db: Make the db
struct private and provide accessors instead
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 <decker.christian@gmail.com>
This commit is contained in:
parent
66a47d2761
commit
803007ecdf
@ -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)
|
||||
|
@ -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(); }
|
||||
|
22
wallet/db.c
22
wallet/db.c
@ -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
|
||||
@ -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)
|
||||
|
17
wallet/db.h
17
wallet/db.h
@ -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.
|
||||
@ -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
|
||||
*
|
||||
|
@ -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 ||
|
||||
@ -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. */
|
||||
|
@ -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)
|
||||
@ -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 */
|
||||
@ -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,
|
||||
@ -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 */
|
||||
@ -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,
|
||||
@ -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,
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user