A list, such as in the top-level reply, or for a sub-table */
| 835 | |
| 836 | /* A list, such as in the top-level reply, or for a sub-table */ |
| 837 | static struct command_result *process_json_list(struct command *cmd, |
| 838 | const char *buf, |
| 839 | const jsmntok_t *arr, |
| 840 | const u64 *parent_rowid, |
| 841 | const struct table_desc *td, |
| 842 | bool update, |
| 843 | u64 *last_created_index, |
| 844 | u64 *last_updated_index) |
| 845 | { |
| 846 | struct sql *sql = sql_of(cmd->plugin); |
| 847 | size_t i; |
| 848 | const jsmntok_t *t; |
| 849 | int err; |
| 850 | sqlite3_stmt *insert_stmt, *delete_stmt; |
| 851 | struct command_result *ret = NULL; |
| 852 | |
| 853 | err = sqlite3_prepare_v2(sql->db, td->insert_stmt, -1, &insert_stmt, NULL); |
| 854 | if (err != SQLITE_OK) { |
| 855 | return command_fail(cmd, LIGHTNINGD, "preparing '%s' failed: %s", |
| 856 | td->insert_stmt, |
| 857 | sqlite3_errmsg(sql->db)); |
| 858 | } |
| 859 | |
| 860 | /* Updating? Delete any previous record */ |
| 861 | if (update) { |
| 862 | err = sqlite3_prepare_v2(sql->db, td->delete_stmt, -1, &delete_stmt, NULL); |
| 863 | if (err != SQLITE_OK) { |
| 864 | return command_fail(cmd, LIGHTNINGD, "preparing '%s' failed: %s", |
| 865 | td->delete_stmt, |
| 866 | sqlite3_errmsg(sql->db)); |
| 867 | } |
| 868 | } else { |
| 869 | delete_stmt = NULL; |
| 870 | } |
| 871 | |
| 872 | json_for_each_arr(i, t, arr) { |
| 873 | /* sqlite3 columns are 1-based! */ |
| 874 | size_t off = 1; |
| 875 | u64 this_rowid; |
| 876 | |
| 877 | if (!td->has_created_index) { |
| 878 | this_rowid = sql->next_rowid++; |
| 879 | /* First entry is always the rowid */ |
| 880 | sqlite3_bind_int64(insert_stmt, off++, this_rowid); |
| 881 | assert(!delete_stmt); |
| 882 | } else { |
| 883 | if (!json_to_u64(buf, |
| 884 | json_get_member(buf, t, "created_index"), |
| 885 | &this_rowid)) |
| 886 | return command_fail(cmd, LIGHTNINGD, "No created_index in %s? '%.*s'", |
| 887 | td->cmdname, |
| 888 | json_tok_full_len(t), |
| 889 | json_tok_full(buf, t)); |
| 890 | |
| 891 | /* For updates, we simply delete old entry: this |
| 892 | * cascades to subtables. We ignore updates on |
| 893 | * entries we don't have yet, too. */ |
| 894 | if (delete_stmt) { |
no test coverage detected