| 1405 | } |
| 1406 | |
| 1407 | static struct command_result *json_sql(struct command *cmd, |
| 1408 | const char *buffer, |
| 1409 | const jsmntok_t *params) |
| 1410 | { |
| 1411 | struct sql *sql = sql_of(cmd->plugin); |
| 1412 | struct db_query *dbq = tal(cmd, struct db_query); |
| 1413 | const char *query; |
| 1414 | int err; |
| 1415 | |
| 1416 | if (!param(cmd, buffer, params, |
| 1417 | p_req("query", param_string, &query), |
| 1418 | NULL)) |
| 1419 | return command_param_failed(); |
| 1420 | |
| 1421 | dbq->tables = tal_arr(dbq, struct table_desc *, 0); |
| 1422 | dbq->authfail = NULL; |
| 1423 | dbq->cmd = cmd; |
| 1424 | /* We might want to warn on SELECT *, since that is not really |
| 1425 | * recommended as fields change, but SELECT COUNT(*) is totally |
| 1426 | * legitimate. So we suppress deprecation errors in this case */ |
| 1427 | dbq->has_wildcard = (strchr(query, '*') != NULL); |
| 1428 | |
| 1429 | /* This both checks we're not altering, *and* tells us what |
| 1430 | * tables to refresh. */ |
| 1431 | err = sqlite3_set_authorizer(sql->db, sqlite_authorize, dbq); |
| 1432 | if (err != SQLITE_OK) { |
| 1433 | plugin_err(cmd->plugin, "Could not set authorizer: %s", |
| 1434 | sqlite3_errmsg(sql->db)); |
| 1435 | } |
| 1436 | |
| 1437 | err = sqlite3_prepare_v2(sql->db, query, -1, &dbq->stmt, NULL); |
| 1438 | sqlite3_set_authorizer(sql->db, NULL, NULL); |
| 1439 | |
| 1440 | if (err != SQLITE_OK) { |
| 1441 | char *errmsg = tal_fmt(tmpctx, "query failed with %s", |
| 1442 | sqlite3_errmsg(sql->db)); |
| 1443 | if (dbq->authfail) |
| 1444 | tal_append_fmt(&errmsg, " (%s)", dbq->authfail); |
| 1445 | return command_fail(cmd, LIGHTNINGD, "%s", errmsg); |
| 1446 | } |
| 1447 | |
| 1448 | return refresh_tables(cmd, dbq); |
| 1449 | } |
| 1450 | |
| 1451 | static bool ignore_column(const struct table_desc *td, const jsmntok_t *t) |
| 1452 | { |
nothing calls this directly
no test coverage detected