1 /*
2 * syscollector - src/include/utils/dbi.h
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 #ifndef SC_UTILS_DBI_H
29 #define SC_UTILS_DBI_H 1
31 #include "utils/time.h"
33 #include <inttypes.h>
34 #include <stddef.h>
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 /*
41 * sc_dbi_data_t:
42 * A datum retrieved from a single field of a query result row.
43 *
44 * The string and binary objects are managed by libdbi, thus, they must not be
45 * freed or modified. If you want to keep them, make sure to make a copy.
46 */
47 typedef struct {
48 int type;
49 union {
50 int64_t integer; /* DBI_TYPE_INTEGER */
51 double decimal; /* DBI_TYPE_DECIMAL */
52 const char *string; /* DBI_TYPE_STRING */
53 sc_time_t datetime; /* DBI_TYPE_DATETIME */
54 struct {
55 size_t length;
56 const unsigned char *datum;
57 } binary; /* DBI_TYPE_BINARY */
58 } data;
59 } sc_dbi_data_t;
61 struct sc_dbi_options;
62 typedef struct sc_dbi_options sc_dbi_options_t;
64 struct sc_dbi_client;
65 typedef struct sc_dbi_client sc_dbi_client_t;
67 typedef int (*sc_dbi_data_cb)(sc_dbi_client_t *, size_t, sc_dbi_data_t *);
69 /*
70 * sc_dbi_options_t:
71 * This object stores DBI connection options (key/value) (e.g. host, dbname,
72 * etc.). It may be used to dynamically create the list of options before
73 * applying it to some client object.
74 */
75 sc_dbi_options_t *
76 sc_dbi_options_create(void);
78 int
79 sc_dbi_options_add(sc_dbi_options_t *options,
80 const char *key, const char *value);
82 void
83 sc_dbi_options_destroy(sc_dbi_options_t *options);
85 /*
86 * sc_dbi_client_create:
87 * Creates a new DBI client object using the specified DBI / DBD 'driver' and
88 * connecting to the specified 'database'.
89 *
90 * Returns:
91 * - the client object on success
92 * - NULL else
93 */
94 sc_dbi_client_t *
95 sc_dbi_client_create(const char *driver, const char *database);
97 /*
98 * sc_dbi_client_set_options:
99 * Apply connection options to an existing client object. This has to be done
100 * before actually connecting to the database using sc_dbi_client_connect().
101 *
102 * Returns:
103 * - 0 on success
104 * - a negative value else
105 */
106 int
107 sc_dbi_client_set_options(sc_dbi_client_t *client,
108 sc_dbi_options_t *options);
110 /*
111 * sc_dbi_client_connect:
112 * Connect to the database using the options registered beforehand.
113 *
114 * This function may also be used to reconnect to the database.
115 *
116 * Returns:
117 * - 0 on success
118 * - a negative value else
119 */
120 int
121 sc_dbi_client_connect(sc_dbi_client_t *client);
123 /*
124 * sc_dbi_exec_query:
125 * Execute an SQL query on the database. The specified 'callback' will be
126 * called for each row returned from the query. If 'n' is a value equal to or
127 * greater than zero, it specifies the number of columns that are expected in
128 * the query result. For each column, the caller then needs to also specify
129 * the requested type (see the DBI_TYPE_* constants). If the number or types
130 * do not match, an error will be reported and the query will fail. That is,
131 * this allows to let sc_dbi_exec_query() do basic verification of the
132 * returned values.
133 *
134 * The callback will receive the client object and an array containing the
135 * field values of the current row. Any string / binary objects are managed by
136 * libdbi, thus, it must not be freed or modified. If you need to keep the
137 * object, make sure to make a copy of it.
138 *
139 * Returns:
140 * - 0 on success
141 * - a negative value else
142 */
143 int
144 sc_dbi_exec_query(sc_dbi_client_t *client, const char *query,
145 sc_dbi_data_cb callback, int n, ...);
147 /*
148 * sc_dbi_client_destroy:
149 * Disconnect from the database and destroy the client object.
150 */
151 void
152 sc_dbi_client_destroy(sc_dbi_client_t *client);
154 #ifdef __cplusplus
155 } /* extern "C" */
156 #endif
158 #endif /* ! SC_UTILS_DBI_H */
160 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */