Skip to content

Commit

Permalink
wallet: Remove printf-like db_select variant
Browse files Browse the repository at this point in the history
We need to have full DB queries that can be extracted at compile time later in
order to be able to rewrite them in other SQL dialects. In addition we had a
bit of unnecessary code-duplication in db_select and db_select_prepare. Now
the former uses the latter internally.

Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed Sep 5, 2019
1 parent b856bb2 commit f08fd9e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 65 deletions.
15 changes: 2 additions & 13 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,24 +586,13 @@ static sqlite3_stmt *db_query(const char *location,
return stmt;
}

sqlite3_stmt *PRINTF_FMT(3, 4)
db_select_(const char *location, struct db *db, const char *fmt, ...)
sqlite3_stmt *db_select_(const char *location, struct db *db, const char *query)
{
va_list ap;
char *query = tal_strdup(db, "SELECT ");
sqlite3_stmt *stmt;

assert(db->in_transaction);

va_start(ap, fmt);
tal_append_vfmt(&query, fmt, ap);
va_end(ap);

stmt = db_query(location, db, query);
if (!stmt)
db_fatal("%s:%s:%s", location, query, sqlite3_errmsg(db->sql));
tal_free(query);

stmt = db_select_prepare(db, query);
return stmt;
}

Expand Down
7 changes: 3 additions & 4 deletions wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ struct db *db_setup(const tal_t *ctx, struct lightningd *ld, struct log *log);
*
* A simpler version of db_select_prepare.
*/
sqlite3_stmt *PRINTF_FMT(3, 4)
db_select_(const char *location, struct db *db, const char *fmt, ...);
#define db_select(db, ...) \
db_select_(__FILE__ ":" stringify(__LINE__), db, __VA_ARGS__)
sqlite3_stmt *db_select_(const char *location, struct db *db, const char *query);
#define db_select(db, query) \
db_select_(__FILE__ ":" stringify(__LINE__), db, query)

/**
* db_begin_transaction - Begin a transaction
Expand Down
100 changes: 52 additions & 48 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,9 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
struct node_id id;
struct wireaddr_internal addr;

sqlite3_stmt *stmt =
db_select(w->db,
"id, node_id, address FROM peers WHERE id=%"PRIu64";", dbid);
sqlite3_stmt *stmt = db_select_prepare(
w->db, "id, node_id, address FROM peers WHERE id=?;");
sqlite3_bind_int64(stmt, 1, dbid);

if (!db_select_step(w->db, stmt))
return NULL;
Expand Down Expand Up @@ -937,8 +937,8 @@ static bool wallet_channels_load_active(struct wallet *w)
", feerate_base"
", feerate_ppm"
", remote_upfront_shutdown_script"
" FROM channels WHERE state < %d;",
CLOSED);
" FROM channels WHERE state < ?;");
sqlite3_bind_int(stmt, 1, CLOSED);

while (db_select_step(w->db, stmt)) {
struct channel *c = wallet_stmt2channel(w, stmt);
Expand Down Expand Up @@ -1098,8 +1098,10 @@ bool wallet_channel_config_load(struct wallet *w, const u64 id,
const char *query =
"id, dust_limit_satoshis, max_htlc_value_in_flight_msat, "
"channel_reserve_satoshis, htlc_minimum_msat, to_self_delay, "
"max_accepted_htlcs FROM channel_configs WHERE id=%" PRIu64 ";";
sqlite3_stmt *stmt = db_select(w->db, query, id);
"max_accepted_htlcs FROM channel_configs WHERE id= ? ;";
sqlite3_stmt *stmt = db_select_prepare(w->db, query);
sqlite3_bind_int64(stmt, 1, id);

if (!db_select_step(w->db, stmt))
return false;

Expand Down Expand Up @@ -1364,9 +1366,9 @@ void wallet_peer_delete(struct wallet *w, u64 peer_dbid)
sqlite3_stmt *stmt;

/* Must not have any channels still using this peer */
stmt = db_select(w->db,
"* FROM channels WHERE peer_id = %"PRIu64,
peer_dbid);
stmt = db_select_prepare(w->db, "* FROM channels WHERE peer_id = ?;");
sqlite3_bind_int64(stmt, 1, peer_dbid);

if (db_select_step(w->db, stmt))
fatal("We have channels using peer %"PRIu64, peer_dbid);

Expand Down Expand Up @@ -1725,25 +1727,26 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
int incount = 0, outcount = 0;

log_debug(wallet->log, "Loading HTLCs for channel %"PRIu64, chan->dbid);
sqlite3_stmt *stmt = db_select(wallet->db,
" id"
", channel_htlc_id"
", msatoshi"
", cltv_expiry"
", hstate"
", payment_hash"
", payment_key"
", routing_onion"
", failuremsg"
", malformed_onion"
", origin_htlc"
", shared_secret"
", received_time"
" FROM channel_htlcs"
" WHERE direction=%d"
" AND channel_id=%" PRIu64
" AND hstate != %d",
DIRECTION_INCOMING, chan->dbid, SENT_REMOVE_ACK_REVOCATION);
sqlite3_stmt *stmt = db_select_prepare(wallet->db, " id"
", channel_htlc_id"
", msatoshi"
", cltv_expiry"
", hstate"
", payment_hash"
", payment_key"
", routing_onion"
", failuremsg"
", malformed_onion"
", origin_htlc"
", shared_secret"
", received_time"
" FROM channel_htlcs"
" WHERE direction= ?"
" AND channel_id= ?"
" AND hstate != ?");
sqlite3_bind_int(stmt, 1, DIRECTION_INCOMING);
sqlite3_bind_int64(stmt, 2, chan->dbid);
sqlite3_bind_int(stmt, 3, SENT_REMOVE_ACK_REVOCATION);

while (db_select_step(wallet->db, stmt)) {
struct htlc_in *in = tal(chan, struct htlc_in);
Expand All @@ -1754,25 +1757,26 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
incount++;
}

stmt = db_select(wallet->db,
" id"
", channel_htlc_id"
", msatoshi"
", cltv_expiry"
", hstate"
", payment_hash"
", payment_key"
", routing_onion"
", failuremsg"
", malformed_onion"
", origin_htlc"
", shared_secret"
", received_time"
" FROM channel_htlcs"
" WHERE direction=%d"
" AND channel_id=%" PRIu64
" AND hstate != %d",
DIRECTION_OUTGOING, chan->dbid, RCVD_REMOVE_ACK_REVOCATION);
stmt = db_select_prepare(wallet->db, " id"
", channel_htlc_id"
", msatoshi"
", cltv_expiry"
", hstate"
", payment_hash"
", payment_key"
", routing_onion"
", failuremsg"
", malformed_onion"
", origin_htlc"
", shared_secret"
", received_time"
" FROM channel_htlcs"
" WHERE direction = ?"
" AND channel_id = ?"
" AND hstate != ?");
sqlite3_bind_int(stmt, 1, DIRECTION_OUTGOING);
sqlite3_bind_int64(stmt, 2, chan->dbid);
sqlite3_bind_int(stmt, 3, RCVD_REMOVE_ACK_REVOCATION);

while (db_select_step(wallet->db, stmt)) {
struct htlc_out *out = tal(chan, struct htlc_out);
Expand Down

0 comments on commit f08fd9e

Please sign in to comment.