Additional SQL code cleanup (#17503)
Remove unneeded check Add macros for sqlite3_finalize and sqlite3_reset Use macro to make parameter binding more readable
Stelios Fragkakis committed
Apr 24, 2024 at 09:56 UTC
e1579abddc252af0616cbcd19c1dab159d47cef7
8 files changed
+303
-630
src/database/sqlite/sqlite_aclk.c
+17
-19
@@ -192,16 +192,16 @@ static int is_host_available(uuid_t *host_id)
192
return 1;
193
}
194
195
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
196
- if (unlikely(rc != SQLITE_OK)) {
197
- error_report("Failed to bind host_id parameter to check host existence");
198
- goto failed;
199
- }
195
+ int param = 0;
196
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
197
+
198
+ param = 0;
199
rc = sqlite3_step_monitored(res);
200
202
-failed:
203
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
204
- error_report("Failed to finalize the prepared statement when checking host existence");
201
+done:
202
+ REPORT_BIND_FAIL(res, param);
203
+
204
+ SQLITE_FINALIZE(res);
205
206
return (rc == SQLITE_ROW);
207
}
@@ -245,9 +245,7 @@ static void sql_delete_aclk_table_list(char *host_guid)
245
while (sqlite3_step_monitored(res) == SQLITE_ROW)
246
buffer_strcat(sql, (char *) sqlite3_column_text(res, 0));
247
248
- rc = sqlite3_finalize(res);
249
- if (unlikely(rc != SQLITE_OK))
250
- error_report("Failed to finalize statement to clean up aclk tables, rc = %d", rc);
248
+ SQLITE_FINALIZE(res);
249
250
rc = db_execute(db_meta, buffer_tostring(sql));
251
if (unlikely(rc))
@@ -281,11 +279,10 @@ static void sql_unregister_node(char *machine_guid)
279
return;
280
}
281
284
- rc = sqlite3_bind_blob(res, 1, &host_uuid, sizeof(host_uuid), SQLITE_STATIC);
285
- if (unlikely(rc != SQLITE_OK)) {
286
- error_report("Failed to bind host_id parameter to remove host node id");
287
- goto skip;
288
- }
282
+ int param = 0;
283
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host_uuid, sizeof(host_uuid), SQLITE_STATIC));
284
+ param = 0;
285
+
286
rc = sqlite3_step_monitored(res);
287
if (unlikely(rc != SQLITE_DONE)) {
288
error_report("Failed to execute command to remove host node id");
@@ -295,9 +292,10 @@ static void sql_unregister_node(char *machine_guid)
292
machine_guid = NULL;
293
}
294
298
-skip:
299
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
300
- error_report("Failed to finalize statement to remove host node id");
295
+done:
296
+ REPORT_BIND_FAIL(res, param);
297
+
298
+ SQLITE_FINALIZE(res);
299
freez(machine_guid);
300
}
301
src/database/sqlite/sqlite_aclk_alert.c
+10
-29
@@ -41,9 +41,7 @@ static void update_filtered(ALARM_ENTRY *ae, int64_t unique_id, char *uuid_str)
41
done:
42
REPORT_BIND_FAIL(res, param);
43
44
- rc = sqlite3_finalize(res);
45
- if (unlikely(rc != SQLITE_OK))
46
- error_report("Failed to finalize statement when trying to update_filtered, rc = %d", rc);
44
+ SQLITE_FINALIZE(res);
45
}
46
47
#define SQL_SELECT_VARIABLE_ALERT_BY_UNIQUE_ID \
@@ -75,9 +73,7 @@ static inline bool is_event_from_alert_variable_config(int64_t unique_id, uuid_t
73
done:
74
REPORT_BIND_FAIL(res, param);
75
78
- rc = sqlite3_finalize(res);
79
- if (unlikely(rc != SQLITE_OK))
80
- error_report("Failed to finalize statement when trying to check for alert variables, rc = %d", rc);
76
+ SQLITE_FINALIZE(res);
77
78
return ret;
79
}
@@ -140,9 +136,7 @@ static bool should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
136
done:
137
REPORT_BIND_FAIL(res, param);
138
143
- rc = sqlite3_finalize(res);
144
- if (unlikely(rc != SQLITE_OK))
145
- error_report("Failed to finalize statement when trying should_send_to_cloud, rc = %d", rc);
139
+ SQLITE_FINALIZE(res);
140
141
return send;
142
}
@@ -188,8 +182,7 @@ void sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, bool skip_filter)
182
error_report("Failed to store alert event %"PRIu32", rc = %d", ae->unique_id, rc);
183
184
done:
191
- if (unlikely(sqlite3_finalize(res_alert) != SQLITE_OK))
192
- error_report("Failed to reset statement in store alert event, rc = %d", rc);
185
+ SQLITE_FINALIZE(res_alert);
186
}
187
188
int rrdcalc_status_to_proto_enum(RRDCALC_STATUS status)
@@ -412,9 +405,7 @@ static void aclk_push_alert_event(struct aclk_sync_cfg_t *wc __maybe_unused)
405
}
406
407
done:
415
- rc = sqlite3_finalize(res);
416
- if (unlikely(rc != SQLITE_OK))
417
- error_report("Failed to finalize statement to send alert entries from the database, rc = %d", rc);
408
+ SQLITE_FINALIZE(res);
409
410
freez(claim_id);
411
buffer_free(sql);
@@ -482,9 +473,7 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host)
473
else
474
rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS);
475
done:
485
- rc = sqlite3_finalize(res);
486
- if (unlikely(rc != SQLITE_OK))
487
- error_report("Failed to finalize statement to queue existing alerts, rc = %d", rc);
476
+ SQLITE_FINALIZE(res);
477
478
skip:
479
rw_spinlock_write_unlock(&host->health_log.spinlock);
@@ -621,9 +610,7 @@ void aclk_push_alert_config_event(char *node_id __maybe_unused, char *config_has
610
nd_log(NDLS_ACCESS, NDLP_WARNING, "ACLK STA [%s (%s)]: Alert config for %s not found.", wc->node_id, wc->host ? rrdhost_hostname(wc->host) : "N/A", config_hash);
611
612
bind_fail:
624
- rc = sqlite3_finalize(res);
625
- if (unlikely(rc != SQLITE_OK))
626
- error_report("Failed to reset statement when pushing alarm config hash, rc = %d", rc);
613
+ SQLITE_FINALIZE(res);
614
615
freez(config_hash);
616
freez(node_id);
@@ -704,9 +691,7 @@ void sql_process_queue_removed_alerts_to_aclk(char *node_id)
691
}
692
693
skip:
707
- rc = sqlite3_finalize(res);
708
- if (unlikely(rc != SQLITE_OK))
709
- error_report("Failed to finalize statement to queue removed alerts, rc = %d", rc);
694
+ SQLITE_FINALIZE(res);
695
}
696
697
void sql_queue_removed_alerts_to_aclk(RRDHOST *host)
@@ -964,9 +949,7 @@ void sql_aclk_alert_clean_dead_entries(RRDHOST *host)
949
error_report("Failed to execute DELETE query for cleaning stale ACLK alert entries.");
950
951
skip:
967
- rc = sqlite3_finalize(res);
968
- if (unlikely(rc != SQLITE_OK))
969
- error_report("Failed to finalize statement for cleaning stale ACLK alert entries.");
952
+ SQLITE_FINALIZE(res);
953
}
954
955
#define SQL_GET_MIN_MAX_ALERT_SEQ "SELECT MIN(sequence_id), MAX(sequence_id), " \
@@ -1001,9 +984,7 @@ int get_proto_alert_status(RRDHOST *host, struct proto_alert_status *proto_alert
984
sqlite3_column_bytes(res, 2) > 0 ? (uint64_t)sqlite3_column_int64(res, 2) : 0;
985
}
986
1004
- rc = sqlite3_finalize(res);
1005
- if (unlikely(rc != SQLITE_OK))
1006
- error_report("Failed to finalize statement to get alert log status from the database, rc = %d", rc);
987
+ SQLITE_FINALIZE(res);
988
989
return 0;
990
}
src/database/sqlite/sqlite_context.c
+57
-126
@@ -76,7 +76,7 @@ int sql_init_context_database(int memory)
76
// Fetching data
77
//
78
#define CTX_GET_CHART_LIST "SELECT c.chart_id, c.type||'.'||c.id, c.name, c.context, c.title, c.unit, c.priority, " \
79
- "c.update_every, c.chart_type, c.family FROM chart c WHERE c.host_id = @host_id AND c.chart_id IS NOT NULL"
79
+ "c.update_every, c.chart_type, c.family FROM chart c WHERE c.host_id = @host_id AND c.chart_id IS NOT NULL"
80
81
void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, void *), void *data)
82
{
@@ -95,13 +95,10 @@ void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, voi
95
return;
96
}
97
}
98
+ int param = 0;
99
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
100
99
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
100
- if (unlikely(rc != SQLITE_OK)) {
101
- error_report("Failed to bind host_id to fetch the chart list");
102
- goto skip_load;
103
- }
104
-
101
+ param = 0;
102
SQL_CHART_DATA chart_data = { 0 };
103
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
104
uuid_copy(chart_data.chart_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
@@ -117,15 +114,15 @@ void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, voi
114
dict_cb(&chart_data, data);
115
}
116
120
-skip_load:
121
- rc = sqlite3_reset(res);
122
- if (rc != SQLITE_OK)
123
- error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
117
+done:
118
+ REPORT_BIND_FAIL(res, param);
119
+
120
+ SQLITE_RESET(res);
121
}
122
123
// Dimension list
124
#define CTX_GET_DIMENSION_LIST "SELECT d.dim_id, d.id, d.name, CASE WHEN INSTR(d.options,\"hidden\") > 0 THEN 1 ELSE 0 END " \
128
- "FROM dimension d WHERE d.chart_id = @id AND d.dim_id IS NOT NULL ORDER BY d.rowid ASC"
125
+ "FROM dimension d WHERE d.chart_id = @id AND d.dim_id IS NOT NULL ORDER BY d.rowid ASC"
126
void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DATA *, void *), void *data)
127
{
128
int rc;
@@ -139,14 +136,12 @@ void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DA
136
}
137
}
138
142
- rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
143
- if (unlikely(rc != SQLITE_OK)) {
144
- error_report("Failed to bind chart_id to fetch dimension list");
145
- goto failed;
146
- }
139
+ int param = 0;
140
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC));
141
142
SQL_DIMENSION_DATA dimension_data;
143
144
+ param = 0;
145
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
146
uuid_copy(dimension_data.dim_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
147
dimension_data.id = (char *) sqlite3_column_text(res, 1);
@@ -155,10 +150,10 @@ void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DA
150
dict_cb(&dimension_data, data);
151
}
152
158
-failed:
159
- rc = sqlite3_reset(res);
160
- if (rc != SQLITE_OK)
161
- error_report("Failed to reset statement that fetches the chart dimension list, rc = %d", rc);
153
+done:
154
+ REPORT_BIND_FAIL(res, param);
155
+
156
+ SQLITE_RESET(res);
157
}
158
159
// LABEL LIST
@@ -177,12 +172,10 @@ void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, v
172
}
173
}
174
180
- rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
181
- if (unlikely(rc != SQLITE_OK)) {
182
- error_report("Failed to bind chart_id to fetch chart labels");
183
- goto failed;
184
- }
175
+ int param = 0;
176
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC));
177
178
+ param = 0;
179
SQL_CLABEL_DATA label_data;
180
181
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
@@ -192,15 +185,15 @@ void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, v
185
dict_cb(&label_data, data);
186
}
187
195
-failed:
196
- rc = sqlite3_reset(res);
197
- if (rc != SQLITE_OK)
198
- error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
188
+done:
189
+ REPORT_BIND_FAIL(res, param);
190
+
191
+ SQLITE_RESET(res);
192
}
193
194
// CONTEXT LIST
195
#define CTX_GET_CONTEXT_LIST "SELECT id, version, title, chart_type, unit, priority, first_time_t, " \
203
- "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id"
196
+ "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id"
197
198
void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_DATA *, void *), void *data)
199
{
@@ -221,12 +214,9 @@ void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_D
214
215
VERSIONED_CONTEXT_DATA context_data = {0};
216
224
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
225
-
226
- if (unlikely(rc != SQLITE_OK)) {
227
- error_report("Failed to bind host_id to fetch versioned context data");
228
- goto failed;
229
- }
217
+ int param = 0;
218
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
219
+ param = 0;
220
221
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
222
context_data.id = (char *) sqlite3_column_text(res, 0);
@@ -242,10 +232,10 @@ void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_D
232
dict_cb(&context_data, data);
233
}
234
245
-failed:
246
- rc = sqlite3_reset(res);
247
- if (rc != SQLITE_OK)
248
- error_report("Failed to reset statement that fetches stored context versioned data, rc = %d", rc);
235
+done:
236
+ REPORT_BIND_FAIL(res, param);
237
+
238
+ SQLITE_RESET(res);
239
}
240
241
@@ -271,81 +261,29 @@ int ctx_store_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
261
return 1;
262
}
263
274
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
275
- if (unlikely(rc != SQLITE_OK)) {
276
- error_report("Failed to bind host_uuid to store context details");
277
- goto skip_store;
278
- }
279
-
280
- rc = bind_text_null(res, 2, context_data->id, 0);
281
- if (unlikely(rc != SQLITE_OK)) {
282
- error_report("Failed to bind context to store context details");
283
- goto skip_store;
284
- }
285
-
286
- rc = sqlite3_bind_int64(res, 3, (time_t) context_data->version);
287
- if (unlikely(rc != SQLITE_OK)) {
288
- error_report("Failed to bind first_time_t to store context details");
289
- goto skip_store;
290
- }
291
-
292
- rc = bind_text_null(res, 4, context_data->title, 0);
293
- if (unlikely(rc != SQLITE_OK)) {
294
- error_report("Failed to bind context to store context details");
295
- goto skip_store;
296
- }
297
-
298
- rc = bind_text_null(res, 5, context_data->chart_type, 0);
299
- if (unlikely(rc != SQLITE_OK)) {
300
- error_report("Failed to bind context to store context details");
301
- goto skip_store;
302
- }
303
-
304
- rc = bind_text_null(res, 6, context_data->units, 0);
305
- if (unlikely(rc != SQLITE_OK)) {
306
- error_report("Failed to bind context to store context details");
307
- goto skip_store;
308
- }
309
-
310
- rc = sqlite3_bind_int64(res, 7, (time_t) context_data->priority);
311
- if (unlikely(rc != SQLITE_OK)) {
312
- error_report("Failed to bind first_time_t to store context details");
313
- goto skip_store;
314
- }
315
-
316
- rc = sqlite3_bind_int64(res, 8, (time_t) context_data->first_time_s);
317
- if (unlikely(rc != SQLITE_OK)) {
318
- error_report("Failed to bind first_time_t to store context details");
319
- goto skip_store;
320
- }
321
-
322
- rc = sqlite3_bind_int64(res, 9, (time_t) context_data->last_time_s);
323
- if (unlikely(rc != SQLITE_OK)) {
324
- error_report("Failed to bind last_time_t to store context details");
325
- goto skip_store;
326
- }
327
-
328
- rc = sqlite3_bind_int(res, 10, context_data->deleted);
329
- if (unlikely(rc != SQLITE_OK)) {
330
- error_report("Failed to bind deleted flag to store context details");
331
- goto skip_store;
332
- }
333
-
334
- rc = bind_text_null(res, 11, context_data->family, 1);
335
- if (unlikely(rc != SQLITE_OK)) {
336
- error_report("Failed to bind context to store details");
337
- goto skip_store;
338
- }
339
-
264
+ int param = 0;
265
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
266
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->id, 0));
267
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->version));
268
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->title, 0));
269
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->chart_type, 0));
270
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->units, 0));
271
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->priority));
272
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->first_time_s));
273
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->last_time_s));
274
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, context_data->deleted));
275
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->family, 1));
276
+
277
+ param = 0;
278
rc_stored = execute_insert(res);
279
280
if (rc_stored != SQLITE_DONE)
281
error_report("Failed store context details for context %s, rc = %d", context_data->id, rc_stored);
282
345
-skip_store:
346
- rc = sqlite3_finalize(res);
347
- if (rc != SQLITE_OK)
348
- error_report("Failed to finalize statement that stores context details, rc = %d", rc);
283
+done:
284
+ REPORT_BIND_FAIL(res, param);
285
+
286
+ SQLITE_FINALIZE(res);
287
288
return (rc_stored != SQLITE_DONE);
289
}
@@ -367,27 +305,20 @@ int ctx_delete_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
305
return 1;
306
}
307
370
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
371
- if (unlikely(rc != SQLITE_OK)) {
372
- error_report("Failed to bind host_id for context data deletion");
373
- goto skip_delete;
374
- }
375
-
376
- rc = sqlite3_bind_text(res, 2, context_data->id, -1, SQLITE_STATIC);
377
- if (unlikely(rc != SQLITE_OK)) {
378
- error_report("Failed to bind context id for context data deletion");
379
- goto skip_delete;
380
- }
308
+ int param = 0;
309
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
310
+ SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, context_data->id, -1, SQLITE_STATIC));
311
312
+ param = 0;
313
rc_stored = execute_insert(res);
314
315
if (rc_stored != SQLITE_DONE)
316
error_report("Failed to delete context %s, rc = %d", context_data->id, rc_stored);
317
387
-skip_delete:
388
- rc = sqlite3_finalize(res);
389
- if (rc != SQLITE_OK)
390
- error_report("Failed to finalize statement where deleting a context, rc = %d", rc);
318
+done:
319
+ REPORT_BIND_FAIL(res, param);
320
+
321
+ SQLITE_FINALIZE(res);
322
323
return (rc_stored != SQLITE_DONE);
324
}
src/database/sqlite/sqlite_db_migration.c
+6
-18
@@ -208,9 +208,7 @@ static int do_migration_v3_v4(sqlite3 *database)
208
freez(table);
209
}
210
211
- rc = sqlite3_finalize(res);
212
- if (unlikely(rc != SQLITE_OK))
213
- error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
211
+ SQLITE_FINALIZE(res);
212
213
return 0;
214
}
@@ -249,9 +247,7 @@ static int do_migration_v6_v7(sqlite3 *database)
247
freez(table);
248
}
249
252
- rc = sqlite3_finalize(res);
253
- if (unlikely(rc != SQLITE_OK))
254
- error_report("Failed to finalize statement when altering aclk_alert tables, rc = %d", rc);
250
+ SQLITE_FINALIZE(res);
251
252
return 0;
253
}
@@ -278,9 +274,7 @@ static int do_migration_v7_v8(sqlite3 *database)
274
freez(table);
275
}
276
281
- rc = sqlite3_finalize(res);
282
- if (unlikely(rc != SQLITE_OK))
283
- error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
277
+ SQLITE_FINALIZE(res);
278
279
return 0;
280
}
@@ -340,9 +334,7 @@ static int do_migration_v8_v9(sqlite3 *database)
334
freez(table);
335
}
336
343
- rc = sqlite3_finalize(res);
344
- if (unlikely(rc != SQLITE_OK))
345
- error_report("Failed to finalize statement when copying health_log tables, rc = %d", rc);
337
+ SQLITE_FINALIZE(res);
338
339
char *table = NULL;
340
dfe_start_read(dict_tables, table) {
@@ -407,9 +399,7 @@ static int do_migration_v14_v15(sqlite3 *database)
399
count++;
400
}
401
410
- rc = sqlite3_finalize(res);
411
- if (unlikely(rc != SQLITE_OK))
412
- error_report("Failed to finalize statement when dropping unused indices, rc = %d", rc);
402
+ SQLITE_FINALIZE(res);
403
404
if (count)
405
(void) db_execute(database, buffer_tostring(wb));
@@ -438,9 +428,7 @@ static int do_migration_v15_v16(sqlite3 *database)
428
count++;
429
}
430
441
- rc = sqlite3_finalize(res);
442
- if (unlikely(rc != SQLITE_OK))
443
- error_report("Failed to finalize statement when running ANALYZE on aclk_alert_tables, rc = %d", rc);
431
+ SQLITE_FINALIZE(res);
432
433
if (count)
434
(void) db_execute(database, buffer_tostring(wb));
src/database/sqlite/sqlite_functions.c
+1
-2
@@ -288,8 +288,7 @@ static int get_pragma_value(sqlite3 *database, const char *sql)
288
if (likely(rc == SQLITE_ROW))
289
result = sqlite3_column_int(res, 0);
290
291
- rc = sqlite3_finalize(res);
292
- (void) rc;
291
+ SQLITE_FINALIZE(res);
292
293
return result;
294
}
src/database/sqlite/sqlite_functions.h
+20
@@ -28,6 +28,26 @@ void analytics_set_data_str(char **name, const char *value);
28
} \
29
} while (0)
30
31
+#define SQLITE_FINALIZE(res) \
32
+ do { \
33
+ if ((res)) { \
34
+ int _rc = sqlite3_finalize((res)); \
35
+ if (_rc != SQLITE_OK) { \
36
+ nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to finalize statement rc=%d in %s", _rc, __FUNCTION__); \
37
+ } \
38
+ } \
39
+ } while (0)
40
+
41
+#define SQLITE_RESET(res) \
42
+ do { \
43
+ if ((res)) { \
44
+ int _rc = sqlite3_reset((res)); \
45
+ if (_rc != SQLITE_OK) { \
46
+ nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to reset statement rc=%d in %s", _rc, __FUNCTION__); \
47
+ } \
48
+ } \
49
+ } while (0)
50
+
51
#define SQL_MAX_RETRY (100)
52
#define SQLITE_INSERT_DELAY (10) // Insert delay in case of lock
53
src/database/sqlite/sqlite_health.c
+43
-107
@@ -61,8 +61,7 @@ static void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae)
61
done:
62
REPORT_BIND_FAIL(res, param);
63
64
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
65
- error_report("HEALTH [%s]: Failed to reset statement for updating health log.", rrdhost_hostname(host));
64
+ SQLITE_RESET(res);
65
}
66
67
/* Health related SQL queries
@@ -128,8 +127,7 @@ static void sql_health_alarm_log_insert_detail(RRDHOST *host, uint64_t health_lo
127
done:
128
REPORT_BIND_FAIL(res, param);
129
131
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
132
- error_report("HEALTH [%s]: Failed to reset statement for inserting to health log detail", rrdhost_hostname(host));
130
+ SQLITE_RESET(res);
131
}
132
133
#define SQL_INSERT_HEALTH_LOG \
@@ -184,8 +182,7 @@ static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae)
182
done:
183
REPORT_BIND_FAIL(res, param);
184
187
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
188
- error_report("HEALTH [%s]: Failed to reset statement for inserting to health log", rrdhost_hostname(host));
185
+ SQLITE_RESET(res);
186
}
187
188
void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
@@ -271,9 +268,7 @@ void sql_health_alarm_log_cleanup(RRDHOST *host, bool claimed) {
268
done:
269
REPORT_BIND_FAIL(res, param);
270
274
- rc = sqlite3_finalize(res);
275
- if (unlikely(rc != SQLITE_OK))
276
- error_report("Failed to finalize the prepared statement to cleanup health log detail table (claimed)");
271
+ SQLITE_FINALIZE(res);
272
}
273
274
#define SQL_INJECT_REMOVED \
@@ -314,8 +309,7 @@ bool sql_update_removed_in_health_log(RRDHOST *host, uint32_t alarm_id, uuid_t *
309
done:
310
REPORT_BIND_FAIL(res, param);
311
317
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
318
- error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
312
+ SQLITE_FINALIZE(res);
313
314
return (param == 0 && rc == SQLITE_DONE);
315
}
@@ -345,8 +339,7 @@ bool sql_update_removed_in_health_log_detail(uint32_t unique_id, uint32_t max_un
339
done:
340
REPORT_BIND_FAIL(res, param);
341
348
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
349
- error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
342
+ SQLITE_FINALIZE(res);
343
344
return (param == 0 && rc == SQLITE_DONE);
345
}
@@ -398,8 +391,7 @@ void sql_inject_removed_status(
391
done:
392
REPORT_BIND_FAIL(res, param);
393
401
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
402
- error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
394
+ SQLITE_FINALIZE(res);
395
}
396
397
#define SQL_SELECT_MAX_UNIQUE_ID \
@@ -422,7 +414,7 @@ uint32_t sql_get_max_unique_id (RRDHOST *host)
414
rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
415
if (unlikely(rc != SQLITE_OK)) {
416
error_report("Failed to bind host_id parameter for SQL_SELECT_MAX_UNIQUE_ID.");
425
- sqlite3_finalize(res);
417
+ SQLITE_FINALIZE(res);
418
return 0;
419
}
420
@@ -430,9 +422,7 @@ uint32_t sql_get_max_unique_id (RRDHOST *host)
422
max_unique_id = (uint32_t) sqlite3_column_int64(res, 0);
423
}
424
433
- rc = sqlite3_finalize(res);
434
- if (unlikely(rc != SQLITE_OK))
435
- error_report("Failed to finalize the statement");
425
+ SQLITE_FINALIZE(res);
426
427
return max_unique_id;
428
}
@@ -457,7 +447,7 @@ void sql_check_removed_alerts_state(RRDHOST *host)
447
rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
448
if (unlikely(rc != SQLITE_OK)) {
449
error_report("Failed to bind host_id parameter for SQL_SELECT_LAST_STATUSES.");
460
- sqlite3_finalize(res);
450
+ SQLITE_FINALIZE(res);
451
return;
452
}
453
@@ -479,9 +469,7 @@ void sql_check_removed_alerts_state(RRDHOST *host)
469
}
470
}
471
482
- rc = sqlite3_finalize(res);
483
- if (unlikely(rc != SQLITE_OK))
484
- error_report("Failed to finalize the statement");
472
+ SQLITE_FINALIZE(res);
473
}
474
475
#define SQL_DELETE_MISSING_CHART_ALERT \
@@ -506,7 +494,7 @@ static void sql_remove_alerts_from_deleted_charts(RRDHOST *host, uuid_t *host_id
494
495
if (unlikely(ret != SQLITE_OK)) {
496
error_report("Failed to bind host_id parameter for sql_remove_alerts_from_deleted_charts.");
509
- sqlite3_finalize(res);
497
+ SQLITE_FINALIZE(res);
498
return;
499
}
500
@@ -514,9 +502,7 @@ static void sql_remove_alerts_from_deleted_charts(RRDHOST *host, uuid_t *host_id
502
if (ret != SQLITE_DONE)
503
error_report("Failed to execute command to delete missing charts from health_log");
504
517
- ret = sqlite3_finalize(res);
518
- if (unlikely(ret != SQLITE_OK))
519
- error_report("Failed to finalize statement when deleting missing charts from health_log");
505
+ SQLITE_FINALIZE(res);
506
}
507
508
static int clean_host_alerts(void *data, int argc, char **argv, char **column)
@@ -589,7 +575,7 @@ void sql_health_alarm_log_load(RRDHOST *host)
575
ret = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
576
if (unlikely(ret != SQLITE_OK)) {
577
error_report("Failed to bind host_id parameter for SQL_LOAD_HEALTH_LOG.");
592
- sqlite3_finalize(res);
578
+ SQLITE_FINALIZE(res);
579
return;
580
}
581
@@ -737,9 +723,7 @@ void sql_health_alarm_log_load(RRDHOST *host)
723
"[%s]: Table health_log, loaded %zd alarm entries, errors in %zd entries.",
724
rrdhost_hostname(host), loaded, errored);
725
740
- ret = sqlite3_finalize(res);
741
- if (unlikely(ret != SQLITE_OK))
742
- error_report("Failed to finalize the health log read statement");
726
+ SQLITE_FINALIZE(res);
727
}
728
729
/*
@@ -873,8 +857,7 @@ void sql_alert_store_config(RRD_ALERT_PROTOTYPE *ap __maybe_unused)
857
done:
858
REPORT_BIND_FAIL(res, param);
859
buffer_free(buf);
876
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
877
- error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
860
+ SQLITE_RESET(res);
861
}
862
863
#define SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT \
@@ -924,11 +907,8 @@ int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_S
907
}
908
909
done:
927
- rc = sqlite3_finalize(res);
928
- if (unlikely(rc != SQLITE_OK))
929
- error_report("Failed to finalize the statement.");
930
-
931
- return ret;
910
+ SQLITE_FINALIZE(res);
911
+ return ret;
912
}
913
914
#define SQL_SELECT_HEALTH_LOG \
@@ -1084,9 +1064,7 @@ void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, time_t after, const ch
1064
buffer_json_finalize(wb);
1065
1066
finish:
1087
- rc = sqlite3_reset(stmt_query);
1088
- if (unlikely(rc != SQLITE_OK))
1089
- error_report("Failed to reset statement for SQL_SELECT_HEALTH_LOG");
1067
+ SQLITE_RESET(stmt_query);
1068
}
1069
1070
#define SQL_COPY_HEALTH_LOG(table) "INSERT OR IGNORE INTO health_log (host_id, alarm_id, config_hash_id, name, chart, family, exec, recipient, units, chart_context) SELECT ?1, alarm_id, config_hash_id, name, chart, family, exec, recipient, units, chart_context from %s", table
@@ -1125,9 +1103,7 @@ int health_migrate_old_health_log_table(char *table) {
1103
1104
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
1105
if (unlikely(rc != SQLITE_OK)) {
1128
- rc = sqlite3_finalize(res);
1129
- if (unlikely(rc != SQLITE_OK))
1130
- error_report("Failed to reset statement to copy health log table, rc = %d", rc);
1106
+ SQLITE_FINALIZE(res);
1107
freez(uuid_from_table);
1108
return 0;
1109
}
@@ -1135,9 +1111,7 @@ int health_migrate_old_health_log_table(char *table) {
1111
rc = execute_insert(res);
1112
if (unlikely(rc != SQLITE_DONE)) {
1113
error_report("Failed to execute SQL_COPY_HEALTH_LOG, rc = %d", rc);
1138
- rc = sqlite3_finalize(res);
1139
- if (unlikely(rc != SQLITE_OK))
1140
- error_report("Failed to reset statement to copy health log table, rc = %d", rc);
1114
+ SQLITE_FINALIZE(res);
1115
freez(uuid_from_table);
1116
}
1117
@@ -1151,18 +1125,14 @@ int health_migrate_old_health_log_table(char *table) {
1125
1126
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
1127
if (unlikely(rc != SQLITE_OK)) {
1154
- rc = sqlite3_finalize(res);
1155
- if (unlikely(rc != SQLITE_OK))
1156
- error_report("Failed to reset statement to copy health log detail, rc = %d", rc);
1128
+ SQLITE_FINALIZE(res);
1129
return 0;
1130
}
1131
1132
rc = execute_insert(res);
1133
if (unlikely(rc != SQLITE_DONE)) {
1134
error_report("Failed to execute SQL_COPY_HEALTH_LOG_DETAIL, rc = %d", rc);
1163
- rc = sqlite3_finalize(res);
1164
- if (unlikely(rc != SQLITE_OK))
1165
- error_report("Failed to reset statement to copy health log detail table, rc = %d", rc);
1135
+ SQLITE_FINALIZE(res);
1136
return 0;
1137
}
1138
@@ -1176,9 +1146,7 @@ int health_migrate_old_health_log_table(char *table) {
1146
rc = execute_insert(res);
1147
if (unlikely(rc != SQLITE_DONE)) {
1148
error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_DETAIL_TRANSITION_ID, rc = %d", rc);
1179
- rc = sqlite3_finalize(res);
1180
- if (unlikely(rc != SQLITE_OK))
1181
- error_report("Failed to reset statement to update health log detail table with transition ids, rc = %d", rc);
1149
+ SQLITE_FINALIZE(res);
1150
return 0;
1151
}
1152
@@ -1191,26 +1159,20 @@ int health_migrate_old_health_log_table(char *table) {
1159
1160
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
1161
if (unlikely(rc != SQLITE_OK)) {
1194
- rc = sqlite3_finalize(res);
1195
- if (unlikely(rc != SQLITE_OK))
1196
- error_report("Failed to reset statement to update health log detail with health log ids, rc = %d", rc);
1162
+ SQLITE_FINALIZE(res);
1163
return 0;
1164
}
1165
1166
rc = sqlite3_bind_blob(res, 2, &uuid, sizeof(uuid), SQLITE_STATIC);
1167
if (unlikely(rc != SQLITE_OK)) {
1202
- rc = sqlite3_finalize(res);
1203
- if (unlikely(rc != SQLITE_OK))
1204
- error_report("Failed to reset statement to update health log detail with health log ids, rc = %d", rc);
1168
+ SQLITE_FINALIZE(res);
1169
return 0;
1170
}
1171
1172
rc = execute_insert(res);
1173
if (unlikely(rc != SQLITE_DONE)) {
1174
error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_DETAIL_HEALTH_LOG_ID, rc = %d", rc);
1211
- rc = sqlite3_finalize(res);
1212
- if (unlikely(rc != SQLITE_OK))
1213
- error_report("Failed to reset statement to update health log detail table with health log ids, rc = %d", rc);
1175
+ SQLITE_FINALIZE(res);
1176
}
1177
1178
//update last transition id
@@ -1222,18 +1184,14 @@ int health_migrate_old_health_log_table(char *table) {
1184
1185
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
1186
if (unlikely(rc != SQLITE_OK)) {
1225
- rc = sqlite3_finalize(res);
1226
- if (unlikely(rc != SQLITE_OK))
1227
- error_report("Failed to reset statement to update health log with last transition id, rc = %d", rc);
1187
+ SQLITE_FINALIZE(res);
1188
return 0;
1189
}
1190
1191
rc = execute_insert(res);
1192
if (unlikely(rc != SQLITE_DONE)) {
1193
error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_LAST_TRANSITION_ID, rc = %d", rc);
1234
- rc = sqlite3_finalize(res);
1235
- if (unlikely(rc != SQLITE_OK))
1236
- error_report("Failed to reset statement to update health log table with last transition id, rc = %d", rc);
1194
+ SQLITE_FINALIZE(res);
1195
}
1196
1197
return 1;
@@ -1257,14 +1215,14 @@ static uint32_t get_next_alarm_event_id(uint64_t health_log_id, uint32_t alarm_i
1215
rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
1216
if (unlikely(rc != SQLITE_OK)) {
1217
error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID.");
1260
- sqlite3_finalize(res);
1218
+ SQLITE_FINALIZE(res);
1219
return alarm_id;
1220
}
1221
1222
rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
1223
if (unlikely(rc != SQLITE_OK)) {
1224
error_report("Failed to bind char parameter for SQL_GET_EVENT_ID.");
1267
- sqlite3_finalize(res);
1225
+ SQLITE_FINALIZE(res);
1226
return alarm_id;
1227
}
1228
@@ -1272,9 +1230,7 @@ static uint32_t get_next_alarm_event_id(uint64_t health_log_id, uint32_t alarm_i
1230
next_event_id = (uint32_t) sqlite3_column_int64(res, 0);
1231
}
1232
1275
- rc = sqlite3_finalize(res);
1276
- if (unlikely(rc != SQLITE_OK))
1277
- error_report("Failed to finalize the statement while getting an alarm id.");
1233
+ SQLITE_FINALIZE(res);
1234
1235
return next_event_id;
1236
}
@@ -1298,21 +1254,21 @@ uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *
1254
rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
1255
if (unlikely(rc != SQLITE_OK)) {
1256
error_report("Failed to bind host_id parameter for SQL_GET_ALARM_ID.");
1301
- sqlite3_finalize(res);
1257
+ SQLITE_FINALIZE(res);
1258
return alarm_id;
1259
}
1260
1261
rc = SQLITE3_BIND_STRING_OR_NULL(res, 2, chart);
1262
if (unlikely(rc != SQLITE_OK)) {
1263
error_report("Failed to bind char parameter for SQL_GET_ALARM_ID.");
1308
- sqlite3_finalize(res);
1264
+ SQLITE_FINALIZE(res);
1265
return alarm_id;
1266
}
1267
1268
rc = SQLITE3_BIND_STRING_OR_NULL(res, 3, name);
1269
if (unlikely(rc != SQLITE_OK)) {
1270
error_report("Failed to bind name parameter for SQL_GET_ALARM_ID.");
1315
- sqlite3_finalize(res);
1271
+ SQLITE_FINALIZE(res);
1272
return alarm_id;
1273
}
1274
@@ -1321,9 +1277,7 @@ uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *
1277
health_log_id = (uint64_t) sqlite3_column_int64(res, 1);
1278
}
1279
1324
- rc = sqlite3_finalize(res);
1325
- if (unlikely(rc != SQLITE_OK))
1326
- error_report("Failed to finalize the statement while getting an alarm id.");
1280
+ SQLITE_FINALIZE(res);
1281
1282
if (alarm_id)
1283
*next_event_id = get_next_alarm_event_id(health_log_id, alarm_id);
@@ -1373,9 +1327,7 @@ bool sql_find_alert_transition(
1327
done:
1328
REPORT_BIND_FAIL(res, param);
1329
1376
- rc = sqlite3_reset(res);
1377
- if (unlikely(rc != SQLITE_OK))
1378
- error_report("Failed to reset the statement when trying to find transition");
1330
+ SQLITE_RESET(res);
1331
1332
return ok;
1333
}
@@ -1467,17 +1419,11 @@ void sql_alert_transitions(
1419
if (rc != SQLITE_DONE)
1420
error_report("Error while populating temp table");
1421
1470
- rc = sqlite3_reset(res);
1471
- if (rc != SQLITE_OK)
1472
- error_report("Error while resetting parameters");
1422
+ SQLITE_RESET(res);
1423
}
1424
dfe_done(t);
1425
1476
- rc = sqlite3_finalize(res);
1477
- if (unlikely(rc != SQLITE_OK)) {
1478
- // log error but continue
1479
- error_report("Failed to finalize statement for sql_alert_transitions temp table population");
1480
- }
1426
+ SQLITE_FINALIZE(res);
1427
1428
command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
1429
@@ -1550,9 +1496,7 @@ run_query:;
1496
done:
1497
REPORT_BIND_FAIL(res, param);
1498
1553
- rc = sqlite3_finalize(res);
1554
- if (unlikely(rc != SQLITE_OK))
1555
- error_report("Failed to finalize statement for sql_alert_transitions");
1499
+ SQLITE_FINALIZE(res);
1500
1501
done_only_drop:
1502
if (likely(!transition)) {
@@ -1616,17 +1560,11 @@ int sql_get_alert_configuration(
1560
if (rc != SQLITE_DONE)
1561
error_report("Error while populating temp table");
1562
1619
- rc = sqlite3_reset(res);
1620
- if (rc != SQLITE_OK)
1621
- error_report("Error while resetting parameters");
1563
+ SQLITE_RESET(res);
1564
}
1565
dfe_done(t);
1566
1625
- rc = sqlite3_finalize(res);
1626
- if (unlikely(rc != SQLITE_OK)) {
1627
- // log error but continue
1628
- error_report("Failed to finalize statement for sql_get_alert_configuration temp table population");
1629
- }
1567
+ SQLITE_FINALIZE(res);
1568
1569
command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
1570
@@ -1685,9 +1623,7 @@ int sql_get_alert_configuration(
1623
added++;
1624
}
1625
1688
- rc = sqlite3_finalize(res);
1689
- if (unlikely(rc != SQLITE_OK))
1690
- error_report("Failed to finalize statement for sql_get_alert_configuration");
1626
+ SQLITE_FINALIZE(res);
1627
1628
fail_only_drop:
1629
(void)snprintfz(sql, sizeof(sql) - 1, "DROP TABLE IF EXISTS c_%p", configs);
src/database/sqlite/sqlite_metadata.c
+149
-329
@@ -286,27 +286,19 @@ int update_node_id(uuid_t *host_id, uuid_t *node_id)
286
return 1;
287
}
288
289
- rc = sqlite3_bind_blob(res, 1, node_id, sizeof(*node_id), SQLITE_STATIC);
290
- if (unlikely(rc != SQLITE_OK)) {
291
- error_report("Failed to bind host_id parameter to store node instance information");
292
- goto failed;
293
- }
294
-
295
- rc = sqlite3_bind_blob(res, 2, host_id, sizeof(*host_id), SQLITE_STATIC);
296
- if (unlikely(rc != SQLITE_OK)) {
297
- error_report("Failed to bind host_id parameter to store node instance information");
298
- goto failed;
299
- }
289
+ int param = 0;
290
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, node_id, sizeof(*node_id), SQLITE_STATIC));
291
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
292
293
+ param = 0;
294
rc = execute_insert(res);
295
if (unlikely(rc != SQLITE_DONE))
296
error_report("Failed to store node instance information, rc = %d", rc);
297
rc = sqlite3_changes(db_meta);
298
306
-failed:
307
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
308
- error_report("Failed to finalize the prepared statement when storing node instance information");
309
-
299
+done:
300
+ REPORT_BIND_FAIL(res, param);
301
+ SQLITE_FINALIZE(res);
302
return rc - 1;
303
}
304
@@ -329,19 +321,18 @@ int get_node_id(uuid_t *host_id, uuid_t *node_id)
321
return 1;
322
}
323
332
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
333
- if (unlikely(rc != SQLITE_OK)) {
334
- error_report("Failed to bind host_id parameter to select node instance information");
335
- goto failed;
336
- }
324
+ int param = 0;
325
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
326
+
327
328
+ param = 0;
329
rc = sqlite3_step_monitored(res);
330
if (likely(rc == SQLITE_ROW && node_id))
331
uuid_copy(*node_id, *((uuid_t *) sqlite3_column_blob(res, 0)));
332
342
-failed:
343
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
344
- error_report("Failed to finalize the prepared statement when selecting node instance information");
333
+done:
334
+ REPORT_BIND_FAIL(res, param);
335
+ SQLITE_FINALIZE(res);
336
337
return (rc == SQLITE_ROW) ? 0 : -1;
338
}
@@ -367,29 +358,22 @@ void invalidate_node_instances(uuid_t *host_id, uuid_t *claim_id)
358
return;
359
}
360
370
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
371
- if (unlikely(rc != SQLITE_OK)) {
372
- error_report("Failed to bind host_id parameter to invalidate node instance information");
373
- goto failed;
374
- }
361
+ int param = 0;
362
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
363
364
if (claim_id)
377
- rc = sqlite3_bind_blob(res, 2, claim_id, sizeof(*claim_id), SQLITE_STATIC);
365
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, claim_id, sizeof(*claim_id), SQLITE_STATIC));
366
else
379
- rc = sqlite3_bind_null(res, 2);
380
-
381
- if (unlikely(rc != SQLITE_OK)) {
382
- error_report("Failed to bind claim_id parameter to invalidate node instance information");
383
- goto failed;
384
- }
367
+ SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
368
369
+ param = 0;
370
rc = execute_insert(res);
371
if (unlikely(rc != SQLITE_DONE))
372
error_report("Failed to invalidate node instance information, rc = %d", rc);
373
390
-failed:
391
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
392
- error_report("Failed to finalize the prepared statement when invalidating node instance information");
374
+done:
375
+ REPORT_BIND_FAIL(res, param);
376
+ SQLITE_FINALIZE(res);
377
}
378
379
#define SQL_GET_NODE_INSTANCE_LIST \
@@ -437,15 +421,18 @@ struct node_instance_list *get_node_list(void)
421
RRDHOST *host = rrdhost_find_by_guid(host_guid);
422
if (!host)
423
continue;
424
+
425
if (rrdhost_flag_check(host, RRDHOST_FLAG_PENDING_CONTEXT_LOAD)) {
441
- netdata_log_info("ACLK: 'host:%s' skipping get node list because context is initializing", rrdhost_hostname(host));
426
+ netdata_log_info(
427
+ "ACLK: 'host:%s' skipping get node list because context is initializing", rrdhost_hostname(host));
428
continue;
429
}
430
+
431
uuid_copy(node_list[row].host_id, *host_id);
432
node_list[row].queryable = 1;
446
- node_list[row].live = (host && (host == localhost || host->receiver
447
- || !(rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)))) ? 1 : 0;
448
- node_list[row].hops = (host && host->system_info) ? host->system_info->hops :
433
+ node_list[row].live =
434
+ (host == localhost || host->receiver || !(rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN))) ? 1 : 0;
435
+ node_list[row].hops = host->system_info ? host->system_info->hops :
436
uuid_memcmp(host_id, &localhost->host_uuid) ? 1 : 0;
437
node_list[row].hostname =
438
sqlite3_column_bytes(res, 2) ? strdupz((char *)sqlite3_column_text(res, 2)) : NULL;
@@ -457,8 +444,7 @@ struct node_instance_list *get_node_list(void)
444
rrd_unlock();
445
446
failed:
460
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
461
- error_report("Failed to finalize the prepared statement when fetching node instance information");
447
+ SQLITE_FINALIZE(res);
448
449
return node_list;
450
}
@@ -482,12 +468,10 @@ void sql_load_node_id(RRDHOST *host)
468
return;
469
}
470
485
- rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
486
- if (unlikely(rc != SQLITE_OK)) {
487
- error_report("Failed to bind host_id parameter to load node instance information");
488
- goto failed;
489
- }
471
+ int param = 0;
472
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC));
473
474
+ param = 0;
475
rc = sqlite3_step_monitored(res);
476
if (likely(rc == SQLITE_ROW)) {
477
if (likely(sqlite3_column_bytes(res, 0) == sizeof(uuid_t)))
@@ -496,9 +480,9 @@ void sql_load_node_id(RRDHOST *host)
480
set_host_node_id(host, NULL);
481
}
482
499
-failed:
500
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
501
- error_report("Failed to finalize the prepared statement when loading node instance information");
483
+done:
484
+ REPORT_BIND_FAIL(res, param);
485
+ SQLITE_FINALIZE(res);
486
}
487
488
#define SELECT_HOST_INFO "SELECT system_key, system_value FROM host_info WHERE host_id = @host_id"
@@ -515,20 +499,18 @@ void sql_build_host_system_info(uuid_t *host_id, struct rrdhost_system_info *sys
499
return;
500
}
501
518
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
519
- if (unlikely(rc != SQLITE_OK)) {
520
- error_report("Failed to bind host parameter host information");
521
- goto skip;
522
- }
502
+ int param = 0;
503
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
504
505
+ param = 0;
506
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
525
- rrdhost_set_system_info_variable(system_info, (char *) sqlite3_column_text(res, 0),
526
- (char *) sqlite3_column_text(res, 1));
507
+ rrdhost_set_system_info_variable(
508
+ system_info, (char *)sqlite3_column_text(res, 0), (char *)sqlite3_column_text(res, 1));
509
}
510
529
-skip:
530
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
531
- error_report("Failed to finalize the prepared statement when reading host information");
511
+done:
512
+ REPORT_BIND_FAIL(res, param);
513
+ SQLITE_FINALIZE(res);
514
}
515
516
#define SELECT_HOST_LABELS "SELECT label_key, label_value, source_type FROM host_label WHERE host_id = @host_id " \
@@ -547,21 +529,23 @@ RRDLABELS *sql_load_host_labels(uuid_t *host_id)
529
return NULL;
530
}
531
550
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
551
- if (unlikely(rc != SQLITE_OK)) {
552
- error_report("Failed to bind host parameter host information");
553
- goto skip;
554
- }
532
+ int param = 0;
533
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
534
535
+ param = 0;
536
labels = rrdlabels_create();
537
538
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
559
- rrdlabels_add(labels, (const char *)sqlite3_column_text(res, 0), (const char *)sqlite3_column_text(res, 1), sqlite3_column_int(res, 2));
539
+ rrdlabels_add(
540
+ labels,
541
+ (const char *)sqlite3_column_text(res, 0),
542
+ (const char *)sqlite3_column_text(res, 1),
543
+ sqlite3_column_int(res, 2));
544
}
545
562
-skip:
563
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
564
- error_report("Failed to finalize the prepared statement when reading host information");
546
+done:
547
+ REPORT_BIND_FAIL(res, param);
548
+ SQLITE_FINALIZE(res);
549
return labels;
550
}
551
@@ -576,22 +560,19 @@ static int exec_statement_with_uuid(const char *sql, uuid_t *uuid)
560
return 1;
561
}
562
579
- rc = sqlite3_bind_blob(res, 1, uuid, sizeof(*uuid), SQLITE_STATIC);
580
- if (unlikely(rc != SQLITE_OK)) {
581
- error_report("Failed to bind UUID parameter to %s, rc = %d", sql, rc);
582
- goto skip;
583
- }
563
+ int param = 0;
564
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, uuid, sizeof(*uuid), SQLITE_STATIC));
565
566
+ param = 0;
567
rc = execute_insert(res);
568
if (likely(rc == SQLITE_DONE))
569
result = SQLITE_OK;
570
else
571
error_report("Failed to execute %s, rc = %d", sql, rc);
572
591
-skip:
592
- rc = sqlite3_finalize(res);
593
- if (unlikely(rc != SQLITE_OK))
594
- error_report("Failed to finalize statement %s, rc = %d", sql, rc);
573
+done:
574
+ REPORT_BIND_FAIL(res, param);
575
+ SQLITE_FINALIZE(res);
576
return result;
577
}
578
@@ -812,8 +793,7 @@ static void delete_host_chart_labels(uuid_t *host_uuid)
793
error_report("Failed to execute command to remove host chart labels");
794
795
failed:
815
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
816
- error_report("Failed to finalize statement to remove host chart labels");
796
+ SQLITE_FINALIZE(res);
797
}
798
799
static int host_label_store_to_sql_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
@@ -907,28 +887,22 @@ static int store_claim_id(uuid_t *host_id, uuid_t *claim_id)
887
return 1;
888
}
889
910
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
911
- if (unlikely(rc != SQLITE_OK)) {
912
- error_report("Failed to bind host_id parameter to store claim id");
913
- goto failed;
914
- }
890
+ int param = 0;
891
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
892
893
if (claim_id)
917
- rc = sqlite3_bind_blob(res, 2, claim_id, sizeof(*claim_id), SQLITE_STATIC);
894
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param,claim_id, sizeof(*claim_id), SQLITE_STATIC));
895
else
919
- rc = sqlite3_bind_null(res, 2);
920
- if (unlikely(rc != SQLITE_OK)) {
921
- error_report("Failed to bind claim_id parameter to host claim id");
922
- goto failed;
923
- }
896
+ SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
897
898
+ param = 0;
899
rc = execute_insert(res);
900
if (unlikely(rc != SQLITE_DONE))
901
error_report("Failed to store host claim id rc = %d", rc);
902
929
-failed:
930
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
931
- error_report("Failed to finalize the prepared statement when storing a host claim id");
903
+done:
904
+ REPORT_BIND_FAIL(res, param);
905
+ SQLITE_FINALIZE(res);
906
907
return rc != SQLITE_DONE;
908
}
@@ -955,9 +929,7 @@ static void delete_dimension_uuid(uuid_t *dimension_uuid, sqlite3_stmt **action_
929
error_report("Failed to delete dimension uuid, rc = %d", rc);
930
931
skip_execution:
958
- rc = sqlite3_reset(res);
959
- if (unlikely(rc != SQLITE_OK))
960
- error_report("Failed to reset statement when deleting dimension UUID, rc = %d", rc);
932
+ SQLITE_RESET(res);
933
}
934
935
//
@@ -975,85 +947,36 @@ static int store_host_metadata(RRDHOST *host)
947
}
948
}
949
978
- rc = sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
979
- if (unlikely(rc != SQLITE_OK))
980
- goto bind_fail;
981
-
982
- rc = bind_text_null(res, ++param, rrdhost_hostname(host), 0);
983
- if (unlikely(rc != SQLITE_OK))
984
- goto bind_fail;
985
-
986
- rc = bind_text_null(res, ++param, rrdhost_registry_hostname(host), 1);
987
- if (unlikely(rc != SQLITE_OK))
988
- goto bind_fail;
989
-
990
- rc = sqlite3_bind_int(res, ++param, host->rrd_update_every);
991
- if (unlikely(rc != SQLITE_OK))
992
- goto bind_fail;
993
-
994
- rc = bind_text_null(res, ++param, rrdhost_os(host), 1);
995
- if (unlikely(rc != SQLITE_OK))
996
- goto bind_fail;
997
-
998
- rc = bind_text_null(res, ++param, rrdhost_timezone(host), 1);
999
- if (unlikely(rc != SQLITE_OK))
1000
- goto bind_fail;
1001
-
1002
- // TODO: remove/migrate
1003
- rc = bind_text_null(res, ++param, "", 1);
1004
- if (unlikely(rc != SQLITE_OK))
1005
- goto bind_fail;
1006
-
1007
- rc = sqlite3_bind_int(res, ++param, host->system_info ? host->system_info->hops : 0);
1008
- if (unlikely(rc != SQLITE_OK))
1009
- goto bind_fail;
1010
-
1011
- rc = sqlite3_bind_int(res, ++param, host->rrd_memory_mode);
1012
- if (unlikely(rc != SQLITE_OK))
1013
- goto bind_fail;
1014
-
1015
- rc = bind_text_null(res, ++param, rrdhost_abbrev_timezone(host), 1);
1016
- if (unlikely(rc != SQLITE_OK))
1017
- goto bind_fail;
1018
-
1019
- rc = sqlite3_bind_int(res, ++param, host->utc_offset);
1020
- if (unlikely(rc != SQLITE_OK))
1021
- goto bind_fail;
1022
-
1023
- rc = bind_text_null(res, ++param, rrdhost_program_name(host), 1);
1024
- if (unlikely(rc != SQLITE_OK))
1025
- goto bind_fail;
1026
-
1027
- rc = bind_text_null(res, ++param, rrdhost_program_version(host), 1);
1028
- if (unlikely(rc != SQLITE_OK))
1029
- goto bind_fail;
1030
-
1031
- rc = sqlite3_bind_int64(res, ++param, host->rrd_history_entries);
1032
- if (unlikely(rc != SQLITE_OK))
1033
- goto bind_fail;
1034
-
1035
- rc = sqlite3_bind_int(res, ++param, (int ) host->health.health_enabled);
1036
- if (unlikely(rc != SQLITE_OK))
1037
- goto bind_fail;
1038
-
1039
- rc = sqlite3_bind_int64(res, ++param, (sqlite3_int64) host->last_connected);
1040
- if (unlikely(rc != SQLITE_OK))
1041
- goto bind_fail;
950
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC));
951
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_hostname(host), 0));
952
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_registry_hostname(host), 1));
953
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, host->rrd_update_every));
954
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_os(host), 1));
955
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_timezone(host), 1));
956
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, "", 1));
957
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, host->system_info ? host->system_info->hops : 0));
958
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, host->rrd_memory_mode));
959
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_abbrev_timezone(host), 1));
960
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, host->utc_offset));
961
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_program_name(host), 1));
962
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_program_version(host), 1));
963
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int64(res, ++param, host->rrd_history_entries));
964
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int ) host->health.health_enabled));
965
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int64(res, ++param, (sqlite3_int64) host->last_connected));
966
967
int store_rc = sqlite3_step_monitored(res);
968
+
969
if (unlikely(store_rc != SQLITE_DONE))
970
error_report("Failed to store host %s, rc = %d", rrdhost_hostname(host), rc);
971
1047
- rc = sqlite3_reset(res);
1048
- if (unlikely(rc != SQLITE_OK))
1049
- error_report("Failed to reset statement to store host %s, rc = %d", rrdhost_hostname(host), rc);
972
+ SQLITE_RESET(res);
973
974
return store_rc != SQLITE_DONE;
975
+
976
bind_fail:
1053
- error_report("Failed to bind %d parameter to store host %s, rc = %d", param, rrdhost_hostname(host), rc);
1054
- rc = sqlite3_reset(res);
1055
- if (unlikely(rc != SQLITE_OK))
1056
- error_report("Failed to reset statement to store host %s, rc = %d", rrdhost_hostname(host), rc);
977
+ REPORT_BIND_FAIL(res, param);
978
+
979
+ SQLITE_RESET(res);
980
return 1;
981
}
982
@@ -1077,32 +1000,22 @@ static int add_host_sysinfo_key_value(const char *name, const char *value, uuid_
1000
}
1001
}
1002
1080
- rc = sqlite3_bind_blob(res, ++param, uuid, sizeof(*uuid), SQLITE_STATIC);
1081
- if (unlikely(rc != SQLITE_OK))
1082
- goto bind_fail;
1083
-
1084
- rc = bind_text_null(res, ++param, name, 0);
1085
- if (unlikely(rc != SQLITE_OK))
1086
- goto bind_fail;
1087
-
1088
- rc = bind_text_null(res, ++param, value ? value : "unknown", 0);
1089
- if (unlikely(rc != SQLITE_OK))
1090
- goto bind_fail;
1003
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_blob(res, ++param, uuid, sizeof(*uuid), SQLITE_STATIC));
1004
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, name, 0));
1005
+ SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, value ? value : "unknown", 0));
1006
1007
int store_rc = sqlite3_step_monitored(res);
1008
if (unlikely(store_rc != SQLITE_DONE))
1009
error_report("Failed to store host info value %s, rc = %d", name, rc);
1010
1096
- rc = sqlite3_reset(res);
1097
- if (unlikely(rc != SQLITE_OK))
1098
- error_report("Failed to reset statement to store host info value %s, rc = %d", name, rc);
1011
+ SQLITE_RESET(res);
1012
1013
return store_rc == SQLITE_DONE;
1014
+
1015
bind_fail:
1102
- error_report("Failed to bind %d parameter to store host info values %s, rc = %d", param, name, rc);
1103
- rc = sqlite3_reset(res);
1104
- if (unlikely(rc != SQLITE_OK))
1105
- error_report("Failed to reset statement to store host info values %s, rc = %d", name, rc);
1016
+ REPORT_BIND_FAIL(res, param);
1017
+
1018
+ SQLITE_RESET(res);
1019
return 0;
1020
}
1021
@@ -1161,89 +1074,41 @@ static int store_chart_metadata(RRDSET *st)
1074
}
1075
}
1076
1164
- rc = sqlite3_bind_blob(res, ++param, &st->chart_uuid, sizeof(st->chart_uuid), SQLITE_STATIC);
1165
- if (unlikely(rc != SQLITE_OK))
1166
- goto bind_fail;
1167
-
1168
- rc = sqlite3_bind_blob(res, ++param, &st->rrdhost->host_uuid, sizeof(st->rrdhost->host_uuid), SQLITE_STATIC);
1169
- if (unlikely(rc != SQLITE_OK))
1170
- goto bind_fail;
1171
-
1172
- rc = sqlite3_bind_text(res, ++param, string2str(st->parts.type), -1, SQLITE_STATIC);
1173
- if (unlikely(rc != SQLITE_OK))
1174
- goto bind_fail;
1175
-
1176
- rc = sqlite3_bind_text(res, ++param, string2str(st->parts.id), -1, SQLITE_STATIC);
1177
- if (unlikely(rc != SQLITE_OK))
1178
- goto bind_fail;
1077
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_blob(res, ++param, &st->chart_uuid, sizeof(st->chart_uuid), SQLITE_STATIC));
1078
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_blob(res, ++param, &st->rrdhost->host_uuid, sizeof(st->rrdhost->host_uuid), SQLITE_STATIC));
1079
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, string2str(st->parts.type), -1, SQLITE_STATIC));
1080
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, string2str(st->parts.id), -1, SQLITE_STATIC));
1081
1082
const char *name = string2str(st->parts.name);
1083
if (name && *name)
1182
- rc = sqlite3_bind_text(res, ++param, name, -1, SQLITE_STATIC);
1084
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, name, -1, SQLITE_STATIC));
1085
else
1184
- rc = sqlite3_bind_null(res, ++param);
1185
- if (unlikely(rc != SQLITE_OK))
1186
- goto bind_fail;
1187
-
1188
- rc = sqlite3_bind_text(res, ++param, rrdset_family(st), -1, SQLITE_STATIC);
1189
- if (unlikely(rc != SQLITE_OK))
1190
- goto bind_fail;
1191
-
1192
- rc = sqlite3_bind_text(res, ++param, rrdset_context(st), -1, SQLITE_STATIC);
1193
- if (unlikely(rc != SQLITE_OK))
1194
- goto bind_fail;
1195
-
1196
- rc = sqlite3_bind_text(res, ++param, rrdset_title(st), -1, SQLITE_STATIC);
1197
- if (unlikely(rc != SQLITE_OK))
1198
- goto bind_fail;
1199
-
1200
- rc = sqlite3_bind_text(res, ++param, rrdset_units(st), -1, SQLITE_STATIC);
1201
- if (unlikely(rc != SQLITE_OK))
1202
- goto bind_fail;
1203
-
1204
- rc = sqlite3_bind_text(res, ++param, rrdset_plugin_name(st), -1, SQLITE_STATIC);
1205
- if (unlikely(rc != SQLITE_OK))
1206
- goto bind_fail;
1207
-
1208
- rc = sqlite3_bind_text(res, ++param, rrdset_module_name(st), -1, SQLITE_STATIC);
1209
- if (unlikely(rc != SQLITE_OK))
1210
- goto bind_fail;
1211
-
1212
- rc = sqlite3_bind_int(res, ++param, (int) st->priority);
1213
- if (unlikely(rc != SQLITE_OK))
1214
- goto bind_fail;
1215
-
1216
- rc = sqlite3_bind_int(res, ++param, st->update_every);
1217
- if (unlikely(rc != SQLITE_OK))
1218
- goto bind_fail;
1219
-
1220
- rc = sqlite3_bind_int(res, ++param, st->chart_type);
1221
- if (unlikely(rc != SQLITE_OK))
1222
- goto bind_fail;
1223
-
1224
- rc = sqlite3_bind_int(res, ++param, st->rrd_memory_mode);
1225
- if (unlikely(rc != SQLITE_OK))
1226
- goto bind_fail;
1227
-
1228
- rc = sqlite3_bind_int(res, ++param, (int) st->db.entries);
1229
- if (unlikely(rc != SQLITE_OK))
1230
- goto bind_fail;
1086
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_null(res, ++param));
1087
+
1088
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, rrdset_family(st), -1, SQLITE_STATIC));
1089
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, rrdset_context(st), -1, SQLITE_STATIC));
1090
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, rrdset_title(st), -1, SQLITE_STATIC));
1091
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, rrdset_units(st), -1, SQLITE_STATIC));
1092
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, rrdset_plugin_name(st), -1, SQLITE_STATIC));
1093
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, rrdset_module_name(st), -1, SQLITE_STATIC));
1094
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int) st->priority));
1095
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, st->update_every));
1096
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, st->chart_type));
1097
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, st->rrd_memory_mode));
1098
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int) st->db.entries));
1099
1100
store_rc = execute_insert(res);
1101
if (unlikely(store_rc != SQLITE_DONE))
1102
error_report("Failed to store chart, rc = %d", store_rc);
1103
1236
- rc = sqlite3_reset(res);
1237
- if (unlikely(rc != SQLITE_OK))
1238
- error_report("Failed to reset statement in chart store function, rc = %d", rc);
1104
+ SQLITE_RESET(res);
1105
1106
return store_rc != SQLITE_DONE;
1107
1108
bind_fail:
1243
- error_report("Failed to bind parameter %d to store chart, rc = %d", param, rc);
1244
- rc = sqlite3_reset(res);
1245
- if (unlikely(rc != SQLITE_OK))
1246
- error_report("Failed to reset statement in chart store function, rc = %d", rc);
1109
+ REPORT_BIND_FAIL(res, param);
1110
+
1111
+ SQLITE_RESET(res);
1112
return 1;
1113
}
1114
@@ -1263,55 +1128,29 @@ static int store_dimension_metadata(RRDDIM *rd)
1128
}
1129
}
1130
1266
- rc = sqlite3_bind_blob(res, ++param, &rd->metric_uuid, sizeof(rd->metric_uuid), SQLITE_STATIC);
1267
- if (unlikely(rc != SQLITE_OK))
1268
- goto bind_fail;
1269
-
1270
- rc = sqlite3_bind_blob(res, ++param, &rd->rrdset->chart_uuid, sizeof(rd->rrdset->chart_uuid), SQLITE_STATIC);
1271
- if (unlikely(rc != SQLITE_OK))
1272
- goto bind_fail;
1273
-
1274
- rc = sqlite3_bind_text(res, ++param, string2str(rd->id), -1, SQLITE_STATIC);
1275
- if (unlikely(rc != SQLITE_OK))
1276
- goto bind_fail;
1277
-
1278
- rc = sqlite3_bind_text(res, ++param, string2str(rd->name), -1, SQLITE_STATIC);
1279
- if (unlikely(rc != SQLITE_OK))
1280
- goto bind_fail;
1281
-
1282
- rc = sqlite3_bind_int(res, ++param, (int) rd->multiplier);
1283
- if (unlikely(rc != SQLITE_OK))
1284
- goto bind_fail;
1285
-
1286
- rc = sqlite3_bind_int(res, ++param, (int ) rd->divisor);
1287
- if (unlikely(rc != SQLITE_OK))
1288
- goto bind_fail;
1289
-
1290
- rc = sqlite3_bind_int(res, ++param, rd->algorithm);
1291
- if (unlikely(rc != SQLITE_OK))
1292
- goto bind_fail;
1293
-
1131
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_blob(res, ++param, &rd->metric_uuid, sizeof(rd->metric_uuid), SQLITE_STATIC));
1132
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_blob(res, ++param, &rd->rrdset->chart_uuid, sizeof(rd->rrdset->chart_uuid), SQLITE_STATIC));
1133
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, string2str(rd->id), -1, SQLITE_STATIC));
1134
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, string2str(rd->name), -1, SQLITE_STATIC));
1135
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int) rd->multiplier));
1136
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int ) rd->divisor));
1137
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, rd->algorithm));
1138
if (rrddim_option_check(rd, RRDDIM_OPTION_HIDDEN))
1295
- rc = sqlite3_bind_text(res, ++param, "hidden", -1, SQLITE_STATIC);
1139
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_text(res, ++param, "hidden", -1, SQLITE_STATIC));
1140
else
1297
- rc = sqlite3_bind_null(res, ++param);
1298
- if (unlikely(rc != SQLITE_OK))
1299
- goto bind_fail;
1141
+ SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_null(res, ++param));
1142
1143
rc = execute_insert(res);
1144
if (unlikely(rc != SQLITE_DONE))
1145
error_report("Failed to store dimension, rc = %d", rc);
1146
1305
- rc = sqlite3_reset(res);
1306
- if (unlikely(rc != SQLITE_OK))
1307
- error_report("Failed to reset statement in store dimension, rc = %d", rc);
1147
+ SQLITE_RESET(res);
1148
return 0;
1149
1150
bind_fail:
1311
- error_report("Failed to bind parameter %d to store dimension, rc = %d", param, rc);
1312
- rc = sqlite3_reset(res);
1313
- if (unlikely(rc != SQLITE_OK))
1314
- error_report("Failed to reset statement in store dimension, rc = %d", rc);
1151
+ REPORT_BIND_FAIL(res, param);
1152
+
1153
+ SQLITE_RESET(res);
1154
return 1;
1155
}
1156
@@ -1415,12 +1254,10 @@ static bool chart_can_be_deleted(uuid_t *chart_uuid, sqlite3_stmt **check_res, b
1254
1255
skip:
1256
if (check_res)
1418
- rc = sqlite3_reset(res);
1257
+ SQLITE_RESET(res);
1258
else
1420
- rc = sqlite3_finalize(res);
1259
+ SQLITE_FINALIZE(res);
1260
1422
- if (unlikely(rc != SQLITE_OK))
1423
- error_report("Failed to %s statement that checks chart uuid existence rc = %d", check_res ? "reset" : "finalize", rc);
1261
return result == 0;
1262
}
1263
@@ -1457,12 +1294,9 @@ static void delete_chart_uuid(uuid_t *chart_uuid, sqlite3_stmt **action_res, boo
1294
1295
skip:
1296
if (action_res)
1460
- rc = sqlite3_reset(res);
1297
+ SQLITE_RESET(res);
1298
else
1462
- rc = sqlite3_finalize(res);
1463
-
1464
- if (unlikely(rc != SQLITE_OK))
1465
- error_report("Failed to %s statement that deletes a chart uuid rc = %d", action_res ? "reset" : "finalize", rc);
1299
+ SQLITE_FINALIZE(res);
1300
}
1301
1302
static void check_dimension_metadata(struct metadata_wc *wc)
@@ -1522,9 +1356,7 @@ static void check_dimension_metadata(struct metadata_wc *wc)
1356
last_row_id ? "resume" : "restart",
1357
(long long)(next_execution_t - now));
1358
1525
- rc = sqlite3_finalize(res);
1526
- if (unlikely(rc != SQLITE_OK))
1527
- error_report("Failed to finalize the prepared statement to check dimensions");
1359
+ SQLITE_FINALIZE(res);
1360
}
1361
1362
static void check_chart_metadata(struct metadata_wc *wc)
@@ -1568,11 +1400,8 @@ static void check_chart_metadata(struct metadata_wc *wc)
1400
true,
1401
false);
1402
1571
- if (check_res)
1572
- sqlite3_finalize(check_res);
1573
-
1574
- if (action_res)
1575
- sqlite3_finalize(action_res);
1403
+ SQLITE_FINALIZE(check_res);
1404
+ SQLITE_FINALIZE(action_res);
1405
1406
now = now_realtime_sec();
1407
if (more_to_do)
@@ -1591,9 +1420,7 @@ static void check_chart_metadata(struct metadata_wc *wc)
1420
last_row_id ? "resume" : "restart",
1421
(long long)(next_execution_t - now));
1422
1594
- rc = sqlite3_finalize(res);
1595
- if (unlikely(rc != SQLITE_OK))
1596
- error_report("Failed to finalize the prepared statement when reading charts");
1423
+ SQLITE_FINALIZE(res);
1424
}
1425
1426
static void check_label_metadata(struct metadata_wc *wc)
@@ -1639,11 +1466,8 @@ static void check_label_metadata(struct metadata_wc *wc)
1466
false,
1467
true);
1468
1642
- if (check_res)
1643
- sqlite3_finalize(check_res);
1644
-
1645
- if (action_res)
1646
- sqlite3_finalize(action_res);
1469
+ SQLITE_FINALIZE(check_res);
1470
+ SQLITE_FINALIZE(action_res);
1471
1472
now = now_realtime_sec();
1473
if (more_to_do)
@@ -1662,9 +1486,7 @@ static void check_label_metadata(struct metadata_wc *wc)
1486
last_row_id ? "resume" : "restart",
1487
(long long)(next_execution_t - now));
1488
1665
- rc = sqlite3_finalize(res);
1666
- if (unlikely(rc != SQLITE_OK))
1667
- error_report("Failed to finalize the prepared statement when checking charts");
1489
+ SQLITE_FINALIZE(res);
1490
}
1491
1492
@@ -2053,10 +1875,8 @@ static bool metadata_scan_host(RRDHOST *host, uint32_t max_count, bool use_trans
1875
if (use_transaction)
1876
(void)db_execute(db_meta, "COMMIT TRANSACTION");
1877
2056
- if (ml_load_stmt) {
2057
- sqlite3_finalize(ml_load_stmt);
2058
- ml_load_stmt = NULL;
2059
- }
1878
+ SQLITE_FINALIZE(ml_load_stmt);
1879
+ ml_load_stmt = NULL;
1880
1881
return more_to_do;
1882
}