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 <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-08-03 18:13:31 +02:00 committed by Rusty Russell
parent d0027b1036
commit a2b5b1561e
4 changed files with 16 additions and 0 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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 */

View File

@ -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);