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 %zu value%s, but the configuration specifies %zu.",
367 r->type, prep_area->ds->ds_num,
368 (prep_area->ds->ds_num == 1) ? "" : "s", r->values_num);
373 /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
374 * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
375 if (r->instances_num > 0) {
376 prep_area->instances_pos =
377 (size_t *)calloc(r->instances_num, sizeof(size_t));
378 if (prep_area->instances_pos == NULL) {
379 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
383 prep_area->instances_buffer =
384 (char **)calloc(r->instances_num, sizeof(char *));
385 if (prep_area->instances_buffer == NULL) {
386 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
389 } /* if (r->instances_num > 0) */
391 prep_area->values_pos = (size_t *)calloc(r->values_num, sizeof(size_t));
392 if (prep_area->values_pos == NULL) {
393 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
397 prep_area->values_buffer = (char **)calloc(r->values_num, sizeof(char *));
398 if (prep_area->values_buffer == NULL) {
399 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
403 prep_area->metadata_pos = (size_t *)calloc(r->metadata_num, sizeof(size_t));
404 if (prep_area->metadata_pos == NULL) {
405 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
409 prep_area->metadata_buffer = (char **)calloc(r->metadata_num, sizeof(char *));
410 if (prep_area->metadata_buffer == NULL) {
411 ERROR("db query utils: udb_result_prepare_result: calloc failed.");
417 /* Determine the position of the plugin instance column {{{ */
418 for (size_t i = 0; i < r->instances_num; i++) {
421 for (j = 0; j < column_num; j++) {
422 if (strcasecmp(r->instances[i], column_names[j]) == 0) {
423 prep_area->instances_pos[i] = j;
428 if (j >= column_num) {
429 ERROR("db query utils: udb_result_prepare_result: "
430 "Column `%s' could not be found.",
434 } /* }}} for (i = 0; i < r->instances_num; i++) */
436 /* Determine the position of the value columns {{{ */
437 for (size_t i = 0; i < r->values_num; i++) {
440 for (j = 0; j < column_num; j++) {
441 if (strcasecmp(r->values[i], column_names[j]) == 0) {
442 prep_area->values_pos[i] = j;
447 if (j >= column_num) {
448 ERROR("db query utils: udb_result_prepare_result: "
449 "Column `%s' could not be found.",
453 } /* }}} for (i = 0; i < r->values_num; i++) */
455 /* Determine the position of the metadata columns {{{ */
456 for (size_t i = 0; i < r->metadata_num; i++) {
459 for (j = 0; j < column_num; j++) {
460 if (strcasecmp(r->metadata[i], column_names[j]) == 0) {
461 prep_area->metadata_pos[i] = j;
466 if (j >= column_num) {
467 ERROR("db query utils: udb_result_prepare_result: "
468 "Metadata column `%s' could not be found.",
472 } /* }}} for (i = 0; i < r->metadata_num; i++) */
476 } /* }}} int udb_result_prepare_result */
478 static void udb_result_free(udb_result_t *r) /* {{{ */
484 sfree(r->instance_prefix);
486 for (size_t i = 0; i < r->instances_num; i++)
487 sfree(r->instances[i]);
490 for (size_t i = 0; i < r->values_num; i++)
494 for (size_t i = 0; i < r->metadata_num; i++)
495 sfree(r->metadata[i]);
498 udb_result_free(r->next);
501 } /* }}} void udb_result_free */
503 static int udb_result_create(const char *query_name, /* {{{ */
504 udb_result_t **r_head, oconfig_item_t *ci) {
508 if (ci->values_num != 0) {
509 WARNING("db query utils: The `Result' block doesn't accept "
510 "any arguments. Ignoring %i argument%s.",
511 ci->values_num, (ci->values_num == 1) ? "" : "s");
514 r = calloc(1, sizeof(*r));
516 ERROR("db query utils: calloc failed.");
520 r->instance_prefix = NULL;
526 /* Fill the `udb_result_t' structure.. */
528 for (int i = 0; i < ci->children_num; i++) {
529 oconfig_item_t *child = ci->children + i;
531 if (strcasecmp("Type", child->key) == 0)
532 status = udb_config_set_string(&r->type, child);
533 else if (strcasecmp("InstancePrefix", child->key) == 0)
534 status = udb_config_set_string(&r->instance_prefix, child);
535 else if (strcasecmp("InstancesFrom", child->key) == 0)
536 status = udb_config_add_string(&r->instances, &r->instances_num, child);
537 else if (strcasecmp("ValuesFrom", child->key) == 0)
538 status = udb_config_add_string(&r->values, &r->values_num, child);
539 else if (strcasecmp("MetadataFrom", child->key) == 0)
540 status = udb_config_add_string(&r->metadata, &r->metadata_num, child);
542 WARNING("db query utils: Query `%s': Option `%s' not allowed here.",
543 query_name, child->key);
551 /* Check that all necessary options have been given. */
552 while (status == 0) {
553 if (r->type == NULL) {
554 WARNING("db query utils: `Type' not given for "
555 "result in query `%s'",
559 if (r->values == NULL) {
560 WARNING("db query utils: `ValuesFrom' not given for "
561 "result in query `%s'",
567 } /* while (status == 0) */
574 /* If all went well, add this result to the list of results. */
575 if (*r_head == NULL) {
581 while (last->next != NULL)
588 } /* }}} int udb_result_create */
591 * Query private functions
593 static void udb_query_free_one(udb_query_t *q) /* {{{ */
600 sfree(q->plugin_instance_from);
602 udb_result_free(q->results);
605 } /* }}} void udb_query_free_one */
608 * Query public functions
610 int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */
611 size_t *ret_query_list_len, oconfig_item_t *ci,
612 udb_query_create_callback_t cb) {
613 udb_query_t **query_list;
614 size_t query_list_len;
619 if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
621 query_list = *ret_query_list;
622 query_list_len = *ret_query_list_len;
624 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
625 WARNING("db query utils: The `Query' block "
626 "needs exactly one string argument.");
630 q = calloc(1, sizeof(*q));
632 ERROR("db query utils: calloc failed.");
636 q->max_version = UINT_MAX;
639 q->plugin_instance_from = NULL;
641 status = udb_config_set_string(&q->name, ci);
647 /* Fill the `udb_query_t' structure.. */
648 for (int i = 0; i < ci->children_num; i++) {
649 oconfig_item_t *child = ci->children + i;
651 if (strcasecmp("Statement", child->key) == 0)
652 status = udb_config_set_string(&q->statement, child);
653 else if (strcasecmp("Result", child->key) == 0)
654 status = udb_result_create(q->name, &q->results, child);
655 else if (strcasecmp("MinVersion", child->key) == 0)
656 status = udb_config_set_uint(&q->min_version, child);
657 else if (strcasecmp("MaxVersion", child->key) == 0)
658 status = udb_config_set_uint(&q->max_version, child);
659 else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
660 status = udb_config_set_string(&q->plugin_instance_from, child);
662 /* Call custom callbacks */
663 else if (cb != NULL) {
664 status = (*cb)(q, child);
666 WARNING("db query utils: The configuration callback failed "
671 WARNING("db query utils: Query `%s': Option `%s' not allowed here.",
672 q->name, child->key);
680 /* Check that all necessary options have been given. */
682 if (q->statement == NULL) {
683 WARNING("db query utils: Query `%s': No `Statement' given.", q->name);
686 if (q->results == NULL) {
687 WARNING("db query utils: Query `%s': No (valid) `Result' block given.",
691 } /* if (status == 0) */
693 /* If all went well, add this query to the list of queries within the
694 * database structure. */
698 temp = realloc(query_list, sizeof(*query_list) * (query_list_len + 1));
700 ERROR("db query utils: realloc failed");
704 query_list[query_list_len] = q;
710 udb_query_free_one(q);
714 *ret_query_list = query_list;
715 *ret_query_list_len = query_list_len;
718 } /* }}} int udb_query_create */
720 void udb_query_free(udb_query_t **query_list, size_t query_list_len) /* {{{ */
722 if (query_list == NULL)
725 for (size_t i = 0; i < query_list_len; i++)
726 udb_query_free_one(query_list[i]);
729 } /* }}} void udb_query_free */
731 int udb_query_pick_from_list_by_name(const char *name, /* {{{ */
732 udb_query_t **src_list,
734 udb_query_t ***dst_list,
735 size_t *dst_list_len) {
738 if ((name == NULL) || (src_list == NULL) || (dst_list == NULL) ||
739 (dst_list_len == NULL)) {
740 ERROR("db query utils: udb_query_pick_from_list_by_name: "
741 "Invalid argument.");
746 for (size_t i = 0; i < src_list_len; i++) {
747 udb_query_t **tmp_list;
750 if (strcasecmp(name, src_list[i]->name) != 0)
753 tmp_list_len = *dst_list_len;
754 tmp_list = realloc(*dst_list, (tmp_list_len + 1) * sizeof(udb_query_t *));
755 if (tmp_list == NULL) {
756 ERROR("db query utils: realloc failed.");
760 tmp_list[tmp_list_len] = src_list[i];
763 *dst_list = tmp_list;
764 *dst_list_len = tmp_list_len;
767 } /* for (i = 0; i < src_list_len; i++) */
769 if (num_added <= 0) {
770 ERROR("db query utils: Cannot find query `%s'. Make sure the <Query> "
771 "block is above the database definition!",
775 DEBUG("db query utils: Added %i versions of query `%s'.", num_added, name);
779 } /* }}} int udb_query_pick_from_list_by_name */
781 int udb_query_pick_from_list(oconfig_item_t *ci, /* {{{ */
782 udb_query_t **src_list, size_t src_list_len,
783 udb_query_t ***dst_list, size_t *dst_list_len) {
786 if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL) ||
787 (dst_list_len == NULL)) {
788 ERROR("db query utils: udb_query_pick_from_list: "
789 "Invalid argument.");
793 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
794 ERROR("db query utils: The `%s' config option "
795 "needs exactly one string argument.",
799 name = ci->values[0].value.string;
801 return (udb_query_pick_from_list_by_name(name, src_list, src_list_len,
802 dst_list, dst_list_len));
803 } /* }}} int udb_query_pick_from_list */
805 const char *udb_query_get_name(udb_query_t *q) /* {{{ */
811 } /* }}} const char *udb_query_get_name */
813 const char *udb_query_get_statement(udb_query_t *q) /* {{{ */
818 return (q->statement);
819 } /* }}} const char *udb_query_get_statement */
821 void udb_query_set_user_data(udb_query_t *q, void *user_data) /* {{{ */
826 q->user_data = user_data;
827 } /* }}} void udb_query_set_user_data */
829 void *udb_query_get_user_data(udb_query_t *q) /* {{{ */
834 return (q->user_data);
835 } /* }}} void *udb_query_get_user_data */
837 int udb_query_check_version(udb_query_t *q, unsigned int version) /* {{{ */
842 if ((version < q->min_version) || (version > q->max_version))
846 } /* }}} int udb_query_check_version */
848 void udb_query_finish_result(udb_query_t const *q, /* {{{ */
849 udb_query_preparation_area_t *prep_area) {
850 udb_result_preparation_area_t *r_area;
853 if ((q == NULL) || (prep_area == NULL))
856 prep_area->column_num = 0;
857 sfree(prep_area->host);
858 sfree(prep_area->plugin);
859 sfree(prep_area->db_name);
861 prep_area->interval = 0;
863 for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL;
864 r = r->next, r_area = r_area->next) {
865 /* this may happen during error conditions of the caller */
868 udb_result_finish_result(r, r_area);
870 } /* }}} void udb_query_finish_result */
872 int udb_query_handle_result(udb_query_t const *q, /* {{{ */
873 udb_query_preparation_area_t *prep_area,
874 char **column_values) {
875 udb_result_preparation_area_t *r_area;
880 if ((q == NULL) || (prep_area == NULL))
883 if ((prep_area->column_num < 1) || (prep_area->host == NULL) ||
884 (prep_area->plugin == NULL) || (prep_area->db_name == NULL)) {
885 ERROR("db query utils: Query `%s': Query is not prepared; "
886 "can't handle result.",
891 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
893 for (size_t i = 0; i < prep_area->column_num; i++) {
894 DEBUG("db query utils: udb_query_handle_result (%s, %s): "
896 prep_area->db_name, q->name, i, column_values[i]);
902 for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL;
903 r = r->next, r_area = r_area->next) {
904 status = udb_result_handle_result(r, prep_area, r_area, q, column_values);
910 ERROR("db query utils: udb_query_handle_result (%s, %s): "
911 "All results failed.",
912 prep_area->db_name, q->name);
917 } /* }}} int udb_query_handle_result */
919 int udb_query_prepare_result(udb_query_t const *q, /* {{{ */
920 udb_query_preparation_area_t *prep_area,
921 const char *host, const char *plugin,
922 const char *db_name, char **column_names,
923 size_t column_num, cdtime_t interval) {
924 udb_result_preparation_area_t *r_area;
928 if ((q == NULL) || (prep_area == NULL))
931 udb_query_finish_result(q, prep_area);
933 prep_area->column_num = column_num;
934 prep_area->host = strdup(host);
935 prep_area->plugin = strdup(plugin);
936 prep_area->db_name = strdup(db_name);
938 prep_area->interval = interval;
940 if ((prep_area->host == NULL) || (prep_area->plugin == NULL) ||
941 (prep_area->db_name == NULL)) {
942 ERROR("db query utils: Query `%s': Prepare failed: Out of memory.",
944 udb_query_finish_result(q, prep_area);
948 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG
950 for (size_t i = 0; i < column_num; i++) {
951 DEBUG("db query utils: udb_query_prepare_result: "
952 "query = %s; column[%zu] = %s;",
953 q->name, i, column_names[i]);
958 /* Determine the position of the PluginInstance column {{{ */
959 if (q->plugin_instance_from != NULL) {
962 for (i = 0; i < column_num; i++) {
963 if (strcasecmp(q->plugin_instance_from, column_names[i]) == 0) {
964 prep_area->plugin_instance_pos = i;
969 if (i >= column_num) {
970 ERROR("db query utils: udb_query_prepare_result: "
971 "Column `%s' from `PluginInstanceFrom' could not be found.",
972 q->plugin_instance_from);
973 udb_query_finish_result(q, prep_area);
979 for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL;
980 r = r->next, r_area = r_area->next) {
982 ERROR("db query utils: Query `%s': Invalid number of result "
983 "preparation areas.",
985 udb_query_finish_result(q, prep_area);
989 status = udb_result_prepare_result(r, r_area, column_names, column_num);
991 udb_query_finish_result(q, prep_area);
997 } /* }}} int udb_query_prepare_result */
999 udb_query_preparation_area_t *
1000 udb_query_allocate_preparation_area(udb_query_t *q) /* {{{ */
1002 udb_query_preparation_area_t *q_area;
1003 udb_result_preparation_area_t **next_r_area;
1006 q_area = calloc(1, sizeof(*q_area));
1010 next_r_area = &q_area->result_prep_areas;
1011 for (r = q->results; r != NULL; r = r->next) {
1012 udb_result_preparation_area_t *r_area;
1014 r_area = calloc(1, sizeof(*r_area));
1015 if (r_area == NULL) {
1016 udb_result_preparation_area_t *a = q_area->result_prep_areas;
1019 udb_result_preparation_area_t *next = a->next;
1028 *next_r_area = r_area;
1029 next_r_area = &r_area->next;
1033 } /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
1035 void udb_query_delete_preparation_area(
1036 udb_query_preparation_area_t *q_area) /* {{{ */
1038 udb_result_preparation_area_t *r_area;
1043 r_area = q_area->result_prep_areas;
1044 while (r_area != NULL) {
1045 udb_result_preparation_area_t *area = r_area;
1047 r_area = r_area->next;
1049 sfree(area->instances_pos);
1050 sfree(area->values_pos);
1051 sfree(area->instances_buffer);
1052 sfree(area->values_buffer);
1056 sfree(q_area->host);
1057 sfree(q_area->plugin);
1058 sfree(q_area->db_name);
1061 } /* }}} void udb_query_delete_preparation_area */