| 2131 | } |
| 2132 | |
| 2133 | static void wallet_peer_save(struct wallet *w, struct peer *peer) |
| 2134 | { |
| 2135 | const char *addr = |
| 2136 | type_to_string(tmpctx, struct wireaddr_internal, &peer->addr); |
| 2137 | struct db_stmt *stmt = |
| 2138 | db_prepare_v2(w->db, SQL("SELECT id FROM peers WHERE node_id = ?")); |
| 2139 | |
| 2140 | db_bind_node_id(stmt, 0, &peer->id); |
| 2141 | db_query_prepared(stmt); |
| 2142 | |
| 2143 | if (db_step(stmt)) { |
| 2144 | /* So we already knew this peer, just return its dbid */ |
| 2145 | peer->dbid = db_col_u64(stmt, "id"); |
| 2146 | tal_free(stmt); |
| 2147 | |
| 2148 | /* Since we're at it update the wireaddr */ |
| 2149 | stmt = db_prepare_v2( |
| 2150 | w->db, SQL("UPDATE peers SET address = ? WHERE id = ?")); |
| 2151 | db_bind_text(stmt, 0, addr); |
| 2152 | db_bind_u64(stmt, 1, peer->dbid); |
| 2153 | db_exec_prepared_v2(take(stmt)); |
| 2154 | |
| 2155 | } else { |
| 2156 | /* Unknown peer, create it from scratch */ |
| 2157 | tal_free(stmt); |
| 2158 | stmt = db_prepare_v2(w->db, |
| 2159 | SQL("INSERT INTO peers (node_id, address) VALUES (?, ?);") |
| 2160 | ); |
| 2161 | db_bind_node_id(stmt, 0, &peer->id); |
| 2162 | db_bind_text(stmt, 1,addr); |
| 2163 | db_exec_prepared_v2(stmt); |
| 2164 | peer->dbid = db_last_insert_id_v2(take(stmt)); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | void wallet_channel_insert(struct wallet *w, struct channel *chan) |
| 2169 | { |
no test coverage detected