Store nulls instead of empty strings in health tables (#13683)
* store nulls instead of empty strings in health tables * remove empty line * make define
Emmanuel Vasilakis committed
Sep 21, 2022 at 10:49 UTC
c87c5c3c5d8bd3de547efc4764bcd13e8d91f6b0
1 file changed
+44
-49
database/sqlite/sqlite_health.c
+44
-49
@@ -4,6 +4,7 @@
4
#include "sqlite_functions.h"
5
6
#define MAX_HEALTH_SQL_SIZE 2048
7
+#define sqlite3_bind_string_or_null(res,key,param) ((key) ? sqlite3_bind_text(res, param, string2str(key), -1, SQLITE_STATIC) : sqlite3_bind_null(res, param))
8
9
/* Health related SQL queries
10
Creates a health log table in sqlite, one per host guid
@@ -215,49 +216,49 @@ void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
216
goto failed;
217
}
218
218
- rc = sqlite3_bind_text(res, 14, ae_name(ae), -1, SQLITE_STATIC);
219
+ rc = sqlite3_bind_string_or_null(res, ae->name, 14);
220
if (unlikely(rc != SQLITE_OK)) {
221
error_report("Failed to bind name parameter for SQL_INSERT_HEALTH_LOG");
222
goto failed;
223
}
224
224
- rc = sqlite3_bind_text(res, 15, ae_chart_name(ae), -1, SQLITE_STATIC);
225
+ rc = sqlite3_bind_string_or_null(res, ae->chart, 15);
226
if (unlikely(rc != SQLITE_OK)) {
227
error_report("Failed to bind chart parameter for SQL_INSERT_HEALTH_LOG");
228
goto failed;
229
}
230
230
- rc = sqlite3_bind_text(res, 16, ae_family(ae), -1, SQLITE_STATIC);
231
+ rc = sqlite3_bind_string_or_null(res, ae->family, 16);
232
if (unlikely(rc != SQLITE_OK)) {
233
error_report("Failed to bind family parameter for SQL_INSERT_HEALTH_LOG");
234
goto failed;
235
}
236
236
- rc = sqlite3_bind_text(res, 17, ae_exec(ae), -1, SQLITE_STATIC);
237
+ rc = sqlite3_bind_string_or_null(res, ae->exec, 17);
238
if (unlikely(rc != SQLITE_OK)) {
239
error_report("Failed to bind exec parameter for SQL_INSERT_HEALTH_LOG");
240
goto failed;
241
}
242
242
- rc = sqlite3_bind_text(res, 18, ae_recipient(ae), -1, SQLITE_STATIC);
243
+ rc = sqlite3_bind_string_or_null(res, ae->recipient, 18);
244
if (unlikely(rc != SQLITE_OK)) {
245
error_report("Failed to bind recipient parameter for SQL_INSERT_HEALTH_LOG");
246
goto failed;
247
}
248
248
- rc = sqlite3_bind_text(res, 19, ae_source(ae), -1, SQLITE_STATIC);
249
+ rc = sqlite3_bind_string_or_null(res, ae->source, 19);
250
if (unlikely(rc != SQLITE_OK)) {
251
error_report("Failed to bind source parameter for SQL_INSERT_HEALTH_LOG");
252
goto failed;
253
}
254
254
- rc = sqlite3_bind_text(res, 20, ae_units(ae), -1, SQLITE_STATIC);
255
+ rc = sqlite3_bind_string_or_null(res, ae->units, 20);
256
if (unlikely(rc != SQLITE_OK)) {
257
error_report("Failed to bind host_id parameter to store node instance information");
258
goto failed;
259
}
260
260
- rc = sqlite3_bind_text(res, 21, ae_info(ae), -1, SQLITE_STATIC);
261
+ rc = sqlite3_bind_string_or_null(res, ae->info, 21);
262
if (unlikely(rc != SQLITE_OK)) {
263
error_report("Failed to bind info parameter for SQL_INSERT_HEALTH_LOG");
264
goto failed;
@@ -305,25 +306,25 @@ void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
306
goto failed;
307
}
308
308
- rc = sqlite3_bind_text(res, 29, ae_classification(ae), -1, SQLITE_STATIC);
309
+ rc = sqlite3_bind_string_or_null(res, ae->classification, 29);
310
if (unlikely(rc != SQLITE_OK)) {
311
error_report("Failed to bind classification parameter for SQL_INSERT_HEALTH_LOG");
312
goto failed;
313
}
314
314
- rc = sqlite3_bind_text(res, 30, ae_component(ae), -1, SQLITE_STATIC);
315
+ rc = sqlite3_bind_string_or_null(res, ae->component, 30);
316
if (unlikely(rc != SQLITE_OK)) {
317
error_report("Failed to bind component parameter for SQL_INSERT_HEALTH_LOG");
318
goto failed;
319
}
320
320
- rc = sqlite3_bind_text(res, 31, ae_type(ae), -1, SQLITE_STATIC);
321
+ rc = sqlite3_bind_string_or_null(res, ae->type, 31);
322
if (unlikely(rc != SQLITE_OK)) {
323
error_report("Failed to bind type parameter for SQL_INSERT_HEALTH_LOG");
324
goto failed;
325
}
326
326
- rc = sqlite3_bind_text(res, 32, ae_chart_context(ae), -1, SQLITE_STATIC);
327
+ rc = sqlite3_bind_string_or_null(res, ae->chart_context, 32);
328
if (unlikely(rc != SQLITE_OK)) {
329
error_report("Failed to bind chart_context parameter for SQL_INSERT_HEALTH_LOG");
330
goto failed;
@@ -845,159 +846,153 @@ int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
846
}
847
848
param++;
848
- rc = sqlite3_bind_blob(res, 1, hash_id, sizeof(*hash_id), SQLITE_STATIC);
849
+ rc = sqlite3_bind_blob(res, param, hash_id, sizeof(*hash_id), SQLITE_STATIC);
850
if (unlikely(rc != SQLITE_OK))
851
goto bind_fail;
852
853
param++;
853
- if (cfg->alarm)
854
- rc = sqlite3_bind_text(res, 2, string2str(cfg->alarm), -1, SQLITE_STATIC);
855
- else
856
- rc = sqlite3_bind_null(res, 2);
854
+ rc = sqlite3_bind_string_or_null(res, cfg->alarm, param);
855
if (unlikely(rc != SQLITE_OK))
856
goto bind_fail;
857
858
param++;
861
- if (cfg->template_key)
862
- rc = sqlite3_bind_text(res, 3, string2str(cfg->template_key), -1, SQLITE_STATIC);
863
- else
864
- rc = sqlite3_bind_null(res, 3);
859
+ rc = sqlite3_bind_string_or_null(res, cfg->template_key, param);
860
if (unlikely(rc != SQLITE_OK))
861
goto bind_fail;
862
863
param++;
869
- rc = sqlite3_bind_text(res, 4, string2str(cfg->on), -1, SQLITE_STATIC);
864
+ rc = sqlite3_bind_string_or_null(res, cfg->on, param);
865
if (unlikely(rc != SQLITE_OK))
866
goto bind_fail;
867
868
param++;
874
- rc = sqlite3_bind_text(res, 5, string2str(cfg->classification), -1, SQLITE_STATIC);
869
+ rc = sqlite3_bind_string_or_null(res, cfg->classification, param);
870
if (unlikely(rc != SQLITE_OK))
871
goto bind_fail;
872
873
param++;
879
- rc = sqlite3_bind_text(res, 6, string2str(cfg->component), -1, SQLITE_STATIC);
874
+ rc = sqlite3_bind_string_or_null(res, cfg->component, param);
875
if (unlikely(rc != SQLITE_OK))
876
goto bind_fail;
877
878
param++;
884
- rc = sqlite3_bind_text(res, 7, string2str(cfg->type), -1, SQLITE_STATIC);
879
+ rc = sqlite3_bind_string_or_null(res, cfg->type, param);
880
if (unlikely(rc != SQLITE_OK))
881
goto bind_fail;
882
883
param++;
889
- rc = sqlite3_bind_text(res, 8, string2str(cfg->os), -1, SQLITE_STATIC);
884
+ rc = sqlite3_bind_string_or_null(res, cfg->os, param);
885
if (unlikely(rc != SQLITE_OK))
886
goto bind_fail;
887
888
param++;
894
- rc = sqlite3_bind_text(res, 9, string2str(cfg->host), -1, SQLITE_STATIC);
889
+ rc = sqlite3_bind_string_or_null(res, cfg->host, param);
890
if (unlikely(rc != SQLITE_OK))
891
goto bind_fail;
892
893
param++;
899
- rc = sqlite3_bind_text(res, 10, string2str(cfg->lookup), -1, SQLITE_STATIC);
894
+ rc = sqlite3_bind_string_or_null(res, cfg->lookup, param);
895
if (unlikely(rc != SQLITE_OK))
896
goto bind_fail;
897
898
param++;
904
- rc = sqlite3_bind_text(res, 11, string2str(cfg->every), -1, SQLITE_STATIC);
899
+ rc = sqlite3_bind_string_or_null(res, cfg->every, param);
900
if (unlikely(rc != SQLITE_OK))
901
goto bind_fail;
902
903
param++;
909
- rc = sqlite3_bind_text(res, 12, string2str(cfg->units), -1, SQLITE_STATIC);
904
+ rc = sqlite3_bind_string_or_null(res, cfg->units, param);
905
if (unlikely(rc != SQLITE_OK))
906
goto bind_fail;
907
908
param++;
914
- rc = sqlite3_bind_text(res, 13, string2str(cfg->calc), -1, SQLITE_STATIC);
909
+ rc = sqlite3_bind_string_or_null(res, cfg->calc, param);
910
if (unlikely(rc != SQLITE_OK))
911
goto bind_fail;
912
913
param++;
919
- rc = sqlite3_bind_text(res, 14, string2str(cfg->families), -1, SQLITE_STATIC);
914
+ rc = sqlite3_bind_string_or_null(res, cfg->families, param);
915
if (unlikely(rc != SQLITE_OK))
916
goto bind_fail;
917
918
param++;
924
- rc = sqlite3_bind_text(res, 15, string2str(cfg->plugin), -1, SQLITE_STATIC);
919
+ rc = sqlite3_bind_string_or_null(res, cfg->plugin, param);
920
if (unlikely(rc != SQLITE_OK))
921
goto bind_fail;
922
923
param++;
929
- rc = sqlite3_bind_text(res, 16, string2str(cfg->module), -1, SQLITE_STATIC);
924
+ rc = sqlite3_bind_string_or_null(res, cfg->module, param);
925
if (unlikely(rc != SQLITE_OK))
926
goto bind_fail;
927
928
param++;
934
- rc = sqlite3_bind_text(res, 17, string2str(cfg->charts), -1, SQLITE_STATIC);
929
+ rc = sqlite3_bind_string_or_null(res, cfg->charts, param);
930
if (unlikely(rc != SQLITE_OK))
931
goto bind_fail;
932
933
param++;
939
- rc = sqlite3_bind_text(res, 18, string2str(cfg->green), -1, SQLITE_STATIC);
934
+ rc = sqlite3_bind_string_or_null(res, cfg->green, param);
935
if (unlikely(rc != SQLITE_OK))
936
goto bind_fail;
937
938
param++;
944
- rc = sqlite3_bind_text(res, 19, string2str(cfg->red), -1, SQLITE_STATIC);
939
+ rc = sqlite3_bind_string_or_null(res, cfg->red, param);
940
if (unlikely(rc != SQLITE_OK))
941
goto bind_fail;
942
943
param++;
949
- rc = sqlite3_bind_text(res, 20, string2str(cfg->warn), -1, SQLITE_STATIC);
944
+ rc = sqlite3_bind_string_or_null(res, cfg->warn, param);
945
if (unlikely(rc != SQLITE_OK))
946
goto bind_fail;
947
948
param++;
954
- rc = sqlite3_bind_text(res, 21, string2str(cfg->crit), -1, SQLITE_STATIC);
949
+ rc = sqlite3_bind_string_or_null(res, cfg->crit, param);
950
if (unlikely(rc != SQLITE_OK))
951
goto bind_fail;
952
953
param++;
959
- rc = sqlite3_bind_text(res, 22, string2str(cfg->exec), -1, SQLITE_STATIC);
954
+ rc = sqlite3_bind_string_or_null(res, cfg->exec, param);
955
if (unlikely(rc != SQLITE_OK))
956
goto bind_fail;
957
958
param++;
964
- rc = sqlite3_bind_text(res, 23, string2str(cfg->to), -1, SQLITE_STATIC);
959
+ rc = sqlite3_bind_string_or_null(res, cfg->to, param);
960
if (unlikely(rc != SQLITE_OK))
961
goto bind_fail;
962
963
param++;
969
- rc = sqlite3_bind_text(res, 24, string2str(cfg->info), -1, SQLITE_STATIC);
964
+ rc = sqlite3_bind_string_or_null(res, cfg->info, param);
965
if (unlikely(rc != SQLITE_OK))
966
goto bind_fail;
967
968
param++;
974
- rc = sqlite3_bind_text(res, 25, string2str(cfg->delay), -1, SQLITE_STATIC);
969
+ rc = sqlite3_bind_string_or_null(res, cfg->delay, param);
970
if (unlikely(rc != SQLITE_OK))
971
goto bind_fail;
972
973
param++;
979
- rc = sqlite3_bind_text(res, 26, string2str(cfg->options), -1, SQLITE_STATIC);
974
+ rc = sqlite3_bind_string_or_null(res, cfg->options, param);
975
if (unlikely(rc != SQLITE_OK))
976
goto bind_fail;
977
978
param++;
984
- rc = sqlite3_bind_text(res, 27, string2str(cfg->repeat), -1, SQLITE_STATIC);
979
+ rc = sqlite3_bind_string_or_null(res, cfg->repeat, param);
980
if (unlikely(rc != SQLITE_OK))
981
goto bind_fail;
982
983
param++;
989
- rc = sqlite3_bind_text(res, 28, string2str(cfg->host_labels), -1, SQLITE_STATIC);
984
+ rc = sqlite3_bind_string_or_null(res, cfg->host_labels, param);
985
if (unlikely(rc != SQLITE_OK))
986
goto bind_fail;
987
988
if (cfg->p_db_lookup_after) {
989
param++;
995
- rc = sqlite3_bind_text(res, 29, string2str(cfg->p_db_lookup_dimensions), -1, SQLITE_STATIC);
990
+ rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_dimensions, param);
991
if (unlikely(rc != SQLITE_OK))
992
goto bind_fail;
993
994
param++;
1000
- rc = sqlite3_bind_text(res, 30, string2str(cfg->p_db_lookup_method), -1, SQLITE_STATIC);
995
+ rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_method, param);
996
if (unlikely(rc != SQLITE_OK))
997
goto bind_fail;
998
@@ -1067,7 +1062,7 @@ int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
1062
skip hash calculations
1063
*/
1064
#if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
1070
-#define DIGEST_ALERT_CONFIG_VAL(v) EVP_DigestUpdate(evpctx, (string2str(v)), string_strlen(v))
1065
+#define DIGEST_ALERT_CONFIG_VAL(v) ((v) ? EVP_DigestUpdate(evpctx, (string2str(v)), string_strlen((v))) : EVP_DigestUpdate(evpctx, "", 1))
1066
#endif
1067
int alert_hash_and_store_config(
1068
uuid_t hash_id,