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
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 * Function called to convert input argument into SQL parameters.
147 * @param pq data about the query
148 * @return -1 on error
151 (*GNUNET_MY_ResultConverter)(void *cls,
152 struct GNUNET_MY_ResultSpec *rs,
153 MYSQL_BIND *results);
156 * Information we pass to #GNUNET_MY_extract_result() to
157 * initialize the arguments of the prepared statement.
159 struct GNUNET_MY_ResultSpec
163 * Function to call for the type conversion.
165 GNUNET_MY_ResultConverter conv;
168 * Closure for @e conv.
173 * Destination for the data.
178 * Allowed size for the data, 0 for variable-size
179 * (in this case, the type of @e dst is a `void **`
180 * and we need to allocate a buffer of the right size).
185 * Where to store actual size of the result.
193 * End of result speceter specification.
195 * @return array last entry for the result specification to use
197 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL }
202 * Obtain fixed size result of @a ptr_size bytes from
203 * MySQL, store in already allocated buffer at @a ptr.
205 * @spec ptr where to write the result
206 * @oaran ptr_size number of bytes available at @a ptr
208 struct GNUNET_MY_ResultSpec
209 GNUNET_MY_result_spec_fixed_size (void *ptr,
213 * Generate query parameter for a string
215 *@param ptr pointer to the string query parameter to pass
217 struct GNUNET_MY_QueryParam
218 GNUNET_MY_query_param_string (const char *ptr);
221 * Generate fixed-size query parameter with size determined
224 * @param x pointer to the query parameter to pass
226 #define GNUNET_MY_query_param_auto_from_type(x) GNUNET_MY_query_param_fixed_size ((x), sizeof (*(x)))
229 * Generate query parameter for an RSA public key. The
230 * database must contain a BLOB type in the respective position.
232 * @param x the query parameter to pass
233 * @return array entry for the query parameters to use
235 struct GNUNET_MY_QueryParam
236 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
239 * Generate query parameter for an RSA signature. The
240 * database must contain a BLOB type in the respective position
242 *@param x the query parameter to pass
243 *@return array entry for the query parameters to use
245 struct GNUNET_MY_QueryParam
246 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
249 * Generate query parameter for an absolute time value.
250 * The database must store a 64-bit integer.
252 *@param x pointer to the query parameter to pass
253 *@return array entry for the query parameters to use
255 struct GNUNET_MY_QueryParam
256 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
259 * Generate query parameter for an absolute time value.
260 * The database must store a 64-bit integer.
262 *@param x pointer to the query parameter to pass
264 struct GNUNET_MY_QueryParam
265 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
268 * Generate query parameter for an uint16_t in host byte order.
270 * @param x pointer to the query parameter to pass
272 struct GNUNET_MY_QueryParam
273 GNUNET_MY_query_param_uint16 (const uint16_t *x);
276 * Generate query parameter for an uint32_t in host byte order
278 *@param x pointer to the query parameter to pass
280 struct GNUNET_MY_QueryParam
281 GNUNET_MY_query_param_uint32 (const uint32_t *x);
284 * Generate query parameter for an uint64_t in host byte order
286 *@param x pointer to the query parameter to pass
288 struct GNUNET_MY_QueryParam
289 GNUNET_MY_query_param_uint64 (const uint64_t *x);
292 * We expect a fixed-size result, with size determined by the type of `* dst`
294 * @spec name name of the field in the table
295 * @spec dst point to where to store the result, type fits expected result size
296 * @return array entry for the result specification to use
298 #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst)))
307 * Variable-size result expected
309 * @param[out] dst where to store the result, allocated
310 * @param[out] sptr where to store the size of @a dst
311 * @return array entru for the result specification to use
313 struct GNUNET_MY_ResultSpec
314 GNUNET_MY_result_spec_variable_size (void **dst,
317 * RSA public key expected
319 * @param name name of the field in the table
320 * @param[out] rsa where to store the result
321 * @return array entry for the result specification to use
323 struct GNUNET_MY_ResultSpec
324 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
327 * RSA signature expected.
329 * @param[out] sig where to store the result;
330 * @return array entry for the result specification to use
332 struct GNUNET_MY_ResultSpec
333 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
336 * 0- terminated string exprected.
338 * @param[out] dst where to store the result, allocated
339 * @return array entry for the result specification to use
341 struct GNUNET_MY_ResultSpec
342 GNUNET_MY_result_spec_string (char **dst);
345 * Absolute time expected
347 * @param name name of the field in the table
348 * @param[out] at where to store the result
349 * @return array entry for the result specification to use
351 struct GNUNET_MY_ResultSpec
352 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
355 * Absolute time in network byte order expected
357 * @param[out] at where to store the result
358 * @return array entry for the result specification to use
360 struct GNUNET_MY_ResultSpec
361 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
366 * @param[out] u16 where to store the result
367 * @return array entry for the result specification to use
369 struct GNUNET_MY_ResultSpec
370 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
375 * @param[out] u32 where to store the result
376 * @return array entry for the result specification to use
378 struct GNUNET_MY_ResultSpec
379 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
384 * @param[out] u64 where to store the result
385 * @return array entry for the result specification to use
387 struct GNUNET_MY_ResultSpec
388 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
391 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
392 struct GNUNET_MY_QueryParam *qp,
393 struct GNUNET_MY_ResultSpec *specs,
396 #if 0 /* keep Emacsens' auto-indent happy */
405 /** @} */ /* end of group */