1 /**
2 * collectd - src/dbi.c
3 * Copyright (C) 2008-2013 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_db_query.h"
28 #include <dbi/dbi.h>
30 /*
31 * Data types
32 */
33 struct cdbi_driver_option_s /* {{{ */
34 {
35 char *key;
36 union
37 {
38 char *string;
39 int numeric;
40 } value;
41 _Bool is_numeric;
42 };
43 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
45 struct cdbi_database_s /* {{{ */
46 {
47 char *name;
48 char *select_db;
50 char *driver;
51 char *host;
52 cdbi_driver_option_t *driver_options;
53 size_t driver_options_num;
55 udb_query_preparation_area_t **q_prep_areas;
56 udb_query_t **queries;
57 size_t queries_num;
59 dbi_conn connection;
60 };
61 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
63 /*
64 * Global variables
65 */
66 static udb_query_t **queries = NULL;
67 static size_t queries_num = 0;
68 static cdbi_database_t **databases = NULL;
69 static size_t databases_num = 0;
71 static int cdbi_read_database (user_data_t *ud);
73 /*
74 * Functions
75 */
76 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
77 char *buffer, size_t buffer_size)
78 {
79 const char *msg;
80 int status;
82 if (conn == NULL)
83 {
84 sstrncpy (buffer, "connection is NULL", buffer_size);
85 return (buffer);
86 }
88 msg = NULL;
89 status = dbi_conn_error (conn, &msg);
90 if ((status >= 0) && (msg != NULL))
91 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
92 else
93 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
94 status);
96 return (buffer);
97 } /* }}} const char *cdbi_conn_error */
99 static int cdbi_result_get_field (dbi_result res, /* {{{ */
100 unsigned int index, char *buffer, size_t buffer_size)
101 {
102 unsigned short src_type;
104 src_type = dbi_result_get_field_type_idx (res, index);
105 if (src_type == DBI_TYPE_ERROR)
106 {
107 ERROR ("dbi plugin: cdbi_result_get: "
108 "dbi_result_get_field_type_idx failed.");
109 return (-1);
110 }
112 if (src_type == DBI_TYPE_INTEGER)
113 {
114 long long value;
116 value = dbi_result_get_longlong_idx (res, index);
117 ssnprintf (buffer, buffer_size, "%lli", value);
118 }
119 else if (src_type == DBI_TYPE_DECIMAL)
120 {
121 double value;
123 value = dbi_result_get_double_idx (res, index);
124 ssnprintf (buffer, buffer_size, "%63.15g", value);
125 }
126 else if (src_type == DBI_TYPE_STRING)
127 {
128 const char *value;
130 value = dbi_result_get_string_idx (res, index);
131 if (value == NULL)
132 sstrncpy (buffer, "", buffer_size);
133 else if (strcmp ("ERROR", value) == 0)
134 return (-1);
135 else
136 sstrncpy (buffer, value, buffer_size);
137 }
138 /* DBI_TYPE_BINARY */
139 /* DBI_TYPE_DATETIME */
140 else
141 {
142 const char *field_name;
144 field_name = dbi_result_get_field_name (res, index);
145 if (field_name == NULL)
146 field_name = "<unknown>";
148 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
149 "source type %hu.",
150 field_name, src_type);
151 return (-1);
152 }
154 return (0);
155 } /* }}} int cdbi_result_get_field */
157 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
158 {
159 size_t i;
161 if (db == NULL)
162 return;
164 sfree (db->name);
165 sfree (db->driver);
167 for (i = 0; i < db->driver_options_num; i++)
168 {
169 sfree (db->driver_options[i].key);
170 if (!db->driver_options[i].is_numeric)
171 sfree (db->driver_options[i].value.string);
172 }
173 sfree (db->driver_options);
175 if (db->q_prep_areas)
176 for (i = 0; i < db->queries_num; ++i)
177 udb_query_delete_preparation_area (db->q_prep_areas[i]);
178 free (db->q_prep_areas);
180 sfree (db);
181 } /* }}} void cdbi_database_free */
183 /* Configuration handling functions {{{
184 *
185 * <Plugin dbi>
186 * <Query "plugin_instance0">
187 * Statement "SELECT name, value FROM table"
188 * <Result>
189 * Type "gauge"
190 * InstancesFrom "name"
191 * ValuesFrom "value"
192 * </Result>
193 * ...
194 * </Query>
195 *
196 * <Database "plugin_instance1">
197 * Driver "mysql"
198 * DriverOption "hostname" "localhost"
199 * ...
200 * Query "plugin_instance0"
201 * </Database>
202 * </Plugin>
203 */
205 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
206 oconfig_item_t *ci)
207 {
208 cdbi_driver_option_t *option;
210 if ((ci->values_num != 2)
211 || (ci->values[0].type != OCONFIG_TYPE_STRING)
212 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
213 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
214 {
215 WARNING ("dbi plugin: The `DriverOption' config option "
216 "needs exactly two arguments.");
217 return (-1);
218 }
220 option = (cdbi_driver_option_t *) realloc (db->driver_options,
221 sizeof (*option) * (db->driver_options_num + 1));
222 if (option == NULL)
223 {
224 ERROR ("dbi plugin: realloc failed");
225 return (-1);
226 }
228 db->driver_options = option;
229 option = db->driver_options + db->driver_options_num;
230 memset (option, 0, sizeof (*option));
232 option->key = strdup (ci->values[0].value.string);
233 if (option->key == NULL)
234 {
235 ERROR ("dbi plugin: strdup failed.");
236 return (-1);
237 }
239 if (ci->values[1].type == OCONFIG_TYPE_STRING)
240 {
241 option->value.string = strdup (ci->values[1].value.string);
242 if (option->value.string == NULL)
243 {
244 ERROR ("dbi plugin: strdup failed.");
245 sfree (option->key);
246 return (-1);
247 }
248 }
249 else
250 {
251 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
252 option->value.numeric = (int) (ci->values[1].value.number + .5);
253 option->is_numeric = 1;
254 }
256 db->driver_options_num++;
257 return (0);
258 } /* }}} int cdbi_config_add_database_driver_option */
260 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
261 {
262 cdbi_database_t *db;
263 int status;
264 int i;
266 if ((ci->values_num != 1)
267 || (ci->values[0].type != OCONFIG_TYPE_STRING))
268 {
269 WARNING ("dbi plugin: The `Database' block "
270 "needs exactly one string argument.");
271 return (-1);
272 }
274 db = (cdbi_database_t *) malloc (sizeof (*db));
275 if (db == NULL)
276 {
277 ERROR ("dbi plugin: malloc failed.");
278 return (-1);
279 }
280 memset (db, 0, sizeof (*db));
282 status = cf_util_get_string (ci, &db->name);
283 if (status != 0)
284 {
285 sfree (db);
286 return (status);
287 }
289 /* Fill the `cdbi_database_t' structure.. */
290 for (i = 0; i < ci->children_num; i++)
291 {
292 oconfig_item_t *child = ci->children + i;
294 if (strcasecmp ("Driver", child->key) == 0)
295 status = cf_util_get_string (child, &db->driver);
296 else if (strcasecmp ("DriverOption", child->key) == 0)
297 status = cdbi_config_add_database_driver_option (db, child);
298 else if (strcasecmp ("SelectDB", child->key) == 0)
299 status = cf_util_get_string (child, &db->select_db);
300 else if (strcasecmp ("Query", child->key) == 0)
301 status = udb_query_pick_from_list (child, queries, queries_num,
302 &db->queries, &db->queries_num);
303 else if (strcasecmp ("Host", child->key) == 0)
304 status = cf_util_get_string (child, &db->host);
305 else
306 {
307 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
308 status = -1;
309 }
311 if (status != 0)
312 break;
313 }
315 /* Check that all necessary options have been given. */
316 while (status == 0)
317 {
318 if (db->driver == NULL)
319 {
320 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
321 status = -1;
322 }
323 if (db->driver_options_num == 0)
324 {
325 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
326 "This will likely not work.", db->name);
327 }
329 break;
330 } /* while (status == 0) */
332 while ((status == 0) && (db->queries_num > 0))
333 {
334 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
335 db->queries_num, sizeof (*db->q_prep_areas));
337 if (db->q_prep_areas == NULL)
338 {
339 WARNING ("dbi plugin: malloc failed");
340 status = -1;
341 break;
342 }
344 for (i = 0; i < db->queries_num; ++i)
345 {
346 db->q_prep_areas[i]
347 = udb_query_allocate_preparation_area (db->queries[i]);
349 if (db->q_prep_areas[i] == NULL)
350 {
351 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
352 status = -1;
353 break;
354 }
355 }
357 break;
358 }
360 /* If all went well, add this database to the global list of databases. */
361 if (status == 0)
362 {
363 cdbi_database_t **temp;
365 temp = (cdbi_database_t **) realloc (databases,
366 sizeof (*databases) * (databases_num + 1));
367 if (temp == NULL)
368 {
369 ERROR ("dbi plugin: realloc failed");
370 status = -1;
371 }
372 else
373 {
374 user_data_t ud;
375 char *name = NULL;
377 databases = temp;
378 databases[databases_num] = db;
379 databases_num++;
381 memset (&ud, 0, sizeof (ud));
382 ud.data = (void *) db;
383 ud.free_func = NULL;
384 name = ssnprintf_alloc("dbi:%s", db->name);
386 plugin_register_complex_read (/* group = */ NULL,
387 /* name = */ name ? name : db->name,
388 /* callback = */ cdbi_read_database,
389 /* interval = */ NULL,
390 /* user_data = */ &ud);
391 free (name);
392 }
393 }
395 if (status != 0)
396 {
397 cdbi_database_free (db);
398 return (-1);
399 }
401 return (0);
402 } /* }}} int cdbi_config_add_database */
404 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
405 {
406 int i;
408 for (i = 0; i < ci->children_num; i++)
409 {
410 oconfig_item_t *child = ci->children + i;
411 if (strcasecmp ("Query", child->key) == 0)
412 udb_query_create (&queries, &queries_num, child,
413 /* callback = */ NULL);
414 else if (strcasecmp ("Database", child->key) == 0)
415 cdbi_config_add_database (child);
416 else
417 {
418 WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key);
419 }
420 } /* for (ci->children) */
422 return (0);
423 } /* }}} int cdbi_config */
425 /* }}} End of configuration handling functions */
427 static int cdbi_init (void) /* {{{ */
428 {
429 static int did_init = 0;
430 int status;
432 if (did_init != 0)
433 return (0);
435 if (queries_num == 0)
436 {
437 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
438 "this plugin can't do anything useful, so we will returns an error.");
439 return (-1);
440 }
442 if (databases_num == 0)
443 {
444 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
445 "this plugin can't do anything useful, so we will returns an error.");
446 return (-1);
447 }
449 status = dbi_initialize (NULL);
450 if (status < 0)
451 {
452 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
453 status);
454 return (-1);
455 }
456 else if (status == 0)
457 {
458 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
459 "install at least one `DBD' or check your installation.");
460 return (-1);
461 }
462 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
463 status, (status == 1) ? "" : "s");
465 return (0);
466 } /* }}} int cdbi_init */
468 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
469 udb_query_t *q, udb_query_preparation_area_t *prep_area)
470 {
471 const char *statement;
472 dbi_result res;
473 size_t column_num;
474 char **column_names;
475 char **column_values;
476 int status;
477 size_t i;
479 /* Macro that cleans up dynamically allocated memory and returns the
480 * specified status. */
481 #define BAIL_OUT(status) \
482 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
483 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
484 if (res != NULL) { dbi_result_free (res); res = NULL; } \
485 return (status)
487 column_names = NULL;
488 column_values = NULL;
489 res = NULL;
491 statement = udb_query_get_statement (q);
492 assert (statement != NULL);
494 res = dbi_conn_query (db->connection, statement);
495 if (res == NULL)
496 {
497 char errbuf[1024];
498 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
499 "dbi_conn_query failed: %s",
500 db->name, udb_query_get_name (q),
501 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
502 BAIL_OUT (-1);
503 }
504 else /* Get the number of columns */
505 {
506 unsigned int db_status;
508 db_status = dbi_result_get_numfields (res);
509 if (db_status == DBI_FIELD_ERROR)
510 {
511 char errbuf[1024];
512 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
513 "dbi_result_get_numfields failed: %s",
514 db->name, udb_query_get_name (q),
515 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
516 BAIL_OUT (-1);
517 }
519 column_num = (size_t) db_status;
520 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
521 db->name, udb_query_get_name (q), column_num);
522 }
524 /* Allocate `column_names' and `column_values'. {{{ */
525 column_names = (char **) calloc (column_num, sizeof (char *));
526 if (column_names == NULL)
527 {
528 ERROR ("dbi plugin: malloc failed.");
529 BAIL_OUT (-1);
530 }
532 column_names[0] = (char *) calloc (column_num,
533 DATA_MAX_NAME_LEN * sizeof (char));
534 if (column_names[0] == NULL)
535 {
536 ERROR ("dbi plugin: malloc failed.");
537 BAIL_OUT (-1);
538 }
539 for (i = 1; i < column_num; i++)
540 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
542 column_values = (char **) calloc (column_num, sizeof (char *));
543 if (column_values == NULL)
544 {
545 ERROR ("dbi plugin: malloc failed.");
546 BAIL_OUT (-1);
547 }
549 column_values[0] = (char *) calloc (column_num,
550 DATA_MAX_NAME_LEN * sizeof (char));
551 if (column_values[0] == NULL)
552 {
553 ERROR ("dbi plugin: malloc failed.");
554 BAIL_OUT (-1);
555 }
556 for (i = 1; i < column_num; i++)
557 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
558 /* }}} */
560 /* Copy the field names to `column_names' */
561 for (i = 0; i < column_num; i++) /* {{{ */
562 {
563 const char *column_name;
565 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
566 if (column_name == NULL)
567 {
568 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
569 "Cannot retrieve name of field %zu.",
570 db->name, udb_query_get_name (q), i + 1);
571 BAIL_OUT (-1);
572 }
574 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
575 } /* }}} for (i = 0; i < column_num; i++) */
577 udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
578 /* plugin = */ "dbi", db->name,
579 column_names, column_num, /* interval = */ 0);
581 /* 0 = error; 1 = success; */
582 status = dbi_result_first_row (res); /* {{{ */
583 if (status != 1)
584 {
585 char errbuf[1024];
586 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
587 "dbi_result_first_row failed: %s. Maybe the statement didn't "
588 "return any rows?",
589 db->name, udb_query_get_name (q),
590 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
591 udb_query_finish_result (q, prep_area);
592 BAIL_OUT (-1);
593 } /* }}} */
595 /* Iterate over all rows and call `udb_query_handle_result' with each list of
596 * values. */
597 while (42) /* {{{ */
598 {
599 status = 0;
600 /* Copy the value of the columns to `column_values' */
601 for (i = 0; i < column_num; i++) /* {{{ */
602 {
603 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
604 column_values[i], DATA_MAX_NAME_LEN);
606 if (status != 0)
607 {
608 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
609 "cdbi_result_get_field (%zu) failed.",
610 db->name, udb_query_get_name (q), i + 1);
611 status = -1;
612 break;
613 }
614 } /* }}} for (i = 0; i < column_num; i++) */
616 /* If all values were copied successfully, call `udb_query_handle_result'
617 * to dispatch the row to the daemon. */
618 if (status == 0) /* {{{ */
619 {
620 status = udb_query_handle_result (q, prep_area, column_values);
621 if (status != 0)
622 {
623 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
624 "udb_query_handle_result failed.",
625 db->name, udb_query_get_name (q));
626 }
627 } /* }}} */
629 /* Get the next row from the database. */
630 status = dbi_result_next_row (res); /* {{{ */
631 if (status != 1)
632 {
633 if (dbi_conn_error (db->connection, NULL) != 0)
634 {
635 char errbuf[1024];
636 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
637 "dbi_result_next_row failed: %s.",
638 db->name, udb_query_get_name (q),
639 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
640 }
641 break;
642 } /* }}} */
643 } /* }}} while (42) */
645 /* Tell the db query interface that we're done with this query. */
646 udb_query_finish_result (q, prep_area);
648 /* Clean up and return `status = 0' (success) */
649 BAIL_OUT (0);
650 #undef BAIL_OUT
651 } /* }}} int cdbi_read_database_query */
653 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
654 {
655 dbi_driver driver;
656 dbi_conn connection;
657 size_t i;
658 int status;
660 if (db->connection != NULL)
661 {
662 status = dbi_conn_ping (db->connection);
663 if (status != 0) /* connection is alive */
664 return (0);
666 dbi_conn_close (db->connection);
667 db->connection = NULL;
668 }
670 driver = dbi_driver_open (db->driver);
671 if (driver == NULL)
672 {
673 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
674 db->driver);
675 INFO ("dbi plugin: Maybe the driver isn't installed? "
676 "Known drivers are:");
677 for (driver = dbi_driver_list (NULL);
678 driver != NULL;
679 driver = dbi_driver_list (driver))
680 {
681 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
682 }
683 return (-1);
684 }
686 connection = dbi_conn_open (driver);
687 if (connection == NULL)
688 {
689 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
690 db->driver);
691 return (-1);
692 }
694 /* Set all the driver options. Because this is a very very very generic
695 * interface, the error handling is kind of long. If an invalid option is
696 * encountered, it will get a list of options understood by the driver and
697 * report that as `INFO'. This way, users hopefully don't have too much
698 * trouble finding out how to configure the plugin correctly.. */
699 for (i = 0; i < db->driver_options_num; i++)
700 {
701 if (db->driver_options[i].is_numeric)
702 {
703 status = dbi_conn_set_option_numeric (connection,
704 db->driver_options[i].key, db->driver_options[i].value.numeric);
705 if (status != 0)
706 {
707 char errbuf[1024];
708 ERROR ("dbi plugin: cdbi_connect_database (%s): "
709 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
710 db->name,
711 db->driver_options[i].key, db->driver_options[i].value.numeric,
712 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
713 }
714 }
715 else
716 {
717 status = dbi_conn_set_option (connection,
718 db->driver_options[i].key, db->driver_options[i].value.string);
719 if (status != 0)
720 {
721 char errbuf[1024];
722 ERROR ("dbi plugin: cdbi_connect_database (%s): "
723 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
724 db->name,
725 db->driver_options[i].key, db->driver_options[i].value.string,
726 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
727 }
728 }
730 if (status != 0)
731 {
732 char const *opt;
734 INFO ("dbi plugin: This is a list of all options understood "
735 "by the `%s' driver:", db->driver);
736 for (opt = dbi_conn_get_option_list (connection, NULL);
737 opt != NULL;
738 opt = dbi_conn_get_option_list (connection, opt))
739 {
740 INFO ("dbi plugin: * %s", opt);
741 }
743 dbi_conn_close (connection);
744 return (-1);
745 }
746 } /* for (i = 0; i < db->driver_options_num; i++) */
748 status = dbi_conn_connect (connection);
749 if (status != 0)
750 {
751 char errbuf[1024];
752 ERROR ("dbi plugin: cdbi_connect_database (%s): "
753 "dbi_conn_connect failed: %s",
754 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
755 dbi_conn_close (connection);
756 return (-1);
757 }
759 if (db->select_db != NULL)
760 {
761 status = dbi_conn_select_db (connection, db->select_db);
762 if (status != 0)
763 {
764 char errbuf[1024];
765 WARNING ("dbi plugin: cdbi_connect_database (%s): "
766 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
767 db->name, db->select_db,
768 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
769 dbi_conn_close (connection);
770 return (-1);
771 }
772 }
774 db->connection = connection;
775 return (0);
776 } /* }}} int cdbi_connect_database */
778 static int cdbi_read_database (user_data_t *ud) /* {{{ */
779 {
780 cdbi_database_t *db = (cdbi_database_t *) ud->data;
781 size_t i;
782 int success;
783 int status;
785 unsigned int db_version;
787 status = cdbi_connect_database (db);
788 if (status != 0)
789 return (status);
790 assert (db->connection != NULL);
792 db_version = dbi_conn_get_engine_version (db->connection);
793 /* TODO: Complain if `db_version == 0' */
795 success = 0;
796 for (i = 0; i < db->queries_num; i++)
797 {
798 /* Check if we know the database's version and if so, if this query applies
799 * to that version. */
800 if ((db_version != 0)
801 && (udb_query_check_version (db->queries[i], db_version) == 0))
802 continue;
804 status = cdbi_read_database_query (db,
805 db->queries[i], db->q_prep_areas[i]);
806 if (status == 0)
807 success++;
808 }
810 if (success == 0)
811 {
812 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
813 return (-1);
814 }
816 return (0);
817 } /* }}} int cdbi_read_database */
819 static int cdbi_shutdown (void) /* {{{ */
820 {
821 size_t i;
823 for (i = 0; i < databases_num; i++)
824 {
825 if (databases[i]->connection != NULL)
826 {
827 dbi_conn_close (databases[i]->connection);
828 databases[i]->connection = NULL;
829 }
830 cdbi_database_free (databases[i]);
831 }
832 sfree (databases);
833 databases_num = 0;
835 udb_query_free (queries, queries_num);
836 queries = NULL;
837 queries_num = 0;
839 return (0);
840 } /* }}} int cdbi_shutdown */
842 void module_register (void) /* {{{ */
843 {
844 plugin_register_complex_config ("dbi", cdbi_config);
845 plugin_register_init ("dbi", cdbi_init);
846 plugin_register_shutdown ("dbi", cdbi_shutdown);
847 } /* }}} void module_register */
849 /*
850 * vim: shiftwidth=2 softtabstop=2 et fdm=marker
851 */