2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @author Christian Grothoff
24 * Helper library to access a MySQL database
26 * @defgroup mysql MySQL library
27 * Helper library to access a MySQL database.
30 #ifndef GNUNET_MY_LIB_H
31 #define GNUNET_MY_LIB_H
33 #include "gnunet_util_lib.h"
34 #include "gnunet_mysql_lib.h"
35 #include <mysql/mysql.h>
40 #if 0 /* keep Emacsens' auto-indent happy */
48 * Information we pass to #GNUNET_MY_exec_prepared() to
49 * initialize the arguments of the prepared statement.
51 struct GNUNET_MY_QueryParam;
55 * Function called to convert input argument into SQL parameters.
58 * @param pq data about the query
59 * @param qbind array of parameters to initialize
63 (*GNUNET_MY_QueryConverter)(void *cls,
64 const struct GNUNET_MY_QueryParam *qp,
69 * Information we pass to #GNUNET_MY_exec_prepared() to
70 * initialize the arguments of the prepared statement.
72 struct GNUNET_MY_QueryParam
76 * Function to call for the type conversion.
78 GNUNET_MY_QueryConverter conv;
81 * Closure for @e conv.
86 * Number of arguments the @a conv converter expects to initialize.
88 unsigned int num_params;
91 * Information to pass to @e conv.
96 * Information to pass to @e conv. Size of @a data.
98 unsigned long data_len ;
104 * End of query parameter specification.
106 * @return array last entry for the result specification to use
108 #define GNUNET_MY_query_param_end { NULL, NULL, 0, NULL, 0 }
113 * Generate query parameter for a buffer @a ptr of
114 * @a ptr_size bytes.FG
116 * @param ptr pointer to the query parameter to pass
117 * @oaran ptr_size number of bytes in @a ptr
119 struct GNUNET_MY_QueryParam
120 GNUNET_MY_query_param_fixed_size (const void *ptr,
124 * Run a prepared SELECT statement.
126 * @param mc mysql context
127 * @param sh handle to SELECT statment
128 * @param params parameters to the statement
132 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
133 struct GNUNET_MYSQL_StatementHandle *sh,
134 const struct GNUNET_MY_QueryParam *params);
138 * Information we pass to #GNUNET_MY_extract_result() to
139 * initialize the arguments of the prepared statement.
141 struct GNUNET_MY_ResultParam;
144 * Information we pass to #GNUNET_MY_extract_result() to
145 * initialize the arguments of the prepared statement.
147 struct GNUNET_MY_ResultSpec;
150 * Function called to convert input argument into SQL parameters.
153 * @param pq data about the query
154 * @return -1 on error
157 (*GNUNET_MY_ResultConverter)(void *cls,
158 struct GNUNET_MY_ResultSpec *rs,
159 MYSQL_BIND *results);
162 * Information we pass to #GNUNET_MY_extract_result() to
163 * initialize the arguments of the prepared statement.
165 struct GNUNET_MY_ResultSpec
169 * Function to call for the type conversion.
171 GNUNET_MY_ResultConverter conv;
174 * Closure for @e conv.
179 * Destination for the data.
184 * Allowed size for the data, 0 for variable-size
185 * (in this case, the type of @e dst is a `void **`
186 * and we need to allocate a buffer of the right size).
191 * Where to store actual size of the result.
199 * End of result speceter specification.
201 * @return array last entry for the result specification to use
203 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL }
208 * Obtain fixed size result of @a ptr_size bytes from
209 * MySQL, store in already allocated buffer at @a ptr.
211 * @spec ptr where to write the result
212 * @oaran ptr_size number of bytes available at @a ptr
214 struct GNUNET_MY_ResultSpec
215 GNUNET_MY_result_spec_fixed_size (void *ptr,
219 * Generate query parameter for a string
221 *@param ptr pointer to the string query parameter to pass
223 struct GNUNET_MY_QueryParam
224 GNUNET_MY_query_param_string (const char *ptr);
227 * Generate fixed-size query parameter with size determined
230 * @param x pointer to the query parameter to pass
232 #define GNUNET_MY_query_param_auto_from_type(x) GNUNET_MY_query_param_fixed_size ((x), sizeof (*(x)))
235 * Generate query parameter for an RSA public key. The
236 * database must contain a BLOB type in the respective position.
238 * @param x the query parameter to pass
239 * @return array entry for the query parameters to use
241 struct GNUNET_MY_QueryParam
242 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
245 * Generate query parameter for an RSA signature. The
246 * database must contain a BLOB type in the respective position
248 *@param x the query parameter to pass
249 *@return array entry for the query parameters to use
251 struct GNUNET_MY_QueryParam
252 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
255 * Generate query parameter for an absolute time value.
256 * The database must store a 64-bit integer.
258 *@param x pointer to the query parameter to pass
259 *@return array entry for the query parameters to use
261 struct GNUNET_MY_QueryParam
262 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
265 * Generate query parameter for an absolute time value.
266 * The database must store a 64-bit integer.
268 *@param x pointer to the query parameter to pass
270 struct GNUNET_MY_QueryParam
271 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
274 * Generate query parameter for an uint16_t in host byte order.
276 * @param x pointer to the query parameter to pass
278 struct GNUNET_MY_QueryParam
279 GNUNET_MY_query_param_uint16 (const uint16_t *x);
282 * Generate query parameter for an uint32_t in host byte order
284 *@param x pointer to the query parameter to pass
286 struct GNUNET_MY_QueryParam
287 GNUNET_MY_query_param_uint32 (const uint32_t *x);
290 * Generate query parameter for an uint64_t in host byte order
292 *@param x pointer to the query parameter to pass
294 struct GNUNET_MY_QueryParam
295 GNUNET_MY_query_param_uint64 (const uint64_t *x);
298 * We expect a fixed-size result, with size determined by the type of `* dst`
300 * @spec name name of the field in the table
301 * @spec dst point to where to store the result, type fits expected result size
302 * @return array entry for the result specification to use
304 #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst)))
313 * Variable-size result expected
315 * @param[out] dst where to store the result, allocated
316 * @param[out] sptr where to store the size of @a dst
317 * @return array entru for the result specification to use
319 struct GNUNET_MY_ResultSpec
320 GNUNET_MY_result_spec_variable_size (void **dst,
323 * RSA public key expected
325 * @param name name of the field in the table
326 * @param[out] rsa where to store the result
327 * @return array entry for the result specification to use
329 struct GNUNET_MY_ResultSpec
330 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
333 * RSA signature expected.
335 * @param[out] sig where to store the result;
336 * @return array entry for the result specification to use
338 struct GNUNET_MY_ResultSpec
339 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
342 * 0- terminated string exprected.
344 * @param[out] dst where to store the result, allocated
345 * @return array entry for the result specification to use
347 struct GNUNET_MY_ResultSpec
348 GNUNET_MY_result_spec_string (char **dst);
351 * Absolute time expected
353 * @param name name of the field in the table
354 * @param[out] at where to store the result
355 * @return array entry for the result specification to use
357 struct GNUNET_MY_ResultSpec
358 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
361 * Absolute time in network byte order expected
363 * @param[out] at where to store the result
364 * @return array entry for the result specification to use
366 struct GNUNET_MY_ResultSpec
367 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
372 * @param[out] u16 where to store the result
373 * @return array entry for the result specification to use
375 struct GNUNET_MY_ResultSpec
376 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
381 * @param[out] u32 where to store the result
382 * @return array entry for the result specification to use
384 struct GNUNET_MY_ResultSpec
385 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
390 * @param[out] u64 where to store the result
391 * @return array entry for the result specification to use
393 struct GNUNET_MY_ResultSpec
394 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
397 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
398 struct GNUNET_MY_QueryParam *qp,
399 struct GNUNET_MY_ResultSpec *specs,
402 #if 0 /* keep Emacsens' auto-indent happy */
411 /** @} */ /* end of group */