db: Add _or_default variants for column accesses

We were implicitly relying on sqlite3 behavior that returns the zero-value for
nulled fields when accessing them. This adds the same behavior explicitly to
the DB abstraction in order to reduce `db_column_is_null` checks in the logic,
but still make it evident what is happening here.

Fixes https://github.com/fiatjaf/mcldsp/issues/1

Signed-off-by: Christian Decker <@cdecker>
This commit is contained in:
Christian Decker 2019-10-17 13:55:11 +02:00 committed by neil saitug
parent 894627a287
commit 12f40f2227
3 changed files with 33 additions and 8 deletions

View file

@ -565,6 +565,14 @@ u64 db_column_u64(struct db_stmt *stmt, int col)
return stmt->db->config->column_u64_fn(stmt, col);
}
int db_column_int_or_default(struct db_stmt *stmt, int col, int def)
{
if (db_column_is_null(stmt, col))
return def;
else
return db_column_int(stmt, col);
}
int db_column_int(struct db_stmt *stmt, int col)
{
return stmt->db->config->column_int_fn(stmt, col);
@ -1143,6 +1151,16 @@ void *db_column_arr_(const tal_t *ctx, struct db_stmt *stmt, int col,
return p;
}
void db_column_amount_msat_or_default(struct db_stmt *stmt, int col,
struct amount_msat *msat,
struct amount_msat def)
{
if (db_column_is_null(stmt, col))
*msat = def;
else
msat->millisatoshis = db_column_u64(stmt, col); /* Raw: low level function */
}
void db_column_amount_msat(struct db_stmt *stmt, int col,
struct amount_msat *msat)
{

View file

@ -159,6 +159,13 @@ struct bitcoin_tx *db_column_tx(const tal_t *ctx, struct db_stmt *stmt, int col)
void *db_column_arr_(const tal_t *ctx, struct db_stmt *stmt, int col,
size_t bytes, const char *label, const char *caller);
/* Some useful default variants */
int db_column_int_or_default(struct db_stmt *stmt, int col, int def);
void db_column_amount_msat_or_default(struct db_stmt *stmt, int col,
struct amount_msat *msat,
struct amount_msat def);
/**
* db_exec_prepared -- Execute a prepared statement
*

View file

@ -1143,14 +1143,14 @@ void wallet_channel_stats_load(struct wallet *w,
/* This must succeed, since we know the channel exists */
assert(res);
stats->in_payments_offered = db_column_int(stmt, 0);
stats->in_payments_fulfilled = db_column_int(stmt, 1);
db_column_amount_msat(stmt, 2, &stats->in_msatoshi_offered);
db_column_amount_msat(stmt, 3, &stats->in_msatoshi_fulfilled);
stats->out_payments_offered = db_column_int(stmt, 4);
stats->out_payments_fulfilled = db_column_int(stmt, 5);
db_column_amount_msat(stmt, 6, &stats->out_msatoshi_offered);
db_column_amount_msat(stmt, 7, &stats->out_msatoshi_fulfilled);
stats->in_payments_offered = db_column_int_or_default(stmt, 0, 0);
stats->in_payments_fulfilled = db_column_int_or_default(stmt, 1, 0);
db_column_amount_msat_or_default(stmt, 2, &stats->in_msatoshi_offered, AMOUNT_MSAT(0));
db_column_amount_msat_or_default(stmt, 3, &stats->in_msatoshi_fulfilled, AMOUNT_MSAT(0));
stats->out_payments_offered = db_column_int_or_default(stmt, 4, 0);
stats->out_payments_fulfilled = db_column_int_or_default(stmt, 5, 0);
db_column_amount_msat_or_default(stmt, 6, &stats->out_msatoshi_offered, AMOUNT_MSAT(0));
db_column_amount_msat_or_default(stmt, 7, &stats->out_msatoshi_fulfilled, AMOUNT_MSAT(0));
tal_free(stmt);
}