mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
db: add generic warn/error function.
This avoids the mess where we override db_fatal for teqsts, and keeps it generic. Also allows us to get rid of one #if DEVELOPER, and an ugly global for bookkeeper. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
52
db/utils.c
52
db/utils.c
@@ -24,7 +24,7 @@ size_t db_query_colnum(const struct db_stmt *stmt,
|
||||
for (;;) {
|
||||
const char *n = stmt->query->colnames[col].sqlname;
|
||||
if (!n)
|
||||
db_fatal("Unknown column name %s in query %s",
|
||||
db_fatal(stmt->db, "Unknown column name %s in query %s",
|
||||
colname, stmt->query->query);
|
||||
if (streq(n, colname))
|
||||
break;
|
||||
@@ -41,7 +41,7 @@ size_t db_query_colnum(const struct db_stmt *stmt,
|
||||
static void db_stmt_free(struct db_stmt *stmt)
|
||||
{
|
||||
if (!stmt->executed)
|
||||
db_fatal("Freeing an un-executed statement from %s: %s",
|
||||
db_fatal(stmt->db, "Freeing an un-executed statement from %s: %s",
|
||||
stmt->location, stmt->query->query);
|
||||
#if DEVELOPER
|
||||
/* If they never got a db_step, we don't track */
|
||||
@@ -51,7 +51,7 @@ static void db_stmt_free(struct db_stmt *stmt)
|
||||
continue;
|
||||
if (!strset_get(stmt->cols_used,
|
||||
stmt->query->colnames[i].sqlname)) {
|
||||
db_fatal("Never accessed column %s in query %s",
|
||||
db_fatal(stmt->db, "Never accessed column %s in query %s",
|
||||
stmt->query->colnames[i].sqlname,
|
||||
stmt->query->query);
|
||||
}
|
||||
@@ -104,14 +104,14 @@ struct db_stmt *db_prepare_v2_(const char *location, struct db *db,
|
||||
query_id += 2;
|
||||
|
||||
if (!db->in_transaction)
|
||||
db_fatal("Attempting to prepare a db_stmt outside of a "
|
||||
db_fatal(db, "Attempting to prepare a db_stmt outside of a "
|
||||
"transaction: %s", location);
|
||||
|
||||
/* Look up the query by its ID */
|
||||
pos = hash_djb2(query_id) % db->queries->query_table_size;
|
||||
for (;;) {
|
||||
if (!db->queries->query_table[pos].name)
|
||||
db_fatal("Could not resolve query %s", query_id);
|
||||
db_fatal(db, "Could not resolve query %s", query_id);
|
||||
if (streq(query_id, db->queries->query_table[pos].name))
|
||||
break;
|
||||
pos = (pos + 1) % db->queries->query_table_size;
|
||||
@@ -154,7 +154,7 @@ bool db_query_prepared_canfail(struct db_stmt *stmt)
|
||||
void db_query_prepared(struct db_stmt *stmt)
|
||||
{
|
||||
if (!db_query_prepared_canfail(stmt))
|
||||
db_fatal("query failed: %s: %s",
|
||||
db_fatal(stmt->db, "query failed: %s: %s",
|
||||
stmt->location, stmt->query->query);
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ void db_exec_prepared_v2(struct db_stmt *stmt TAKES)
|
||||
* we report an error. */
|
||||
if (!ret) {
|
||||
assert(stmt->error);
|
||||
db_fatal("Error executing statement: %s", stmt->error);
|
||||
db_fatal(stmt->db, "Error executing statement: %s", stmt->error);
|
||||
}
|
||||
|
||||
if (taken(stmt))
|
||||
@@ -260,7 +260,7 @@ void db_assert_no_outstanding_statements(struct db *db)
|
||||
|
||||
stmt = list_top(&db->pending_statements, struct db_stmt, list);
|
||||
if (stmt)
|
||||
db_fatal("Unfinalized statement %s", stmt->location);
|
||||
db_fatal(stmt->db, "Unfinalized statement %s", stmt->location);
|
||||
}
|
||||
#else
|
||||
void db_assert_no_outstanding_statements(struct db *db)
|
||||
@@ -277,7 +277,7 @@ static void destroy_db(struct db *db)
|
||||
db->config->teardown_fn(db);
|
||||
}
|
||||
|
||||
static struct db_config *db_config_find(const char *dsn)
|
||||
static struct db_config *db_config_find(const struct db *db, const char *dsn)
|
||||
{
|
||||
size_t num_configs;
|
||||
struct db_config **configs = autodata_get(db_backends, &num_configs);
|
||||
@@ -285,7 +285,7 @@ static struct db_config *db_config_find(const char *dsn)
|
||||
sep = strstr(dsn, "://");
|
||||
|
||||
if (!sep)
|
||||
db_fatal("%s doesn't look like a valid data-source name (missing \"://\" separator.", dsn);
|
||||
db_fatal(db, "%s doesn't look like a valid data-source name (missing \"://\" separator.", dsn);
|
||||
|
||||
driver_name = tal_strndup(tmpctx, dsn, sep - dsn);
|
||||
|
||||
@@ -319,23 +319,43 @@ void db_prepare_for_changes(struct db *db)
|
||||
db->changes = tal_arr(db, const char *, 0);
|
||||
}
|
||||
|
||||
struct db *db_open(const tal_t *ctx, const char *filename)
|
||||
void db_fatal(const struct db *db, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
db->errorfn(db->errorfn_arg, true, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void db_warn(const struct db *db, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
db->errorfn(db->errorfn_arg, false, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
struct db *db_open_(const tal_t *ctx, const char *filename,
|
||||
void (*errorfn)(void *arg, bool fatal, const char *fmt, va_list ap),
|
||||
void *arg)
|
||||
{
|
||||
struct db *db;
|
||||
|
||||
db = tal(ctx, struct db);
|
||||
db->filename = tal_strdup(db, filename);
|
||||
db->errorfn = errorfn;
|
||||
db->errorfn_arg = arg;
|
||||
list_head_init(&db->pending_statements);
|
||||
if (!strstr(db->filename, "://"))
|
||||
db_fatal("Could not extract driver name from \"%s\"", db->filename);
|
||||
db_fatal(db, "Could not extract driver name from \"%s\"", db->filename);
|
||||
|
||||
db->config = db_config_find(db->filename);
|
||||
db->config = db_config_find(db, db->filename);
|
||||
if (!db->config)
|
||||
db_fatal("Unable to find DB driver for %s", db->filename);
|
||||
db_fatal(db, "Unable to find DB driver for %s", db->filename);
|
||||
|
||||
db->queries = db_queries_find(db->config);
|
||||
if (!db->queries)
|
||||
db_fatal("Unable to find DB queries for %s", db->config->name);
|
||||
db_fatal(db, "Unable to find DB queries for %s", db->config->name);
|
||||
|
||||
tal_add_destructor(db, destroy_db);
|
||||
db->in_transaction = NULL;
|
||||
@@ -346,7 +366,7 @@ struct db *db_open(const tal_t *ctx, const char *filename)
|
||||
|
||||
db_prepare_for_changes(db);
|
||||
if (db->config->setup_fn && !db->config->setup_fn(db))
|
||||
db_fatal("Error calling DB setup: %s", db->error);
|
||||
db_fatal(db, "Error calling DB setup: %s", db->error);
|
||||
db_report_changes(db, NULL, 0);
|
||||
|
||||
return db;
|
||||
|
||||
Reference in New Issue
Block a user