db/bindings: now bindings are always in order, remove index.

Simply always bind the next one.  No arithmetic required now!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-07-14 09:58:46 +09:30
parent 9af407a47a
commit 0bcff1e76d
10 changed files with 804 additions and 816 deletions

View File

@@ -16,18 +16,11 @@
#define NSEC_IN_SEC 1000000000 #define NSEC_IN_SEC 1000000000
static int check_bind_pos(struct db_stmt *stmt, int pos) static size_t check_bind_pos(struct db_stmt *stmt)
{ {
if (pos == BIND_NEXT) { size_t pos = ++stmt->bind_pos;
/* Don't mix BIND_NEXT with other args! */
assert(stmt->bindings[stmt->bind_pos+1].type == DB_BINDING_UNINITIALIZED);
return ++stmt->bind_pos;
}
/* Don't mix BIND_NEXT with other args! */
assert(stmt->bind_pos == -1);
assert(pos >= 0);
assert(pos < tal_count(stmt->bindings)); assert(pos < tal_count(stmt->bindings));
return pos; return pos;
} }
@@ -50,9 +43,9 @@ static bool db_column_null_warn(struct db_stmt *stmt, const char *colname,
return true; return true;
} }
void db_bind_int(struct db_stmt *stmt, int pos, int val) void db_bind_int(struct db_stmt *stmt, int val)
{ {
pos = check_bind_pos(stmt, pos); size_t pos = check_bind_pos(stmt);
memcheck(&val, sizeof(val)); memcheck(&val, sizeof(val));
stmt->bindings[pos].type = DB_BINDING_INT; stmt->bindings[pos].type = DB_BINDING_INT;
stmt->bindings[pos].v.i = val; stmt->bindings[pos].v.i = val;
@@ -73,58 +66,59 @@ int db_col_is_null(struct db_stmt *stmt, const char *colname)
return db_column_is_null(stmt, db_query_colnum(stmt, colname)); return db_column_is_null(stmt, db_query_colnum(stmt, colname));
} }
void db_bind_null(struct db_stmt *stmt, int pos) void db_bind_null(struct db_stmt *stmt)
{ {
pos = check_bind_pos(stmt, pos); size_t pos = check_bind_pos(stmt);
stmt->bindings[pos].type = DB_BINDING_NULL; stmt->bindings[pos].type = DB_BINDING_NULL;
} }
void db_bind_u64(struct db_stmt *stmt, int pos, u64 val) void db_bind_u64(struct db_stmt *stmt, u64 val)
{ {
size_t pos = check_bind_pos(stmt);
memcheck(&val, sizeof(val)); memcheck(&val, sizeof(val));
pos = check_bind_pos(stmt, pos);
stmt->bindings[pos].type = DB_BINDING_UINT64; stmt->bindings[pos].type = DB_BINDING_UINT64;
stmt->bindings[pos].v.u64 = val; stmt->bindings[pos].v.u64 = val;
} }
void db_bind_blob(struct db_stmt *stmt, int pos, const u8 *val, size_t len) void db_bind_blob(struct db_stmt *stmt, const u8 *val, size_t len)
{ {
pos = check_bind_pos(stmt, pos); size_t pos = check_bind_pos(stmt);
stmt->bindings[pos].type = DB_BINDING_BLOB; stmt->bindings[pos].type = DB_BINDING_BLOB;
stmt->bindings[pos].v.blob = memcheck(val, len); stmt->bindings[pos].v.blob = memcheck(val, len);
stmt->bindings[pos].len = len; stmt->bindings[pos].len = len;
} }
void db_bind_text(struct db_stmt *stmt, int pos, const char *val) void db_bind_text(struct db_stmt *stmt, const char *val)
{ {
pos = check_bind_pos(stmt, pos); size_t pos = check_bind_pos(stmt);
stmt->bindings[pos].type = DB_BINDING_TEXT; stmt->bindings[pos].type = DB_BINDING_TEXT;
stmt->bindings[pos].v.text = val; stmt->bindings[pos].v.text = val;
stmt->bindings[pos].len = strlen(val); stmt->bindings[pos].len = strlen(val);
} }
void db_bind_preimage(struct db_stmt *stmt, int pos, const struct preimage *p) void db_bind_preimage(struct db_stmt *stmt, const struct preimage *p)
{ {
db_bind_blob(stmt, pos, p->r, sizeof(struct preimage)); db_bind_blob(stmt, p->r, sizeof(struct preimage));
} }
void db_bind_sha256(struct db_stmt *stmt, int pos, const struct sha256 *s) void db_bind_sha256(struct db_stmt *stmt, const struct sha256 *s)
{ {
db_bind_blob(stmt, pos, s->u.u8, sizeof(struct sha256)); db_bind_blob(stmt, s->u.u8, sizeof(struct sha256));
} }
void db_bind_sha256d(struct db_stmt *stmt, int pos, const struct sha256_double *s) void db_bind_sha256d(struct db_stmt *stmt, const struct sha256_double *s)
{ {
db_bind_sha256(stmt, pos, &s->sha); db_bind_sha256(stmt, &s->sha);
} }
void db_bind_secret(struct db_stmt *stmt, int pos, const struct secret *s) void db_bind_secret(struct db_stmt *stmt, const struct secret *s)
{ {
assert(sizeof(s->data) == 32); assert(sizeof(s->data) == 32);
db_bind_blob(stmt, pos, s->data, sizeof(s->data)); db_bind_blob(stmt, s->data, sizeof(s->data));
} }
void db_bind_secret_arr(struct db_stmt *stmt, int col, const struct secret *s) void db_bind_secret_arr(struct db_stmt *stmt, const struct secret *s)
{ {
size_t num = tal_count(s), elsize = sizeof(s->data); size_t num = tal_count(s), elsize = sizeof(s->data);
u8 *ser = tal_arr(stmt, u8, num * elsize); u8 *ser = tal_arr(stmt, u8, num * elsize);
@@ -132,30 +126,30 @@ void db_bind_secret_arr(struct db_stmt *stmt, int col, const struct secret *s)
for (size_t i = 0; i < num; ++i) for (size_t i = 0; i < num; ++i)
memcpy(ser + i * elsize, &s[i], elsize); memcpy(ser + i * elsize, &s[i], elsize);
db_bind_blob(stmt, col, ser, tal_count(ser)); db_bind_blob(stmt, ser, tal_count(ser));
} }
void db_bind_txid(struct db_stmt *stmt, int pos, const struct bitcoin_txid *t) void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t)
{ {
db_bind_sha256d(stmt, pos, &t->shad); db_bind_sha256d(stmt, &t->shad);
} }
void db_bind_channel_id(struct db_stmt *stmt, int pos, const struct channel_id *id) void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id)
{ {
db_bind_blob(stmt, pos, id->id, sizeof(id->id)); db_bind_blob(stmt, id->id, sizeof(id->id));
} }
void db_bind_channel_type(struct db_stmt *stmt, int pos, const struct channel_type *type) void db_bind_channel_type(struct db_stmt *stmt, const struct channel_type *type)
{ {
db_bind_talarr(stmt, pos, type->features); db_bind_talarr(stmt, type->features);
} }
void db_bind_node_id(struct db_stmt *stmt, int pos, const struct node_id *id) void db_bind_node_id(struct db_stmt *stmt, const struct node_id *id)
{ {
db_bind_blob(stmt, pos, id->k, sizeof(id->k)); db_bind_blob(stmt, id->k, sizeof(id->k));
} }
void db_bind_node_id_arr(struct db_stmt *stmt, int col, void db_bind_node_id_arr(struct db_stmt *stmt,
const struct node_id *ids) const struct node_id *ids)
{ {
/* Copy into contiguous array: ARM will add padding to struct node_id! */ /* Copy into contiguous array: ARM will add padding to struct node_id! */
@@ -168,23 +162,23 @@ void db_bind_node_id_arr(struct db_stmt *stmt, int col,
ids[i].k, ids[i].k,
sizeof(ids[i].k)); sizeof(ids[i].k));
} }
db_bind_blob(stmt, col, arr, tal_count(arr)); db_bind_blob(stmt, arr, tal_count(arr));
} }
void db_bind_pubkey(struct db_stmt *stmt, int pos, const struct pubkey *pk) void db_bind_pubkey(struct db_stmt *stmt, const struct pubkey *pk)
{ {
u8 *der = tal_arr(stmt, u8, PUBKEY_CMPR_LEN); u8 *der = tal_arr(stmt, u8, PUBKEY_CMPR_LEN);
pubkey_to_der(der, pk); pubkey_to_der(der, pk);
db_bind_blob(stmt, pos, der, PUBKEY_CMPR_LEN); db_bind_blob(stmt, der, PUBKEY_CMPR_LEN);
} }
void db_bind_short_channel_id(struct db_stmt *stmt, int col, void db_bind_short_channel_id(struct db_stmt *stmt,
const struct short_channel_id *id) const struct short_channel_id *id)
{ {
db_bind_u64(stmt, col, id->u64); db_bind_u64(stmt, id->u64);
} }
void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col, void db_bind_short_channel_id_arr(struct db_stmt *stmt,
const struct short_channel_id *id) const struct short_channel_id *id)
{ {
u8 *ser = tal_arr(stmt, u8, 0); u8 *ser = tal_arr(stmt, u8, 0);
@@ -193,69 +187,69 @@ void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col,
for (size_t i = 0; i < num; ++i) for (size_t i = 0; i < num; ++i)
towire_short_channel_id(&ser, &id[i]); towire_short_channel_id(&ser, &id[i]);
db_bind_talarr(stmt, col, ser); db_bind_talarr(stmt, ser);
} }
void db_bind_signature(struct db_stmt *stmt, int col, void db_bind_signature(struct db_stmt *stmt,
const secp256k1_ecdsa_signature *sig) const secp256k1_ecdsa_signature *sig)
{ {
u8 *buf = tal_arr(stmt, u8, 64); u8 *buf = tal_arr(stmt, u8, 64);
int ret = secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, int ret = secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
buf, sig); buf, sig);
assert(ret == 1); assert(ret == 1);
db_bind_blob(stmt, col, buf, 64); db_bind_blob(stmt, buf, 64);
} }
void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t) void db_bind_timeabs(struct db_stmt *stmt, struct timeabs t)
{ {
u64 timestamp = t.ts.tv_nsec + (((u64) t.ts.tv_sec) * ((u64) NSEC_IN_SEC)); u64 timestamp = t.ts.tv_nsec + (((u64) t.ts.tv_sec) * ((u64) NSEC_IN_SEC));
db_bind_u64(stmt, col, timestamp); db_bind_u64(stmt, timestamp);
} }
void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx) void db_bind_tx(struct db_stmt *stmt, const struct wally_tx *tx)
{ {
u8 *ser = linearize_wtx(stmt, tx); u8 *ser = linearize_wtx(stmt, tx);
assert(ser); assert(ser);
db_bind_talarr(stmt, col, ser); db_bind_talarr(stmt, ser);
} }
void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt) void db_bind_psbt(struct db_stmt *stmt, const struct wally_psbt *psbt)
{ {
size_t bytes_written; size_t bytes_written;
const u8 *ser = psbt_get_bytes(stmt, psbt, &bytes_written); const u8 *ser = psbt_get_bytes(stmt, psbt, &bytes_written);
assert(ser); assert(ser);
db_bind_blob(stmt, col, ser, bytes_written); db_bind_blob(stmt, ser, bytes_written);
} }
void db_bind_amount_msat(struct db_stmt *stmt, int pos, void db_bind_amount_msat(struct db_stmt *stmt,
const struct amount_msat *msat) const struct amount_msat *msat)
{ {
db_bind_u64(stmt, pos, msat->millisatoshis); /* Raw: low level function */ db_bind_u64(stmt, msat->millisatoshis); /* Raw: low level function */
} }
void db_bind_amount_sat(struct db_stmt *stmt, int pos, void db_bind_amount_sat(struct db_stmt *stmt,
const struct amount_sat *sat) const struct amount_sat *sat)
{ {
db_bind_u64(stmt, pos, sat->satoshis); /* Raw: low level function */ db_bind_u64(stmt, sat->satoshis); /* Raw: low level function */
} }
void db_bind_json_escape(struct db_stmt *stmt, int pos, void db_bind_json_escape(struct db_stmt *stmt,
const struct json_escape *esc) const struct json_escape *esc)
{ {
db_bind_text(stmt, pos, esc->s); db_bind_text(stmt, esc->s);
} }
void db_bind_onionreply(struct db_stmt *stmt, int pos, const struct onionreply *r) void db_bind_onionreply(struct db_stmt *stmt, const struct onionreply *r)
{ {
db_bind_talarr(stmt, pos, r->contents); db_bind_talarr(stmt, r->contents);
} }
void db_bind_talarr(struct db_stmt *stmt, int col, const u8 *arr) void db_bind_talarr(struct db_stmt *stmt, const u8 *arr)
{ {
if (!arr) if (!arr)
db_bind_null(stmt, col); db_bind_null(stmt);
else else
db_bind_blob(stmt, col, arr, tal_bytelen(arr)); db_bind_blob(stmt, arr, tal_bytelen(arr));
} }
static size_t db_column_bytes(struct db_stmt *stmt, int col) static size_t db_column_bytes(struct db_stmt *stmt, int col)

View File

@@ -17,55 +17,50 @@ struct onionreply;
struct wally_psbt; struct wally_psbt;
struct wally_tx; struct wally_tx;
/* Magic pos argument meaning "the next field" */ /* These bind the next `?` in stmt (they keep an internal counter). */
#define BIND_NEXT -77 void db_bind_null(struct db_stmt *stmt);
void db_bind_int(struct db_stmt *stmt, int val);
int db_col_is_null(struct db_stmt *stmt, const char *colname); void db_bind_u64(struct db_stmt *stmt, u64 val);
void db_bind_blob(struct db_stmt *stmt, const u8 *val, size_t len);
void db_bind_int(struct db_stmt *stmt, int pos, int val); void db_bind_text(struct db_stmt *stmt, const char *val);
int db_col_int(struct db_stmt *stmt, const char *colname); void db_bind_preimage(struct db_stmt *stmt, const struct preimage *p);
void db_bind_sha256(struct db_stmt *stmt, const struct sha256 *s);
void db_bind_null(struct db_stmt *stmt, int pos); void db_bind_sha256d(struct db_stmt *stmt, const struct sha256_double *s);
void db_bind_int(struct db_stmt *stmt, int pos, int val); void db_bind_secret(struct db_stmt *stmt, const struct secret *s);
void db_bind_u64(struct db_stmt *stmt, int pos, u64 val); void db_bind_secret_arr(struct db_stmt *stmt, const struct secret *s);
void db_bind_blob(struct db_stmt *stmt, int pos, const u8 *val, size_t len); void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t);
void db_bind_text(struct db_stmt *stmt, int pos, const char *val); void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id);
void db_bind_preimage(struct db_stmt *stmt, int pos, const struct preimage *p); void db_bind_channel_type(struct db_stmt *stmt, const struct channel_type *type);
void db_bind_sha256(struct db_stmt *stmt, int pos, const struct sha256 *s); void db_bind_node_id(struct db_stmt *stmt, const struct node_id *ni);
void db_bind_sha256d(struct db_stmt *stmt, int pos, const struct sha256_double *s); void db_bind_node_id_arr(struct db_stmt *stmt,
void db_bind_secret(struct db_stmt *stmt, int pos, const struct secret *s);
void db_bind_secret_arr(struct db_stmt *stmt, int col, const struct secret *s);
void db_bind_txid(struct db_stmt *stmt, int pos, const struct bitcoin_txid *t);
void db_bind_channel_id(struct db_stmt *stmt, int pos, const struct channel_id *id);
void db_bind_channel_type(struct db_stmt *stmt, int pos, const struct channel_type *type);
void db_bind_node_id(struct db_stmt *stmt, int pos, const struct node_id *ni);
void db_bind_node_id_arr(struct db_stmt *stmt, int col,
const struct node_id *ids); const struct node_id *ids);
void db_bind_pubkey(struct db_stmt *stmt, int pos, const struct pubkey *p); void db_bind_pubkey(struct db_stmt *stmt, const struct pubkey *p);
void db_bind_short_channel_id(struct db_stmt *stmt, int col, void db_bind_short_channel_id(struct db_stmt *stmt,
const struct short_channel_id *id); const struct short_channel_id *id);
void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col, void db_bind_short_channel_id_arr(struct db_stmt *stmt,
const struct short_channel_id *id); const struct short_channel_id *id);
void db_bind_signature(struct db_stmt *stmt, int col, void db_bind_signature(struct db_stmt *stmt,
const secp256k1_ecdsa_signature *sig); const secp256k1_ecdsa_signature *sig);
void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t); void db_bind_timeabs(struct db_stmt *stmt, struct timeabs t);
void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx); void db_bind_tx(struct db_stmt *stmt, const struct wally_tx *tx);
void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt); void db_bind_psbt(struct db_stmt *stmt, const struct wally_psbt *psbt);
void db_bind_amount_msat(struct db_stmt *stmt, int pos, void db_bind_amount_msat(struct db_stmt *stmt,
const struct amount_msat *msat); const struct amount_msat *msat);
void db_bind_amount_sat(struct db_stmt *stmt, int pos, void db_bind_amount_sat(struct db_stmt *stmt,
const struct amount_sat *sat); const struct amount_sat *sat);
void db_bind_json_escape(struct db_stmt *stmt, int pos, void db_bind_json_escape(struct db_stmt *stmt,
const struct json_escape *esc); const struct json_escape *esc);
void db_bind_onionreply(struct db_stmt *stmt, int col, void db_bind_onionreply(struct db_stmt *stmt,
const struct onionreply *r); const struct onionreply *r);
void db_bind_talarr(struct db_stmt *stmt, int col, const u8 *arr); void db_bind_talarr(struct db_stmt *stmt, const u8 *arr);
/* Modern variants: get columns by name from SELECT */ /* Modern variants: get columns by name from SELECT */
/* Bridge function to get column number from SELECT /* Bridge function to get column number from SELECT
(must exist) */ (must exist) */
size_t db_query_colnum(const struct db_stmt *stmt, const char *colname); size_t db_query_colnum(const struct db_stmt *stmt, const char *colname);
int db_col_is_null(struct db_stmt *stmt, const char *colname);
int db_col_int(struct db_stmt *stmt, const char *colname);
u64 db_col_u64(struct db_stmt *stmt, const char *colname); u64 db_col_u64(struct db_stmt *stmt, const char *colname);
size_t db_col_bytes(struct db_stmt *stmt, const char *colname); size_t db_col_bytes(struct db_stmt *stmt, const char *colname);
const void* db_col_blob(struct db_stmt *stmt, const char *colname); const void* db_col_blob(struct db_stmt *stmt, const char *colname);

View File

@@ -62,16 +62,16 @@ void db_set_intvar(struct db *db, const char *varname, s64 val)
{ {
size_t changes; size_t changes;
struct db_stmt *stmt = db_prepare_v2(db, SQL("UPDATE vars SET intval=? WHERE name=?;")); struct db_stmt *stmt = db_prepare_v2(db, SQL("UPDATE vars SET intval=? WHERE name=?;"));
db_bind_int(stmt, BIND_NEXT, val); db_bind_int(stmt, val);
db_bind_text(stmt, BIND_NEXT, varname); db_bind_text(stmt, varname);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
changes = db_count_changes(stmt); changes = db_count_changes(stmt);
tal_free(stmt); tal_free(stmt);
if (changes == 0) { if (changes == 0) {
stmt = db_prepare_v2(db, SQL("INSERT INTO vars (name, intval) VALUES (?, ?);")); stmt = db_prepare_v2(db, SQL("INSERT INTO vars (name, intval) VALUES (?, ?);"));
db_bind_text(stmt, BIND_NEXT, varname); db_bind_text(stmt, varname);
db_bind_int(stmt, BIND_NEXT, val); db_bind_int(stmt, val);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
tal_free(stmt); tal_free(stmt);
} }
@@ -82,7 +82,7 @@ s64 db_get_intvar(struct db *db, const char *varname, s64 defval)
s64 res = defval; s64 res = defval;
struct db_stmt *stmt = db_prepare_v2( struct db_stmt *stmt = db_prepare_v2(
db, SQL("SELECT intval FROM vars WHERE name= ? LIMIT 1")); db, SQL("SELECT intval FROM vars WHERE name= ? LIMIT 1"));
db_bind_text(stmt, BIND_NEXT, varname); db_bind_text(stmt, varname);
if (db_query_prepared_canfail(stmt) && db_step(stmt)) if (db_query_prepared_canfail(stmt) && db_step(stmt))
res = db_col_int(stmt, "intval"); res = db_col_int(stmt, "intval");
@@ -110,7 +110,7 @@ static void db_data_version_incr(struct db *db)
"SET intval = intval + 1 " "SET intval = intval + 1 "
"WHERE name = 'data_version'" "WHERE name = 'data_version'"
" AND intval = ?")); " AND intval = ?"));
db_bind_int(stmt, BIND_NEXT, db->data_version); db_bind_int(stmt, db->data_version);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
if (db_count_changes(stmt) != 1) if (db_count_changes(stmt) != 1)
db_fatal(stmt->db, "Optimistic lock on the database failed. There" db_fatal(stmt->db, "Optimistic lock on the database failed. There"

View File

@@ -136,7 +136,7 @@ static bool db_migrate(struct plugin *p, struct db *db, bool *created)
/* Finally, update the version number in the version table */ /* Finally, update the version number in the version table */
stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;")); stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;"));
db_bind_int(stmt, BIND_NEXT, available); db_bind_int(stmt, available);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
return current != orig; return current != orig;
@@ -179,7 +179,7 @@ static void migration_remove_dupe_lease_fees(struct plugin *p, struct db *db)
/* same acct as last, we found a duplicate */ /* same acct as last, we found a duplicate */
del_stmt = db_prepare_v2(db, SQL("DELETE FROM channel_events" del_stmt = db_prepare_v2(db, SQL("DELETE FROM channel_events"
" WHERE id=?")); " WHERE id=?"));
db_bind_u64(del_stmt, BIND_NEXT, id); db_bind_u64(del_stmt, id);
db_exec_prepared_v2(take(del_stmt)); db_exec_prepared_v2(take(del_stmt));
} }
tal_free(stmt); tal_free(stmt);

View File

@@ -192,7 +192,7 @@ static struct income_event *maybe_chain_income(const tal_t *ctx,
" WHERE " " WHERE "
" e.spending_txid = ?")); " e.spending_txid = ?"));
db_bind_txid(stmt, BIND_NEXT, &ev->outpoint.txid); db_bind_txid(stmt, &ev->outpoint.txid);
db_query_prepared(stmt); db_query_prepared(stmt);
if (!db_step(stmt)) { if (!db_step(stmt)) {
tal_free(stmt); tal_free(stmt);

View File

@@ -170,8 +170,8 @@ struct chain_event **list_chain_events_timebox(const tal_t *ctx,
" AND e.timestamp <= ?" " AND e.timestamp <= ?"
" ORDER BY e.timestamp, e.id;")); " ORDER BY e.timestamp, e.id;"));
db_bind_u64(stmt, BIND_NEXT, start_time); db_bind_u64(stmt, start_time);
db_bind_u64(stmt, BIND_NEXT, end_time); db_bind_u64(stmt, end_time);
return find_chain_events(ctx, take(stmt)); return find_chain_events(ctx, take(stmt));
} }
@@ -211,7 +211,7 @@ struct chain_event **account_get_chain_events(const tal_t *ctx,
" WHERE e.account_id = ?" " WHERE e.account_id = ?"
" ORDER BY e.timestamp, e.id")); " ORDER BY e.timestamp, e.id"));
db_bind_int(stmt, BIND_NEXT, acct->db_id); db_bind_int(stmt, acct->db_id);
return find_chain_events(ctx, take(stmt)); return find_chain_events(ctx, take(stmt));
} }
@@ -250,7 +250,7 @@ static struct chain_event **find_txos_for_tx(const tal_t *ctx,
", e.spending_txid NULLS FIRST" ", e.spending_txid NULLS FIRST"
", e.blockheight")); ", e.blockheight"));
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
return find_chain_events(ctx, take(stmt)); return find_chain_events(ctx, take(stmt));
} }
@@ -317,8 +317,8 @@ u64 onchain_fee_last_timestamp(struct db *db,
" ORDER BY timestamp DESC")); " ORDER BY timestamp DESC"));
db_bind_u64(stmt, BIND_NEXT, acct_db_id); db_bind_u64(stmt, acct_db_id);
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) if (db_step(stmt))
@@ -345,7 +345,7 @@ struct fee_sum **find_account_onchain_fees(const tal_t *ctx,
" GROUP BY txid, update_count" " GROUP BY txid, update_count"
" ORDER BY txid, update_count")); " ORDER BY txid, update_count"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_query_prepared(stmt); db_query_prepared(stmt);
sums = tal_arr(ctx, struct fee_sum *, 0); sums = tal_arr(ctx, struct fee_sum *, 0);
@@ -551,8 +551,8 @@ struct account *find_close_account(const tal_t *ctx,
" e.tag = ?" " e.tag = ?"
" AND e.spending_txid = ?")); " AND e.spending_txid = ?"));
db_bind_text(stmt, BIND_NEXT, mvt_tag_str(CHANNEL_CLOSE)); db_bind_text(stmt, mvt_tag_str(CHANNEL_CLOSE));
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) { if (db_step(stmt)) {
@@ -607,7 +607,7 @@ void maybe_mark_account_onchain(struct db *db, struct account *acct)
" ORDER BY blockheight DESC" " ORDER BY blockheight DESC"
" LIMIT 1")); " LIMIT 1"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_query_prepared(stmt); db_query_prepared(stmt);
ok = db_step(stmt); ok = db_step(stmt);
assert(ok); assert(ok);
@@ -620,8 +620,8 @@ void maybe_mark_account_onchain(struct db *db, struct account *acct)
" onchain_resolved_block = ?" " onchain_resolved_block = ?"
" WHERE" " WHERE"
" id = ?")); " id = ?"));
db_bind_int(stmt, BIND_NEXT, acct->onchain_resolved_block); db_bind_int(stmt, acct->onchain_resolved_block);
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -639,8 +639,8 @@ void add_payment_hash_desc(struct db *db,
" ev_desc = ?" " ev_desc = ?"
" WHERE" " WHERE"
" payment_id = ?")); " payment_id = ?"));
db_bind_text(stmt, BIND_NEXT, desc); db_bind_text(stmt, desc);
db_bind_sha256(stmt, BIND_NEXT, payment_hash); db_bind_sha256(stmt, payment_hash);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
/* Ok, now we update the account with this blockheight */ /* Ok, now we update the account with this blockheight */
@@ -648,8 +648,8 @@ void add_payment_hash_desc(struct db *db,
" ev_desc = ?" " ev_desc = ?"
" WHERE" " WHERE"
" payment_id = ?")); " payment_id = ?"));
db_bind_text(stmt, BIND_NEXT, desc); db_bind_text(stmt, desc);
db_bind_sha256(stmt, BIND_NEXT, payment_hash); db_bind_sha256(stmt, payment_hash);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -685,7 +685,7 @@ struct chain_event *find_chain_event_by_id(const tal_t *ctx,
" WHERE " " WHERE "
" e.id = ?")); " e.id = ?"));
db_bind_u64(stmt, BIND_NEXT, event_db_id); db_bind_u64(stmt, event_db_id);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) if (db_step(stmt))
e = stmt2chain_event(ctx, stmt); e = stmt2chain_event(ctx, stmt);
@@ -735,7 +735,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
" AND e.account_id = ?" " AND e.account_id = ?"
" AND e.utxo_txid = ?" " AND e.utxo_txid = ?"
" AND e.outnum = ?")); " AND e.outnum = ?"));
db_bind_txid(stmt, BIND_NEXT, spending_txid); db_bind_txid(stmt, spending_txid);
} else { } else {
stmt = db_prepare_v2(db, SQL("SELECT" stmt = db_prepare_v2(db, SQL("SELECT"
" e.id" " e.id"
@@ -765,12 +765,12 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
" AND e.utxo_txid = ?" " AND e.utxo_txid = ?"
" AND e.outnum = ?" " AND e.outnum = ?"
" AND e.spending_txid IS NULL")); " AND e.spending_txid IS NULL"));
db_bind_text(stmt, BIND_NEXT, tag); db_bind_text(stmt, tag);
} }
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_bind_txid(stmt, BIND_NEXT, &outpoint->txid); db_bind_txid(stmt, &outpoint->txid);
db_bind_int(stmt, BIND_NEXT, outpoint->n); db_bind_int(stmt, outpoint->n);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) if (db_step(stmt))
@@ -803,10 +803,10 @@ char *account_get_balance(const tal_t *ctx,
" AND ce.ignored != ?" " AND ce.ignored != ?"
" GROUP BY ce.currency")); " GROUP BY ce.currency"));
db_bind_text(stmt, BIND_NEXT, acct_name); db_bind_text(stmt, acct_name);
/* We populate ignored with a 0 or 1, /* We populate ignored with a 0 or 1,
* if we want both 0+1, we just ignore everything with a 2 */ * if we want both 0+1, we just ignore everything with a 2 */
db_bind_int(stmt, BIND_NEXT, skip_ignored ? 1 : 2); db_bind_int(stmt, skip_ignored ? 1 : 2);
db_query_prepared(stmt); db_query_prepared(stmt);
*balances = tal_arr(ctx, struct acct_balance *, 0); *balances = tal_arr(ctx, struct acct_balance *, 0);
if (account_exists) if (account_exists)
@@ -836,7 +836,7 @@ char *account_get_balance(const tal_t *ctx,
" ON a.id = ce.account_id" " ON a.id = ce.account_id"
" WHERE a.name = ?" " WHERE a.name = ?"
" GROUP BY ce.currency")); " GROUP BY ce.currency"));
db_bind_text(stmt, BIND_NEXT, acct_name); db_bind_text(stmt, acct_name);
db_query_prepared(stmt); db_query_prepared(stmt);
while (db_step(stmt)) { while (db_step(stmt)) {
@@ -924,8 +924,8 @@ struct channel_event **list_channel_events_timebox(const tal_t *ctx,
" AND e.timestamp <= ?" " AND e.timestamp <= ?"
" ORDER BY e.timestamp, e.id;")); " ORDER BY e.timestamp, e.id;"));
db_bind_u64(stmt, BIND_NEXT, start_time); db_bind_u64(stmt, start_time);
db_bind_u64(stmt, BIND_NEXT, end_time); db_bind_u64(stmt, end_time);
db_query_prepared(stmt); db_query_prepared(stmt);
results = tal_arr(ctx, struct channel_event *, 0); results = tal_arr(ctx, struct channel_event *, 0);
@@ -971,7 +971,7 @@ struct channel_event **account_get_channel_events(const tal_t *ctx,
" WHERE e.account_id = ?" " WHERE e.account_id = ?"
" ORDER BY e.timestamp, e.id")); " ORDER BY e.timestamp, e.id"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_query_prepared(stmt); db_query_prepared(stmt);
results = tal_arr(ctx, struct channel_event *, 0); results = tal_arr(ctx, struct channel_event *, 0);
@@ -1025,7 +1025,7 @@ struct onchain_fee **account_get_chain_fees(const tal_t *ctx, struct db *db,
", of.txid" ", of.txid"
", of.update_count")); ", of.update_count"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_query_prepared(stmt); db_query_prepared(stmt);
results = tal_arr(ctx, struct onchain_fee *, 0); results = tal_arr(ctx, struct onchain_fee *, 0);
@@ -1064,8 +1064,8 @@ struct onchain_fee **list_chain_fees_timebox(const tal_t *ctx, struct db *db,
", of.txid" ", of.txid"
", of.update_count")); ", of.update_count"));
db_bind_u64(stmt, BIND_NEXT, start_time); db_bind_u64(stmt, start_time);
db_bind_u64(stmt, BIND_NEXT, end_time); db_bind_u64(stmt, end_time);
db_query_prepared(stmt); db_query_prepared(stmt);
results = tal_arr(ctx, struct onchain_fee *, 0); results = tal_arr(ctx, struct onchain_fee *, 0);
@@ -1142,7 +1142,7 @@ struct account *find_account(const tal_t *ctx,
" FROM accounts" " FROM accounts"
" WHERE name = ?")); " WHERE name = ?"));
db_bind_text(stmt, BIND_NEXT, name); db_bind_text(stmt, name);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) if (db_step(stmt))
@@ -1176,7 +1176,7 @@ struct onchain_fee **account_onchain_fees(const tal_t *ctx,
" ON a.id = of.account_id" " ON a.id = of.account_id"
" WHERE of.account_id = ?;")); " WHERE of.account_id = ?;"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_query_prepared(stmt); db_query_prepared(stmt);
results = tal_arr(ctx, struct onchain_fee *, 0); results = tal_arr(ctx, struct onchain_fee *, 0);
@@ -1233,14 +1233,14 @@ void account_add(struct db *db, struct account *acct)
" VALUES" " VALUES"
" (?, ?, ?, ?, ?);")); " (?, ?, ?, ?, ?);"));
db_bind_text(stmt, BIND_NEXT, acct->name); db_bind_text(stmt, acct->name);
if (acct->peer_id) if (acct->peer_id)
db_bind_node_id(stmt, BIND_NEXT, acct->peer_id); db_bind_node_id(stmt, acct->peer_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_int(stmt, BIND_NEXT, acct->is_wallet ? 1 : 0); db_bind_int(stmt, acct->is_wallet ? 1 : 0);
db_bind_int(stmt, BIND_NEXT, acct->we_opened ? 1 : 0); db_bind_int(stmt, acct->we_opened ? 1 : 0);
db_bind_int(stmt, BIND_NEXT, acct->leased ? 1 : 0); db_bind_int(stmt, acct->leased ? 1 : 0);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
acct->db_id = db_last_insert_id_v2(stmt); acct->db_id = db_last_insert_id_v2(stmt);
@@ -1328,24 +1328,24 @@ void maybe_update_account(struct db *db,
" name = ?")); " name = ?"));
if (acct->open_event_db_id) if (acct->open_event_db_id)
db_bind_u64(stmt, BIND_NEXT, *acct->open_event_db_id); db_bind_u64(stmt, *acct->open_event_db_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
if (acct->closed_event_db_id) if (acct->closed_event_db_id)
db_bind_u64(stmt, BIND_NEXT, *acct->closed_event_db_id); db_bind_u64(stmt, *acct->closed_event_db_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_int(stmt, BIND_NEXT, acct->we_opened ? 1 : 0); db_bind_int(stmt, acct->we_opened ? 1 : 0);
db_bind_int(stmt, BIND_NEXT, acct->leased ? 1 : 0); db_bind_int(stmt, acct->leased ? 1 : 0);
db_bind_int(stmt, BIND_NEXT, acct->closed_count); db_bind_int(stmt, acct->closed_count);
if (acct->peer_id) if (acct->peer_id)
db_bind_node_id(stmt, BIND_NEXT, acct->peer_id); db_bind_node_id(stmt, acct->peer_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_text(stmt, BIND_NEXT, acct->name); db_bind_text(stmt, acct->name);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -1373,27 +1373,27 @@ void log_channel_event(struct db *db,
" VALUES" " VALUES"
" (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
db_bind_text(stmt, BIND_NEXT, e->tag); db_bind_text(stmt, e->tag);
db_bind_amount_msat(stmt, BIND_NEXT, &e->credit); db_bind_amount_msat(stmt, &e->credit);
db_bind_amount_msat(stmt, BIND_NEXT, &e->debit); db_bind_amount_msat(stmt, &e->debit);
db_bind_amount_msat(stmt, BIND_NEXT, &e->fees); db_bind_amount_msat(stmt, &e->fees);
db_bind_text(stmt, BIND_NEXT, e->currency); db_bind_text(stmt, e->currency);
if (e->payment_id) if (e->payment_id)
db_bind_sha256(stmt, BIND_NEXT, e->payment_id); db_bind_sha256(stmt, e->payment_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_int(stmt, BIND_NEXT, e->part_id); db_bind_int(stmt, e->part_id);
db_bind_u64(stmt, BIND_NEXT, e->timestamp); db_bind_u64(stmt, e->timestamp);
if (e->desc) if (e->desc)
db_bind_text(stmt, BIND_NEXT, e->desc); db_bind_text(stmt, e->desc);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
if (e->rebalance_id) if (e->rebalance_id)
db_bind_u64(stmt, BIND_NEXT, *e->rebalance_id); db_bind_u64(stmt, *e->rebalance_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
e->db_id = db_last_insert_id_v2(stmt); e->db_id = db_last_insert_id_v2(stmt);
@@ -1433,8 +1433,8 @@ static struct chain_event **find_chain_events_bytxid(const tal_t *ctx, struct db
" OR (e.utxo_txid = ? AND e.spending_txid IS NULL)" " OR (e.utxo_txid = ? AND e.spending_txid IS NULL)"
" ORDER BY e.account_id")); " ORDER BY e.account_id"));
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
return find_chain_events(ctx, take(stmt)); return find_chain_events(ctx, take(stmt));
} }
@@ -1448,7 +1448,7 @@ static u64 find_acct_id(struct db *db, const char *name)
" FROM accounts" " FROM accounts"
" WHERE name = ?")); " WHERE name = ?"));
db_bind_text(stmt, BIND_NEXT, name); db_bind_text(stmt, name);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) if (db_step(stmt))
acct_id = db_col_u64(stmt, "id"); acct_id = db_col_u64(stmt, "id");
@@ -1481,8 +1481,8 @@ static void insert_chain_fees_diff(struct db *db,
" AND account_id = ?" " AND account_id = ?"
" ORDER BY update_count")); " ORDER BY update_count"));
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
db_bind_u64(stmt, BIND_NEXT, acct_id); db_bind_u64(stmt, acct_id);
db_query_prepared(stmt); db_query_prepared(stmt);
/* If there's no current record, add it */ /* If there's no current record, add it */
@@ -1527,13 +1527,13 @@ static void insert_chain_fees_diff(struct db *db,
") VALUES" ") VALUES"
" (?, ?, ?, ?, ?, ?, ?);")); " (?, ?, ?, ?, ?, ?, ?);"));
db_bind_u64(stmt, BIND_NEXT, acct_id); db_bind_u64(stmt, acct_id);
db_bind_txid(stmt, BIND_NEXT, txid); db_bind_txid(stmt, txid);
db_bind_amount_msat(stmt, BIND_NEXT, &credit); db_bind_amount_msat(stmt, &credit);
db_bind_amount_msat(stmt, BIND_NEXT, &debit); db_bind_amount_msat(stmt, &debit);
db_bind_text(stmt, BIND_NEXT, currency); db_bind_text(stmt, currency);
db_bind_u64(stmt, BIND_NEXT, timestamp); db_bind_u64(stmt, timestamp);
db_bind_int(stmt, BIND_NEXT, ++update_count); db_bind_int(stmt, ++update_count);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -1702,8 +1702,8 @@ void maybe_record_rebalance(struct db *db,
" AND e.credit = ?" " AND e.credit = ?"
" AND e.rebalance_id IS NULL")); " AND e.rebalance_id IS NULL"));
db_bind_sha256(stmt, BIND_NEXT, out->payment_id); db_bind_sha256(stmt, out->payment_id);
db_bind_amount_msat(stmt, BIND_NEXT, &credit); db_bind_amount_msat(stmt, &credit);
db_query_prepared(stmt); db_query_prepared(stmt);
if (!db_step(stmt)) { if (!db_step(stmt)) {
@@ -1722,16 +1722,16 @@ void maybe_record_rebalance(struct db *db,
" rebalance_id = ?" " rebalance_id = ?"
" WHERE" " WHERE"
" id = ?")); " id = ?"));
db_bind_u64(stmt, BIND_NEXT, *out->rebalance_id); db_bind_u64(stmt, *out->rebalance_id);
db_bind_u64(stmt, BIND_NEXT, out->db_id); db_bind_u64(stmt, out->db_id);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
stmt = db_prepare_v2(db, SQL("UPDATE channel_events SET" stmt = db_prepare_v2(db, SQL("UPDATE channel_events SET"
" rebalance_id = ?" " rebalance_id = ?"
" WHERE" " WHERE"
" id = ?")); " id = ?"));
db_bind_u64(stmt, BIND_NEXT, out->db_id); db_bind_u64(stmt, out->db_id);
db_bind_u64(stmt, BIND_NEXT, *out->rebalance_id); db_bind_u64(stmt, *out->rebalance_id);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -1953,9 +1953,9 @@ void maybe_closeout_external_deposits(struct db *db,
" AND a.name = ?")); " AND a.name = ?"));
/* Blockheight for unconfirmeds is zero */ /* Blockheight for unconfirmeds is zero */
db_bind_int(stmt, BIND_NEXT, 0); db_bind_int(stmt, 0);
db_bind_txid(stmt, BIND_NEXT, ev->spending_txid); db_bind_txid(stmt, ev->spending_txid);
db_bind_text(stmt, BIND_NEXT, EXTERNAL_ACCT); db_bind_text(stmt, EXTERNAL_ACCT);
db_query_prepared(stmt); db_query_prepared(stmt);
while (db_step(stmt)) { while (db_step(stmt)) {
@@ -1967,8 +1967,8 @@ void maybe_closeout_external_deposits(struct db *db,
" blockheight = ?" " blockheight = ?"
" WHERE id = ?")); " WHERE id = ?"));
db_bind_int(update_stmt, BIND_NEXT, ev->blockheight); db_bind_int(update_stmt, ev->blockheight);
db_bind_u64(update_stmt, BIND_NEXT, id); db_bind_u64(update_stmt, id);
db_exec_prepared_v2(take(update_stmt)); db_exec_prepared_v2(take(update_stmt));
} }
@@ -2009,37 +2009,37 @@ bool log_chain_event(struct db *db,
" VALUES " " VALUES "
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"));
db_bind_u64(stmt, BIND_NEXT, acct->db_id); db_bind_u64(stmt, acct->db_id);
if (e->origin_acct) if (e->origin_acct)
db_bind_text(stmt, BIND_NEXT, e->origin_acct); db_bind_text(stmt, e->origin_acct);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_text(stmt, BIND_NEXT, e->tag); db_bind_text(stmt, e->tag);
db_bind_amount_msat(stmt, BIND_NEXT, &e->credit); db_bind_amount_msat(stmt, &e->credit);
db_bind_amount_msat(stmt, BIND_NEXT, &e->debit); db_bind_amount_msat(stmt, &e->debit);
db_bind_amount_msat(stmt, BIND_NEXT, &e->output_value); db_bind_amount_msat(stmt, &e->output_value);
db_bind_text(stmt, BIND_NEXT, e->currency); db_bind_text(stmt, e->currency);
db_bind_u64(stmt, BIND_NEXT, e->timestamp); db_bind_u64(stmt, e->timestamp);
db_bind_int(stmt, BIND_NEXT, e->blockheight); db_bind_int(stmt, e->blockheight);
db_bind_txid(stmt, BIND_NEXT, &e->outpoint.txid); db_bind_txid(stmt, &e->outpoint.txid);
db_bind_int(stmt, BIND_NEXT, e->outpoint.n); db_bind_int(stmt, e->outpoint.n);
if (e->payment_id) if (e->payment_id)
db_bind_sha256(stmt, BIND_NEXT, e->payment_id); db_bind_sha256(stmt, e->payment_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
if (e->spending_txid) if (e->spending_txid)
db_bind_txid(stmt, BIND_NEXT, e->spending_txid); db_bind_txid(stmt, e->spending_txid);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_int(stmt, BIND_NEXT, e->ignored ? 1 : 0); db_bind_int(stmt, e->ignored ? 1 : 0);
db_bind_int(stmt, BIND_NEXT, e->stealable ? 1 : 0); db_bind_int(stmt, e->stealable ? 1 : 0);
if (e->desc) if (e->desc)
db_bind_text(stmt, BIND_NEXT, e->desc); db_bind_text(stmt, e->desc);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
e->db_id = db_last_insert_id_v2(stmt); e->db_id = db_last_insert_id_v2(stmt);
e->acct_db_id = acct->db_id; e->acct_db_id = acct->db_id;

View File

@@ -1004,7 +1004,7 @@ static bool db_migrate(struct lightningd *ld, struct db *db,
/* Finally update the version number in the version table */ /* Finally update the version number in the version table */
stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;")); stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;"));
db_bind_int(stmt, BIND_NEXT, available); db_bind_int(stmt, available);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
tal_free(stmt); tal_free(stmt);
@@ -1012,8 +1012,8 @@ static bool db_migrate(struct lightningd *ld, struct db *db,
if (current != orig) { if (current != orig) {
stmt = db_prepare_v2( stmt = db_prepare_v2(
db, SQL("INSERT INTO db_upgrades VALUES (?, ?);")); db, SQL("INSERT INTO db_upgrades VALUES (?, ?);"));
db_bind_int(stmt, BIND_NEXT, orig); db_bind_int(stmt, orig);
db_bind_text(stmt, BIND_NEXT, version()); db_bind_text(stmt, version());
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
tal_free(stmt); tal_free(stmt);
} }
@@ -1063,8 +1063,8 @@ static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db
struct db_stmt *stmt = db_prepare_v2( struct db_stmt *stmt = db_prepare_v2(
db, SQL("UPDATE channels SET feerate_base = ?, feerate_ppm = ?;")); db, SQL("UPDATE channels SET feerate_base = ?, feerate_ppm = ?;"));
db_bind_int(stmt, BIND_NEXT, ld->config.fee_base); db_bind_int(stmt, ld->config.fee_base);
db_bind_int(stmt, BIND_NEXT, ld->config.fee_per_satoshi); db_bind_int(stmt, ld->config.fee_per_satoshi);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
tal_free(stmt); tal_free(stmt);
@@ -1160,9 +1160,9 @@ void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db)
" SET scriptpubkey = ?" " SET scriptpubkey = ?"
" WHERE prev_out_tx = ? " " WHERE prev_out_tx = ? "
" AND prev_out_index = ?")); " AND prev_out_index = ?"));
db_bind_blob(update_stmt, BIND_NEXT, scriptPubkey, tal_bytelen(scriptPubkey)); db_bind_blob(update_stmt, scriptPubkey, tal_bytelen(scriptPubkey));
db_bind_txid(update_stmt, BIND_NEXT, &txid); db_bind_txid(update_stmt, &txid);
db_bind_int(update_stmt, BIND_NEXT, outnum); db_bind_int(update_stmt, outnum);
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
} }
@@ -1201,8 +1201,8 @@ static void fillin_missing_channel_id(struct lightningd *ld, struct db *db)
update_stmt = db_prepare_v2(db, SQL("UPDATE channels" update_stmt = db_prepare_v2(db, SQL("UPDATE channels"
" SET full_channel_id = ?" " SET full_channel_id = ?"
" WHERE id = ?;")); " WHERE id = ?;"));
db_bind_channel_id(update_stmt, BIND_NEXT, &cid); db_bind_channel_id(update_stmt, &cid);
db_bind_u64(update_stmt, BIND_NEXT, id); db_bind_u64(update_stmt, id);
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
@@ -1258,13 +1258,13 @@ static void fillin_missing_local_basepoints(struct lightningd *ld,
", delayed_payment_basepoint_local = ?" ", delayed_payment_basepoint_local = ?"
", funding_pubkey_local = ? " ", funding_pubkey_local = ? "
"WHERE id = ?;")); "WHERE id = ?;"));
db_bind_pubkey(upstmt, BIND_NEXT, &base.revocation); db_bind_pubkey(upstmt, &base.revocation);
db_bind_pubkey(upstmt, BIND_NEXT, &base.payment); db_bind_pubkey(upstmt, &base.payment);
db_bind_pubkey(upstmt, BIND_NEXT, &base.htlc); db_bind_pubkey(upstmt, &base.htlc);
db_bind_pubkey(upstmt, BIND_NEXT, &base.delayed_payment); db_bind_pubkey(upstmt, &base.delayed_payment);
db_bind_pubkey(upstmt, BIND_NEXT, &funding_pubkey); db_bind_pubkey(upstmt, &funding_pubkey);
db_bind_u64(upstmt, BIND_NEXT, dbid); db_bind_u64(upstmt, dbid);
db_exec_prepared_v2(take(upstmt)); db_exec_prepared_v2(take(upstmt));
} }
@@ -1380,9 +1380,9 @@ migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db)
" SET last_tx = ?" " SET last_tx = ?"
" WHERE channel_id = ?" " WHERE channel_id = ?"
" AND funding_tx_id = ?;")); " AND funding_tx_id = ?;"));
db_bind_psbt(update_stmt, BIND_NEXT, last_tx->psbt); db_bind_psbt(update_stmt, last_tx->psbt);
db_bind_int(update_stmt, BIND_NEXT, cdb_id); db_bind_int(update_stmt, cdb_id);
db_bind_txid(update_stmt, BIND_NEXT, &funding_txid); db_bind_txid(update_stmt, &funding_txid);
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
} }
@@ -1474,8 +1474,8 @@ void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db)
update_stmt = db_prepare_v2(db, SQL("UPDATE channels" update_stmt = db_prepare_v2(db, SQL("UPDATE channels"
" SET last_tx = ?" " SET last_tx = ?"
" WHERE id = ?;")); " WHERE id = ?;"));
db_bind_psbt(update_stmt, BIND_NEXT, last_tx->psbt); db_bind_psbt(update_stmt, last_tx->psbt);
db_bind_int(update_stmt, BIND_NEXT, cdb_id); db_bind_int(update_stmt, cdb_id);
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
} }
@@ -1511,8 +1511,8 @@ static void migrate_channels_scids_as_integers(struct lightningd *ld,
stmt = db_prepare_v2(db, SQL("UPDATE channels" stmt = db_prepare_v2(db, SQL("UPDATE channels"
" SET scid = ?" " SET scid = ?"
" WHERE short_channel_id = ?")); " WHERE short_channel_id = ?"));
db_bind_short_channel_id(stmt, BIND_NEXT, &scid); db_bind_short_channel_id(stmt, &scid);
db_bind_text(stmt, BIND_NEXT, scids[i]); db_bind_text(stmt, scids[i]);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
/* This was reported to happen with an (old, closed) channel: that we'd have /* This was reported to happen with an (old, closed) channel: that we'd have
@@ -1566,8 +1566,8 @@ static void migrate_payments_scids_as_integers(struct lightningd *ld,
update_stmt = db_prepare_v2(db, SQL("UPDATE payments SET" update_stmt = db_prepare_v2(db, SQL("UPDATE payments SET"
" failscid = ?" " failscid = ?"
" WHERE id = ?")); " WHERE id = ?"));
db_bind_short_channel_id(update_stmt, BIND_NEXT, &scid); db_bind_short_channel_id(update_stmt, &scid);
db_bind_u64(update_stmt, BIND_NEXT, db_col_u64(stmt, "id")); db_bind_u64(update_stmt, db_col_u64(stmt, "id"));
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
} }
@@ -1624,8 +1624,8 @@ static void migrate_fill_in_channel_type(struct lightningd *ld,
update_stmt = db_prepare_v2(db, SQL("UPDATE channels SET" update_stmt = db_prepare_v2(db, SQL("UPDATE channels SET"
" channel_type = ?" " channel_type = ?"
" WHERE id = ?")); " WHERE id = ?"));
db_bind_channel_type(update_stmt, BIND_NEXT, type); db_bind_channel_type(update_stmt, type);
db_bind_u64(update_stmt, BIND_NEXT, id); db_bind_u64(update_stmt, id);
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
} }
@@ -1697,8 +1697,8 @@ static void migrate_invalid_last_tx_psbts(struct lightningd *ld,
update_stmt = db_prepare_v2(db, SQL("UPDATE channels" update_stmt = db_prepare_v2(db, SQL("UPDATE channels"
" SET last_tx = ?" " SET last_tx = ?"
" WHERE id = ?;")); " WHERE id = ?;"));
db_bind_psbt(update_stmt, BIND_NEXT, psbt); db_bind_psbt(update_stmt, psbt);
db_bind_u64(update_stmt, BIND_NEXT, id); db_bind_u64(update_stmt, id);
db_exec_prepared_v2(update_stmt); db_exec_prepared_v2(update_stmt);
tal_free(update_stmt); tal_free(update_stmt);
} }

View File

@@ -123,9 +123,9 @@ static void update_db_expirations(struct invoices *invoices, u64 now)
" SET state = ?" " SET state = ?"
" WHERE state = ?" " WHERE state = ?"
" AND expiry_time <= ?;")); " AND expiry_time <= ?;"));
db_bind_int(stmt, BIND_NEXT, EXPIRED); db_bind_int(stmt, EXPIRED);
db_bind_int(stmt, BIND_NEXT, UNPAID); db_bind_int(stmt, UNPAID);
db_bind_u64(stmt, BIND_NEXT, now); db_bind_u64(stmt, now);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -170,8 +170,8 @@ static void trigger_expiration(struct invoices *invoices)
" FROM invoices" " FROM invoices"
" WHERE state = ?" " WHERE state = ?"
" AND expiry_time <= ?")); " AND expiry_time <= ?"));
db_bind_int(stmt, BIND_NEXT, UNPAID); db_bind_int(stmt, UNPAID);
db_bind_u64(stmt, BIND_NEXT, now); db_bind_u64(stmt, now);
db_query_prepared(stmt); db_query_prepared(stmt);
while (db_step(stmt)) { while (db_step(stmt)) {
@@ -207,7 +207,7 @@ static void install_expiration_timer(struct invoices *invoices)
stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT MIN(expiry_time)" stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT MIN(expiry_time)"
" FROM invoices" " FROM invoices"
" WHERE state = ?;")); " WHERE state = ?;"));
db_bind_int(stmt, BIND_NEXT, UNPAID); db_bind_int(stmt, UNPAID);
db_query_prepared(stmt); db_query_prepared(stmt);
@@ -286,25 +286,25 @@ bool invoices_create(struct invoices *invoices,
" , NULL, NULL" " , NULL, NULL"
" , NULL, ?, ?, ?, ?);")); " , NULL, ?, ?, ?, ?);"));
db_bind_sha256(stmt, BIND_NEXT, rhash); db_bind_sha256(stmt, rhash);
db_bind_preimage(stmt, BIND_NEXT, r); db_bind_preimage(stmt, r);
db_bind_int(stmt, BIND_NEXT, UNPAID); db_bind_int(stmt, UNPAID);
if (msat) if (msat)
db_bind_amount_msat(stmt, BIND_NEXT, msat); db_bind_amount_msat(stmt, msat);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_bind_json_escape(stmt, BIND_NEXT, label); db_bind_json_escape(stmt, label);
db_bind_u64(stmt, BIND_NEXT, expiry_time); db_bind_u64(stmt, expiry_time);
db_bind_text(stmt, BIND_NEXT, b11enc); db_bind_text(stmt, b11enc);
if (!description) if (!description)
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
else else
db_bind_text(stmt, BIND_NEXT, description); db_bind_text(stmt, description);
db_bind_talarr(stmt, BIND_NEXT, features); db_bind_talarr(stmt, features);
if (local_offer_id) if (local_offer_id)
db_bind_sha256(stmt, BIND_NEXT, local_offer_id); db_bind_sha256(stmt, local_offer_id);
else else
db_bind_null(stmt, BIND_NEXT); db_bind_null(stmt);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
@@ -333,7 +333,7 @@ bool invoices_find_by_label(struct invoices *invoices,
stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id"
" FROM invoices" " FROM invoices"
" WHERE label = ?;")); " WHERE label = ?;"));
db_bind_json_escape(stmt, BIND_NEXT, label); db_bind_json_escape(stmt, label);
db_query_prepared(stmt); db_query_prepared(stmt);
if (!db_step(stmt)) { if (!db_step(stmt)) {
@@ -355,7 +355,7 @@ bool invoices_find_by_rhash(struct invoices *invoices,
stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id" stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT id"
" FROM invoices" " FROM invoices"
" WHERE payment_hash = ?;")); " WHERE payment_hash = ?;"));
db_bind_sha256(stmt, BIND_NEXT, rhash); db_bind_sha256(stmt, rhash);
db_query_prepared(stmt); db_query_prepared(stmt);
if (!db_step(stmt)) { if (!db_step(stmt)) {
@@ -377,8 +377,8 @@ bool invoices_find_unpaid(struct invoices *invoices,
" FROM invoices" " FROM invoices"
" WHERE payment_hash = ?" " WHERE payment_hash = ?"
" AND state = ?;")); " AND state = ?;"));
db_bind_sha256(stmt, BIND_NEXT, rhash); db_bind_sha256(stmt, rhash);
db_bind_int(stmt, BIND_NEXT, UNPAID); db_bind_int(stmt, UNPAID);
db_query_prepared(stmt); db_query_prepared(stmt);
if (!db_step(stmt)) { if (!db_step(stmt)) {
@@ -398,7 +398,7 @@ bool invoices_delete(struct invoices *invoices, u64 inv_dbid)
/* Delete from database. */ /* Delete from database. */
stmt = db_prepare_v2(invoices->wallet->db, stmt = db_prepare_v2(invoices->wallet->db,
SQL("DELETE FROM invoices WHERE id=?;")); SQL("DELETE FROM invoices WHERE id=?;"));
db_bind_u64(stmt, BIND_NEXT, inv_dbid); db_bind_u64(stmt, inv_dbid);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
changes = db_count_changes(stmt); changes = db_count_changes(stmt);
@@ -420,7 +420,7 @@ bool invoices_delete_description(struct invoices *invoices, u64 inv_dbid)
stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices" stmt = db_prepare_v2(invoices->wallet->db, SQL("UPDATE invoices"
" SET description = NULL" " SET description = NULL"
" WHERE ID = ?;")); " WHERE ID = ?;"));
db_bind_u64(stmt, BIND_NEXT, inv_dbid); db_bind_u64(stmt, inv_dbid);
db_exec_prepared_v2(stmt); db_exec_prepared_v2(stmt);
changes = db_count_changes(stmt); changes = db_count_changes(stmt);
@@ -437,8 +437,8 @@ void invoices_delete_expired(struct invoices *invoices,
"DELETE FROM invoices" "DELETE FROM invoices"
" WHERE state = ?" " WHERE state = ?"
" AND expiry_time <= ?;")); " AND expiry_time <= ?;"));
db_bind_int(stmt, BIND_NEXT, EXPIRED); db_bind_int(stmt, EXPIRED);
db_bind_u64(stmt, BIND_NEXT, max_expiry_time); db_bind_u64(stmt, max_expiry_time);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
} }
@@ -484,7 +484,7 @@ static enum invoice_status invoice_get_status(struct invoices *invoices,
stmt = db_prepare_v2( stmt = db_prepare_v2(
invoices->wallet->db, SQL("SELECT state FROM invoices WHERE id = ?;")); invoices->wallet->db, SQL("SELECT state FROM invoices WHERE id = ?;"));
db_bind_u64(stmt, BIND_NEXT, inv_dbid); db_bind_u64(stmt, inv_dbid);
db_query_prepared(stmt); db_query_prepared(stmt);
res = db_step(stmt); res = db_step(stmt);
@@ -502,7 +502,7 @@ static void maybe_mark_offer_used(struct db *db, u64 inv_dbid)
stmt = db_prepare_v2( stmt = db_prepare_v2(
db, SQL("SELECT local_offer_id FROM invoices WHERE id = ?;")); db, SQL("SELECT local_offer_id FROM invoices WHERE id = ?;"));
db_bind_u64(stmt, BIND_NEXT, inv_dbid); db_bind_u64(stmt, inv_dbid);
db_query_prepared(stmt); db_query_prepared(stmt);
db_step(stmt); db_step(stmt);
@@ -539,11 +539,11 @@ bool invoices_resolve(struct invoices *invoices,
" , msatoshi_received=?" " , msatoshi_received=?"
" , paid_timestamp=?" " , paid_timestamp=?"
" WHERE id=?;")); " WHERE id=?;"));
db_bind_int(stmt, BIND_NEXT, PAID); db_bind_int(stmt, PAID);
db_bind_u64(stmt, BIND_NEXT, pay_index); db_bind_u64(stmt, pay_index);
db_bind_amount_msat(stmt, BIND_NEXT, &received); db_bind_amount_msat(stmt, &received);
db_bind_u64(stmt, BIND_NEXT, paid_timestamp); db_bind_u64(stmt, paid_timestamp);
db_bind_u64(stmt, BIND_NEXT, inv_dbid); db_bind_u64(stmt, inv_dbid);
db_exec_prepared_v2(take(stmt)); db_exec_prepared_v2(take(stmt));
maybe_mark_offer_used(invoices->wallet->db, inv_dbid); maybe_mark_offer_used(invoices->wallet->db, inv_dbid);
@@ -596,7 +596,7 @@ void invoices_waitany(const tal_t *ctx,
" WHERE pay_index IS NOT NULL" " WHERE pay_index IS NOT NULL"
" AND pay_index > ?" " AND pay_index > ?"
" ORDER BY pay_index ASC LIMIT 1;")); " ORDER BY pay_index ASC LIMIT 1;"));
db_bind_u64(stmt, BIND_NEXT, lastpay_index); db_bind_u64(stmt, lastpay_index);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) { if (db_step(stmt)) {
@@ -656,7 +656,7 @@ struct invoice_details *invoices_get_details(const tal_t *ctx,
", local_offer_id" ", local_offer_id"
" FROM invoices" " FROM invoices"
" WHERE id = ?;")); " WHERE id = ?;"));
db_bind_u64(stmt, BIND_NEXT, inv_dbid); db_bind_u64(stmt, inv_dbid);
db_query_prepared(stmt); db_query_prepared(stmt);
res = db_step(stmt); res = db_step(stmt);
assert(res); assert(res);

View File

@@ -1568,7 +1568,7 @@ static int count_inflights(struct wallet *w, u64 channel_dbid)
stmt = db_prepare_v2(w->db, SQL("SELECT COUNT(1)" stmt = db_prepare_v2(w->db, SQL("SELECT COUNT(1)"
" FROM channel_funding_inflights" " FROM channel_funding_inflights"
" WHERE channel_id = ?;")); " WHERE channel_id = ?;"));
db_bind_u64(stmt, 0, channel_dbid); db_bind_u64(stmt, channel_dbid);
db_query_prepared(stmt); db_query_prepared(stmt);
if (!db_step(stmt)) if (!db_step(stmt))
abort(); abort();

File diff suppressed because it is too large Load Diff