db: minor refactor

This commit is contained in:
CismonX 2025-02-02 17:55:14 +08:00
parent 54c81c43bb
commit d08848c097
No known key found for this signature in database
GPG key ID: 3094873E29A482FB

View file

@ -33,10 +33,6 @@
#include "xstd.h" #include "xstd.h"
struct db_check_ctx {
int status;
};
struct db_pragma_ctx { struct db_pragma_ctx {
char const *val; char const *val;
size_t val_len; size_t val_len;
@ -55,16 +51,14 @@ db_check_cb (
void *user_data, void *user_data,
sqlite3_stmt *stmt sqlite3_stmt *stmt
) { ) {
struct db_check_ctx *ctx = user_data; int *status_ptr = user_data;
size_t nbytes = sqlite3_column_bytes(stmt, 0); char const *result = (char const *)sqlite3_column_text(stmt, 0);
unsigned char const *result = sqlite3_column_text(stmt, 0);
xassert(result != NULL); xassert(result != NULL);
ctx->status = 0; if (0 != strcmp("ok", result)) {
if (nbytes != strlen("ok") || 0 != memcmp("ok", result, nbytes)) {
log_printf("%s: expected 'ok', got '%s'", sqlite3_sql(stmt), result); log_printf("%s: expected 'ok', got '%s'", sqlite3_sql(stmt), result);
ctx->status = -EIO; *status_ptr = -EIO;
} }
return 0; return 0;
} }
@ -119,13 +113,13 @@ db_check (
return -1; return -1;
} }
struct db_check_ctx qctx; int status = 0;
ssize_t nrows = db_query(stmt, NULL, 0, false, db_check_cb, &qctx); ssize_t nrows = db_query(stmt, NULL, 0, false, db_check_cb, &status);
if (nrows < 0) { if (nrows < 0) {
return nrows; return nrows;
} }
xassert(nrows == 1); xassert(nrows == 1);
return qctx.status; return status;
} }
int int