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;
103 * End of query parameter specification.
105 * @return array last entry for the result specification to use
107 #define GNUNET_MY_query_param_end { NULL, NULL, 0, NULL, 0 }
112 * Generate query parameter for a buffer @a ptr of
113 * @a ptr_size bytes.FG
115 * @param ptr pointer to the query parameter to pass
116 * @oaran ptr_size number of bytes in @a ptr
118 struct GNUNET_MY_QueryParam
119 GNUNET_MY_query_param_fixed_size (const void *ptr,
123 * Run a prepared SELECT statement.
125 * @param mc mysql context
126 * @param sh handle to SELECT statment
127 * @param params parameters to the statement
131 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
132 struct GNUNET_MYSQL_StatementHandle *sh,
133 const struct GNUNET_MY_QueryParam *params);
137 * Information we pass to #GNUNET_MY_extract_result() to
138 * initialize the arguments of the prepared statement.
140 struct GNUNET_MY_ResultParam;
143 * Information we pass to #GNUNET_MY_extract_result() to
144 * initialize the arguments of the prepared statement.
146 struct GNUNET_MY_ResultSpec;
149 * Function called to convert input argument into SQL parameters.
153 * @param stmt the mysql statement that is being run
154 * @param column the column that is being processed
155 * @param[out] results
156 * @return -1 on error
159 (*GNUNET_MY_ResultConverter)(void *cls,
160 struct GNUNET_MY_ResultSpec *rs,
163 MYSQL_BIND *results);
166 * Function called to cleanup result data.
169 * @param rs spec to clean up
172 (*GNUNET_MY_ResultCleanup)(void *cls,
173 struct GNUNET_MY_ResultSpec *rs);
177 * Information we pass to #GNUNET_MY_extract_result() to
178 * initialize the arguments of the prepared statement.
180 struct GNUNET_MY_ResultSpec
184 * Function to call to initialize the MYSQL_BIND array.
186 GNUNET_MY_ResultConverter pre_conv;
189 * Function to call for converting the result. Can be NULL.
191 GNUNET_MY_ResultConverter post_conv;
194 * Function to call for cleaning up the result. Can be NULL.
196 GNUNET_MY_ResultCleanup cleaner;
199 * Closure for @e conv.
204 * Destination for the data.
209 * Allowed size for the data, 0 for variable-size
210 * (in this case, the type of @e dst is a `void **`
211 * and we need to allocate a buffer of the right size).
216 * Where to store actual size of the result.
221 * How many fields does this result specification occupy
222 * in the result returned by MySQL.
224 unsigned int num_fields;
227 * Location where we temporarily store the output buffer
228 * length from MySQL. Internal to libgnunetmy.
230 unsigned long mysql_bind_output_length;
236 * End of result speceter specification.
238 * @return array last entry for the result specification to use
240 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL, 0 }
245 * Obtain fixed size result of @a ptr_size bytes from
246 * MySQL, store in already allocated buffer at @a ptr.
248 * @spec ptr where to write the result
249 * @oaran ptr_size number of bytes available at @a ptr
251 struct GNUNET_MY_ResultSpec
252 GNUNET_MY_result_spec_fixed_size (void *ptr,
256 * Generate query parameter for a string
258 *@param ptr pointer to the string query parameter to pass
260 struct GNUNET_MY_QueryParam
261 GNUNET_MY_query_param_string (const char *ptr);
264 * Generate fixed-size query parameter with size determined
267 * @param x pointer to the query parameter to pass
269 #define GNUNET_MY_query_param_auto_from_type(x) GNUNET_MY_query_param_fixed_size ((x), sizeof (*(x)))
272 * Generate query parameter for an RSA public key. The
273 * database must contain a BLOB type in the respective position.
275 * @param x the query parameter to pass
276 * @return array entry for the query parameters to use
278 struct GNUNET_MY_QueryParam
279 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
282 * Generate query parameter for an RSA signature. The
283 * database must contain a BLOB type in the respective position
285 *@param x the query parameter to pass
286 *@return array entry for the query parameters to use
288 struct GNUNET_MY_QueryParam
289 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
292 * Generate query parameter for an absolute time value.
293 * The database must store a 64-bit integer.
295 *@param x pointer to the query parameter to pass
296 *@return array entry for the query parameters to use
298 struct GNUNET_MY_QueryParam
299 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
302 * Generate query parameter for an absolute time value.
303 * The database must store a 64-bit integer.
305 *@param x pointer to the query parameter to pass
307 struct GNUNET_MY_QueryParam
308 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
311 * Generate query parameter for an uint16_t in host byte order.
313 * @param x pointer to the query parameter to pass
315 struct GNUNET_MY_QueryParam
316 GNUNET_MY_query_param_uint16 (const uint16_t *x);
319 * Generate query parameter for an uint32_t in host byte order
321 *@param x pointer to the query parameter to pass
323 struct GNUNET_MY_QueryParam
324 GNUNET_MY_query_param_uint32 (const uint32_t *x);
327 * Generate query parameter for an uint64_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_uint64 (const uint64_t *x);
335 * We expect a fixed-size result, with size determined by the type of `* dst`
337 * @spec name name of the field in the table
338 * @spec dst point to where to store the result, type fits expected result size
339 * @return array entry for the result specification to use
341 #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst)))
350 * Variable-size result expected
352 * @param[out] dst where to store the result, allocated
353 * @param[out] sptr where to store the size of @a dst
354 * @return array entru for the result specification to use
356 struct GNUNET_MY_ResultSpec
357 GNUNET_MY_result_spec_variable_size (void **dst,
360 * RSA public key expected
362 * @param name name of the field in the table
363 * @param[out] rsa 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_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
370 * RSA signature expected.
372 * @param[out] sig 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_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
379 * 0- terminated string exprected.
381 * @param[out] dst where to store the result, allocated
382 * @return array entry for the result specification to use
384 struct GNUNET_MY_ResultSpec
385 GNUNET_MY_result_spec_string (char **dst);
388 * Absolute time expected
390 * @param name name of the field in the table
391 * @param[out] at where to store the result
392 * @return array entry for the result specification to use
394 struct GNUNET_MY_ResultSpec
395 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
398 * Absolute time in network byte order expected
400 * @param[out] at where to store the result
401 * @return array entry for the result specification to use
403 struct GNUNET_MY_ResultSpec
404 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
409 * @param[out] u16 where to store the result
410 * @return array entry for the result specification to use
412 struct GNUNET_MY_ResultSpec
413 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
418 * @param[out] u32 where to store the result
419 * @return array entry for the result specification to use
421 struct GNUNET_MY_ResultSpec
422 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
427 * @param[out] u64 where to store the result
428 * @return array entry for the result specification to use
430 struct GNUNET_MY_ResultSpec
431 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
435 * Extract results from a query result according to the given
436 * specification. Always fetches the next row.
438 * @param sh statement that returned results
439 * @param rs specification to extract for
441 * #GNUNET_YES if all results could be extracted
442 * #GNUNET_NO if there is no more data in the result set
443 * #GNUNET_SYSERR if a result was invalid
446 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
447 struct GNUNET_MY_ResultSpec *specs);
452 * Free all memory that was allocated in @a rs during
453 * #GNUNET_MY_extract_result().
455 * @param rs reult specification to clean up
458 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs);
461 #if 0 /* keep Emacsens' auto-indent happy */
470 /** @} */ /* end of group */