summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStelios Fragkakis <52996999+stelfrag@users.noreply.github.com>2024-04-24 09:56:15 +0300
committerGitHub <noreply@github.com>2024-04-24 09:56:15 +0300
commite1579abddc252af0616cbcd19c1dab159d47cef7 (patch)
treeac7dfdd9b3288ba01e628906da5456a4f6dce23c
parentaa6d30384d26c6df33acd67dc8402ae9e3195297 (diff)
Additional SQL code cleanup (#17503)
Remove unneeded check Add macros for sqlite3_finalize and sqlite3_reset Use macro to make parameter binding more readable
-rw-r--r--src/database/sqlite/sqlite_aclk.c36
-rw-r--r--src/database/sqlite/sqlite_aclk_alert.c39
-rw-r--r--src/database/sqlite/sqlite_context.c183
-rw-r--r--src/database/sqlite/sqlite_db_migration.c24
-rw-r--r--src/database/sqlite/sqlite_functions.c3
-rw-r--r--src/database/sqlite/sqlite_functions.h20
-rw-r--r--src/database/sqlite/sqlite_health.c150
-rw-r--r--src/database/sqlite/sqlite_metadata.c478
8 files changed, 303 insertions, 630 deletions
diff --git a/src/database/sqlite/sqlite_aclk.c b/src/database/sqlite/sqlite_aclk.c
index c410406b22..afd504f199 100644
--- a/src/database/sqlite/sqlite_aclk.c
+++ b/src/database/sqlite/sqlite_aclk.c
@@ -192,16 +192,16 @@ static int is_host_available(uuid_t *host_id)
return 1;
}
- rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_id parameter to check host existence");
- goto failed;
- }
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
+
+ param = 0;
rc = sqlite3_step_monitored(res);
-failed:
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
- error_report("Failed to finalize the prepared statement when checking host existence");
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_FINALIZE(res);
return (rc == SQLITE_ROW);
}
@@ -245,9 +245,7 @@ static void sql_delete_aclk_table_list(char *host_guid)
while (sqlite3_step_monitored(res) == SQLITE_ROW)
buffer_strcat(sql, (char *) sqlite3_column_text(res, 0));
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement to clean up aclk tables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
rc = db_execute(db_meta, buffer_tostring(sql));
if (unlikely(rc))
@@ -281,11 +279,10 @@ static void sql_unregister_node(char *machine_guid)
return;
}
- rc = sqlite3_bind_blob(res, 1, &host_uuid, sizeof(host_uuid), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_id parameter to remove host node id");
- goto skip;
- }
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host_uuid, sizeof(host_uuid), SQLITE_STATIC));
+ param = 0;
+
rc = sqlite3_step_monitored(res);
if (unlikely(rc != SQLITE_DONE)) {
error_report("Failed to execute command to remove host node id");
@@ -295,9 +292,10 @@ static void sql_unregister_node(char *machine_guid)
machine_guid = NULL;
}
-skip:
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
- error_report("Failed to finalize statement to remove host node id");
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_FINALIZE(res);
freez(machine_guid);
}
diff --git a/src/database/sqlite/sqlite_aclk_alert.c b/src/database/sqlite/sqlite_aclk_alert.c
index e345f145bd..0433ff6a17 100644
--- a/src/database/sqlite/sqlite_aclk_alert.c
+++ b/src/database/sqlite/sqlite_aclk_alert.c
@@ -41,9 +41,7 @@ static void update_filtered(ALARM_ENTRY *ae, int64_t unique_id, char *uuid_str)
done:
REPORT_BIND_FAIL(res, param);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when trying to update_filtered, rc = %d", rc);
+ SQLITE_FINALIZE(res);
}
#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
done:
REPORT_BIND_FAIL(res, param);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when trying to check for alert variables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return ret;
}
@@ -140,9 +136,7 @@ static bool should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
done:
REPORT_BIND_FAIL(res, param);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when trying should_send_to_cloud, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return send;
}
@@ -188,8 +182,7 @@ void sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, bool skip_filter)
error_report("Failed to store alert event %"PRIu32", rc = %d", ae->unique_id, rc);
done:
- if (unlikely(sqlite3_finalize(res_alert) != SQLITE_OK))
- error_report("Failed to reset statement in store alert event, rc = %d", rc);
+ SQLITE_FINALIZE(res_alert);
}
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)
}
done:
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement to send alert entries from the database, rc = %d", rc);
+ SQLITE_FINALIZE(res);
freez(claim_id);
buffer_free(sql);
@@ -482,9 +473,7 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host)
else
rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS);
done:
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement to queue existing alerts, rc = %d", rc);
+ SQLITE_FINALIZE(res);
skip:
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
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);
bind_fail:
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement when pushing alarm config hash, rc = %d", rc);
+ SQLITE_FINALIZE(res);
freez(config_hash);
freez(node_id);
@@ -704,9 +691,7 @@ void sql_process_queue_removed_alerts_to_aclk(char *node_id)
}
skip:
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement to queue removed alerts, rc = %d", rc);
+ SQLITE_FINALIZE(res);
}
void sql_queue_removed_alerts_to_aclk(RRDHOST *host)
@@ -964,9 +949,7 @@ void sql_aclk_alert_clean_dead_entries(RRDHOST *host)
error_report("Failed to execute DELETE query for cleaning stale ACLK alert entries.");
skip:
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement for cleaning stale ACLK alert entries.");
+ SQLITE_FINALIZE(res);
}
#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
sqlite3_column_bytes(res, 2) > 0 ? (uint64_t)sqlite3_column_int64(res, 2) : 0;
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement to get alert log status from the database, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
diff --git a/src/database/sqlite/sqlite_context.c b/src/database/sqlite/sqlite_context.c
index 3f5ee016bb..8d3de05c1c 100644
--- a/src/database/sqlite/sqlite_context.c
+++ b/src/database/sqlite/sqlite_context.c
@@ -76,7 +76,7 @@ int sql_init_context_database(int memory)
// Fetching data
//
#define CTX_GET_CHART_LIST "SELECT c.chart_id, c.type||'.'||c.id, c.name, c.context, c.title, c.unit, c.priority, " \
- "c.update_every, c.chart_type, c.family FROM chart c WHERE c.host_id = @host_id AND c.chart_id IS NOT NULL"
+ "c.update_every, c.chart_type, c.family FROM chart c WHERE c.host_id = @host_id AND c.chart_id IS NOT NULL"
void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, void *), void *data)
{
@@ -95,13 +95,10 @@ void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, voi
return;
}
}
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_id to fetch the chart list");
- goto skip_load;
- }
-
+ param = 0;
SQL_CHART_DATA chart_data = { 0 };
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
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
dict_cb(&chart_data, data);
}
-skip_load:
- rc = sqlite3_reset(res);
- if (rc != SQLITE_OK)
- error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_RESET(res);
}
// Dimension list
#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 " \
- "FROM dimension d WHERE d.chart_id = @id AND d.dim_id IS NOT NULL ORDER BY d.rowid ASC"
+ "FROM dimension d WHERE d.chart_id = @id AND d.dim_id IS NOT NULL ORDER BY d.rowid ASC"
void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DATA *, void *), void *data)
{
int rc;
@@ -139,14 +136,12 @@ void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DA
}
}
- rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind chart_id to fetch dimension list");
- goto failed;
- }
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC));
SQL_DIMENSION_DATA dimension_data;
+ param = 0;
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
uuid_copy(dimension_data.dim_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
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
dict_cb(&dimension_data, data);
}
-failed:
- rc = sqlite3_reset(res);
- if (rc != SQLITE_OK)
- error_report("Failed to reset statement that fetches the chart dimension list, rc = %d", rc);
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_RESET(res);
}
// LABEL LIST
@@ -177,12 +172,10 @@ void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, v
}
}
- rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind chart_id to fetch chart labels");
- goto failed;
- }
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC));
+ param = 0;
SQL_CLABEL_DATA label_data;
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
dict_cb(&label_data, data);
}
-failed:
- rc = sqlite3_reset(res);
- if (rc != SQLITE_OK)
- error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_RESET(res);
}
// CONTEXT LIST
#define CTX_GET_CONTEXT_LIST "SELECT id, version, title, chart_type, unit, priority, first_time_t, " \
- "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id"
+ "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id"
void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_DATA *, void *), void *data)
{
@@ -221,12 +214,9 @@ void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_D
VERSIONED_CONTEXT_DATA context_data = {0};
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
-
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_id to fetch versioned context data");
- goto failed;
- }
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
+ param = 0;
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
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
dict_cb(&context_data, data);
}
-failed:
- rc = sqlite3_reset(res);
- if (rc != SQLITE_OK)
- error_report("Failed to reset statement that fetches stored context versioned data, rc = %d", rc);
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_RESET(res);
}
@@ -271,81 +261,29 @@ int ctx_store_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
return 1;
}
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_uuid to store context details");
- goto skip_store;
- }
-
- rc = bind_text_null(res, 2, context_data->id, 0);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind context to store context details");
- goto skip_store;
- }
-
- rc = sqlite3_bind_int64(res, 3, (time_t) context_data->version);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind first_time_t to store context details");
- goto skip_store;
- }
-
- rc = bind_text_null(res, 4, context_data->title, 0);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind context to store context details");
- goto skip_store;
- }
-
- rc = bind_text_null(res, 5, context_data->chart_type, 0);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind context to store context details");
- goto skip_store;
- }
-
- rc = bind_text_null(res, 6, context_data->units, 0);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind context to store context details");
- goto skip_store;
- }
-
- rc = sqlite3_bind_int64(res, 7, (time_t) context_data->priority);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind first_time_t to store context details");
- goto skip_store;
- }
-
- rc = sqlite3_bind_int64(res, 8, (time_t) context_data->first_time_s);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind first_time_t to store context details");
- goto skip_store;
- }
-
- rc = sqlite3_bind_int64(res, 9, (time_t) context_data->last_time_s);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind last_time_t to store context details");
- goto skip_store;
- }
-
- rc = sqlite3_bind_int(res, 10, context_data->deleted);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind deleted flag to store context details");
- goto skip_store;
- }
-
- rc = bind_text_null(res, 11, context_data->family, 1);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind context to store details");
- goto skip_store;
- }
-
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->id, 0));
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->version));
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->title, 0));
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->chart_type, 0));
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->units, 0));
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->priority));
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->first_time_s));
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (time_t) context_data->last_time_s));
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, context_data->deleted));
+ SQLITE_BIND_FAIL(done, bind_text_null(res, ++param, context_data->family, 1));
+
+ param = 0;
rc_stored = execute_insert(res);
if (rc_stored != SQLITE_DONE)
error_report("Failed store context details for context %s, rc = %d", context_data->id, rc_stored);
-skip_store:
- rc = sqlite3_finalize(res);
- if (rc != SQLITE_OK)
- error_report("Failed to finalize statement that stores context details, rc = %d", rc);
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_FINALIZE(res);
return (rc_stored != SQLITE_DONE);
}
@@ -367,27 +305,20 @@ int ctx_delete_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
return 1;
}
- rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_id for context data deletion");
- goto skip_delete;
- }
-
- rc = sqlite3_bind_text(res, 2, context_data->id, -1, SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind context id for context data deletion");
- goto skip_delete;
- }
+ int param = 0;
+ SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_uuid, sizeof(*host_uuid), SQLITE_STATIC));
+ SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, context_data->id, -1, SQLITE_STATIC));
+ param = 0;
rc_stored = execute_insert(res);
if (rc_stored != SQLITE_DONE)
error_report("Failed to delete context %s, rc = %d", context_data->id, rc_stored);
-skip_delete:
- rc = sqlite3_finalize(res);
- if (rc != SQLITE_OK)
- error_report("Failed to finalize statement where deleting a context, rc = %d", rc);
+done:
+ REPORT_BIND_FAIL(res, param);
+
+ SQLITE_FINALIZE(res);
return (rc_stored != SQLITE_DONE);
}
diff --git a/src/database/sqlite/sqlite_db_migration.c b/src/database/sqlite/sqlite_db_migration.c
index ea35276115..88abd84924 100644
--- a/src/database/sqlite/sqlite_db_migration.c
+++ b/src/database/sqlite/sqlite_db_migration.c
@@ -208,9 +208,7 @@ static int do_migration_v3_v4(sqlite3 *database)
freez(table);
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
@@ -249,9 +247,7 @@ static int do_migration_v6_v7(sqlite3 *database)
freez(table);
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when altering aclk_alert tables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
@@ -278,9 +274,7 @@ static int do_migration_v7_v8(sqlite3 *database)
freez(table);
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
@@ -340,9 +334,7 @@ static int do_migration_v8_v9(sqlite3 *database)
freez(table);
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when copying health_log tables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
char *table = NULL;
dfe_start_read(dict_tables, table) {
@@ -407,9 +399,7 @@ static int do_migration_v14_v15(sqlite3 *database)
count++;
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when dropping unused indices, rc = %d", rc);
+ SQLITE_FINALIZE(res);
if (count)
(void) db_execute(database, buffer_tostring(wb));
@@ -438,9 +428,7 @@ static int do_migration_v15_v16(sqlite3 *database)
count++;
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize statement when running ANALYZE on aclk_alert_tables, rc = %d", rc);
+ SQLITE_FINALIZE(res);
if (count)
(void) db_execute(database, buffer_tostring(wb));
diff --git a/src/database/sqlite/sqlite_functions.c b/src/database/sqlite/sqlite_functions.c
index e8f60b25ae..868656411d 100644
--- a/src/database/sqlite/sqlite_functions.c
+++ b/src/database/sqlite/sqlite_functions.c
@@ -288,8 +288,7 @@ static int get_pragma_value(sqlite3 *database, const char *sql)
if (likely(rc == SQLITE_ROW))
result = sqlite3_column_int(res, 0);
- rc = sqlite3_finalize(res);
- (void) rc;
+ SQLITE_FINALIZE(res);
return result;
}
diff --git a/src/database/sqlite/sqlite_functions.h b/src/database/sqlite/sqlite_functions.h
index fdac07a4b9..6f5eebcce8 100644
--- a/src/database/sqlite/sqlite_functions.h
+++ b/src/database/sqlite/sqlite_functions.h
@@ -28,6 +28,26 @@ void analytics_set_data_str(char **name, const char *value);
} \
} while (0)
+#define SQLITE_FINALIZE(res) \
+ do { \
+ if ((res)) { \
+ int _rc = sqlite3_finalize((res)); \
+ if (_rc != SQLITE_OK) { \
+ nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to finalize statement rc=%d in %s", _rc, __FUNCTION__); \
+ } \
+ } \
+ } while (0)
+
+#define SQLITE_RESET(res) \
+ do { \
+ if ((res)) { \
+ int _rc = sqlite3_reset((res)); \
+ if (_rc != SQLITE_OK) { \
+ nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to reset statement rc=%d in %s", _rc, __FUNCTION__); \
+ } \
+ } \
+ } while (0)
+
#define SQL_MAX_RETRY (100)
#define SQLITE_INSERT_DELAY (10) // Insert delay in case of lock
diff --git a/src/database/sqlite/sqlite_health.c b/src/database/sqlite/sqlite_health.c
index 8ce9b04942..c69e29b882 100644
--- a/src/database/sqlite/sqlite_health.c
+++ b/src/database/sqlite/sqlite_health.c
@@ -61,8 +61,7 @@ static void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae)
done:
REPORT_BIND_FAIL(res, param);
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
- error_report("HEALTH [%s]: Failed to reset statement for updating health log.", rrdhost_hostname(host));
+ SQLITE_RESET(res);
}
/* Health related SQL queries
@@ -128,8 +127,7 @@ static void sql_health_alarm_log_insert_detail(RRDHOST *host, uint64_t health_lo
done:
REPORT_BIND_FAIL(res, param);
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
- error_report("HEALTH [%s]: Failed to reset statement for inserting to health log detail", rrdhost_hostname(host));
+ SQLITE_RESET(res);
}
#define SQL_INSERT_HEALTH_LOG \
@@ -184,8 +182,7 @@ static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae)
done:
REPORT_BIND_FAIL(res, param);
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
- error_report("HEALTH [%s]: Failed to reset statement for inserting to health log", rrdhost_hostname(host));
+ SQLITE_RESET(res);
}
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) {
done:
REPORT_BIND_FAIL(res, param);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize the prepared statement to cleanup health log detail table (claimed)");
+ SQLITE_FINALIZE(res);
}
#define SQL_INJECT_REMOVED \
@@ -314,8 +309,7 @@ bool sql_update_removed_in_health_log(RRDHOST *host, uint32_t alarm_id, uuid_t *
done:
REPORT_BIND_FAIL(res, param);
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
- error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
+ SQLITE_FINALIZE(res);
return (param == 0 && rc == SQLITE_DONE);
}
@@ -345,8 +339,7 @@ bool sql_update_removed_in_health_log_detail(uint32_t unique_id, uint32_t max_un
done:
REPORT_BIND_FAIL(res, param);
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
- error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
+ SQLITE_FINALIZE(res);
return (param == 0 && rc == SQLITE_DONE);
}
@@ -398,8 +391,7 @@ void sql_inject_removed_status(
done:
REPORT_BIND_FAIL(res, param);
- if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
- error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
+ SQLITE_FINALIZE(res);
}
#define SQL_SELECT_MAX_UNIQUE_ID \
@@ -422,7 +414,7 @@ uint32_t sql_get_max_unique_id (RRDHOST *host)
rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
error_report("Failed to bind host_id parameter for SQL_SELECT_MAX_UNIQUE_ID.");
- sqlite3_finalize(res);
+ SQLITE_FINALIZE(res);
return 0;
}
@@ -430,9 +422,7 @@ uint32_t sql_get_max_unique_id (RRDHOST *host)
max_unique_id = (uint32_t) sqlite3_column_int64(res, 0);
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize the statement");
+ SQLITE_FINALIZE(res);
return max_unique_id;
}
@@ -457,7 +447,7 @@ void sql_check_removed_alerts_state(RRDHOST *host)
rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
error_report("Failed to bind host_id parameter for SQL_SELECT_LAST_STATUSES.");
- sqlite3_finalize(res);
+ SQLITE_FINALIZE(res);
return;
}
@@ -479,9 +469,7 @@ void sql_check_removed_alerts_state(RRDHOST *host)
}
}
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize the statement");
+ SQLITE_FINALIZE(res);
}
#define SQL_DELETE_MISSING_CHART_ALERT \
@@ -506,7 +494,7 @@ static void sql_remove_alerts_from_deleted_charts(RRDHOST *host, uuid_t *host_id
if (unlikely(ret != SQLITE_OK)) {
error_report("Failed to bind host_id parameter for sql_remove_alerts_from_deleted_charts.");
- sqlite3_finalize(res);
+ SQLITE_FINALIZE(res);
return;
}
@@ -514,9 +502,7 @@ static void sql_remove_alerts_from_deleted_charts(RRDHOST *host, uuid_t *host_id
if (ret != SQLITE_DONE)
error_report("Failed to execute command to delete missing charts from health_log");
- ret = sqlite3_finalize(res);
- if (unlikely(ret != SQLITE_OK))
- error_report("Failed to finalize statement when deleting missing charts from health_log");
+ SQLITE_FINALIZE(res);
}
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)
ret = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
if (unlikely(ret != SQLITE_OK)) {
error_report("Failed to bind host_id parameter for SQL_LOAD_HEALTH_LOG.");
- sqlite3_finalize(res);
+ SQLITE_FINALIZE(res);
return;
}
@@ -737,9 +723,7 @@ void sql_health_alarm_log_load(RRDHOST *host)
"[%s]: Table health_log, loaded %zd alarm entries, errors in %zd entries.",
rrdhost_hostname(host), loaded, errored);
- ret = sqlite3_finalize(res);
- if (unlikely(ret != SQLITE_OK))
- error_report("Failed to finalize the health log read statement");
+ SQLITE_FINALIZE(res);
}
/*
@@ -873,8 +857,7 @@ void sql_alert_store_config(RRD_ALERT_PROTOTYPE *ap __maybe_unused)
done:
REPORT_BIND_FAIL(res, param);
buffer_free(buf);
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
- error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
+ SQLITE_RESET(res);
}
#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
}
done:
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to finalize the statement.");
-
- return ret;
+ SQLITE_FINALIZE(res);
+ return ret;
}
#define SQL_SELECT_HEALTH_LOG \
@@ -1084,9 +1064,7 @@ void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, time_t after, const ch
buffer_json_finalize(wb);
finish:
- rc = sqlite3_reset(stmt_query);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement for SQL_SELECT_HEALTH_LOG");
+ SQLITE_RESET(stmt_query);
}
#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) {
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to copy health log table, rc = %d", rc);
+ SQLITE_FINALIZE(res);
freez(uuid_from_table);
return 0;
}
@@ -1135,9 +1111,7 @@ int health_migrate_old_health_log_table(char *table) {
rc = execute_insert(res);
if (unlikely(rc != SQLITE_DONE)) {
error_report("Failed to execute SQL_COPY_HEALTH_LOG, rc = %d", rc);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to copy health log table, rc = %d", rc);
+ SQLITE_FINALIZE(res);
freez(uuid_from_table);
}
@@ -1151,18 +1125,14 @@ int health_migrate_old_health_log_table(char *table) {
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to copy health log detail, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
rc = execute_insert(res);
if (unlikely(rc != SQLITE_DONE)) {
error_report("Failed to execute SQL_COPY_HEALTH_LOG_DETAIL, rc = %d", rc);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to copy health log detail table, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
@@ -1176,9 +1146,7 @@ int health_migrate_old_health_log_table(char *table) {
rc = execute_insert(res);
if (unlikely(rc != SQLITE_DONE)) {
error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_DETAIL_TRANSITION_ID, rc = %d", rc);
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to update health log detail table with transition ids, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
@@ -1191,26 +1159,20 @@ int health_migrate_old_health_log_table(char *table) {
rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to update health log detail with health log ids, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
rc = sqlite3_bind_blob(res, 2, &uuid, sizeof(uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
- rc = sqlite3_finalize(res);
- if (unlikely(rc != SQLITE_OK))
- error_report("Failed to reset statement to update health log detail with health log ids, rc = %d", rc);
+ SQLITE_FINALIZE(res);
return 0;
}
rc = execute_insert(res