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>
30 #include "configfile.h"
31 #include "utils_db_query.h"
36 struct udb_result_s; /* {{{ */
37 typedef struct udb_result_s udb_result_t;
41 char *instance_prefix;
52 struct udb_query_s /* {{{ */
57 char *plugin_instance_from;
59 unsigned int min_version;
60 unsigned int max_version;
62 udb_result_t *results;
65 struct udb_result_preparation_area_s /* {{{ */
68 size_t *instances_pos;
71 char **instances_buffer;
73 char **metadata_buffer;
74 char *plugin_instance;
76 struct udb_result_preparation_area_s *next;
78 typedef struct udb_result_preparation_area_s udb_result_preparation_area_t;
80 struct udb_query_preparation_area_s /* {{{ */
83 size_t plugin_instance_pos;
90 udb_result_preparation_area_t *result_prep_areas;
94 * Config Private functions
96 static int udb_config_set_string (char **ret_string, /* {{{ */
101 if ((ci->values_num != 1)
102 || (ci->values[0].type != OCONFIG_TYPE_STRING))
104 WARNING ("db query utils: The `%s' config option "
105 "needs exactly one string argument.", ci->key);
109 string = strdup (ci->values[0].value.string);
112 ERROR ("db query utils: strdup failed.");
116 if (*ret_string != NULL)
118 *ret_string = string;
121 } /* }}} int udb_config_set_string */
123 static int udb_config_add_string (char ***ret_array, /* {{{ */
124 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 (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 (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;
206 assert (r_area->ds != NULL);
207 assert (((size_t) r_area->ds->ds_num) == r->values_num);
208 assert (r->values_num > 0);
210 vl.values = calloc (r->values_num, sizeof (*vl.values));
211 if (vl.values == NULL)
213 ERROR ("db query utils: calloc failed.");
216 vl.values_len = r_area->ds->ds_num;
218 for (i = 0; i < r->values_num; i++)
220 char *value_str = r_area->values_buffer[i];
222 if (0 != parse_value (value_str, &vl.values[i], r_area->ds->ds[i].type))
224 ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
225 value_str, DS_TYPE_TO_STRING (r_area->ds->ds[i].type));
232 if (q_area->interval > 0)
233 vl.interval = q_area->interval;
235 sstrncpy (vl.host, q_area->host, sizeof (vl.host));
236 sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
237 sstrncpy (vl.type, r->type, sizeof (vl.type));
239 /* Set vl.plugin_instance */
240 if (q->plugin_instance_from != NULL) {
241 sstrncpy (vl.plugin_instance, r_area->plugin_instance, sizeof (vl.plugin_instance));
244 sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.plugin_instance));
247 /* Set vl.type_instance {{{ */
248 if (r->instances_num == 0)
250 if (r->instance_prefix == NULL)
251 vl.type_instance[0] = 0;
253 sstrncpy (vl.type_instance, r->instance_prefix,
254 sizeof (vl.type_instance));
256 else /* if ((r->instances_num > 0) */
258 if (r->instance_prefix == NULL)
260 strjoin (vl.type_instance, sizeof (vl.type_instance),
261 r_area->instances_buffer, r->instances_num, "-");
265 char tmp[DATA_MAX_NAME_LEN];
267 strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
268 r->instances_num, "-");
269 tmp[sizeof (tmp) - 1] = 0;
271 snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
272 r->instance_prefix, tmp);
275 vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
278 /* Annotate meta data. {{{ */
279 if (r->metadata_num > 0)
281 vl.meta = meta_data_create ();
284 ERROR ("db query utils:: meta_data_create failed.");
288 for (i = 0; i < r->metadata_num; i++)
290 status = meta_data_add_string (vl.meta, r->metadata[i],
291 r_area->metadata_buffer[i]);
294 ERROR ("db query utils:: meta_data_add_string failed.");
295 meta_data_destroy (vl.meta);
303 plugin_dispatch_values (&vl);
305 if (r->metadata_num > 0)
307 meta_data_destroy (vl.meta);
312 } /* }}} void udb_result_submit */
314 static void udb_result_finish_result (udb_result_t const *r, /* {{{ */
315 udb_result_preparation_area_t *prep_area)
317 if ((r == NULL) || (prep_area == NULL))
320 prep_area->ds = NULL;
321 sfree (prep_area->instances_pos);
322 sfree (prep_area->values_pos);
323 sfree (prep_area->metadata_pos);
324 sfree (prep_area->instances_buffer);
325 sfree (prep_area->values_buffer);
326 sfree (prep_area->metadata_buffer);
327 } /* }}} void udb_result_finish_result */
329 static int udb_result_handle_result (udb_result_t *r, /* {{{ */
330 udb_query_preparation_area_t *q_area,
331 udb_result_preparation_area_t *r_area,
332 udb_query_t const *q, char **column_values)
336 assert (r && q_area && r_area);
338 for (i = 0; i < r->instances_num; i++)
339 r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
341 for (i = 0; i < r->values_num; i++)
342 r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
344 for (i = 0; i < r->metadata_num; i++)
345 r_area->metadata_buffer[i] = column_values[r_area->metadata_pos[i]];
347 if (q->plugin_instance_from)
348 r_area->plugin_instance = column_values[q_area->plugin_instance_pos];
350 return udb_result_submit (r, r_area, q, q_area);
351 } /* }}} int udb_result_handle_result */
353 static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */
354 udb_result_preparation_area_t *prep_area,
355 char **column_names, size_t column_num)
359 if ((r == NULL) || (prep_area == NULL))
362 #define BAIL_OUT(status) \
363 prep_area->ds = NULL; \
364 sfree (prep_area->instances_pos); \
365 sfree (prep_area->values_pos); \
366 sfree (prep_area->metadata_pos); \
367 sfree (prep_area->instances_buffer); \
368 sfree (prep_area->values_buffer); \
369 sfree (prep_area->metadata_buffer); \
372 /* Make sure previous preparations are cleaned up. */
373 udb_result_finish_result (r, prep_area);
374 prep_area->instances_pos = NULL;
375 prep_area->values_pos = NULL;
376 prep_area->metadata_pos = NULL;
378 /* Read `ds' and check number of values {{{ */
379 prep_area->ds = plugin_get_ds (r->type);
380 if (prep_area->ds == NULL)
382 ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
383 "known by the daemon. See types.db(5) for details.",
388 if (prep_area->ds->ds_num != r->values_num)
390 ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
391 "requires exactly %zu value%s, but the configuration specifies %zu.",
393 prep_area->ds->ds_num, (prep_area->ds->ds_num == 1) ? "" : "s",
399 /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
400 * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
401 if (r->instances_num > 0)
403 prep_area->instances_pos
404 = (size_t *) calloc (r->instances_num, sizeof (size_t));
405 if (prep_area->instances_pos == NULL)
407 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
411 prep_area->instances_buffer
412 = (char **) calloc (r->instances_num, sizeof (char *));
413 if (prep_area->instances_buffer == NULL)
415 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
418 } /* if (r->instances_num > 0) */
420 prep_area->values_pos
421 = (size_t *) calloc (r->values_num, sizeof (size_t));
422 if (prep_area->values_pos == NULL)
424 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
428 prep_area->values_buffer
429 = (char **) calloc (r->values_num, sizeof (char *));
430 if (prep_area->values_buffer == NULL)
432 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
436 prep_area->metadata_pos
437 = (size_t *) calloc (r->metadata_num, sizeof (size_t));
438 if (prep_area->metadata_pos == NULL)
440 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
444 prep_area->metadata_buffer
445 = (char **) calloc (r->metadata_num, sizeof (char *));
446 if (prep_area->metadata_buffer == NULL)
448 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
454 /* Determine the position of the plugin instance column {{{ */
455 for (i = 0; i < r->instances_num; i++)
459 for (j = 0; j < column_num; j++)
461 if (strcasecmp (r->instances[i], column_names[j]) == 0)
463 prep_area->instances_pos[i] = j;
470 ERROR ("db query utils: udb_result_prepare_result: "
471 "Column `%s' could not be found.",
475 } /* }}} for (i = 0; i < r->instances_num; i++) */
478 /* Determine the position of the value columns {{{ */
479 for (i = 0; i < r->values_num; i++)
483 for (j = 0; j < column_num; j++)
485 if (strcasecmp (r->values[i], column_names[j]) == 0)
487 prep_area->values_pos[i] = j;
494 ERROR ("db query utils: udb_result_prepare_result: "
495 "Column `%s' could not be found.",
499 } /* }}} for (i = 0; i < r->values_num; i++) */
501 /* Determine the position of the metadata columns {{{ */
502 for (i = 0; i < r->metadata_num; i++)
506 for (j = 0; j < column_num; j++)
508 if (strcasecmp (r->metadata[i], column_names[j]) == 0)
510 prep_area->metadata_pos[i] = j;
517 ERROR ("db query utils: udb_result_prepare_result: "
518 "Metadata column `%s' could not be found.",
522 } /* }}} for (i = 0; i < r->metadata_num; i++) */
526 } /* }}} int udb_result_prepare_result */
528 static void udb_result_free (udb_result_t *r) /* {{{ */
536 sfree (r->instance_prefix);
538 for (i = 0; i < r->instances_num; i++)
539 sfree (r->instances[i]);
540 sfree (r->instances);
542 for (i = 0; i < r->values_num; i++)
543 sfree (r->values[i]);
546 for (i = 0; i < r->metadata_num; i++)
547 sfree (r->metadata[i]);
550 udb_result_free (r->next);
553 } /* }}} void udb_result_free */
555 static int udb_result_create (const char *query_name, /* {{{ */
556 udb_result_t **r_head, oconfig_item_t *ci)
562 if (ci->values_num != 0)
564 WARNING ("db query utils: The `Result' block doesn't accept "
565 "any arguments. Ignoring %i argument%s.",
566 ci->values_num, (ci->values_num == 1) ? "" : "s");
569 r = calloc (1, sizeof (*r));
572 ERROR ("db query utils: calloc failed.");
576 r->instance_prefix = NULL;
582 /* Fill the `udb_result_t' structure.. */
584 for (i = 0; i < ci->children_num; i++)
586 oconfig_item_t *child = ci->children + i;
588 if (strcasecmp ("Type", child->key) == 0)
589 status = udb_config_set_string (&r->type, child);
590 else if (strcasecmp ("InstancePrefix", child->key) == 0)
591 status = udb_config_set_string (&r->instance_prefix, child);
592 else if (strcasecmp ("InstancesFrom", child->key) == 0)
593 status = udb_config_add_string (&r->instances, &r->instances_num, child);
594 else if (strcasecmp ("ValuesFrom", child->key) == 0)
595 status = udb_config_add_string (&r->values, &r->values_num, child);
596 else if (strcasecmp ("MetadataFrom", child->key) == 0)
597 status = udb_config_add_string (&r->metadata, &r->metadata_num, child);
600 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
601 query_name, child->key);
609 /* Check that all necessary options have been given. */
614 WARNING ("db query utils: `Type' not given for "
615 "result in query `%s'", query_name);
618 if (r->values == NULL)
620 WARNING ("db query utils: `ValuesFrom' not given for "
621 "result in query `%s'", query_name);
626 } /* while (status == 0) */
634 /* If all went well, add this result to the list of results. */
644 while (last->next != NULL)
651 } /* }}} int udb_result_create */
654 * Query private functions
656 static void udb_query_free_one (udb_query_t *q) /* {{{ */
662 sfree (q->statement);
663 sfree (q->plugin_instance_from);
665 udb_result_free (q->results);
668 } /* }}} void udb_query_free_one */
671 * Query public functions
673 int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */
674 size_t *ret_query_list_len, oconfig_item_t *ci,
675 udb_query_create_callback_t cb)
677 udb_query_t **query_list;
678 size_t query_list_len;
684 if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
686 query_list = *ret_query_list;
687 query_list_len = *ret_query_list_len;
689 if ((ci->values_num != 1)
690 || (ci->values[0].type != OCONFIG_TYPE_STRING))
692 WARNING ("db query utils: The `Query' block "
693 "needs exactly one string argument.");
697 q = calloc (1, sizeof (*q));
700 ERROR ("db query utils: calloc failed.");
704 q->max_version = UINT_MAX;
707 q->plugin_instance_from = NULL;
709 status = udb_config_set_string (&q->name, ci);
716 /* Fill the `udb_query_t' structure.. */
717 for (i = 0; i < ci->children_num; i++)
719 oconfig_item_t *child = ci->children + i;
721 if (strcasecmp ("Statement", child->key) == 0)
722 status = udb_config_set_string (&q->statement, child);
723 else if (strcasecmp ("Result", child->key) == 0)
724 status = udb_result_create (q->name, &q->results, child);
725 else if (strcasecmp ("MinVersion", child->key) == 0)
726 status = udb_config_set_uint (&q->min_version, child);
727 else if (strcasecmp ("MaxVersion", child->key) == 0)
728 status = udb_config_set_uint (&q->max_version, child);
729 else if (strcasecmp ("PluginInstanceFrom", child->key) == 0)
730 status = udb_config_set_string (&q->plugin_instance_from, child);
732 /* Call custom callbacks */
735 status = (*cb) (q, child);
738 WARNING ("db query utils: The configuration callback failed "
739 "to handle `%s'.", child->key);
744 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
745 q->name, child->key);
753 /* Check that all necessary options have been given. */
756 if (q->statement == NULL)
758 WARNING ("db query utils: Query `%s': No `Statement' given.", q->name);
761 if (q->results == NULL)
763 WARNING ("db query utils: Query `%s': No (valid) `Result' block given.",
767 } /* if (status == 0) */
769 /* If all went well, add this query to the list of queries within the
770 * database structure. */
775 temp = realloc (query_list,
776 sizeof (*query_list) * (query_list_len + 1));
779 ERROR ("db query utils: realloc failed");
785 query_list[query_list_len] = q;
792 udb_query_free_one (q);
796 *ret_query_list = query_list;
797 *ret_query_list_len = query_list_len;
800 } /* }}} int udb_query_create */
802 void udb_query_free (udb_query_t **query_list, size_t query_list_len) /* {{{ */
806 if (query_list == NULL)
809 for (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)
822 if ((name == NULL) || (src_list == NULL) || (dst_list == NULL)
823 || (dst_list_len == NULL))
825 ERROR ("db query utils: udb_query_pick_from_list_by_name: "
826 "Invalid argument.");
831 for (i = 0; i < src_list_len; i++)
833 udb_query_t **tmp_list;
836 if (strcasecmp (name, src_list[i]->name) != 0)
839 tmp_list_len = *dst_list_len;
840 tmp_list = realloc (*dst_list, (tmp_list_len + 1)
841 * sizeof (udb_query_t *));
842 if (tmp_list == NULL)
844 ERROR ("db query utils: realloc failed.");
848 tmp_list[tmp_list_len] = src_list[i];
851 *dst_list = tmp_list;
852 *dst_list_len = tmp_list_len;
855 } /* for (i = 0; i < src_list_len; i++) */
859 ERROR ("db query utils: Cannot find query `%s'. Make sure the <Query> "
860 "block is above the database definition!",
866 DEBUG ("db query utils: Added %i versions of query `%s'.",
871 } /* }}} int udb_query_pick_from_list_by_name */
873 int udb_query_pick_from_list (oconfig_item_t *ci, /* {{{ */
874 udb_query_t **src_list, size_t src_list_len,
875 udb_query_t ***dst_list, size_t *dst_list_len)
879 if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL)
880 || (dst_list_len == NULL))
882 ERROR ("db query utils: udb_query_pick_from_list: "
883 "Invalid argument.");
887 if ((ci->values_num != 1)
888 || (ci->values[0].type != OCONFIG_TYPE_STRING))
890 ERROR ("db query utils: The `%s' config option "
891 "needs exactly one string argument.", ci->key);
894 name = ci->values[0].value.string;
896 return (udb_query_pick_from_list_by_name (name,
897 src_list, src_list_len,
898 dst_list, dst_list_len));
899 } /* }}} int udb_query_pick_from_list */
901 const char *udb_query_get_name (udb_query_t *q) /* {{{ */
907 } /* }}} const char *udb_query_get_name */
909 const char *udb_query_get_statement (udb_query_t *q) /* {{{ */
914 return (q->statement);
915 } /* }}} const char *udb_query_get_statement */
917 void udb_query_set_user_data (udb_query_t *q, void *user_data) /* {{{ */
922 q->user_data = user_data;
923 } /* }}} void udb_query_set_user_data */
925 void *udb_query_get_user_data (udb_query_t *q) /* {{{ */
930 return (q->user_data);
931 } /* }}} void *udb_query_get_user_data */
933 int udb_query_check_version (udb_query_t *q, unsigned int version) /* {{{ */
938 if ((version < q->min_version) || (version > q->max_version))
942 } /* }}} int udb_query_check_version */
944 void udb_query_finish_result (udb_query_t const *q, /* {{{ */
945 udb_query_preparation_area_t *prep_area)
947 udb_result_preparation_area_t *r_area;
950 if ((q == NULL) || (prep_area == NULL))
953 prep_area->column_num = 0;
954 sfree (prep_area->host);
955 sfree (prep_area->plugin);
956 sfree (prep_area->db_name);
958 prep_area->interval = 0;
960 for (r = q->results, r_area = prep_area->result_prep_areas;
961 r != NULL; r = r->next, r_area = r_area->next)
963 /* this may happen during error conditions of the caller */
966 udb_result_finish_result (r, r_area);
968 } /* }}} void udb_query_finish_result */
970 int udb_query_handle_result (udb_query_t const *q, /* {{{ */
971 udb_query_preparation_area_t *prep_area, char **column_values)
973 udb_result_preparation_area_t *r_area;
978 if ((q == NULL) || (prep_area == NULL))
981 if ((prep_area->column_num < 1) || (prep_area->host == NULL)
982 || (prep_area->plugin == NULL) || (prep_area->db_name == NULL))
984 ERROR ("db query utils: Query `%s': Query is not prepared; "
985 "can't handle result.", q->name);
989 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
994 for (i = 0; i < prep_area->column_num; i++)
996 DEBUG ("db query utils: udb_query_handle_result (%s, %s): "
998 prep_area->db_name, q->name, i, column_values[i]);
1004 for (r = q->results, r_area = prep_area->result_prep_areas;
1005 r != NULL; r = r->next, r_area = r_area->next)
1007 status = udb_result_handle_result (r, prep_area, r_area,
1015 ERROR ("db query utils: udb_query_handle_result (%s, %s): "
1016 "All results failed.", prep_area->db_name, q->name);
1021 } /* }}} int udb_query_handle_result */
1023 int udb_query_prepare_result (udb_query_t const *q, /* {{{ */
1024 udb_query_preparation_area_t *prep_area,
1025 const char *host, const char *plugin, const char *db_name,
1026 char **column_names, size_t column_num, cdtime_t interval)
1028 udb_result_preparation_area_t *r_area;
1032 if ((q == NULL) || (prep_area == NULL))
1035 udb_query_finish_result (q, prep_area);
1037 prep_area->column_num = column_num;
1038 prep_area->host = strdup (host);
1039 prep_area->plugin = strdup (plugin);
1040 prep_area->db_name = strdup (db_name);
1042 prep_area->interval = interval;
1044 if ((prep_area->host == NULL) || (prep_area->plugin == NULL)
1045 || (prep_area->db_name == NULL))
1047 ERROR ("db query utils: Query `%s': Prepare failed: Out of memory.", q->name);
1048 udb_query_finish_result (q, prep_area);
1052 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG
1057 for (i = 0; i < column_num; i++)
1059 DEBUG ("db query utils: udb_query_prepare_result: "
1060 "query = %s; column[%zu] = %s;",
1061 q->name, i, column_names[i]);
1066 /* Determine the position of the PluginInstance column {{{ */
1067 if (q->plugin_instance_from != NULL)
1071 for (i = 0; i < column_num; i++)
1073 if (strcasecmp (q->plugin_instance_from, column_names[i]) == 0)
1075 prep_area->plugin_instance_pos = i;
1080 if (i >= column_num)
1082 ERROR ("db query utils: udb_query_prepare_result: "
1083 "Column `%s' from `PluginInstanceFrom' could not be found.",
1084 q->plugin_instance_from);
1085 udb_query_finish_result (q, prep_area);
1091 for (r = q->results, r_area = prep_area->result_prep_areas;
1092 r != NULL; r = r->next, r_area = r_area->next)
1096 ERROR ("db query utils: Query `%s': Invalid number of result "
1097 "preparation areas.", q->name);
1098 udb_query_finish_result (q, prep_area);
1102 status = udb_result_prepare_result (r, r_area, column_names, column_num);
1105 udb_query_finish_result (q, prep_area);
1111 } /* }}} int udb_query_prepare_result */
1113 udb_query_preparation_area_t *
1114 udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
1116 udb_query_preparation_area_t *q_area;
1117 udb_result_preparation_area_t **next_r_area;
1120 q_area = calloc (1, sizeof (*q_area));
1124 next_r_area = &q_area->result_prep_areas;
1125 for (r = q->results; r != NULL; r = r->next)
1127 udb_result_preparation_area_t *r_area;
1129 r_area = calloc (1, sizeof (*r_area));
1132 udb_result_preparation_area_t *a = q_area->result_prep_areas;
1136 udb_result_preparation_area_t *next = a->next;
1145 *next_r_area = r_area;
1146 next_r_area = &r_area->next;
1150 } /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
1153 udb_query_delete_preparation_area (udb_query_preparation_area_t *q_area) /* {{{ */
1155 udb_result_preparation_area_t *r_area;
1160 r_area = q_area->result_prep_areas;
1161 while (r_area != NULL)
1163 udb_result_preparation_area_t *area = r_area;
1165 r_area = r_area->next;
1167 sfree (area->instances_pos);
1168 sfree (area->values_pos);
1169 sfree (area->instances_buffer);
1170 sfree (area->values_buffer);
1174 sfree (q_area->host);
1175 sfree (q_area->plugin);
1176 sfree (q_area->db_name);
1179 } /* }}} void udb_query_delete_preparation_area */
1181 /* vim: set sw=2 sts=2 et fdm=marker : */