2 * collectd - src/utils_db_query.c
3 * Copyright (C) 2008,2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_db_query.h"
36 struct udb_result_s; /* {{{ */
37 typedef struct udb_result_s udb_result_t;
40 char *instance_prefix;
51 struct udb_query_s /* {{{ */
56 char *plugin_instance_from;
58 unsigned int min_version;
59 unsigned int max_version;
61 udb_result_t *results;
64 struct udb_result_preparation_area_s /* {{{ */
67 size_t *instances_pos;
70 char **instances_buffer;
72 char **metadata_buffer;
73 char *plugin_instance;
75 struct udb_result_preparation_area_s *next;
77 typedef struct udb_result_preparation_area_s udb_result_preparation_area_t;
79 struct udb_query_preparation_area_s /* {{{ */
82 size_t plugin_instance_pos;
89 udb_result_preparation_area_t *result_prep_areas;
93 * Config Private functions
95 static int udb_config_set_string(char **ret_string, /* {{{ */
99 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
100 WARNING("db query utils: The `%s' config option "
101 "needs exactly one string argument.",
106 string = strdup(ci->values[0].value.string);
107 if (string == NULL) {
108 ERROR("db query utils: strdup failed.");
112 if (*ret_string != NULL)
114 *ret_string = string;
117 } /* }}} int udb_config_set_string */
119 static int udb_config_add_string(char ***ret_array, /* {{{ */
120 size_t *ret_array_len, oconfig_item_t *ci) {
124 if (ci->values_num < 1) {
125 WARNING("db query utils: The `%s' config option "
126 "needs at least one argument.",
131 for (int i = 0; i < ci->values_num; i++) {
132 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
133 WARNING("db query utils: Argument %i to the `%s' option "
140 array_len = *ret_array_len;
141 array = realloc(*ret_array, sizeof(char *) * (array_len + ci->values_num));
143 ERROR("db query utils: realloc failed.");
148 for (int i = 0; i < ci->values_num; i++) {
149 array[array_len] = strdup(ci->values[i].value.string);
150 if (array[array_len] == NULL) {
151 ERROR("db query utils: strdup failed.");
152 *ret_array_len = array_len;
158 *ret_array_len = array_len;
160 } /* }}} int udb_config_add_string */
162 static int udb_config_set_uint(unsigned int *ret_value, /* {{{ */
163 oconfig_item_t *ci) {
166 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
167 WARNING("db query utils: The `%s' config option "
168 "needs exactly one numeric argument.",
173 tmp = ci->values[0].value.number;
174 if ((tmp < 0.0) || (tmp > ((double)UINT_MAX)))
177 *ret_value = (unsigned int)(tmp + .5);
179 } /* }}} int udb_config_set_uint */
182 * Result private functions
184 static int udb_result_submit(udb_result_t *r, /* {{{ */
185 udb_result_preparation_area_t *r_area,
186 udb_query_t const *q,
187 udb_query_preparation_area_t *q_area) {
188 value_list_t vl = VALUE_LIST_INIT;
191 assert(r_area->ds != NULL);
192 assert(((size_t)r_area->ds->ds_num) == r->values_num);
193 assert(r->values_num > 0);
195 vl.values = calloc(r->values_num, sizeof(*vl.values));
196 if (vl.values == NULL) {
197 ERROR("db query utils: calloc failed.");
200 vl.values_len = r_area->ds->ds_num;
202 for (size_t i = 0; i < r->values_num; i++) {
203 char *value_str = r_area->values_buffer[i];
205 if (0 != parse_value(value_str, &vl.values[i], r_area->ds->ds[i].type)) {
206 ERROR("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
207 value_str, DS_TYPE_TO_STRING(r_area->ds->ds[i].type));
214 if (q_area->interval > 0)
215 vl.interval = q_area->interval;
217 sstrncpy(vl.host, q_area->host, sizeof(vl.host));
218 sstrncpy(vl.plugin, q_area->plugin, sizeof(vl.plugin));
219 sstrncpy(vl.type, r->type, sizeof(vl.type));
221 /* Set vl.plugin_instance */
222 if (q->plugin_instance_from != NULL) {
223 sstrncpy(vl.plugin_instance, r_area->plugin_instance,
224 sizeof(vl.plugin_instance));
226 sstrncpy(vl.plugin_instance, q_area->db_name, sizeof(vl.plugin_instance));
229 /* Set vl.type_instance {{{ */
230 if (r->instances_num == 0) {
231 if (r->instance_prefix == NULL)
232 vl.type_instance[0] = 0;
234 sstrncpy(vl.type_instance, r->instance_prefix, sizeof(vl.type_instance));
235 } else /* if ((r->instances_num > 0) */
237 if (r->instance_prefix == NULL) {
238 int status = strjoin(vl.type_instance, sizeof(vl.type_instance),
239 r_area->instances_buffer, r->instances_num, "-");
242 "udb_result_submit: creating type_instance failed with status %d.",
247 char tmp[DATA_MAX_NAME_LEN];
249 int status = strjoin(tmp, sizeof(tmp), r_area->instances_buffer,
250 r->instances_num, "-");
253 "udb_result_submit: creating type_instance failed with status %d.",
257 tmp[sizeof(tmp) - 1] = 0;
259 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s",
260 r->instance_prefix, tmp);
263 vl.type_instance[sizeof(vl.type_instance) - 1] = 0;
266 /* Annotate meta data. {{{ */
267 if (r->metadata_num > 0) {
268 vl.meta = meta_data_create();
269 if (vl.meta == NULL) {
270 ERROR("db query utils:: meta_data_create failed.");
274 for (size_t i = 0; i < r->metadata_num; i++) {
275 int status = meta_data_add_string(vl.meta, r->metadata[i],
276 r_area->metadata_buffer[i]);
278 ERROR("db query utils:: meta_data_add_string failed.");
279 meta_data_destroy(vl.meta);
287 plugin_dispatch_values(&vl);
289 if (r->metadata_num > 0) {
290 meta_data_destroy(vl.meta);
295 } /* }}} void udb_result_submit */
297 static void udb_result_finish_result(udb_result_t const *r, /* {{{ */
298 udb_result_preparation_area_t *prep_area) {
299 if ((r == NULL) || (prep_area == NULL))
302 prep_area->ds = NULL;
303 sfree(prep_area->instances_pos);
304 sfree(prep_area->values_pos);
305 sfree(prep_area->metadata_pos);
306 sfree(prep_area->instances_buffer);
307 sfree(prep_area->values_buffer);
308 sfree(prep_area->metadata_buffer);
309 } /* }}} void udb_result_finish_result */
311 static int udb_result_handle_result(udb_result_t *r, /* {{{ */
312 udb_query_preparation_area_t *q_area,
313 udb_result_preparation_area_t *r_area,
314 udb_query_t const *q,
315 char **column_values) {
316 assert(r && q_area && r_area);
318 for (size_t i = 0; i < r->instances_num; i++)
319 r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
321 for (size_t i = 0; i < r->values_num; i++)
322 r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
324 for (size_t i = 0; i < r->metadata_num; i++)
325 r_area->metadata_buffer[i] = column_values[r_area->metadata_pos[i]];
327 if (q->plugin_instance_from)
328 r_area->plugin_instance = column_values[q_area->plugin_instance_pos];
330 return udb_result_submit(r, r_area, q, q_area);
331 } /* }}} int udb_result_handle_result */
333 static int udb_result_prepare_result(udb_result_t const *r, /* {{{ */
334 udb_result_preparation_area_t *prep_area,
335 char **column_names, size_t column_num) {
336 if ((r == NULL) || (prep_area == NULL))
339 #define BAIL_OUT(status) \
340 prep_area->ds = NULL; \
341 sfree(prep_area->instances_pos); \
342 sfree(prep_area->values_pos); \
343 sfree(prep_area->metadata_pos); \
344 sfree(prep_area->instances_buffer); \
345 sfree(prep_area->values_buffer); \
346 sfree(prep_area->metadata_buffer); \
349 /* Make sure previous preparations are cleaned up. */
350 udb_result_finish_result(r, prep_area);
351 prep_area->instances_pos = NULL;
352 prep_area->values_pos = NULL;
353 prep_area->metadata_pos = NULL;
355 /* Read `ds' and check number of values {{{ */
356 prep_area->ds = plugin_get_ds(r->type);
357 if (prep_area->ds == NULL) {
358 ERROR("db query utils: udb_result_prepare_result: Type `%s' is not "
359 "known by the daemon. See types.db(5) for details.",
364 if (prep_area->ds->ds_num != r->values_num) {
365 ERROR("db query utils: udb_result_prepare_result: The type `%s' "
366 "requires exactly %" PRIsz
367 " value%s, but the configuration specifies %" PRIsz ".",
368 r->type, prep_area->ds->ds_num,
369 (prep_area->ds->ds_num == 1) ? "" : "s", r->values_num);
374 /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
375 * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
376 if (r->instances_num > 0) {
377 prep_area->instances_pos =
378 (size_t *)calloc(r->instances_num, sizeof(size_t));
379 if (prep_area->instances_pos == NULL) {
380 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
384 prep_area->instances_buffer =
385 (char **)calloc(r->instances_num, sizeof(char *));
386 if (prep_area->instances_buffer == NULL) {
387 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
390 } /* if (r->instances_num > 0) */
392 prep_area->values_pos = (size_t *)calloc(r->values_num, sizeof(size_t));
393 if (prep_area->values_pos == NULL) {
394 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
398 prep_area->values_buffer = (char **)calloc(r->values_num, sizeof(char *));
399 if (prep_area->values_buffer == NULL) {
400 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
404 prep_area->metadata_pos = (size_t *)calloc(r->metadata_num, sizeof(size_t));
405 if (prep_area->metadata_pos == NULL) {
406 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
410 prep_area->metadata_buffer = (char **)calloc(r->metadata_num, sizeof(char *));
411 if (prep_area->metadata_buffer == NULL) {
412 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
418 /* Determine the position of the plugin instance column {{{ */
419 for (size_t i = 0; i < r->instances_num; i++) {
422 for (j = 0; j < column_num; j++) {
423 if (strcasecmp(r->instances[i], column_names[j]) == 0) {
424 prep_area->instances_pos[i] = j;
429 if (j >= column_num) {
430 ERROR("db query utils: udb_result_prepare_result: "
431 "Column `%s' could not be found.",
435 } /* }}} for (i = 0; i < r->instances_num; i++) */
437 /* Determine the position of the value columns {{{ */
438 for (size_t i = 0; i < r->values_num; i++) {
441 for (j = 0; j < column_num; j++) {
442 if (strcasecmp(r->values[i], column_names[j]) == 0) {
443 prep_area->values_pos[i] = j;
448 if (j >= column_num) {
449 ERROR("db query utils: udb_result_prepare_result: "
450 "Column `%s' could not be found.",
454 } /* }}} for (i = 0; i < r->values_num; i++) */
456 /* Determine the position of the metadata columns {{{ */
457 for (size_t i = 0; i < r->metadata_num; i++) {
460 for (j = 0; j < column_num; j++) {
461 if (strcasecmp(r->metadata[i], column_names[j]) == 0) {
462 prep_area->metadata_pos[i] = j;
467 if (j >= column_num) {
468 ERROR("db query utils: udb_result_prepare_result: "
469 "Metadata column `%s' could not be found.",
473 } /* }}} for (i = 0; i < r->metadata_num; i++) */
477 } /* }}} int udb_result_prepare_result */
479 static void udb_result_free(udb_result_t *r) /* {{{ */
485 sfree(r->instance_prefix);
487 for (size_t i = 0; i < r->instances_num; i++)
488 sfree(r->instances[i]);
491 for (size_t i = 0; i < r->values_num; i++)
495 for (size_t i = 0; i < r->metadata_num; i++)
496 sfree(r->metadata[i]);
499 udb_result_free(r->next);
502 } /* }}} void udb_result_free */
504 static int udb_result_create(const char *query_name, /* {{{ */
505 udb_result_t **r_head, oconfig_item_t *ci) {
509 if (ci->values_num != 0) {
510 WARNING("db query utils: The `Result' block doesn't accept "
511 "any arguments. Ignoring %i argument%s.",
512 ci->values_num, (ci->values_num == 1) ? "" : "s");
515 r = calloc(1, sizeof(*r));
517 ERROR("db query utils: calloc failed.");
521 r->instance_prefix = NULL;
527 /* Fill the `udb_result_t' structure.. */
529 for (int i = 0; i < ci->children_num; i++) {
530 oconfig_item_t *child = ci->children + i;
532 if (strcasecmp("Type", child->key) == 0)
533 status = udb_config_set_string(&r->type, child);
534 else if (strcasecmp("InstancePrefix", child->key) == 0)
535 status = udb_config_set_string(&r->instance_prefix, child);
536 else if (strcasecmp("InstancesFrom", child->key) == 0)
537 status = udb_config_add_string(&r->instances, &r->instances_num, child);
538 else if (strcasecmp("ValuesFrom", child->key) == 0)
539 status = udb_config_add_string(&r->values, &r->values_num, child);
540 else if (strcasecmp("MetadataFrom", child->key) == 0)
541 status = udb_config_add_string(&r->metadata, &r->metadata_num, child);
543 WARNING("db query utils: Query `%s': Option `%s' not allowed here.",
544 query_name, child->key);
552 /* Check that all necessary options have been given. */
553 while (status == 0) {
554 if (r->type == NULL) {
555 WARNING("db query utils: `Type' not given for "
556 "result in query `%s'",
560 if (r->values == NULL) {
561 WARNING("db query utils: `ValuesFrom' not given for "
562 "result in query `%s'",
568 } /* while (status == 0) */
575 /* If all went well, add this result to the list of results. */
576 if (*r_head == NULL) {
582 while (last->next != NULL)
589 } /* }}} int udb_result_create */
592 * Query private functions
594 static void udb_query_free_one(udb_query_t *q) /* {{{ */
601 sfree(q->plugin_instance_from);
603 udb_result_free(q->results);
606 } /* }}} void udb_query_free_one */
609 * Query public functions
611 int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */
612 size_t *ret_query_list_len, oconfig_item_t *ci,
613 udb_query_create_callback_t cb) {
614 udb_query_t **query_list;
615 size_t query_list_len;
620 if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
622 query_list = *ret_query_list;
623 query_list_len = *ret_query_list_len;
625 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
626 WARNING("db query utils: The `Query' block "
627 "needs exactly one string argument.");
631 q = calloc(1, sizeof(*q));
633 ERROR("db query utils: calloc failed.");
637 q->max_version = UINT_MAX;
640 q->plugin_instance_from = NULL;
642 status = udb_config_set_string(&q->name, ci);
648 /* Fill the `udb_query_t' structure.. */
649 for (int i = 0; i < ci->children_num; i++) {
650 oconfig_item_t *child = ci->children + i;
652 if (strcasecmp("Statement", child->key) == 0)
653 status = udb_config_set_string(&q->statement, child);
654 else if (strcasecmp("Result", child->key) == 0)
655 status = udb_result_create(q->name, &q->results, child);
656 else if (strcasecmp("MinVersion", child->key) == 0)
657 status = udb_config_set_uint(&q->min_version, child);
658 else if (strcasecmp("MaxVersion", child->key) == 0)
659 status = udb_config_set_uint(&q->max_version, child);
660 else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
661 status = udb_config_set_string(&q->plugin_instance_from, child);
663 /* Call custom callbacks */
664 else if (cb != NULL) {
665 status = (*cb)(q, child);
667 WARNING("db query utils: The configuration callback failed "
672 WARNING("db query utils: Query `%s': Option `%s' not allowed here.",
673 q->name, child->key);
681 /* Check that all necessary options have been given. */
683 if (q->statement == NULL) {
684 WARNING("db query utils: Query `%s': No `Statement' given.", q->name);
687 if (q->results == NULL) {
688 WARNING("db query utils: Query `%s': No (valid) `Result' block given.",
692 } /* if (status == 0) */
694 /* If all went well, add this query to the list of queries within the
695 * database structure. */
699 temp = realloc(query_list, sizeof(*query_list) * (query_list_len + 1));
701 ERROR("db query utils: realloc failed");
705 query_list[query_list_len] = q;
711 udb_query_free_one(q);
715 *ret_query_list = query_list;
716 *ret_query_list_len = query_list_len;
719 } /* }}} int udb_query_create */
721 void udb_query_free(udb_query_t **query_list, size_t query_list_len) /* {{{ */
723 if (query_list == NULL)
726 for (size_t i = 0; i < query_list_len; i++)
727 udb_query_free_one(query_list[i]);
730 } /* }}} void udb_query_free */
732 int udb_query_pick_from_list_by_name(const char *name, /* {{{ */
733 udb_query_t **src_list,
735 udb_query_t ***dst_list,
736 size_t *dst_list_len) {
739 if ((name == NULL) || (src_list == NULL) || (dst_list == NULL) ||
740 (dst_list_len == NULL)) {
741 ERROR("db query utils: udb_query_pick_from_list_by_name: "
742 "Invalid argument.");
747 for (size_t i = 0; i < src_list_len; i++) {
748 udb_query_t **tmp_list;
751 if (strcasecmp(name, src_list[i]->name) != 0)
754 tmp_list_len = *dst_list_len;
755 tmp_list = realloc(*dst_list, (tmp_list_len + 1) * sizeof(udb_query_t *));
756 if (tmp_list == NULL) {
757 ERROR("db query utils: realloc failed.");
761 tmp_list[tmp_list_len] = src_list[i];
764 *dst_list = tmp_list;
765 *dst_list_len = tmp_list_len;
768 } /* for (i = 0; i < src_list_len; i++) */
770 if (num_added <= 0) {
771 ERROR("db query utils: Cannot find query `%s'. Make sure the <Query> "
772 "block is above the database definition!",
776 DEBUG("db query utils: Added %i versions of query `%s'.", num_added, name);
780 } /* }}} int udb_query_pick_from_list_by_name */
782 int udb_query_pick_from_list(oconfig_item_t *ci, /* {{{ */
783 udb_query_t **src_list, size_t src_list_len,
784 udb_query_t ***dst_list, size_t *dst_list_len) {
787 if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL) ||
788 (dst_list_len == NULL)) {
789 ERROR("db query utils: udb_query_pick_from_list: "
790 "Invalid argument.");
794 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
795 ERROR("db query utils: The `%s' config option "
796 "needs exactly one string argument.",
800 name = ci->values[0].value.string;
802 return udb_query_pick_from_list_by_name(name, src_list, src_list_len,
803 dst_list, dst_list_len);
804 } /* }}} int udb_query_pick_from_list */
806 const char *udb_query_get_name(udb_query_t *q) /* {{{ */
812 } /* }}} const char *udb_query_get_name */
814 const char *udb_query_get_statement(udb_query_t *q) /* {{{ */
820 } /* }}} const char *udb_query_get_statement */
822 void udb_query_set_user_data(udb_query_t *q, void *user_data) /* {{{ */
827 q->user_data = user_data;
828 } /* }}} void udb_query_set_user_data */
830 void *udb_query_get_user_data(udb_query_t *q) /* {{{ */
836 } /* }}} void *udb_query_get_user_data */
838 int udb_query_check_version(udb_query_t *q, unsigned int version) /* {{{ */
843 if ((version < q->min_version) || (version > q->max_version))
847 } /* }}} int udb_query_check_version */
849 void udb_query_finish_result(udb_query_t const *q, /* {{{ */
850 udb_query_preparation_area_t *prep_area) {
851 udb_result_preparation_area_t *r_area;
854 if ((q == NULL) || (prep_area == NULL))
857 prep_area->column_num = 0;
858 sfree(prep_area->host);
859 sfree(prep_area->plugin);
860 sfree(prep_area->db_name);
862 prep_area->interval = 0;
864 for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL;
865 r = r->next, r_area = r_area->next) {
866 /* this may happen during error conditions of the caller */
869 udb_result_finish_result(r, r_area);
871 } /* }}} void udb_query_finish_result */
873 int udb_query_handle_result(udb_query_t const *q, /* {{{ */
874 udb_query_preparation_area_t *prep_area,
875 char **column_values) {
876 udb_result_preparation_area_t *r_area;
881 if ((q == NULL) || (prep_area == NULL))
884 if ((prep_area->column_num < 1) || (prep_area->host == NULL) ||
885 (prep_area->plugin == NULL) || (prep_area->db_name == NULL)) {
886 ERROR("db query utils: Query `%s': Query is not prepared; "
887 "can't handle result.",
892 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
894 for (size_t i = 0; i < prep_area->column_num; i++) {
895 DEBUG("db query utils: udb_query_handle_result (%s, %s): "
896 "column[%" PRIsz "] = %s;",
897 prep_area->db_name, q->name, i, column_values[i]);
903 for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL;
904 r = r->next, r_area = r_area->next) {
905 status = udb_result_handle_result(r, prep_area, r_area, q, column_values);
911 ERROR("db query utils: udb_query_handle_result (%s, %s): "
912 "All results failed.",
913 prep_area->db_name, q->name);
918 } /* }}} int udb_query_handle_result */
920 int udb_query_prepare_result(udb_query_t const *q, /* {{{ */
921 udb_query_preparation_area_t *prep_area,
922 const char *host, const char *plugin,
923 const char *db_name, char **column_names,
924 size_t column_num, cdtime_t interval) {
925 udb_result_preparation_area_t *r_area;
929 if ((q == NULL) || (prep_area == NULL))
932 udb_query_finish_result(q, prep_area);
934 prep_area->column_num = column_num;
935 prep_area->host = strdup(host);
936 prep_area->plugin = strdup(plugin);
937 prep_area->db_name = strdup(db_name);
939 prep_area->interval = interval;
941 if ((prep_area->host == NULL) || (prep_area->plugin == NULL) ||
942 (prep_area->db_name == NULL)) {
943 ERROR("db query utils: Query `%s': Prepare failed: Out of memory.",
945 udb_query_finish_result(q, prep_area);
949 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG
951 for (size_t i = 0; i < column_num; i++) {
952 DEBUG("db query utils: udb_query_prepare_result: "
953 "query = %s; column[%" PRIsz "] = %s;",
954 q->name, i, column_names[i]);
959 /* Determine the position of the PluginInstance column {{{ */
960 if (q->plugin_instance_from != NULL) {
963 for (i = 0; i < column_num; i++) {
964 if (strcasecmp(q->plugin_instance_from, column_names[i]) == 0) {
965 prep_area->plugin_instance_pos = i;
970 if (i >= column_num) {
971 ERROR("db query utils: udb_query_prepare_result: "
972 "Column `%s' from `PluginInstanceFrom' could not be found.",
973 q->plugin_instance_from);
974 udb_query_finish_result(q, prep_area);
980 for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL;
981 r = r->next, r_area = r_area->next) {
983 ERROR("db query utils: Query `%s': Invalid number of result "
984 "preparation areas.",
986 udb_query_finish_result(q, prep_area);
990 status = udb_result_prepare_result(r, r_area, column_names, column_num);
992 udb_query_finish_result(q, prep_area);
998 } /* }}} int udb_query_prepare_result */
1000 udb_query_preparation_area_t *
1001 udb_query_allocate_preparation_area(udb_query_t *q) /* {{{ */
1003 udb_query_preparation_area_t *q_area;
1004 udb_result_preparation_area_t **next_r_area;
1007 q_area = calloc(1, sizeof(*q_area));
1011 next_r_area = &q_area->result_prep_areas;
1012 for (r = q->results; r != NULL; r = r->next) {
1013 udb_result_preparation_area_t *r_area;
1015 r_area = calloc(1, sizeof(*r_area));
1016 if (r_area == NULL) {
1017 udb_result_preparation_area_t *a = q_area->result_prep_areas;
1020 udb_result_preparation_area_t *next = a->next;
1029 *next_r_area = r_area;
1030 next_r_area = &r_area->next;
1034 } /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
1036 void udb_query_delete_preparation_area(
1037 udb_query_preparation_area_t *q_area) /* {{{ */
1039 udb_result_preparation_area_t *r_area;
1044 r_area = q_area->result_prep_areas;
1045 while (r_area != NULL) {
1046 udb_result_preparation_area_t *area = r_area;
1048 r_area = r_area->next;
1050 sfree(area->instances_pos);
1051 sfree(area->values_pos);
1052 sfree(area->instances_buffer);
1053 sfree(area->values_buffer);
1057 sfree(q_area->host);
1058 sfree(q_area->plugin);
1059 sfree(q_area->db_name);
1062 } /* }}} void udb_query_delete_preparation_area */