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 "configfile.h"
32 #include "utils_db_query.h"
37 struct udb_result_s; /* {{{ */
38 typedef struct udb_result_s udb_result_t;
42 char *instance_prefix;
53 struct udb_query_s /* {{{ */
58 char *plugin_instance_from;
60 unsigned int min_version;
61 unsigned int max_version;
63 udb_result_t *results;
66 struct udb_result_preparation_area_s /* {{{ */
69 size_t *instances_pos;
72 char **instances_buffer;
74 char **metadata_buffer;
75 char *plugin_instance;
77 struct udb_result_preparation_area_s *next;
79 typedef struct udb_result_preparation_area_s udb_result_preparation_area_t;
81 struct udb_query_preparation_area_s /* {{{ */
84 size_t plugin_instance_pos;
91 udb_result_preparation_area_t *result_prep_areas;
95 * Config Private functions
97 static int udb_config_set_string (char **ret_string, /* {{{ */
102 if ((ci->values_num != 1)
103 || (ci->values[0].type != OCONFIG_TYPE_STRING))
105 WARNING ("db query utils: The `%s' config option "
106 "needs exactly one string argument.", ci->key);
110 string = strdup (ci->values[0].value.string);
113 ERROR ("db query utils: strdup failed.");
117 if (*ret_string != NULL)
119 *ret_string = string;
122 } /* }}} int udb_config_set_string */
124 static int udb_config_add_string (char ***ret_array, /* {{{ */
125 size_t *ret_array_len, oconfig_item_t *ci)
130 if (ci->values_num < 1)
132 WARNING ("db query utils: The `%s' config option "
133 "needs at least one argument.", ci->key);
137 for (int i = 0; i < ci->values_num; i++)
139 if (ci->values[i].type != OCONFIG_TYPE_STRING)
141 WARNING ("db query utils: Argument %i to the `%s' option "
142 "is not a string.", i + 1, ci->key);
147 array_len = *ret_array_len;
148 array = realloc (*ret_array,
149 sizeof (char *) * (array_len + ci->values_num));
152 ERROR ("db query utils: realloc failed.");
157 for (int i = 0; i < ci->values_num; i++)
159 array[array_len] = strdup (ci->values[i].value.string);
160 if (array[array_len] == NULL)
162 ERROR ("db query utils: strdup failed.");
163 *ret_array_len = array_len;
169 *ret_array_len = array_len;
171 } /* }}} int udb_config_add_string */
173 static int udb_config_set_uint (unsigned int *ret_value, /* {{{ */
178 if ((ci->values_num != 1)
179 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
181 WARNING ("db query utils: The `%s' config option "
182 "needs exactly one numeric argument.", ci->key);
186 tmp = ci->values[0].value.number;
187 if ((tmp < 0.0) || (tmp > ((double) UINT_MAX)))
190 *ret_value = (unsigned int) (tmp + .5);
192 } /* }}} int udb_config_set_uint */
195 * Result private functions
197 static int udb_result_submit (udb_result_t *r, /* {{{ */
198 udb_result_preparation_area_t *r_area,
199 udb_query_t const *q, udb_query_preparation_area_t *q_area)
201 value_list_t vl = VALUE_LIST_INIT;
204 assert (r_area->ds != NULL);
205 assert (((size_t) r_area->ds->ds_num) == r->values_num);
206 assert (r->values_num > 0);
208 vl.values = calloc (r->values_num, sizeof (*vl.values));
209 if (vl.values == NULL)
211 ERROR ("db query utils: calloc failed.");
214 vl.values_len = r_area->ds->ds_num;
216 for (size_t i = 0; i < r->values_num; i++)
218 char *value_str = r_area->values_buffer[i];
220 if (0 != parse_value (value_str, &vl.values[i], r_area->ds->ds[i].type))
222 ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
223 value_str, DS_TYPE_TO_STRING (r_area->ds->ds[i].type));
230 if (q_area->interval > 0)
231 vl.interval = q_area->interval;
233 sstrncpy (vl.host, q_area->host, sizeof (vl.host));
234 sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
235 sstrncpy (vl.type, r->type, sizeof (vl.type));
237 /* Set vl.plugin_instance */
238 if (q->plugin_instance_from != NULL) {
239 sstrncpy (vl.plugin_instance, r_area->plugin_instance, sizeof (vl.plugin_instance));
242 sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.plugin_instance));
245 /* Set vl.type_instance {{{ */
246 if (r->instances_num == 0)
248 if (r->instance_prefix == NULL)
249 vl.type_instance[0] = 0;
251 sstrncpy (vl.type_instance, r->instance_prefix,
252 sizeof (vl.type_instance));
254 else /* if ((r->instances_num > 0) */
256 if (r->instance_prefix == NULL)
258 int status = strjoin (vl.type_instance, sizeof (vl.type_instance),
259 r_area->instances_buffer, r->instances_num, "-");
262 ERROR ("udb_result_submit: creating type_instance failed with status %d.",
269 char tmp[DATA_MAX_NAME_LEN];
271 int status = strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
272 r->instances_num, "-");
275 ERROR ("udb_result_submit: creating type_instance failed with status %d.",
279 tmp[sizeof (tmp) - 1] = 0;
281 snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
282 r->instance_prefix, tmp);
285 vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
288 /* Annotate meta data. {{{ */
289 if (r->metadata_num > 0)
291 vl.meta = meta_data_create ();
294 ERROR ("db query utils:: meta_data_create failed.");
298 for (size_t i = 0; i < r->metadata_num; i++)
300 int status = meta_data_add_string (vl.meta, r->metadata[i],
301 r_area->metadata_buffer[i]);
304 ERROR ("db query utils:: meta_data_add_string failed.");
305 meta_data_destroy (vl.meta);
313 plugin_dispatch_values (&vl);
315 if (r->metadata_num > 0)
317 meta_data_destroy (vl.meta);
322 } /* }}} void udb_result_submit */
324 static void udb_result_finish_result (udb_result_t const *r, /* {{{ */
325 udb_result_preparation_area_t *prep_area)
327 if ((r == NULL) || (prep_area == NULL))
330 prep_area->ds = NULL;
331 sfree (prep_area->instances_pos);
332 sfree (prep_area->values_pos);
333 sfree (prep_area->metadata_pos);
334 sfree (prep_area->instances_buffer);
335 sfree (prep_area->values_buffer);
336 sfree (prep_area->metadata_buffer);
337 } /* }}} void udb_result_finish_result */
339 static int udb_result_handle_result (udb_result_t *r, /* {{{ */
340 udb_query_preparation_area_t *q_area,
341 udb_result_preparation_area_t *r_area,
342 udb_query_t const *q, char **column_values)
344 assert (r && q_area && r_area);
346 for (size_t i = 0; i < r->instances_num; i++)
347 r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
349 for (size_t i = 0; i < r->values_num; i++)
350 r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
352 for (size_t i = 0; i < r->metadata_num; i++)
353 r_area->metadata_buffer[i] = column_values[r_area->metadata_pos[i]];
355 if (q->plugin_instance_from)
356 r_area->plugin_instance = column_values[q_area->plugin_instance_pos];
358 return udb_result_submit (r, r_area, q, q_area);
359 } /* }}} int udb_result_handle_result */
361 static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */
362 udb_result_preparation_area_t *prep_area,
363 char **column_names, size_t column_num)
365 if ((r == NULL) || (prep_area == NULL))
368 #define BAIL_OUT(status) \
369 prep_area->ds = NULL; \
370 sfree (prep_area->instances_pos); \
371 sfree (prep_area->values_pos); \
372 sfree (prep_area->metadata_pos); \
373 sfree (prep_area->instances_buffer); \
374 sfree (prep_area->values_buffer); \
375 sfree (prep_area->metadata_buffer); \
378 /* Make sure previous preparations are cleaned up. */
379 udb_result_finish_result (r, prep_area);
380 prep_area->instances_pos = NULL;
381 prep_area->values_pos = NULL;
382 prep_area->metadata_pos = NULL;
384 /* Read `ds' and check number of values {{{ */
385 prep_area->ds = plugin_get_ds (r->type);
386 if (prep_area->ds == NULL)
388 ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
389 "known by the daemon. See types.db(5) for details.",
394 if (prep_area->ds->ds_num != r->values_num)
396 ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
397 "requires exactly %zu value%s, but the configuration specifies %zu.",
399 prep_area->ds->ds_num, (prep_area->ds->ds_num == 1) ? "" : "s",
405 /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
406 * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
407 if (r->instances_num > 0)
409 prep_area->instances_pos
410 = (size_t *) calloc (r->instances_num, sizeof (size_t));
411 if (prep_area->instances_pos == NULL)
413 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
417 prep_area->instances_buffer
418 = (char **) calloc (r->instances_num, sizeof (char *));
419 if (prep_area->instances_buffer == NULL)
421 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
424 } /* if (r->instances_num > 0) */
426 prep_area->values_pos
427 = (size_t *) calloc (r->values_num, sizeof (size_t));
428 if (prep_area->values_pos == NULL)
430 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
434 prep_area->values_buffer
435 = (char **) calloc (r->values_num, sizeof (char *));
436 if (prep_area->values_buffer == NULL)
438 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
442 prep_area->metadata_pos
443 = (size_t *) calloc (r->metadata_num, sizeof (size_t));
444 if (prep_area->metadata_pos == NULL)
446 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
450 prep_area->metadata_buffer
451 = (char **) calloc (r->metadata_num, sizeof (char *));
452 if (prep_area->metadata_buffer == NULL)
454 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
460 /* Determine the position of the plugin instance column {{{ */
461 for (size_t i = 0; i < r->instances_num; i++)
465 for (j = 0; j < column_num; j++)
467 if (strcasecmp (r->instances[i], column_names[j]) == 0)
469 prep_area->instances_pos[i] = j;
476 ERROR ("db query utils: udb_result_prepare_result: "
477 "Column `%s' could not be found.",
481 } /* }}} for (i = 0; i < r->instances_num; i++) */
484 /* Determine the position of the value columns {{{ */
485 for (size_t i = 0; i < r->values_num; i++)
489 for (j = 0; j < column_num; j++)
491 if (strcasecmp (r->values[i], column_names[j]) == 0)
493 prep_area->values_pos[i] = j;
500 ERROR ("db query utils: udb_result_prepare_result: "
501 "Column `%s' could not be found.",
505 } /* }}} for (i = 0; i < r->values_num; i++) */
507 /* Determine the position of the metadata columns {{{ */
508 for (size_t i = 0; i < r->metadata_num; i++)
512 for (j = 0; j < column_num; j++)
514 if (strcasecmp (r->metadata[i], column_names[j]) == 0)
516 prep_area->metadata_pos[i] = j;
523 ERROR ("db query utils: udb_result_prepare_result: "
524 "Metadata column `%s' could not be found.",
528 } /* }}} for (i = 0; i < r->metadata_num; i++) */
532 } /* }}} int udb_result_prepare_result */
534 static void udb_result_free (udb_result_t *r) /* {{{ */
540 sfree (r->instance_prefix);
542 for (size_t i = 0; i < r->instances_num; i++)
543 sfree (r->instances[i]);
544 sfree (r->instances);
546 for (size_t i = 0; i < r->values_num; i++)
547 sfree (r->values[i]);
550 for (size_t i = 0; i < r->metadata_num; i++)
551 sfree (r->metadata[i]);
554 udb_result_free (r->next);
557 } /* }}} void udb_result_free */
559 static int udb_result_create (const char *query_name, /* {{{ */
560 udb_result_t **r_head, oconfig_item_t *ci)
565 if (ci->values_num != 0)
567 WARNING ("db query utils: The `Result' block doesn't accept "
568 "any arguments. Ignoring %i argument%s.",
569 ci->values_num, (ci->values_num == 1) ? "" : "s");
572 r = calloc (1, sizeof (*r));
575 ERROR ("db query utils: calloc failed.");
579 r->instance_prefix = NULL;
585 /* Fill the `udb_result_t' structure.. */
587 for (int i = 0; i < ci->children_num; i++)
589 oconfig_item_t *child = ci->children + i;
591 if (strcasecmp ("Type", child->key) == 0)
592 status = udb_config_set_string (&r->type, child);
593 else if (strcasecmp ("InstancePrefix", child->key) == 0)
594 status = udb_config_set_string (&r->instance_prefix, child);
595 else if (strcasecmp ("InstancesFrom", child->key) == 0)
596 status = udb_config_add_string (&r->instances, &r->instances_num, child);
597 else if (strcasecmp ("ValuesFrom", child->key) == 0)
598 status = udb_config_add_string (&r->values, &r->values_num, child);
599 else if (strcasecmp ("MetadataFrom", child->key) == 0)
600 status = udb_config_add_string (&r->metadata, &r->metadata_num, child);
603 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
604 query_name, child->key);
612 /* Check that all necessary options have been given. */
617 WARNING ("db query utils: `Type' not given for "
618 "result in query `%s'", query_name);
621 if (r->values == NULL)
623 WARNING ("db query utils: `ValuesFrom' not given for "
624 "result in query `%s'", query_name);
629 } /* while (status == 0) */
637 /* If all went well, add this result to the list of results. */
647 while (last->next != NULL)
654 } /* }}} int udb_result_create */
657 * Query private functions
659 static void udb_query_free_one (udb_query_t *q) /* {{{ */
665 sfree (q->statement);
666 sfree (q->plugin_instance_from);
668 udb_result_free (q->results);
671 } /* }}} void udb_query_free_one */
674 * Query public functions
676 int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */
677 size_t *ret_query_list_len, oconfig_item_t *ci,
678 udb_query_create_callback_t cb)
680 udb_query_t **query_list;
681 size_t query_list_len;
686 if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
688 query_list = *ret_query_list;
689 query_list_len = *ret_query_list_len;
691 if ((ci->values_num != 1)
692 || (ci->values[0].type != OCONFIG_TYPE_STRING))
694 WARNING ("db query utils: The `Query' block "
695 "needs exactly one string argument.");
699 q = calloc (1, sizeof (*q));
702 ERROR ("db query utils: calloc failed.");
706 q->max_version = UINT_MAX;
709 q->plugin_instance_from = NULL;
711 status = udb_config_set_string (&q->name, ci);
718 /* Fill the `udb_query_t' structure.. */
719 for (int i = 0; i < ci->children_num; i++)
721 oconfig_item_t *child = ci->children + i;
723 if (strcasecmp ("Statement", child->key) == 0)
724 status = udb_config_set_string (&q->statement, child);
725 else if (strcasecmp ("Result", child->key) == 0)
726 status = udb_result_create (q->name, &q->results, child);
727 else if (strcasecmp ("MinVersion", child->key) == 0)
728 status = udb_config_set_uint (&q->min_version, child);
729 else if (strcasecmp ("MaxVersion", child->key) == 0)
730 status = udb_config_set_uint (&q->max_version, child);
731 else if (strcasecmp ("PluginInstanceFrom", child->key) == 0)
732 status = udb_config_set_string (&q->plugin_instance_from, child);
734 /* Call custom callbacks */
737 status = (*cb) (q, child);
740 WARNING ("db query utils: The configuration callback failed "
741 "to handle `%s'.", child->key);
746 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
747 q->name, child->key);
755 /* Check that all necessary options have been given. */
758 if (q->statement == NULL)
760 WARNING ("db query utils: Query `%s': No `Statement' given.", q->name);
763 if (q->results == NULL)
765 WARNING ("db query utils: Query `%s': No (valid) `Result' block given.",
769 } /* if (status == 0) */
771 /* If all went well, add this query to the list of queries within the
772 * database structure. */
777 temp = realloc (query_list,
778 sizeof (*query_list) * (query_list_len + 1));
781 ERROR ("db query utils: realloc failed");
787 query_list[query_list_len] = q;
794 udb_query_free_one (q);
798 *ret_query_list = query_list;
799 *ret_query_list_len = query_list_len;
802 } /* }}} int udb_query_create */
804 void udb_query_free (udb_query_t **query_list, size_t query_list_len) /* {{{ */
806 if (query_list == NULL)
809 for (size_t i = 0; i < query_list_len; i++)
810 udb_query_free_one (query_list[i]);
813 } /* }}} void udb_query_free */
815 int udb_query_pick_from_list_by_name (const char *name, /* {{{ */
816 udb_query_t **src_list, size_t src_list_len,
817 udb_query_t ***dst_list, size_t *dst_list_len)
821 if ((name == NULL) || (src_list == NULL) || (dst_list == NULL)
822 || (dst_list_len == NULL))
824 ERROR ("db query utils: udb_query_pick_from_list_by_name: "
825 "Invalid argument.");
830 for (size_t i = 0; i < src_list_len; i++)
832 udb_query_t **tmp_list;
835 if (strcasecmp (name, src_list[i]->name) != 0)
838 tmp_list_len = *dst_list_len;
839 tmp_list = realloc (*dst_list, (tmp_list_len + 1)
840 * sizeof (udb_query_t *));
841 if (tmp_list == NULL)
843 ERROR ("db query utils: realloc failed.");
847 tmp_list[tmp_list_len] = src_list[i];
850 *dst_list = tmp_list;
851 *dst_list_len = tmp_list_len;
854 } /* for (i = 0; i < src_list_len; i++) */
858 ERROR ("db query utils: Cannot find query `%s'. Make sure the <Query> "
859 "block is above the database definition!",
865 DEBUG ("db query utils: Added %i versions of query `%s'.",
870 } /* }}} int udb_query_pick_from_list_by_name */
872 int udb_query_pick_from_list (oconfig_item_t *ci, /* {{{ */
873 udb_query_t **src_list, size_t src_list_len,
874 udb_query_t ***dst_list, size_t *dst_list_len)
878 if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL)
879 || (dst_list_len == NULL))
881 ERROR ("db query utils: udb_query_pick_from_list: "
882 "Invalid argument.");
886 if ((ci->values_num != 1)
887 || (ci->values[0].type != OCONFIG_TYPE_STRING))
889 ERROR ("db query utils: The `%s' config option "
890 "needs exactly one string argument.", ci->key);
893 name = ci->values[0].value.string;
895 return (udb_query_pick_from_list_by_name (name,
896 src_list, src_list_len,
897 dst_list, dst_list_len));
898 } /* }}} int udb_query_pick_from_list */
900 const char *udb_query_get_name (udb_query_t *q) /* {{{ */
906 } /* }}} const char *udb_query_get_name */
908 const char *udb_query_get_statement (udb_query_t *q) /* {{{ */
913 return (q->statement);
914 } /* }}} const char *udb_query_get_statement */
916 void udb_query_set_user_data (udb_query_t *q, void *user_data) /* {{{ */
921 q->user_data = user_data;
922 } /* }}} void udb_query_set_user_data */
924 void *udb_query_get_user_data (udb_query_t *q) /* {{{ */
929 return (q->user_data);
930 } /* }}} void *udb_query_get_user_data */
932 int udb_query_check_version (udb_query_t *q, unsigned int version) /* {{{ */
937 if ((version < q->min_version) || (version > q->max_version))
941 } /* }}} int udb_query_check_version */
943 void udb_query_finish_result (udb_query_t const *q, /* {{{ */
944 udb_query_preparation_area_t *prep_area)
946 udb_result_preparation_area_t *r_area;
949 if ((q == NULL) || (prep_area == NULL))
952 prep_area->column_num = 0;
953 sfree (prep_area->host);
954 sfree (prep_area->plugin);
955 sfree (prep_area->db_name);
957 prep_area->interval = 0;
959 for (r = q->results, r_area = prep_area->result_prep_areas;
960 r != NULL; r = r->next, r_area = r_area->next)
962 /* this may happen during error conditions of the caller */
965 udb_result_finish_result (r, r_area);
967 } /* }}} void udb_query_finish_result */
969 int udb_query_handle_result (udb_query_t const *q, /* {{{ */
970 udb_query_preparation_area_t *prep_area, char **column_values)
972 udb_result_preparation_area_t *r_area;
977 if ((q == NULL) || (prep_area == NULL))
980 if ((prep_area->column_num < 1) || (prep_area->host == NULL)
981 || (prep_area->plugin == NULL) || (prep_area->db_name == NULL))
983 ERROR ("db query utils: Query `%s': Query is not prepared; "
984 "can't handle result.", q->name);
988 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
991 for (size_t i = 0; i < prep_area->column_num; i++)
993 DEBUG ("db query utils: udb_query_handle_result (%s, %s): "
995 prep_area->db_name, q->name, i, column_values[i]);
1001 for (r = q->results, r_area = prep_area->result_prep_areas;
1002 r != NULL; r = r->next, r_area = r_area->next)
1004 status = udb_result_handle_result (r, prep_area, r_area,
1012 ERROR ("db query utils: udb_query_handle_result (%s, %s): "
1013 "All results failed.", prep_area->db_name, q->name);
1018 } /* }}} int udb_query_handle_result */
1020 int udb_query_prepare_result (udb_query_t const *q, /* {{{ */
1021 udb_query_preparation_area_t *prep_area,
1022 const char *host, const char *plugin, const char *db_name,
1023 char **column_names, size_t column_num, cdtime_t interval)
1025 udb_result_preparation_area_t *r_area;
1029 if ((q == NULL) || (prep_area == NULL))
1032 udb_query_finish_result (q, prep_area);
1034 prep_area->column_num = column_num;
1035 prep_area->host = strdup (host);
1036 prep_area->plugin = strdup (plugin);
1037 prep_area->db_name = strdup (db_name);
1039 prep_area->interval = interval;
1041 if ((prep_area->host == NULL) || (prep_area->plugin == NULL)
1042 || (prep_area->db_name == NULL))
1044 ERROR ("db query utils: Query `%s': Prepare failed: Out of memory.", q->name);
1045 udb_query_finish_result (q, prep_area);
1049 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG
1052 for (size_t i = 0; i < column_num; i++)
1054 DEBUG ("db query utils: udb_query_prepare_result: "
1055 "query = %s; column[%zu] = %s;",
1056 q->name, i, column_names[i]);
1061 /* Determine the position of the PluginInstance column {{{ */
1062 if (q->plugin_instance_from != NULL)
1066 for (i = 0; i < column_num; i++)
1068 if (strcasecmp (q->plugin_instance_from, column_names[i]) == 0)
1070 prep_area->plugin_instance_pos = i;
1075 if (i >= column_num)
1077 ERROR ("db query utils: udb_query_prepare_result: "
1078 "Column `%s' from `PluginInstanceFrom' could not be found.",
1079 q->plugin_instance_from);
1080 udb_query_finish_result (q, prep_area);
1086 for (r = q->results, r_area = prep_area->result_prep_areas;
1087 r != NULL; r = r->next, r_area = r_area->next)
1091 ERROR ("db query utils: Query `%s': Invalid number of result "
1092 "preparation areas.", q->name);
1093 udb_query_finish_result (q, prep_area);
1097 status = udb_result_prepare_result (r, r_area, column_names, column_num);
1100 udb_query_finish_result (q, prep_area);
1106 } /* }}} int udb_query_prepare_result */
1108 udb_query_preparation_area_t *
1109 udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
1111 udb_query_preparation_area_t *q_area;
1112 udb_result_preparation_area_t **next_r_area;
1115 q_area = calloc (1, sizeof (*q_area));
1119 next_r_area = &q_area->result_prep_areas;
1120 for (r = q->results; r != NULL; r = r->next)
1122 udb_result_preparation_area_t *r_area;
1124 r_area = calloc (1, sizeof (*r_area));
1127 udb_result_preparation_area_t *a = q_area->result_prep_areas;
1131 udb_result_preparation_area_t *next = a->next;
1140 *next_r_area = r_area;
1141 next_r_area = &r_area->next;
1145 } /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
1148 udb_query_delete_preparation_area (udb_query_preparation_area_t *q_area) /* {{{ */
1150 udb_result_preparation_area_t *r_area;
1155 r_area = q_area->result_prep_areas;
1156 while (r_area != NULL)
1158 udb_result_preparation_area_t *area = r_area;
1160 r_area = r_area->next;
1162 sfree (area->instances_pos);
1163 sfree (area->values_pos);
1164 sfree (area->instances_buffer);
1165 sfree (area->values_buffer);
1169 sfree (q_area->host);
1170 sfree (q_area->plugin);
1171 sfree (q_area->db_name);
1174 } /* }}} void udb_query_delete_preparation_area */
1176 /* vim: set sw=2 sts=2 et fdm=marker : */