| 311 | } |
| 312 | |
| 313 | struct db *db_open(const tal_t *ctx, const char *filename) |
| 314 | { |
| 315 | struct db *db; |
| 316 | |
| 317 | db = tal(ctx, struct db); |
| 318 | db->filename = tal_strdup(db, filename); |
| 319 | list_head_init(&db->pending_statements); |
| 320 | if (!strstr(db->filename, "://")) |
| 321 | db_fatal("Could not extract driver name from \"%s\"", db->filename); |
| 322 | |
| 323 | db->config = db_config_find(db->filename); |
| 324 | if (!db->config) |
| 325 | db_fatal("Unable to find DB driver for %s", db->filename); |
| 326 | |
| 327 | db->queries = db_queries_find(db->config); |
| 328 | if (!db->queries) |
| 329 | db_fatal("Unable to find DB queries for %s", db->config->name); |
| 330 | |
| 331 | tal_add_destructor(db, destroy_db); |
| 332 | db->in_transaction = NULL; |
| 333 | db->changes = NULL; |
| 334 | |
| 335 | /* This must be outside a transaction, so catch it */ |
| 336 | assert(!db->in_transaction); |
| 337 | |
| 338 | db_prepare_for_changes(db); |
| 339 | if (db->config->setup_fn && !db->config->setup_fn(db)) |
| 340 | db_fatal("Error calling DB setup: %s", db->error); |
| 341 | db_report_changes(db, NULL, 0); |
| 342 | |
| 343 | return db; |
| 344 | } |