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 * Function called to cleanup result data.
72 * @param rs spec to clean up
75 (*GNUNET_MY_QueryCleanup)(void *cls,
76 struct GNUNET_MY_QueryParam *qp);
78 * Information we pass to #GNUNET_MY_exec_prepared() to
79 * initialize the arguments of the prepared statement.
83 struct GNUNET_MY_QueryParam
87 * Function to call for the type conversion.
89 GNUNET_MY_QueryConverter conv;
92 * Function to call for cleaning up the query. Can be NULL.
94 GNUNET_MY_QueryCleanup cleaner;
97 * Closure for @e conv.
102 * Number of arguments the @a conv converter expects to initialize.
104 unsigned int num_params;
107 * Information to pass to @e conv.
112 * Information to pass to @e conv. Size of @a data.
114 unsigned long data_len;
119 * End of query parameter specification.
121 * @return array last entry for the result specification to use
123 #define GNUNET_MY_query_param_end { NULL, NULL, NULL, 0, NULL, 0 }
128 * Generate query parameter for a buffer @a ptr of
129 * @a ptr_size bytes.FG
131 * @param ptr pointer to the query parameter to pass
132 * @oaran ptr_size number of bytes in @a ptr
134 struct GNUNET_MY_QueryParam
135 GNUNET_MY_query_param_fixed_size (const void *ptr,
139 * Run a prepared SELECT statement.
141 * @param mc mysql context
142 * @param sh handle to SELECT statment
143 * @param params parameters to the statement
147 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
148 struct GNUNET_MYSQL_StatementHandle *sh,
149 const struct GNUNET_MY_QueryParam *params);
153 * Information we pass to #GNUNET_MY_extract_result() to
154 * initialize the arguments of the prepared statement.
156 struct GNUNET_MY_ResultParam;
159 * Information we pass to #GNUNET_MY_extract_result() to
160 * initialize the arguments of the prepared statement.
162 struct GNUNET_MY_ResultSpec;
165 * Function called to convert input argument into SQL parameters.
169 * @param stmt the mysql statement that is being run
170 * @param column the column that is being processed
171 * @param[out] results
172 * @return -1 on error
175 (*GNUNET_MY_ResultConverter)(void *cls,
176 struct GNUNET_MY_ResultSpec *rs,
179 MYSQL_BIND *results);
182 * Function called to cleanup result data.
185 * @param rs spec to clean up
188 (*GNUNET_MY_ResultCleanup)(void *cls,
189 struct GNUNET_MY_ResultSpec *rs);
193 * Information we pass to #GNUNET_MY_extract_result() to
194 * initialize the arguments of the prepared statement.
196 struct GNUNET_MY_ResultSpec
200 * Function to call to initialize the MYSQL_BIND array.
202 GNUNET_MY_ResultConverter pre_conv;
205 * Function to call for converting the result. Can be NULL.
207 GNUNET_MY_ResultConverter post_conv;
210 * Function to call for cleaning up the result. Can be NULL.
212 GNUNET_MY_ResultCleanup cleaner;
215 * Closure for @e conv.
220 * Destination for the data.
225 * Allowed size for the data, 0 for variable-size
226 * (in this case, the type of @e dst is a `void **`
227 * and we need to allocate a buffer of the right size).
232 * Where to store actual size of the result.
237 * How many fields does this result specification occupy
238 * in the result returned by MySQL.
240 unsigned int num_fields;
243 * Location where we temporarily store the output buffer
244 * length from MySQL. Internal to libgnunetmy.
246 unsigned long mysql_bind_output_length;
252 * End of result speceter specification.
254 * @return array last entry for the result specification to use
256 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL, 0 }
261 * Obtain fixed size result of @a ptr_size bytes from
262 * MySQL, store in already allocated buffer at @a ptr.
264 * @spec ptr where to write the result
265 * @oaran ptr_size number of bytes available at @a ptr
267 struct GNUNET_MY_ResultSpec
268 GNUNET_MY_result_spec_fixed_size (void *ptr,
272 * Generate query parameter for a string
274 *@param ptr pointer to the string query parameter to pass
276 struct GNUNET_MY_QueryParam
277 GNUNET_MY_query_param_string (const char *ptr);
280 * Generate fixed-size query parameter with size determined
283 * @param x pointer to the query parameter to pass
285 #define GNUNET_MY_query_param_auto_from_type(x) GNUNET_MY_query_param_fixed_size ((x), sizeof (*(x)))
288 * Generate query parameter for an RSA public key. The
289 * database must contain a BLOB type in the respective position.
291 * @param x the query parameter to pass
292 * @return array entry for the query parameters to use
294 struct GNUNET_MY_QueryParam
295 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
298 * Generate query parameter for an RSA signature. The
299 * database must contain a BLOB type in the respective position
301 *@param x the query parameter to pass
302 *@return array entry for the query parameters to use
304 struct GNUNET_MY_QueryParam
305 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
308 * Generate query parameter for an absolute time value.
309 * The database must store a 64-bit integer.
311 *@param x pointer to the query parameter to pass
312 *@return array entry for the query parameters to use
314 struct GNUNET_MY_QueryParam
315 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
318 * Generate query parameter for an absolute time value.
319 * The database must store a 64-bit integer.
321 *@param x pointer to the query parameter to pass
323 struct GNUNET_MY_QueryParam
324 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
327 * Generate query parameter for an uint16_t in host byte order.
329 * @param x pointer to the query parameter to pass
331 struct GNUNET_MY_QueryParam
332 GNUNET_MY_query_param_uint16 (const uint16_t *x);
335 * Generate query parameter for an uint32_t in host byte order
337 *@param x pointer to the query parameter to pass
339 struct GNUNET_MY_QueryParam
340 GNUNET_MY_query_param_uint32 (const uint32_t *x);
343 * Generate query parameter for an uint64_t in host byte order
345 *@param x pointer to the query parameter to pass
347 struct GNUNET_MY_QueryParam
348 GNUNET_MY_query_param_uint64 (const uint64_t *x);
351 * We expect a fixed-size result, with size determined by the type of `* dst`
353 * @spec name name of the field in the table
354 * @spec dst point to where to store the result, type fits expected result size
355 * @return array entry for the result specification to use
357 #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst)))
366 * Variable-size result expected
368 * @param[out] dst where to store the result, allocated
369 * @param[out] sptr where to store the size of @a dst
370 * @return array entru for the result specification to use
372 struct GNUNET_MY_ResultSpec
373 GNUNET_MY_result_spec_variable_size (void **dst,
376 * RSA public key expected
378 * @param name name of the field in the table
379 * @param[out] rsa where to store the result
380 * @return array entry for the result specification to use
382 struct GNUNET_MY_ResultSpec
383 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
386 * RSA signature expected.
388 * @param[out] sig where to store the result;
389 * @return array entry for the result specification to use
391 struct GNUNET_MY_ResultSpec
392 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
395 * 0- terminated string exprected.
397 * @param[out] dst where to store the result, allocated
398 * @return array entry for the result specification to use
400 struct GNUNET_MY_ResultSpec
401 GNUNET_MY_result_spec_string (char **dst);
404 * Absolute time expected
406 * @param name name of the field in the table
407 * @param[out] at where to store the result
408 * @return array entry for the result specification to use
410 struct GNUNET_MY_ResultSpec
411 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
414 * Absolute time in network byte order expected
416 * @param[out] at where to store the result
417 * @return array entry for the result specification to use
419 struct GNUNET_MY_ResultSpec
420 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
425 * @param[out] u16 where to store the result
426 * @return array entry for the result specification to use
428 struct GNUNET_MY_ResultSpec
429 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
434 * @param[out] u32 where to store the result
435 * @return array entry for the result specification to use
437 struct GNUNET_MY_ResultSpec
438 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
443 * @param[out] u64 where to store the result
444 * @return array entry for the result specification to use
446 struct GNUNET_MY_ResultSpec
447 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
451 * Extract results from a query result according to the given
452 * specification. Always fetches the next row.
454 * @param sh statement that returned results
455 * @param rs specification to extract for
457 * #GNUNET_YES if all results could be extracted
458 * #GNUNET_NO if there is no more data in the result set
459 * #GNUNET_SYSERR if a result was invalid
462 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
463 struct GNUNET_MY_ResultSpec *specs);
467 * Free all memory that was allocated in @a qp during
468 * #GNUNET_MY_exect_prepared().
470 * @param qp query specification to clean up
473 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp);
477 * Free all memory that was allocated in @a rs during
478 * #GNUNET_MY_extract_result().
480 * @param rs reult specification to clean up
483 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs);
486 #if 0 /* keep Emacsens' auto-indent happy */
495 /** @} */ /* end of group */