1 /*
2 * syscollector - src/utils/dbi.c
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "utils/dbi.h"
30 #include <assert.h>
32 #include <dbi/dbi.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 /*
40 * private data types
41 */
43 typedef struct {
44 char *key;
45 char *value;
46 } sc_dbi_option_t;
48 struct sc_dbi_options {
49 sc_dbi_option_t *options;
50 size_t options_num;
51 };
53 struct sc_dbi_client {
54 char *driver;
55 char *database;
57 dbi_conn conn;
59 sc_dbi_options_t *options;
60 };
62 /*
63 * private helper functions
64 */
66 static const char *
67 sc_dbi_strerror(dbi_conn conn)
68 {
69 const char *errmsg = NULL;
70 dbi_conn_error(conn, &errmsg);
71 return errmsg;
72 } /* sc_dbi_strerror */
74 static int
75 sc_dbi_get_field(dbi_result res, unsigned int i,
76 int type, sc_data_t *data)
77 {
78 switch (type) {
79 case SC_TYPE_INTEGER:
80 data->data.integer = dbi_result_get_longlong_idx(res, i);
81 break;
82 case SC_TYPE_DECIMAL:
83 data->data.decimal = dbi_result_get_double_idx(res, i);
84 break;
85 case SC_TYPE_STRING:
86 data->data.string = dbi_result_get_string_idx(res, i);
87 break;
88 case SC_TYPE_DATETIME:
89 {
90 /* libdbi does not provide any higher resolutions than that */
91 time_t datetime = dbi_result_get_datetime_idx(res, i);
92 data->data.datetime = SECS_TO_SC_TIME(datetime);
93 }
94 break;
95 case SC_TYPE_BINARY:
96 {
97 size_t length = dbi_result_get_field_length_idx(res, i);
98 const unsigned char *datum = dbi_result_get_binary_idx(res, i);
99 data->data.binary.length = length;
100 data->data.binary.datum = datum;
101 }
102 break;
103 default:
104 fprintf(stderr, "dbi: Unexpected type %i while "
105 "parsing query result.\n", type);
106 return -1;
107 }
109 data->type = type;
110 return 0;
111 } /* sc_dbi_get_field */
113 static int
114 sc_dbi_get_data(sc_dbi_client_t *client, dbi_result res,
115 unsigned int num_fields, sc_dbi_data_cb callback)
116 {
117 sc_data_t data[num_fields];
118 int types[num_fields];
119 unsigned int i;
121 unsigned long long num_rows;
122 unsigned long long success = 0, n;
124 assert(client && res && callback);
125 assert(num_fields > 0);
127 for (i = 0; i < num_fields; ++i) {
128 types[i] = dbi_result_get_field_type_idx(res, i + 1);
129 if (types[i] == DBI_TYPE_ERROR) {
130 fprintf(stderr, "dbi: failed to fetch data: %s\n",
131 sc_dbi_strerror(client->conn));
132 return -1;
133 }
134 types[i] = DBI_TYPE_TO_SC(types[i]);
135 }
137 num_rows = dbi_result_get_numrows(res);
138 if (num_rows < 1)
139 return -1;
141 for (n = 0; n < num_rows; ++n) {
142 if (! dbi_result_seek_row(res, n + 1)) {
143 fprintf(stderr, "dbi: Failed to retrieve row %llu: %s\n",
144 n, sc_dbi_strerror(client->conn));
145 continue;
146 }
148 for (i = 0; i < num_fields; ++i)
149 if (sc_dbi_get_field(res, (unsigned int)(i + 1),
150 types[i], &data[i]))
151 continue;
153 if (callback(client, num_fields, data))
154 continue;
156 ++success;
157 }
159 if (! success)
160 return -1;
161 return 0;
162 } /* sc_dbi_get_data */
164 /*
165 * public API
166 */
168 sc_dbi_options_t *
169 sc_dbi_options_create(void)
170 {
171 sc_dbi_options_t *options;
173 options = malloc(sizeof(options));
174 if (! options)
175 return NULL;
177 options->options = NULL;
178 options->options_num = 0;
179 return options;
180 } /* sc_dbi_options_create */
182 int
183 sc_dbi_options_add(sc_dbi_options_t *options,
184 const char *key, const char *value)
185 {
186 sc_dbi_option_t *new;
188 if ((! options) || (! key) || (! value))
189 return -1;
191 new = realloc(options->options,
192 (options->options_num + 1) * sizeof(*options->options));
193 if (! new)
194 return -1;
196 options->options = new;
197 new = options->options + options->options_num;
199 new->key = strdup(key);
200 new->value = strdup(value);
202 if ((! new->key) || (! new->value)) {
203 if (new->key)
204 free(new->key);
205 if (new->value)
206 free(new->value);
207 return -1;
208 }
210 ++options->options_num;
211 return 0;
212 } /* sc_dbi_options_add */
214 void
215 sc_dbi_options_destroy(sc_dbi_options_t *options)
216 {
217 size_t i;
219 if (! options)
220 return;
222 for (i = 0; i < options->options_num; ++i) {
223 sc_dbi_option_t *opt = options->options + i;
225 if (opt->key)
226 free(opt->key);
227 if (opt->value)
228 free(opt->value);
229 }
231 if (options->options)
232 free(options->options);
233 options->options = NULL;
234 options->options_num = 0;
235 free(options);
236 } /* sc_dbi_options_destroy */
238 sc_dbi_client_t *
239 sc_dbi_client_create(const char *driver, const char *database)
240 {
241 sc_dbi_client_t *client;
243 if ((! driver) || (! database))
244 return NULL;
246 client = malloc(sizeof(*client));
247 if (! client)
248 return NULL;
249 memset(client, 0, sizeof(*client));
251 client->conn = NULL;
252 client->options = NULL;
254 client->driver = strdup(driver);
255 client->database = strdup(database);
256 if ((! client->driver) || (! client->database)) {
257 sc_dbi_client_destroy(client);
258 return NULL;
259 }
260 return client;
261 } /* sc_dbi_client_create */
263 int
264 sc_dbi_client_set_options(sc_dbi_client_t *client,
265 sc_dbi_options_t *options)
266 {
267 if (! client)
268 return -1;
270 if (client->options)
271 sc_dbi_options_destroy(client->options);
272 client->options = options;
273 return 0;
274 } /* sc_dbi_client_set_options */
276 int
277 sc_dbi_client_connect(sc_dbi_client_t *client)
278 {
279 dbi_driver driver;
280 size_t i;
282 if ((! client) || (! client->driver) || (! client->database))
283 return -1;
285 if (client->conn)
286 dbi_conn_close(client->conn);
288 driver = dbi_driver_open(client->driver);
289 if (! driver) {
290 fprintf(stderr, "dbi: failed to open DBI driver '%s'; "
291 "possibly it's not installed.\n",
292 client->driver);
294 fprintf(stderr, "dbi: known drivers:\n");
295 for (driver = dbi_driver_list(NULL); driver;
296 driver = dbi_driver_list(driver)) {
297 fprintf(stderr, "\t- %s\n", dbi_driver_get_name(driver));
298 }
299 return -1;
300 }
302 client->conn = dbi_conn_open(driver);
303 if (! client->conn) {
304 fprintf(stderr, "dbi: failed to open connection object.\n");
305 return -1;
306 }
308 if (client->options) {
309 for (i = 0; i < client->options->options_num; ++i) {
310 const char *opt;
312 if (! dbi_conn_set_option(client->conn,
313 client->options->options[i].key,
314 client->options->options[i].value))
315 continue;
316 /* else: error */
318 fprintf(stderr, "dbi: failed to set option '%s': %s\n",
319 client->options->options[i].key,
320 sc_dbi_strerror(client->conn));
322 fprintf(stderr, "dbi: known driver options:\n");
323 for (opt = dbi_conn_get_option_list(client->conn, NULL); opt;
324 opt = dbi_conn_get_option_list(client->conn, opt))
325 fprintf(stderr, "\t- %s\n", opt);
327 dbi_conn_close(client->conn);
328 return -1;
329 }
330 }
332 if (dbi_conn_set_option(client->conn, "dbname", client->database)) {
333 fprintf(stderr, "dbi: failed to set option 'dbname': %s\n",
334 sc_dbi_strerror(client->conn));
335 dbi_conn_close(client->conn);
336 return -1;
337 }
339 if (dbi_conn_connect(client->conn) < 0) {
340 fprintf(stderr, "dbi: failed to connect to database '%s': %s\n",
341 client->database, sc_dbi_strerror(client->conn));
342 dbi_conn_close(client->conn);
343 return -1;
344 }
345 return 0;
346 } /* sc_dbi_client_connect */
348 int
349 sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
350 sc_dbi_data_cb callback, int n, ...)
351 {
352 dbi_result res;
353 unsigned int num_fields;
355 int status;
357 if ((! client) || (! query))
358 return -1;
360 res = dbi_conn_query(client->conn, query);
361 if (! res) {
362 fprintf(stderr, "dbi: failed to execute query '%s': %s\n",
363 query, sc_dbi_strerror(client->conn));
364 return -1;
365 }
367 if (dbi_result_get_numrows(res) == DBI_ROW_ERROR) {
368 fprintf(stderr, "dbi: failed to fetch rows for query '%s': %s\n",
369 query, sc_dbi_strerror(client->conn));
370 dbi_result_free(res);
371 return -1;
372 }
374 if (dbi_result_get_numrows(res) < 1) { /* no data */
375 dbi_result_free(res);
376 return 0;
377 }
379 num_fields = dbi_result_get_numfields(res);
381 if (n >= 0) {
382 va_list types;
383 int i;
385 if (n != (int)num_fields) {
386 fprintf(stderr, "dbi: number of returned fields (%i) does not "
387 "match the number of requested fields (%i) "
388 "for query '%s'.\n", num_fields, n, query);
389 dbi_result_free(res);
390 return -1;
391 }
393 va_start(types, n);
394 status = 0;
396 for (i = 0; i < n; ++i) {
397 unsigned short field_type = dbi_result_get_field_type_idx(res,
398 (unsigned int)(i + 1));
400 unsigned int type = va_arg(types, unsigned int);
402 field_type = DBI_TYPE_TO_SC(field_type);
404 /* column count starts at 1 */
405 if ((unsigned int)field_type != type) {
406 fprintf(stderr, "dbi: type of column '%s' (%u) does not match "
407 "requested type (%u).\n",
408 dbi_result_get_field_name(res, (unsigned int)i + 1),
409 field_type, type);
410 status = -1;
411 }
412 }
414 va_end(types);
416 if (status) {
417 dbi_result_free(res);
418 return status;
419 }
420 }
422 if (num_fields < 1) { /* no data */
423 dbi_result_free(res);
424 return 0;
425 }
427 status = sc_dbi_get_data(client, res, num_fields, callback);
429 dbi_result_free(res);
430 return status;
431 } /* sc_dbi_exec_query */
433 void
434 sc_dbi_client_destroy(sc_dbi_client_t *client)
435 {
436 if (! client)
437 return;
439 if (client->driver)
440 free(client->driver);
441 client->driver = NULL;
443 if (client->database)
444 free(client->database);
445 client->database = NULL;
447 if (client->conn)
448 dbi_conn_close(client->conn);
450 if (client->options)
451 sc_dbi_options_destroy(client->options);
452 client->options = NULL;
454 free(client);
455 } /* sc_dbi_client_destroy */
457 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */