mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-07 16:14:26 +01:00
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:
124
db/bindings.c
124
db/bindings.c
@@ -16,18 +16,11 @@
|
||||
|
||||
#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) {
|
||||
/* 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);
|
||||
size_t pos = ++stmt->bind_pos;
|
||||
assert(pos < tal_count(stmt->bindings));
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
@@ -50,9 +43,9 @@ static bool db_column_null_warn(struct db_stmt *stmt, const char *colname,
|
||||
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));
|
||||
stmt->bindings[pos].type = DB_BINDING_INT;
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
pos = check_bind_pos(stmt, pos);
|
||||
stmt->bindings[pos].type = DB_BINDING_UINT64;
|
||||
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].v.blob = memcheck(val, 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].v.text = 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);
|
||||
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);
|
||||
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)
|
||||
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)
|
||||
{
|
||||
/* 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,
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
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)
|
||||
{
|
||||
u8 *buf = tal_arr(stmt, u8, 64);
|
||||
int ret = secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
|
||||
buf, sig);
|
||||
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));
|
||||
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);
|
||||
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;
|
||||
const u8 *ser = psbt_get_bytes(stmt, psbt, &bytes_written);
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
db_bind_null(stmt, col);
|
||||
db_bind_null(stmt);
|
||||
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)
|
||||
|
||||
@@ -17,55 +17,50 @@ struct onionreply;
|
||||
struct wally_psbt;
|
||||
struct wally_tx;
|
||||
|
||||
/* Magic pos argument meaning "the next field" */
|
||||
#define BIND_NEXT -77
|
||||
|
||||
int db_col_is_null(struct db_stmt *stmt, const char *colname);
|
||||
|
||||
void db_bind_int(struct db_stmt *stmt, int pos, int val);
|
||||
int db_col_int(struct db_stmt *stmt, const char *colname);
|
||||
|
||||
void db_bind_null(struct db_stmt *stmt, int pos);
|
||||
void db_bind_int(struct db_stmt *stmt, int pos, int val);
|
||||
void db_bind_u64(struct db_stmt *stmt, int pos, u64 val);
|
||||
void db_bind_blob(struct db_stmt *stmt, int pos, const u8 *val, size_t len);
|
||||
void db_bind_text(struct db_stmt *stmt, int pos, const char *val);
|
||||
void db_bind_preimage(struct db_stmt *stmt, int pos, const struct preimage *p);
|
||||
void db_bind_sha256(struct db_stmt *stmt, int pos, const struct sha256 *s);
|
||||
void db_bind_sha256d(struct db_stmt *stmt, int pos, const struct sha256_double *s);
|
||||
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,
|
||||
/* These bind the next `?` in stmt (they keep an internal counter). */
|
||||
void db_bind_null(struct db_stmt *stmt);
|
||||
void db_bind_int(struct db_stmt *stmt, int val);
|
||||
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_text(struct db_stmt *stmt, const char *val);
|
||||
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_sha256d(struct db_stmt *stmt, const struct sha256_double *s);
|
||||
void db_bind_secret(struct db_stmt *stmt, const struct secret *s);
|
||||
void db_bind_secret_arr(struct db_stmt *stmt, const struct secret *s);
|
||||
void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t);
|
||||
void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id);
|
||||
void db_bind_channel_type(struct db_stmt *stmt, const struct channel_type *type);
|
||||
void db_bind_node_id(struct db_stmt *stmt, const struct node_id *ni);
|
||||
void db_bind_node_id_arr(struct db_stmt *stmt,
|
||||
const struct node_id *ids);
|
||||
void db_bind_pubkey(struct db_stmt *stmt, int pos, const struct pubkey *p);
|
||||
void db_bind_short_channel_id(struct db_stmt *stmt, int col,
|
||||
void db_bind_pubkey(struct db_stmt *stmt, const struct pubkey *p);
|
||||
void db_bind_short_channel_id(struct db_stmt *stmt,
|
||||
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);
|
||||
void db_bind_signature(struct db_stmt *stmt, int col,
|
||||
void db_bind_signature(struct db_stmt *stmt,
|
||||
const secp256k1_ecdsa_signature *sig);
|
||||
void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t);
|
||||
void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx);
|
||||
void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt);
|
||||
void db_bind_amount_msat(struct db_stmt *stmt, int pos,
|
||||
void db_bind_timeabs(struct db_stmt *stmt, struct timeabs t);
|
||||
void db_bind_tx(struct db_stmt *stmt, const struct wally_tx *tx);
|
||||
void db_bind_psbt(struct db_stmt *stmt, const struct wally_psbt *psbt);
|
||||
void db_bind_amount_msat(struct db_stmt *stmt,
|
||||
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);
|
||||
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);
|
||||
void db_bind_onionreply(struct db_stmt *stmt, int col,
|
||||
void db_bind_onionreply(struct db_stmt *stmt,
|
||||
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 */
|
||||
/* Bridge function to get column number from SELECT
|
||||
(must exist) */
|
||||
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);
|
||||
size_t db_col_bytes(struct db_stmt *stmt, const char *colname);
|
||||
const void* db_col_blob(struct db_stmt *stmt, const char *colname);
|
||||
|
||||
12
db/exec.c
12
db/exec.c
@@ -62,16 +62,16 @@ void db_set_intvar(struct db *db, const char *varname, s64 val)
|
||||
{
|
||||
size_t changes;
|
||||
struct db_stmt *stmt = db_prepare_v2(db, SQL("UPDATE vars SET intval=? WHERE name=?;"));
|
||||
db_bind_int(stmt, BIND_NEXT, val);
|
||||
db_bind_text(stmt, BIND_NEXT, varname);
|
||||
db_bind_int(stmt, val);
|
||||
db_bind_text(stmt, varname);
|
||||
db_exec_prepared_v2(stmt);
|
||||
changes = db_count_changes(stmt);
|
||||
tal_free(stmt);
|
||||
|
||||
if (changes == 0) {
|
||||
stmt = db_prepare_v2(db, SQL("INSERT INTO vars (name, intval) VALUES (?, ?);"));
|
||||
db_bind_text(stmt, BIND_NEXT, varname);
|
||||
db_bind_int(stmt, BIND_NEXT, val);
|
||||
db_bind_text(stmt, varname);
|
||||
db_bind_int(stmt, val);
|
||||
db_exec_prepared_v2(stmt);
|
||||
tal_free(stmt);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ s64 db_get_intvar(struct db *db, const char *varname, s64 defval)
|
||||
s64 res = defval;
|
||||
struct db_stmt *stmt = db_prepare_v2(
|
||||
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))
|
||||
res = db_col_int(stmt, "intval");
|
||||
|
||||
@@ -110,7 +110,7 @@ static void db_data_version_incr(struct db *db)
|
||||
"SET intval = intval + 1 "
|
||||
"WHERE name = 'data_version'"
|
||||
" AND intval = ?"));
|
||||
db_bind_int(stmt, BIND_NEXT, db->data_version);
|
||||
db_bind_int(stmt, db->data_version);
|
||||
db_exec_prepared_v2(stmt);
|
||||
if (db_count_changes(stmt) != 1)
|
||||
db_fatal(stmt->db, "Optimistic lock on the database failed. There"
|
||||
|
||||
Reference in New Issue
Block a user