From befab73070b625ceb8e774601aacb05cc4809c20 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 15 Nov 2021 04:25:46 +1030 Subject: [PATCH] db: improve db_col_* APIs. 1. db_col_text becomes db_col_strdup, which is what is usually wanted. 2. db_col_short_channel_id becomes db_col_short_channel_id_str, to emphasize that it stores in string form. Modern versions should store u64. Signed-off-by: Rusty Russell --- wallet/db.c | 11 +++++++---- wallet/db.h | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index 5b2b3da4d..9f53bff77 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -2230,7 +2230,9 @@ const void *db_col_blob(struct db_stmt *stmt, const char *colname) return stmt->db->config->column_blob_fn(stmt, col); } -const unsigned char *db_col_text(struct db_stmt *stmt, const char *colname) +char *db_col_strdup(const tal_t *ctx, + struct db_stmt *stmt, + const char *colname) { size_t col = db_query_colnum(stmt, colname); @@ -2238,7 +2240,7 @@ const unsigned char *db_col_text(struct db_stmt *stmt, const char *colname) log_broken(stmt->db->log, "Accessing a null column %s/%zu in query %s", colname, col, stmt->query->query); return NULL; } - return stmt->db->config->column_text_fn(stmt, col); + return tal_strdup(ctx, (char *)stmt->db->config->column_text_fn(stmt, col)); } void db_col_preimage(struct db_stmt *stmt, const char *colname, @@ -2295,8 +2297,9 @@ void db_col_pubkey(struct db_stmt *stmt, assert(ok); } -bool db_col_short_channel_id(struct db_stmt *stmt, const char *colname, - struct short_channel_id *dest) +/* Yes, we put this in as a string. Past mistakes; do not use! */ +bool db_col_short_channel_id_str(struct db_stmt *stmt, const char *colname, + struct short_channel_id *dest) { size_t col = db_query_colnum(stmt, colname); const char *source = db_column_blob(stmt, col); diff --git a/wallet/db.h b/wallet/db.h index a18e5cf3b..9729623ba 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -190,7 +190,9 @@ int db_col_int(struct db_stmt *stmt, const char *colname); size_t db_col_bytes(struct db_stmt *stmt, const char *colname); int db_col_is_null(struct db_stmt *stmt, const char *colname); const void* db_col_blob(struct db_stmt *stmt, const char *colname); -const unsigned char *db_col_text(struct db_stmt *stmt, const char *colname); +char *db_col_strdup(const tal_t *ctx, + struct db_stmt *stmt, + const char *colname); void db_col_preimage(struct db_stmt *stmt, const char *colname, struct preimage *preimage); void db_col_amount_msat(struct db_stmt *stmt, const char *colname, struct amount_msat *msat); void db_col_amount_sat(struct db_stmt *stmt, const char *colname, struct amount_sat *sat); @@ -207,7 +209,7 @@ struct node_id *db_col_node_id_arr(const tal_t *ctx, struct db_stmt *stmt, const char *colname); void db_col_pubkey(struct db_stmt *stmt, const char *colname, struct pubkey *p); -bool db_col_short_channel_id(struct db_stmt *stmt, const char *colname, +bool db_col_short_channel_id_str(struct db_stmt *stmt, const char *colname, struct short_channel_id *dest); struct short_channel_id * db_col_short_channel_id_arr(const tal_t *ctx, struct db_stmt *stmt, const char *colname);