2 This file is part of GNUnet
3 Copyright (C) 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Christian Grothoff
22 * @author Christophe Genevey
25 * Helper library to access a MySQL database
27 * @defgroup mysql MySQL library
28 * Helper library to access a MySQL database.
31 #ifndef GNUNET_MY_LIB_H
32 #define GNUNET_MY_LIB_H
34 #include "gnunet_util_lib.h"
35 #include "gnunet_mysql_lib.h"
36 #include <mysql/mysql.h>
41 #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,
78 * Information we pass to #GNUNET_MY_exec_prepared() to
79 * initialize the arguments of the prepared statement.
83 struct GNUNET_MY_QueryParam
86 * Function to call for the type conversion.
88 GNUNET_MY_QueryConverter conv;
91 * Function to call for cleaning up the query. Can be NULL.
93 GNUNET_MY_QueryCleanup cleaner;
96 * Closure for @e conv.
101 * Number of arguments the @a conv converter expects to initialize.
103 unsigned int num_params;
106 * Information to pass to @e conv.
111 * Information to pass to @e conv. Size of @a data.
113 unsigned long data_len;
117 * End of query parameter specification.
119 * @return array last entry for the result specification to use
121 #define GNUNET_MY_query_param_end { NULL, NULL, NULL, 0, NULL, 0 }
125 * Generate query parameter for a buffer @a ptr of
126 * @a ptr_size bytes.FG
128 * @param ptr pointer to the query parameter to pass
129 * @oaran ptr_size number of bytes in @a ptr
131 struct GNUNET_MY_QueryParam
132 GNUNET_MY_query_param_fixed_size (const void *ptr,
137 * Run a prepared SELECT statement.
139 * @param mc mysql context
140 * @param sh handle to SELECT statment
141 * @param params parameters to the statement
145 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
146 struct GNUNET_MYSQL_StatementHandle *sh,
147 struct GNUNET_MY_QueryParam *params);
151 * Information we pass to #GNUNET_MY_extract_result() to
152 * initialize the arguments of the prepared statement.
154 struct GNUNET_MY_ResultParam;
157 * Information we pass to #GNUNET_MY_extract_result() to
158 * initialize the arguments of the prepared statement.
160 struct GNUNET_MY_ResultSpec;
163 * Function called to convert input argument into SQL parameters.
167 * @param stmt the mysql statement that is being run
168 * @param column the column that is being processed
169 * @param[out] results
170 * @return -1 on error
173 (*GNUNET_MY_ResultConverter)(void *cls,
174 struct GNUNET_MY_ResultSpec *rs,
177 MYSQL_BIND *results);
180 * Function called to cleanup result data.
183 * @param rs spec to clean up
186 (*GNUNET_MY_ResultCleanup)(void *cls,
187 struct GNUNET_MY_ResultSpec *rs);
191 * Information we pass to #GNUNET_MY_extract_result() to
192 * initialize the arguments of the prepared statement.
194 struct GNUNET_MY_ResultSpec
197 * Function to call to initialize the MYSQL_BIND array.
199 GNUNET_MY_ResultConverter pre_conv;
202 * Function to call for converting the result. Can be NULL.
204 GNUNET_MY_ResultConverter post_conv;
207 * Function to call for cleaning up the result. Can be NULL.
209 GNUNET_MY_ResultCleanup cleaner;
212 * Closure for @e conv.
217 * Destination for the data.
222 * Allowed size for the data, 0 for variable-size
223 * (in this case, the type of @e dst is a `void **`
224 * and we need to allocate a buffer of the right size).
229 * Where to store actual size of the result.
234 * How many fields does this result specification occupy
235 * in the result returned by MySQL.
237 unsigned int num_fields;
240 * Location where we temporarily store the output buffer
241 * length from MySQL. Internal to libgnunetmy.
243 unsigned long mysql_bind_output_length;
246 * Memory for MySQL to notify us about NULL values.
253 * End of result speceter specification.
255 * @return array last entry for the result specification to use
257 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL, 0, 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( \
286 x) GNUNET_MY_query_param_fixed_size ((x), sizeof(*(x)))
289 * Generate query parameter for an RSA public key. The
290 * database must contain a BLOB type in the respective position.
292 * @param x the query parameter to pass
293 * @return array entry for the query parameters to use
295 struct GNUNET_MY_QueryParam
296 GNUNET_MY_query_param_rsa_public_key (const struct
297 GNUNET_CRYPTO_RsaPublicKey *x);
300 * Generate query parameter for an RSA signature. The
301 * database must contain a BLOB type in the respective position
303 *@param x the query parameter to pass
304 *@return array entry for the query parameters to use
306 struct GNUNET_MY_QueryParam
307 GNUNET_MY_query_param_rsa_signature (const struct
308 GNUNET_CRYPTO_RsaSignature *x);
311 * Generate query parameter for an absolute time value.
312 * The database must store a 64-bit integer.
314 *@param x pointer to the query parameter to pass
315 *@return array entry for the query parameters to use
317 struct GNUNET_MY_QueryParam
318 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
322 * Generate query parameter for an absolute time value.
323 * The database must store a 64-bit integer.
325 *@param x pointer to the query parameter to pass
327 struct GNUNET_MY_QueryParam
328 GNUNET_MY_query_param_absolute_time_nbo (const struct
329 GNUNET_TIME_AbsoluteNBO *x);
332 * Generate query parameter for an uint16_t in host byte order.
334 * @param x pointer to the query parameter to pass
336 struct GNUNET_MY_QueryParam
337 GNUNET_MY_query_param_uint16 (const uint16_t *x);
340 * Generate query parameter for an uint32_t in host byte order
342 *@param x pointer to the query parameter to pass
344 struct GNUNET_MY_QueryParam
345 GNUNET_MY_query_param_uint32 (const uint32_t *x);
348 * Generate query parameter for an uint64_t in host byte order
350 *@param x pointer to the query parameter to pass
352 struct GNUNET_MY_QueryParam
353 GNUNET_MY_query_param_uint64 (const uint64_t *x);
356 * We expect a fixed-size result, with size determined by the type of `* dst`
358 * @spec name name of the field in the table
359 * @spec dst point to where to store the result, type fits expected result size
360 * @return array entry for the result specification to use
362 #define GNUNET_MY_result_spec_auto_from_type( \
363 dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof(*(dst)))
367 * Variable-size result expected
369 * @param[out] dst where to store the result, allocated
370 * @param[out] sptr where to store the size of @a dst
371 * @return array entru for the result specification to use
373 struct GNUNET_MY_ResultSpec
374 GNUNET_MY_result_spec_variable_size (void **dst,
378 * RSA public key expected
380 * @param name name of the field in the table
381 * @param[out] rsa 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_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
389 * RSA signature expected.
391 * @param[out] sig 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_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
398 * 0- terminated string exprected.
400 * @param[out] dst where to store the result, allocated
401 * @return array entry for the result specification to use
403 struct GNUNET_MY_ResultSpec
404 GNUNET_MY_result_spec_string (char **dst);
407 * Absolute time expected
409 * @param name name of the field in the table
410 * @param[out] at where to store the result
411 * @return array entry for the result specification to use
413 struct GNUNET_MY_ResultSpec
414 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
417 * Absolute time in network byte order expected
419 * @param[out] at where to store the result
420 * @return array entry for the result specification to use
422 struct GNUNET_MY_ResultSpec
423 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
428 * @param[out] u16 where to store the result
429 * @return array entry for the result specification to use
431 struct GNUNET_MY_ResultSpec
432 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
437 * @param[out] u32 where to store the result
438 * @return array entry for the result specification to use
440 struct GNUNET_MY_ResultSpec
441 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
446 * @param[out] u64 where to store the result
447 * @return array entry for the result specification to use
449 struct GNUNET_MY_ResultSpec
450 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
454 * Extract results from a query result according to the given
455 * specification. Always fetches the next row.
457 * @param sh statement that returned results
458 * @param rs specification to extract for
460 * #GNUNET_YES if all results could be extracted
461 * #GNUNET_NO if there is no more data in the result set
462 * #GNUNET_SYSERR if a result was invalid
465 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
466 struct GNUNET_MY_ResultSpec *specs);
470 * Free all memory that was allocated in @a qp during
471 * #GNUNET_MY_exect_prepared().
473 * @param qp query specification to clean up
474 * @param qbind mysql query
477 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
482 * Free all memory that was allocated in @a rs during
483 * #GNUNET_MY_extract_result().
485 * @param rs reult specification to clean up
488 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs);
491 #if 0 /* keep Emacsens' auto-indent happy */
500 /** @} */ /* end of group */