1 /*
2 * SysDB - 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"
29 #include "utils/error.h"
31 #include <assert.h>
33 #include <dbi/dbi.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 /*
41 * private data types
42 */
44 typedef struct {
45 char *key;
46 char *value;
47 } sdb_dbi_option_t;
49 struct sdb_dbi_options {
50 sdb_dbi_option_t *options;
51 size_t options_num;
52 };
54 struct sdb_dbi_client {
55 char *driver;
56 char *database;
58 dbi_conn conn;
60 sdb_dbi_options_t *options;
61 };
63 /*
64 * private helper functions
65 */
67 static const char *
68 sdb_dbi_strerror(dbi_conn conn)
69 {
70 const char *errmsg = NULL;
71 dbi_conn_error(conn, &errmsg);
72 return errmsg;
73 } /* sdb_dbi_strerror */
75 static int
76 sdb_dbi_get_field(dbi_result res, unsigned int i,
77 int type, sdb_data_t *data)
78 {
79 switch (type) {
80 case SDB_TYPE_INTEGER:
81 data->data.integer = dbi_result_get_longlong_idx(res, i);
82 break;
83 case SDB_TYPE_DECIMAL:
84 data->data.decimal = dbi_result_get_double_idx(res, i);
85 break;
86 case SDB_TYPE_STRING:
87 data->data.string = dbi_result_get_string_copy_idx(res, i);
88 break;
89 case SDB_TYPE_DATETIME:
90 {
91 /* libdbi does not provide any higher resolutions than that */
92 time_t datetime = dbi_result_get_datetime_idx(res, i);
93 data->data.datetime = SECS_TO_SDB_TIME(datetime);
94 }
95 break;
96 case SDB_TYPE_BINARY:
97 {
98 size_t length = dbi_result_get_field_length_idx(res, i);
99 unsigned char *datum = dbi_result_get_binary_copy_idx(res, i);
100 data->data.binary.length = length;
101 data->data.binary.datum = datum;
102 }
103 break;
104 default:
105 sdb_log(SDB_LOG_ERR, "dbi: Unexpected type %i while "
106 "parsing query result.", type);
107 return -1;
108 }
110 data->type = type;
111 return 0;
112 } /* sdb_dbi_get_field */
114 static int
115 sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res,
116 unsigned int num_fields, sdb_dbi_data_cb callback,
117 sdb_object_t *user_data)
118 {
119 sdb_data_t data[num_fields];
120 int types[num_fields];
121 unsigned int i;
123 unsigned long long num_rows;
124 unsigned long long success = 0, n;
126 assert(client && res && callback);
127 assert(num_fields > 0);
129 for (i = 0; i < num_fields; ++i) {
130 types[i] = dbi_result_get_field_type_idx(res, i + 1);
131 if (types[i] == DBI_TYPE_ERROR) {
132 sdb_log(SDB_LOG_ERR, "dbi: failed to fetch data: %s",
133 sdb_dbi_strerror(client->conn));
134 return -1;
135 }
136 types[i] = DBI_TYPE_TO_SC(types[i]);
137 }
139 num_rows = dbi_result_get_numrows(res);
140 if (num_rows < 1)
141 return -1;
143 for (n = 0; n < num_rows; ++n) {
144 int status;
146 if (! dbi_result_seek_row(res, n + 1)) {
147 sdb_log(SDB_LOG_ERR, "dbi: Failed to retrieve row %llu: %s",
148 n, sdb_dbi_strerror(client->conn));
149 continue;
150 }
152 for (i = 0; i < num_fields; ++i)
153 if (sdb_dbi_get_field(res, (unsigned int)(i + 1),
154 types[i], &data[i]))
155 continue;
157 status = callback(client, num_fields, data, user_data);
158 for (i = 0; i < num_fields; ++i) {
159 if ((data[i].type == SDB_TYPE_STRING) && (data[i].data.string))
160 free(data[i].data.string);
161 else if ((data[i].type == SDB_TYPE_BINARY)
162 && (data[i].data.binary.datum))
163 free(data[i].data.binary.datum);
164 }
166 if (status)
167 continue;
169 ++success;
170 }
172 if (! success)
173 return -1;
174 return 0;
175 } /* sdb_dbi_get_data */
177 /*
178 * public API
179 */
181 sdb_dbi_options_t *
182 sdb_dbi_options_create(void)
183 {
184 sdb_dbi_options_t *options;
186 options = malloc(sizeof(*options));
187 if (! options)
188 return NULL;
190 options->options = NULL;
191 options->options_num = 0;
192 return options;
193 } /* sdb_dbi_options_create */
195 int
196 sdb_dbi_options_add(sdb_dbi_options_t *options,
197 const char *key, const char *value)
198 {
199 sdb_dbi_option_t *new;
201 if ((! options) || (! key) || (! value))
202 return -1;
204 new = realloc(options->options,
205 (options->options_num + 1) * sizeof(*options->options));
206 if (! new)
207 return -1;
209 options->options = new;
210 new = options->options + options->options_num;
212 new->key = strdup(key);
213 new->value = strdup(value);
215 if ((! new->key) || (! new->value)) {
216 if (new->key)
217 free(new->key);
218 if (new->value)
219 free(new->value);
220 return -1;
221 }
223 ++options->options_num;
224 return 0;
225 } /* sdb_dbi_options_add */
227 void
228 sdb_dbi_options_destroy(sdb_dbi_options_t *options)
229 {
230 size_t i;
232 if (! options)
233 return;
235 for (i = 0; i < options->options_num; ++i) {
236 sdb_dbi_option_t *opt = options->options + i;
238 if (opt->key)
239 free(opt->key);
240 if (opt->value)
241 free(opt->value);
242 }
244 if (options->options)
245 free(options->options);
246 options->options = NULL;
247 options->options_num = 0;
248 free(options);
249 } /* sdb_dbi_options_destroy */
251 sdb_dbi_client_t *
252 sdb_dbi_client_create(const char *driver, const char *database)
253 {
254 sdb_dbi_client_t *client;
256 if ((! driver) || (! database))
257 return NULL;
259 client = malloc(sizeof(*client));
260 if (! client)
261 return NULL;
262 memset(client, 0, sizeof(*client));
264 client->conn = NULL;
265 client->options = NULL;
267 client->driver = strdup(driver);
268 client->database = strdup(database);
269 if ((! client->driver) || (! client->database)) {
270 sdb_dbi_client_destroy(client);
271 return NULL;
272 }
273 return client;
274 } /* sdb_dbi_client_create */
276 int
277 sdb_dbi_client_set_options(sdb_dbi_client_t *client,
278 sdb_dbi_options_t *options)
279 {
280 if (! client)
281 return -1;
283 if (client->options)
284 sdb_dbi_options_destroy(client->options);
285 client->options = options;
286 return 0;
287 } /* sdb_dbi_client_set_options */
289 int
290 sdb_dbi_client_connect(sdb_dbi_client_t *client)
291 {
292 dbi_driver driver;
293 size_t i;
295 if ((! client) || (! client->driver) || (! client->database))
296 return -1;
298 if (client->conn) {
299 dbi_conn_close(client->conn);
300 client->conn = NULL;
301 }
303 driver = dbi_driver_open(client->driver);
304 if (! driver) {
305 sdb_error_set("dbi: failed to open DBI driver '%s'; "
306 "possibly it's not installed.\n",
307 client->driver);
309 sdb_error_append("dbi: known drivers:\n");
310 for (driver = dbi_driver_list(NULL); driver;
311 driver = dbi_driver_list(driver)) {
312 sdb_error_append("\t- %s\n", dbi_driver_get_name(driver));
313 }
314 sdb_error_chomp();
315 sdb_error_log(SDB_LOG_ERR);
316 return -1;
317 }
319 client->conn = dbi_conn_open(driver);
320 if (! client->conn) {
321 sdb_log(SDB_LOG_ERR, "dbi: failed to open connection "
322 "object.");
323 return -1;
324 }
326 if (client->options) {
327 for (i = 0; i < client->options->options_num; ++i) {
328 const char *opt;
330 if (! dbi_conn_set_option(client->conn,
331 client->options->options[i].key,
332 client->options->options[i].value))
333 continue;
334 /* else: error */
336 sdb_error_set("dbi: failed to set option '%s': %s\n",
337 client->options->options[i].key,
338 sdb_dbi_strerror(client->conn));
340 sdb_error_append("dbi: known driver options:\n");
341 for (opt = dbi_conn_get_option_list(client->conn, NULL); opt;
342 opt = dbi_conn_get_option_list(client->conn, opt))
343 sdb_error_append("\t- %s\n", opt);
344 sdb_error_chomp();
345 sdb_error_log(SDB_LOG_ERR);
347 dbi_conn_close(client->conn);
348 client->conn = NULL;
349 return -1;
350 }
351 }
353 if (dbi_conn_set_option(client->conn, "dbname", client->database)) {
354 sdb_log(SDB_LOG_ERR, "dbi: failed to set option 'dbname': %s",
355 sdb_dbi_strerror(client->conn));
356 dbi_conn_close(client->conn);
357 client->conn = NULL;
358 return -1;
359 }
361 if (dbi_conn_connect(client->conn) < 0) {
362 sdb_log(SDB_LOG_ERR, "dbi: failed to connect to database '%s': %s",
363 client->database, sdb_dbi_strerror(client->conn));
364 dbi_conn_close(client->conn);
365 client->conn = NULL;
366 return -1;
367 }
368 return 0;
369 } /* sdb_dbi_client_connect */
371 int
372 sdb_dbi_client_check_conn(sdb_dbi_client_t *client)
373 {
374 if (! client)
375 return -1;
377 if (! client->conn)
378 return sdb_dbi_client_connect(client);
380 if (dbi_conn_ping(client->conn))
381 return 0;
382 return sdb_dbi_client_connect(client);
383 } /* sdb_dbi_client_check_conn */
385 int
386 sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query,
387 sdb_dbi_data_cb callback, sdb_object_t *user_data, int n, ...)
388 {
389 dbi_result res;
390 unsigned int num_fields;
392 int status;
394 if ((! client) || (! client->conn) || (! query))
395 return -1;
397 res = dbi_conn_query(client->conn, query);
398 if (! res) {
399 sdb_log(SDB_LOG_ERR, "dbi: failed to execute query '%s': %s",
400 query, sdb_dbi_strerror(client->conn));
401 return -1;
402 }
404 if (dbi_result_get_numrows(res) == DBI_ROW_ERROR) {
405 sdb_log(SDB_LOG_ERR, "dbi: failed to fetch rows for query "
406 "'%s': %s", query, sdb_dbi_strerror(client->conn));
407 dbi_result_free(res);
408 return -1;
409 }
411 if (dbi_result_get_numrows(res) < 1) { /* no data */
412 dbi_result_free(res);
413 return 0;
414 }
416 num_fields = dbi_result_get_numfields(res);
418 if (n >= 0) {
419 va_list types;
420 int i;
422 if (n != (int)num_fields) {
423 sdb_log(SDB_LOG_ERR, "dbi: number of returned fields (%i) "
424 "does not match the number of requested fields (%i) "
425 "for query '%s'.", num_fields, n, query);
426 dbi_result_free(res);
427 return -1;
428 }
430 va_start(types, n);
431 status = 0;
433 for (i = 0; i < n; ++i) {
434 unsigned short field_type = dbi_result_get_field_type_idx(res,
435 (unsigned int)(i + 1));
437 unsigned int type = va_arg(types, unsigned int);
439 field_type = DBI_TYPE_TO_SC(field_type);
441 /* column count starts at 1 */
442 if ((unsigned int)field_type != type) {
443 sdb_log(SDB_LOG_ERR, "dbi: type of column '%s' (%u) "
444 "does not match requested type (%u).",
445 dbi_result_get_field_name(res, (unsigned int)i + 1),
446 field_type, type);
447 status = -1;
448 }
449 }
451 va_end(types);
453 if (status) {
454 dbi_result_free(res);
455 return status;
456 }
457 }
459 if (num_fields < 1) { /* no data */
460 dbi_result_free(res);
461 return 0;
462 }
464 status = sdb_dbi_get_data(client, res, num_fields, callback, user_data);
466 dbi_result_free(res);
467 return status;
468 } /* sdb_dbi_exec_query */
470 void
471 sdb_dbi_client_destroy(sdb_dbi_client_t *client)
472 {
473 if (! client)
474 return;
476 if (client->driver)
477 free(client->driver);
478 client->driver = NULL;
480 if (client->database)
481 free(client->database);
482 client->database = NULL;
484 if (client->conn)
485 dbi_conn_close(client->conn);
486 client->conn = NULL;
488 if (client->options)
489 sdb_dbi_options_destroy(client->options);
490 client->options = NULL;
492 free(client);
493 } /* sdb_dbi_client_destroy */
495 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */