Skip to content

Commit 0bcff1e

Browse files
committed
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>
1 parent 9af407a commit 0bcff1e

File tree

10 files changed

+810
-822
lines changed

10 files changed

+810
-822
lines changed

db/bindings.c

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@
1616

1717
#define NSEC_IN_SEC 1000000000
1818

19-
static int check_bind_pos(struct db_stmt *stmt, int pos)
19+
static size_t check_bind_pos(struct db_stmt *stmt)
2020
{
21-
if (pos == BIND_NEXT) {
22-
/* Don't mix BIND_NEXT with other args! */
23-
assert(stmt->bindings[stmt->bind_pos+1].type == DB_BINDING_UNINITIALIZED);
24-
return ++stmt->bind_pos;
25-
}
26-
27-
/* Don't mix BIND_NEXT with other args! */
28-
assert(stmt->bind_pos == -1);
29-
assert(pos >= 0);
21+
size_t pos = ++stmt->bind_pos;
3022
assert(pos < tal_count(stmt->bindings));
23+
3124
return pos;
3225
}
3326

@@ -50,9 +43,9 @@ static bool db_column_null_warn(struct db_stmt *stmt, const char *colname,
5043
return true;
5144
}
5245

53-
void db_bind_int(struct db_stmt *stmt, int pos, int val)
46+
void db_bind_int(struct db_stmt *stmt, int val)
5447
{
55-
pos = check_bind_pos(stmt, pos);
48+
size_t pos = check_bind_pos(stmt);
5649
memcheck(&val, sizeof(val));
5750
stmt->bindings[pos].type = DB_BINDING_INT;
5851
stmt->bindings[pos].v.i = val;
@@ -73,89 +66,90 @@ int db_col_is_null(struct db_stmt *stmt, const char *colname)
7366
return db_column_is_null(stmt, db_query_colnum(stmt, colname));
7467
}
7568

76-
void db_bind_null(struct db_stmt *stmt, int pos)
69+
void db_bind_null(struct db_stmt *stmt)
7770
{
78-
pos = check_bind_pos(stmt, pos);
71+
size_t pos = check_bind_pos(stmt);
7972
stmt->bindings[pos].type = DB_BINDING_NULL;
8073
}
8174

82-
void db_bind_u64(struct db_stmt *stmt, int pos, u64 val)
75+
void db_bind_u64(struct db_stmt *stmt, u64 val)
8376
{
77+
size_t pos = check_bind_pos(stmt);
78+
8479
memcheck(&val, sizeof(val));
85-
pos = check_bind_pos(stmt, pos);
8680
stmt->bindings[pos].type = DB_BINDING_UINT64;
8781
stmt->bindings[pos].v.u64 = val;
8882
}
8983

90-
void db_bind_blob(struct db_stmt *stmt, int pos, const u8 *val, size_t len)
84+
void db_bind_blob(struct db_stmt *stmt, const u8 *val, size_t len)
9185
{
92-
pos = check_bind_pos(stmt, pos);
86+
size_t pos = check_bind_pos(stmt);
9387
stmt->bindings[pos].type = DB_BINDING_BLOB;
9488
stmt->bindings[pos].v.blob = memcheck(val, len);
9589
stmt->bindings[pos].len = len;
9690
}
9791

98-
void db_bind_text(struct db_stmt *stmt, int pos, const char *val)
92+
void db_bind_text(struct db_stmt *stmt, const char *val)
9993
{
100-
pos = check_bind_pos(stmt, pos);
94+
size_t pos = check_bind_pos(stmt);
10195
stmt->bindings[pos].type = DB_BINDING_TEXT;
10296
stmt->bindings[pos].v.text = val;
10397
stmt->bindings[pos].len = strlen(val);
10498
}
10599

106-
void db_bind_preimage(struct db_stmt *stmt, int pos, const struct preimage *p)
100+
void db_bind_preimage(struct db_stmt *stmt, const struct preimage *p)
107101
{
108-
db_bind_blob(stmt, pos, p->r, sizeof(struct preimage));
102+
db_bind_blob(stmt, p->r, sizeof(struct preimage));
109103
}
110104

