16
16
17
17
#define NSEC_IN_SEC 1000000000
18
18
19
- static int check_bind_pos (struct db_stmt * stmt , int pos )
19
+ static size_t check_bind_pos (struct db_stmt * stmt )
20
20
{
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 ;
30
22
assert (pos < tal_count (stmt -> bindings ));
23
+
31
24
return pos ;
32
25
}
33
26
@@ -50,9 +43,9 @@ static bool db_column_null_warn(struct db_stmt *stmt, const char *colname,
50
43
return true;
51
44
}
52
45
53
- void db_bind_int (struct db_stmt * stmt , int pos , int val )
46
+ void db_bind_int (struct db_stmt * stmt , int val )
54
47
{
55
- pos = check_bind_pos (stmt , pos );
48
+ size_t pos = check_bind_pos (stmt );
56
49
memcheck (& val , sizeof (val ));
57
50
stmt -> bindings [pos ].type = DB_BINDING_INT ;
58
51
stmt -> bindings [pos ].v .i = val ;
@@ -73,89 +66,90 @@ int db_col_is_null(struct db_stmt *stmt, const char *colname)
73
66
return db_column_is_null (stmt , db_query_colnum (stmt , colname ));
74
67
}
75
68
76
- void db_bind_null (struct db_stmt * stmt , int pos )
69
+ void db_bind_null (struct db_stmt * stmt )
77
70
{
78
- pos = check_bind_pos (stmt , pos );
71
+ size_t pos = check_bind_pos (stmt );
79
72
stmt -> bindings [pos ].type = DB_BINDING_NULL ;
80
73
}
81
74
82
- void db_bind_u64 (struct db_stmt * stmt , int pos , u64 val )
75
+ void db_bind_u64 (struct db_stmt * stmt , u64 val )
83
76
{
77
+ size_t pos = check_bind_pos (stmt );
78
+
84
79
memcheck (& val , sizeof (val ));
85
- pos = check_bind_pos (stmt , pos );
86
80
stmt -> bindings [pos ].type = DB_BINDING_UINT64 ;
87
81
stmt -> bindings [pos ].v .u64 = val ;
88
82
}
89
83
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 )
91
85
{
92
- pos = check_bind_pos (stmt , pos );
86
+ size_t pos = check_bind_pos (stmt );
93
87
stmt -> bindings [pos ].type = DB_BINDING_BLOB ;
94
88
stmt -> bindings [pos ].v .blob = memcheck (val , len );
95
89
stmt -> bindings [pos ].len = len ;
96
90
}
97
91
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 )
99
93
{
100
- pos = check_bind_pos (stmt , pos );
94
+ size_t pos = check_bind_pos (stmt );
101
95
stmt -> bindings [pos ].type = DB_BINDING_TEXT ;
102
96
stmt -> bindings [pos ].v .text = val ;
103
97
stmt -> bindings [pos ].len = strlen (val );
104
98
}
105
99
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 )
107
101
{
108
- db_bind_blob (stmt , pos , p -> r , sizeof (struct preimage ));
102
+ db_bind_blob (stmt , p -> r , sizeof (struct preimage ));
109
103
}
110
104
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 )
112
106
{
113
- db_bind_blob (stmt , pos , s -> u .u8 , sizeof (struct sha256 ));
107
+ db_bind_blob (stmt , s -> u .u8 , sizeof (struct sha256 ));
114
108
}
115
109
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 )
117
111
{
118
- db_bind_sha256 (stmt , pos , & s -> sha );
112
+ db_bind_sha256 (stmt , & s -> sha );
119
113
}
120
114
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 )
122
116
{
123
117
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 ));
125
119
}
126
120
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 )
128
122
{
129
123
size_t num = tal_count (s ), elsize = sizeof (s -> data );
130
124
u8 * ser = tal_arr (stmt , u8 , num * elsize );
131
125
132
126
for (size_t i = 0 ; i < num ; ++ i )
133
127
memcpy (ser + i * elsize , & s [i ], elsize );
134
128
135
- db_bind_blob (stmt , col , ser , tal_count (ser ));
129
+ db_bind_blob (stmt , ser , tal_count (ser ));
136
130
}
137
131
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 )
139
133
{
140
- db_bind_sha256d (stmt , pos , & t -> shad );
134
+ db_bind_sha256d (stmt , & t -> shad );
141
135
}
142
136
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 )
144
138
{
145
- db_bind_blob (stmt , pos , id -> id , sizeof (id -> id ));
139
+ db_bind_blob (stmt , id -> id , sizeof (id -> id ));
146
140
}
147
141
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 )
149
143
{
150
- db_bind_talarr (stmt , pos , type -> features );
144
+ db_bind_talarr (stmt , type -> features );
151
145
}
152
146
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 )
154
148
{
155
- db_bind_blob (stmt , pos , id -> k , sizeof (id -> k ));
149
+ db_bind_blob (stmt , id -> k , sizeof (id -> k ));
156
150
}
157
151
158
- void db_bind_node_id_arr (struct db_stmt * stmt , int col ,
152
+ void db_bind_node_id_arr (struct db_stmt * stmt ,
159
153
const struct node_id * ids )
160
154
{
161
155
/* 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,
168
162
ids [i ].k ,
169
163
sizeof (ids [i ].k ));
170
164
}
171
- db_bind_blob (stmt , col , arr , tal_count (arr ));
165
+ db_bind_blob (stmt , arr , tal_count (arr ));
172
166
}
173
167
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 )
175
169
{
176
170
u8 * der = tal_arr (stmt , u8 , PUBKEY_CMPR_LEN );
177
171
pubkey_to_der (der , pk );
178
- db_bind_blob (stmt , pos , der , PUBKEY_CMPR_LEN );
172
+ db_bind_blob (stmt , der , PUBKEY_CMPR_LEN );
179
173
}
180
174
181
- void db_bind_short_channel_id (struct db_stmt * stmt , int col ,
175
+ void db_bind_short_channel_id (struct db_stmt * stmt ,
182
176
const struct short_channel_id * id )
183
177
{
184
- db_bind_u64 (stmt , col , id -> u64 );
178
+ db_bind_u64 (stmt , id -> u64 );
185
179
}
186
180
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 ,
188
182
const struct short_channel_id * id )
189
183
{
190
184
u8 * ser = tal_arr (stmt , u8 , 0 );
@@ -193,69 +187,69 @@ void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col,
193
187
for (size_t i = 0 ; i < num ; ++ i )
194
188
towire_short_channel_id (& ser , & id [i ]);
195
189
196
- db_bind_talarr (stmt , col , ser );
190
+ db_bind_talarr (stmt , ser );
197
191
}
198
192
199
- void db_bind_signature (struct db_stmt * stmt , int col ,
193
+ void db_bind_signature (struct db_stmt * stmt ,
200
194
const secp256k1_ecdsa_signature * sig )
201
195
{
202
196
u8 * buf = tal_arr (stmt , u8 , 64 );
203
197
int ret = secp256k1_ecdsa_signature_serialize_compact (secp256k1_ctx ,
204
198
buf , sig );
205
199
assert (ret == 1 );
206
- db_bind_blob (stmt , col , buf , 64 );
200
+ db_bind_blob (stmt , buf , 64 );
207
201
}
208
202
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 )
210
204
{
211
205
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 );
213
207
}
214
208
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 )
216
210
{
217
211
u8 * ser = linearize_wtx (stmt , tx );
218
212
assert (ser );
219
- db_bind_talarr (stmt , col , ser );
213
+ db_bind_talarr (stmt , ser );
220
214
}
221
215
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 )
223
217
{
224
218
size_t bytes_written ;
225
219
const u8 * ser = psbt_get_bytes (stmt , psbt , & bytes_written );
226
220
assert (ser );
227
- db_bind_blob (stmt , col , ser , bytes_written );
221
+ db_bind_blob (stmt , ser , bytes_written );
228
222
}
229
223
230
- void db_bind_amount_msat (struct db_stmt * stmt , int pos ,
224
+ void db_bind_amount_msat (struct db_stmt * stmt ,
231
225
const struct amount_msat * msat )
232
226
{
233
- db_bind_u64 (stmt , pos , msat -> millisatoshis ); /* Raw: low level function */
227
+ db_bind_u64 (stmt , msat -> millisatoshis ); /* Raw: low level function */
234
228
}
235
229
236
- void db_bind_amount_sat (struct db_stmt * stmt , int pos ,
230
+ void db_bind_amount_sat (struct db_stmt * stmt ,
237
231
const struct amount_sat * sat )
238
232
{
239
- db_bind_u64 (stmt , pos , sat -> satoshis ); /* Raw: low level function */
233
+ db_bind_u64 (stmt , sat -> satoshis ); /* Raw: low level function */
240
234
}
241
235
242
- void db_bind_json_escape (struct db_stmt * stmt , int pos ,
236
+ void db_bind_json_escape (struct db_stmt * stmt ,
243
237
const struct json_escape * esc )
244
238
{
245
- db_bind_text (stmt , pos , esc -> s );
239
+ db_bind_text (stmt , esc -> s );
246
240
}
247
241
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 )
249
243
{
250
- db_bind_talarr (stmt , pos , r -> contents );
244
+ db_bind_talarr (stmt , r -> contents );
251
245
}
252
246
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 )
254
248
{
255
249
if (!arr )
256
- db_bind_null (stmt , col );
250
+ db_bind_null (stmt );
257
251
else
258
- db_bind_blob (stmt , col , arr , tal_bytelen (arr ));
252
+ db_bind_blob (stmt , arr , tal_bytelen (arr ));
259
253
}
260
254
261
255
static size_t db_column_bytes (struct db_stmt * stmt , int col )
0 commit comments