db_col_optional: wrapper for case where a field is allowed to be NULL.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-03-20 10:48:35 +10:30
parent d9e274cee2
commit aae77802ef
2 changed files with 25 additions and 0 deletions

View File

@ -367,6 +367,17 @@ void db_col_short_channel_id(struct db_stmt *stmt, const char *colname,
dest->u64 = db_col_u64(stmt, colname);
}
void *db_col_optional_(tal_t *dst,
struct db_stmt *stmt, const char *colname,
void (*colfn)(struct db_stmt *, const char *, void *))
{
if (db_col_is_null(stmt, colname))
return tal_free(dst);
colfn(stmt, colname, dst);
return dst;
}
struct short_channel_id *
db_col_short_channel_id_arr(const tal_t *ctx, struct db_stmt *stmt, const char *colname)
{

View File

@ -105,6 +105,20 @@ void *db_col_arr_(const tal_t *ctx, struct db_stmt *stmt, const char *colname,
size_t bytes, const char *label, const char *caller);
/* Assumes void db_col_@type(stmt, colname, addr), and struct @type! */
#define db_col_optional(ctx, stmt, colname, type) \
((struct type *)db_col_optional_(tal(ctx, struct type), \
(stmt), (colname), \
typesafe_cb_cast(void (*)(struct db_stmt *, const char *, void *), \
void (*)(struct db_stmt *, const char *, struct type *), \
db_col_##type)))
void *WARN_UNUSED_RESULT db_col_optional_(tal_t *dst,
struct db_stmt *stmt,
const char *colname,
void (*colfn)(struct db_stmt *,
const char *, void *));
/* Some useful default variants */
int db_col_int_or_default(struct db_stmt *stmt, const char *colname, int def);
void db_col_amount_msat_or_default(struct db_stmt *stmt, const char *colname,