Code

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