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