111-
void db_bind_sha256(struct db_stmt *stmt, int pos, const struct sha256 *s)
105+
void db_bind_sha256(struct db_stmt *stmt, const struct sha256 *s)
112106
{
113-
db_bind_blob(stmt, pos, s->u.u8, sizeof(struct sha256));
107+
db_bind_blob(stmt, s->u.u8, sizeof(struct sha256));
114108
}
115109

116-
void db_bind_sha256d(struct db_stmt *stmt, int pos, const struct sha256_double *s)
110+
void db_bind_sha256d(struct db_stmt *stmt, const struct sha256_double *s)
117111
{
118-
db_bind_sha256(stmt, pos, &s->sha);
112+
db_bind_sha256(stmt, &s->sha);
119113
}
120114

121-
void db_bind_secret(struct db_stmt *stmt, int pos, const struct secret *s)
115+
void db_bind_secret(struct db_stmt *stmt, const struct secret *s)
122116
{
123117
assert(sizeof(s->data) == 32);
124-
db_bind_blob(stmt, pos, s->data, sizeof(s->data));
118+
db_bind_blob(stmt, s->data, sizeof(s->data));
125119
}
126120

127-
void db_bind_secret_arr(struct db_stmt *stmt, int col, const struct secret *s)
121+
void db_bind_secret_arr(struct db_stmt *stmt, const struct secret *s)
128122
{
129123
size_t num = tal_count(s), elsize = sizeof(s->data);
130124
u8 *ser = tal_arr(stmt, u8, num * elsize);
131125

132126
for (size_t i = 0; i < num; ++i)
133127
memcpy(ser + i * elsize, &s[i], elsize);
134128

135-
db_bind_blob(stmt, col, ser, tal_count(ser));
129+
db_bind_blob(stmt, ser, tal_count(ser));
136130
}
137131

138-
void db_bind_txid(struct db_stmt *stmt, int pos, const struct bitcoin_txid *t)
132+
void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t)
139133
{
140-
db_bind_sha256d(stmt, pos, &t->shad);
134+
db_bind_sha256d(stmt, &t->shad);
141135
}
142136

143-
void db_bind_channel_id(struct db_stmt *stmt, int pos, const struct channel_id *id)
137+
void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id)
144138
{
145-
db_bind_blob(stmt, pos, id->id, sizeof(id->id));
139+
db_bind_blob(stmt, id->id, sizeof(id->id));
146140
}
147141

148-
void db_bind_channel_type(struct db_stmt *stmt, int pos, const struct channel_type *type)
142+
void db_bind_channel_type(struct db_stmt *stmt, const struct channel_type *type)
149143
{
150-
db_bind_talarr(stmt, pos, type->features);
144+
db_bind_talarr(stmt, type->features);
151145
}
152146

153-
void db_bind_node_id(struct db_stmt *stmt, int pos, const struct node_id *id)
147+
void db_bind_node_id(struct db_stmt *stmt, const struct node_id *id)
154148
{
155-
db_bind_blob(stmt, pos, id->k, sizeof(id->k));
149+
db_bind_blob(stmt, id->k, sizeof(id->k));
156150
}
157151

