1 /*
2 * SysDB - 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 SDB_UTILS_DBI_H
29 #define SDB_UTILS_DBI_H 1
31 #include "core/object.h"
32 #include "core/data.h"
34 #include <stddef.h>
36 /* translate libdbi types to SysDB types */
37 #define DBI_TYPE_TO_SDB(dt) \
38 (((dt) == DBI_TYPE_INTEGER) \
39 ? SDB_TYPE_INTEGER \
40 : ((dt) == DBI_TYPE_DECIMAL) \
41 ? SDB_TYPE_DECIMAL \
42 : ((dt) == DBI_TYPE_STRING) \
43 ? SDB_TYPE_STRING \
44 : ((dt) == DBI_TYPE_DATETIME) \
45 ? SDB_TYPE_DATETIME \
46 : ((dt) == DBI_TYPE_BINARY) \
47 ? SDB_TYPE_BINARY : 0)
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
53 struct sdb_dbi_options;
54 typedef struct sdb_dbi_options sdb_dbi_options_t;
56 struct sdb_dbi_client;
57 typedef struct sdb_dbi_client sdb_dbi_client_t;
59 typedef int (*sdb_dbi_data_cb)(sdb_dbi_client_t *,
60 size_t, sdb_data_t *, sdb_object_t *);
62 /*
63 * sdb_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 sdb_dbi_options_t *
69 sdb_dbi_options_create(void);
71 int
72 sdb_dbi_options_add(sdb_dbi_options_t *options,
73 const char *key, const char *value);
75 void
76 sdb_dbi_options_destroy(sdb_dbi_options_t *options);
78 /*
79 * sdb_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 sdb_dbi_client_t *
88 sdb_dbi_client_create(const char *driver, const char *database);
90 /*
91 * sdb_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 sdb_dbi_client_connect().
94 *
95 * Returns:
96 * - 0 on success
97 * - a negative value else
98 */
99 int
100 sdb_dbi_client_set_options(sdb_dbi_client_t *client,
101 sdb_dbi_options_t *options);
103 /*
104 * sdb_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 sdb_dbi_client_connect(sdb_dbi_client_t *client);
116 /*
117 * sdb_dbi_client_check_conn:
118 * Check the database-connection and try to reconnect if that fails.
119 *
120 * Returns:
121 * - 0 on success (the connection is connected after the call)
122 * - a negative value else (failed to reconnect)
123 */
124 int
125 sdb_dbi_client_check_conn(sdb_dbi_client_t *client);
127 /*
128 * sdb_dbi_exec_query:
129 * Execute an SQL query on the database. The specified 'callback' will be
130 * called for each row returned from the query. If 'n' is a value equal to or
131 * greater than zero, it specifies the number of columns that are expected in
132 * the query result. For each column, the caller then needs to also specify
133 * the requested type (see the DBI_TYPE_* constants). If the number or types
134 * do not match, an error will be reported and the query will fail. That is,
135 * this allows to let sdb_dbi_exec_query() do basic verification of the
136 * returned values.
137 *
138 * The callback will receive the client object and an array containing the
139 * field values of the current row. Any string / binary objects are managed by
140 * libdbi, thus, it must not be freed or modified. If you need to keep the
141 * object, make sure to make a copy of it.
142 *
143 * Returns:
144 * - 0 on success
145 * - a negative value else
146 */
147 int
148 sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query,
149 sdb_dbi_data_cb callback, sdb_object_t *user_data, int n, ...);
151 /*
152 * sdb_dbi_client_destroy:
153 * Disconnect from the database and destroy the client object.
154 */
155 void
156 sdb_dbi_client_destroy(sdb_dbi_client_t *client);
158 #ifdef __cplusplus
159 } /* extern "C" */
160 #endif
162 #endif /* ! SDB_UTILS_DBI_H */
164 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */