From a2b5b1561e23847f85df7bc283bcdd0d15df3f22 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 3 Aug 2019 18:13:31 +0200 Subject: [PATCH] db: Add method to count changed rows of a db_stmt I was hoping to get rid of these by using "ON CONFLICT" upserts, however sqlite3 only started supporting them in version 3.24.0 which is newer than some of our deployment targets. Signed-off-by: Christian Decker --- wallet/db.c | 5 +++++ wallet/db.h | 1 + wallet/db_common.h | 2 ++ wallet/db_sqlite3.c | 8 ++++++++ 4 files changed, 16 insertions(+) diff --git a/wallet/db.c b/wallet/db.c index 4e0346b50..8d4c03105 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -640,6 +640,11 @@ const unsigned char *db_column_text(struct db_stmt *stmt, int col) return stmt->db->config->column_blob_fn(stmt, col); } +size_t db_count_changes(struct db_stmt *stmt) +{ + return stmt->db->config->count_changes_fn(stmt); +} + bool db_select_step_(const char *location, struct db *db, struct sqlite3_stmt *stmt) { int ret; diff --git a/wallet/db.h b/wallet/db.h index 8b388ee59..a81dccfde 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -257,6 +257,7 @@ int db_column_is_null(struct db_stmt *stmt, int col); const void* db_column_blob(struct db_stmt *stmt, int col); const unsigned char *db_column_text(struct db_stmt *stmt, int col); bool db_query_prepared(struct db_stmt *stmt); +size_t db_count_changes(struct db_stmt *stmt); struct db_stmt *db_prepare_v2_(const char *location, struct db *db, const char *query_id); diff --git a/wallet/db_common.h b/wallet/db_common.h index 5947a7ccc..31aef4efe 100644 --- a/wallet/db_common.h +++ b/wallet/db_common.h @@ -122,6 +122,8 @@ struct db_config { const void *(*column_blob_fn)(struct db_stmt *stmt, int col); const unsigned char *(*column_text_fn)(struct db_stmt *stmt, int col); s64 (*column_int_fn)(struct db_stmt *stmt, int col); + + size_t (*count_changes_fn)(struct db_stmt *stmt); }; /* Provide a way for DB backends to register themselves */ diff --git a/wallet/db_sqlite3.c b/wallet/db_sqlite3.c index 189b0e751..0da021cd4 100644 --- a/wallet/db_sqlite3.c +++ b/wallet/db_sqlite3.c @@ -167,6 +167,12 @@ static void db_sqlite3_stmt_free(struct db_stmt *stmt) stmt->inner_stmt = NULL; } +static size_t db_sqlite3_count_changes(struct db_stmt *stmt) +{ + sqlite3 *s = stmt->db->conn; + return sqlite3_changes(s); +} + struct db_config db_sqlite3_config = { .name = "sqlite3", .queries = db_sqlite3_queries, @@ -185,6 +191,8 @@ struct db_config db_sqlite3_config = { .column_bytes_fn = &db_sqlite3_column_bytes, .column_blob_fn = &db_sqlite3_column_blob, .column_text_fn = &db_sqlite3_column_text, + + .count_changes_fn = &db_sqlite3_count_changes, }; AUTODATA(db_backends, &db_sqlite3_config);