158-
void db_bind_node_id_arr(struct db_stmt *stmt, int col,
152+
void db_bind_node_id_arr(struct db_stmt *stmt,
159153
const struct node_id *ids)
160154
{
161155
/* 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,
168162
ids[i].k,
169163
sizeof(ids[i].k));
170164
}
171-
db_bind_blob(stmt, col, arr, tal_count(arr));
165+
db_bind_blob(stmt, arr, tal_count(arr));
172166
}
173167

174-
void db_bind_pubkey(struct db_stmt *stmt, int pos, const struct pubkey *pk)
168+
void db_bind_pubkey(struct db_stmt *stmt, const struct pubkey *pk)
175169
{
176170
u8 *der = tal_arr(stmt, u8, PUBKEY_CMPR_LEN);
177171
pubkey_to_der(der, pk);
178-
db_bind_blob(stmt, pos, der, PUBKEY_CMPR_LEN);
172+
db_bind_blob(stmt, der, PUBKEY_CMPR_LEN);
179173
}
180174

181-
void db_bind_short_channel_id(struct db_stmt *stmt, int col,
175+
void db_bind_short_channel_id(struct db_stmt *stmt,
182176
const struct short_channel_id *id)
183177
{
184-
db_bind_u64(stmt, col, id->u64);
178+
db_bind_u64(stmt, id->u64);
185179
}
186180

187-
void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col,
181+
void db_bind_short_channel_id_arr(struct db_stmt *stmt,
188182
const struct short_channel_id *id)
189183
{
190184
u8 *ser = tal_arr(stmt, u8, 0);
@@ -193,69 +187,69 @@ void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col,
193187
for (size_t i = 0; i < num; ++i)
194188
towire_short_channel_id(&ser, &id[i]);
195189

196-
db_bind_talarr(stmt, col, ser);
190+
db_bind_talarr(stmt, ser);
197191
}
198192

199-
void db_bind_signature(struct db_stmt *stmt, int col,
193+
void db_bind_signature(struct db_stmt *stmt,
200194
const secp256k1_ecdsa_signature *sig)
201195
{
202196
u8 *buf = tal_arr(stmt, u8, 64);
203197
int ret = secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
204198
buf, sig);
205199
assert(ret == 1);
206-
db_bind_blob(stmt, col, buf, 64);
200+
db_bind_blob(stmt, buf, 64);
207201
}
208202

209-
void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t)
203+
void db_bind_timeabs(struct db_stmt *stmt, struct timeabs t)
210204
{
211205
u64 timestamp = t.ts.tv_nsec + (((u64) t.ts.tv_sec) * ((u64) NSEC_IN_SEC));
212-
db_bind_u64(stmt, col, timestamp);
206+
db_bind_u64(stmt, timestamp);
213207
}
214208

215-
void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx)
209+
void db_bind_tx(struct db_stmt *stmt, const struct wally_tx *tx)
216210
{
217211
u8 *ser = linearize_wtx(stmt, tx);
218212
assert(ser);
219-
db_bind_talarr(stmt, col, ser);
213+
db_bind_talarr(stmt, ser);
220214
}
221215

222-
void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt)
216+
void db_bind_psbt(struct db_stmt *stmt, const struct wally_psbt *psbt)
223217
{
224218
size_t bytes_written;
225219
const u8 *ser = psbt_get_bytes(stmt, psbt, &bytes_written);
226220
assert(ser);
227-
db_bind_blob(stmt, col, ser, bytes_written);
221+
db_bind_blob(stmt, ser, bytes_written);
228222
}
229223

230-
void db_bind_amount_msat(struct db_stmt *stmt, int pos,
224+
void db_bind_amount_msat(struct db_stmt *stmt,
231225
const struct amount_msat *msat)
232226
{
233-
db_bind_u64(stmt, pos, msat->millisatoshis); /* Raw: low level function */
227+
db_bind_u64(stmt, msat->millisatoshis); /* Raw: low level function */
234228
}
235229

236-
void db_bind_amount_sat(struct db_stmt *stmt, int pos,
230+
void db_bind_amount_sat(struct db_stmt *stmt,
237231
const struct amount_sat *sat)
238232
{
239-
db_bind_u64(stmt, pos, sat->satoshis); /* Raw: low level function */
233+
db_bind_u64(stmt, sat->satoshis); /* Raw: low level function */
240234
}
241235

242-
void db_bind_json_escape(struct db_stmt *stmt, int pos,
236+
void db_bind_json_escape(struct db_stmt *stmt,
243237
const struct json_escape *esc)
244238
{
245-
db_bind_text(stmt, pos, esc->s);
239+
db_bind_text(stmt, esc->s);
246240
}
247241

248-
void db_bind_onionreply(struct db_stmt *stmt, int pos, const struct onionreply *r)
242+
void db_bind_onionreply(struct db_stmt *stmt, const struct onionreply *r)
249243
{
250-
db_bind_talarr(stmt, pos, r->contents);
244+
db_bind_talarr(stmt, r->contents);
251245
}
252246

253-
void db_bind_talarr(struct db_stmt *stmt, int col, const u8 *arr)
247+
void db_bind_talarr(struct db_stmt *stmt, const u8 *arr)
254248
{
255249
if (!arr)
256-
db_bind_null(stmt, col);
250+
db_bind_null(stmt);
257251
else
258-
db_bind_blob(stmt, col, arr, tal_bytelen(arr));
252+
db_bind_blob(stmt, arr, tal_bytelen(arr));
259253
}
260254

261255
static size_t db_column_bytes(struct db_stmt *stmt, int col)

db/bindings.h

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,50 @@ struct onionreply;
1717
struct wally_psbt;
1818
struct wally_tx;
1919

20-
/* Magic pos argument meaning "the next field" */
21-
#define BIND_NEXT -77
22-
23-
int db_col_is_null(struct db_stmt *stmt, const char *colname);
24-
25-
void db_bind_int(struct db_stmt *stmt, int pos, int val);
26-
int db_col_int(struct db_stmt *stmt, const char *colname);
27-
28-
void db_bind_null(struct db_stmt *stmt, int pos);
29-
void db_bind_int(struct db_stmt *stmt, int pos, int val);
30-
void db_bind_u64(struct db_stmt *stmt, int pos, u64 val);
31-
void db_bind_blob(struct db_stmt *stmt, int pos, const u8 *val, size_t len);
32-
void db_bind_text(struct db_stmt *stmt, int pos, const char *val);
33-
void db_bind_preimage(struct db_stmt *stmt, int pos, const struct preimage *p);
34-
void db_bind_sha256(struct db_stmt *stmt, int pos, const struct sha256 *s);
35-
void db_bind_sha256d(struct db_stmt *stmt, int pos, const struct sha256_double *s);
36-
void db_bind_secret(struct db_stmt *stmt, int pos, const struct secret *s);
37-
void db_bind_secret_arr(struct db_stmt *stmt, int col, const struct secret *s);
38-
void db_bind_txid(struct db_stmt *stmt, int pos, const struct bitcoin_txid *t);
39-
void db_bind_channel_id(struct db_stmt *stmt, int pos, const struct channel_id *id);
40-
void db_bind_channel_type(struct db_stmt *stmt, int pos, const struct channel_type *type);
41-
void db_bind_node_id(struct db_stmt *stmt, int pos, const struct node_id *ni);
42-
void db_bind_node_id_arr(struct db_stmt *stmt, int col,
20+
/* These bind the next `?` in stmt (they keep an internal counter). */
21+
void db_bind_null(struct db_stmt *stmt);
22+
void db_bind_int(struct db_stmt *stmt, int val);
23+
void db_bind_u64(struct db_stmt *stmt, u64 val);
24+
void db_bind_blob(struct db_stmt *stmt, const u8 *val, size_t len);
25+
void db_bind_text(struct db_stmt *stmt, const char *val);
26+
void db_bind_preimage(struct db_stmt *stmt, const struct preimage *p);
27+
void db_bind_sha256(struct db_stmt *stmt, const struct sha256 *s);
28+
void db_bind_sha256d(struct db_stmt *stmt, const struct sha256_double *s);
29+
void db_bind_secret(struct db_stmt *stmt, const struct secret *s);
30+
void db_bind_secret_arr(struct db_stmt *stmt, const struct secret *s);
31+
void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t);
32+
void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id);
33+
void db_bind_channel_type(struct db_stmt *stmt, const struct channel_type *type);
34+
void db_bind_node_id(struct db_stmt *stmt, const struct node_id *ni);
35+
void db_bind_node_id_arr(struct db_stmt *stmt,
4336
const struct node_id *ids);
44-
void db_bind_pubkey(struct db_stmt *stmt, int pos, const struct pubkey *p);
45-
void db_bind_short_channel_id(struct db_stmt *stmt, int col,
37+
void db_bind_pubkey(struct db_stmt *stmt, const struct pubkey *p);
38+
void db_bind_short_channel_id(struct db_stmt *stmt,
4639
const struct short_channel_id *id);
47-
void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col,
40+
void db_bind_short_channel_id_arr(struct db_stmt *stmt,
4841
const struct short_channel_id *id);
49-
void db_bind_signature(struct db_stmt *stmt, int col,
42+
void db_bind_signature(struct db_stmt *stmt,
5043
const secp256k1_ecdsa_signature *sig);
51-
void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t);
52-
void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx);
53-
void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt);
54-
void db_bind_amount_msat(struct db_stmt *stmt, int pos,
44+
void db_bind_timeabs(struct db_stmt *stmt, struct timeabs t);
45+
void db_bind_tx(struct db_stmt *stmt, const struct wally_tx *tx);
46+
void db_bind_psbt(struct db_stmt *stmt, const struct wally_psbt *psbt);
47+
void db_bind_amount_msat(struct db_stmt *stmt,
5548
const struct amount_msat *msat);
56-
void db_bind_amount_sat(struct db_stmt *stmt, int pos,
49+
void db_bind_amount_sat(struct db_stmt *stmt,
5750
const struct amount_sat *sat);
58-
void db_bind_json_escape(struct db_stmt *stmt, int pos,
51+
void db_bind_json_escape(struct db_stmt *stmt,
5952
const struct json_escape *esc);
60-
void db_bind_onionreply(struct db_stmt *stmt, int col,
53+
void db_bind_onionreply(struct db_stmt *stmt,
6154
const struct onionreply *r);
62-
void db_bind_talarr(struct db_stmt *stmt, int col, const u8 *arr);
55+
void db_bind_talarr(struct db_stmt *stmt, const u8 *arr);
6356

6457
/* Modern variants: get columns by name from SELECT */
6558
/* Bridge function to get column number from SELECT
6659
(must exist) */
6760
size_t db_query_colnum(const struct db_stmt *stmt, const char *colname);
6861

62+
int db_col_is_null(struct db_stmt *stmt, const char *colname);
63+
int db_col_int(struct db_stmt *stmt, const char *colname);
6964
u64 db_col_u64(struct db_stmt *stmt, const char *colname);
7065
size_t db_col_bytes(struct db_stmt *stmt, const char *colname);
7166
const void* db_col_blob(struct db_stmt *stmt, const char *colname);

db/exec.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ void db_set_intvar(struct db *db, const char *varname, s64 val)
6262
{
6363
size_t changes;
6464
struct db_stmt *stmt = db_prepare_v2(db, SQL("UPDATE vars SET intval=? WHERE name=?;"));
65-
db_bind_int(stmt, BIND_NEXT, val);
66-
db_bind_text(stmt, BIND_NEXT, varname);
65+
db_bind_int(stmt, val);
66+
db_bind_text(stmt, varname);
6767
db_exec_prepared_v2(stmt);
6868
changes = db_count_changes(stmt);
6969
tal_free(stmt);
7070

7171
if (changes == 0) {
7272
stmt = db_prepare_v2(db, SQL("INSERT INTO vars (name, intval) VALUES (?, ?);"));
73-
db_bind_text(stmt, BIND_NEXT, varname);
74-
db_bind_int(stmt, BIND_NEXT, val);
73+
db_bind_text(stmt, varname);
74+
db_bind_int(stmt, val);
7575
db_exec_prepared_v2(stmt);
7676
tal_free(stmt);
7777
}
@@ -82,7 +82,7 @@ s64 db_get_intvar(struct db *db, const char *varname, s64 defval)
8282
s64 res = defval;
8383
struct db_stmt *stmt = db_prepare_v2(
8484
db, SQL("SELECT intval FROM vars WHERE name= ? LIMIT 1"));
85-
db_bind_text(stmt, BIND_NEXT, varname);
85+
db_bind_text(stmt, varname);
8686
if (db_query_prepared_canfail(stmt) && db_step(stmt))
8787
res = db_col_int(stmt, "intval");
8888

@@ -110,7 +110,7 @@ static void db_data_version_incr(struct db *db)
110110
"SET intval = intval + 1 "
111111
"WHERE name = 'data_version'"
112112
" AND intval = ?"));
113-
db_bind_int(stmt, BIND_NEXT, db->data_version);
113+
db_bind_int(stmt, db->data_version);
114114
db_exec_prepared_v2(stmt);
115115
if (db_count_changes(stmt) != 1)
116116
db_fatal(stmt->db, "Optimistic lock on the database failed. There"

0 commit comments

Comments
 (0)