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 */
49 * Information we pass to #GNUNET_MY_exec_prepared() to
50 * initialize the arguments of the prepared statement.
52 struct GNUNET_MY_QueryParam;
56 * Function called to convert input argument into SQL parameters.
59 * @param pq data about the query
60 * @param qbind array of parameters to initialize
64 (*GNUNET_MY_QueryConverter)(void *cls,
65 const struct GNUNET_MY_QueryParam *qp,
70 * Function called to cleanup result data.
73 * @param rs spec to clean up
76 (*GNUNET_MY_QueryCleanup)(void *cls,
79 * Information we pass to #GNUNET_MY_exec_prepared() to
80 * initialize the arguments of the prepared statement.
84 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;
118 * End of query parameter specification.
120 * @return array last entry for the result specification to use
122 #define GNUNET_MY_query_param_end { NULL, NULL, NULL, 0, NULL, 0 }
126 * Generate query parameter for a buffer @a ptr of
127 * @a ptr_size bytes.FG
129 * @param ptr pointer to the query parameter to pass
130 * @oaran ptr_size number of bytes in @a ptr
132 struct GNUNET_MY_QueryParam
133 GNUNET_MY_query_param_fixed_size (const void *ptr,
138 * Run a prepared SELECT statement.
140 * @param mc mysql context
141 * @param sh handle to SELECT statment
142 * @param params parameters to the statement
146 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
147 struct GNUNET_MYSQL_StatementHandle *sh,
148 struct GNUNET_MY_QueryParam *params);
152 * Information we pass to #GNUNET_MY_extract_result() to
153 * initialize the arguments of the prepared statement.
155 struct GNUNET_MY_ResultParam;
158 * Information we pass to #GNUNET_MY_extract_result() to
159 * initialize the arguments of the prepared statement.
161 struct GNUNET_MY_ResultSpec;
164 * Function called to convert input argument into SQL parameters.
168 * @param stmt the mysql statement that is being run
169 * @param column the column that is being processed
170 * @param[out] results
171 * @return -1 on error
174 (*GNUNET_MY_ResultConverter)(void *cls,
175 struct GNUNET_MY_ResultSpec *rs,
178 MYSQL_BIND *results);
181 * Function called to cleanup result data.
184 * @param rs spec to clean up
187 (*GNUNET_MY_ResultCleanup)(void *cls,
188 struct GNUNET_MY_ResultSpec *rs);
192 * Information we pass to #GNUNET_MY_extract_result() to
193 * initialize the arguments of the prepared statement.
195 struct GNUNET_MY_ResultSpec
198 * Function to call to initialize the MYSQL_BIND array.
200 GNUNET_MY_ResultConverter pre_conv;
203 * Function to call for converting the result. Can be NULL.
205 GNUNET_MY_ResultConverter post_conv;
208 * Function to call for cleaning up the result. Can be NULL.
210 GNUNET_MY_ResultCleanup cleaner;
213 * Closure for @e conv.
218 * Destination for the data.
223 * Allowed size for the data, 0 for variable-size
224 * (in this case, the type of @e dst is a `void **`
225 * and we need to allocate a buffer of the right size).
230 * Where to store actual size of the result.
235 * How many fields does this result specification occupy
236 * in the result returned by MySQL.
238 unsigned int num_fields;
241 * Location where we temporarily store the output buffer
242 * length from MySQL. Internal to libgnunetmy.
244 unsigned long mysql_bind_output_length;
247 * Memory for MySQL to notify us about NULL values.
254 * End of result speceter specification.
256 * @return array last entry for the result specification to use
258 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL, 0, 0 }
263 * Obtain fixed size result of @a ptr_size bytes from
264 * MySQL, store in already allocated buffer at @a ptr.
266 * @spec ptr where to write the result
267 * @oaran ptr_size number of bytes available at @a ptr
269 struct GNUNET_MY_ResultSpec
270 GNUNET_MY_result_spec_fixed_size (void *ptr,
274 * Generate query parameter for a string
276 *@param ptr pointer to the string query parameter to pass
278 struct GNUNET_MY_QueryParam
279 GNUNET_MY_query_param_string (const char *ptr);
282 * Generate fixed-size query parameter with size determined
285 * @param x pointer to the query parameter to pass
287 #define GNUNET_MY_query_param_auto_from_type( \
288 x) GNUNET_MY_query_param_fixed_size ((x), sizeof(*(x)))
291 * Generate query parameter for an RSA public key. The
292 * database must contain a BLOB type in the respective position.
294 * @param x the query parameter to pass
295 * @return array entry for the query parameters to use
297 struct GNUNET_MY_QueryParam
298 GNUNET_MY_query_param_rsa_public_key (const struct
299 GNUNET_CRYPTO_RsaPublicKey *x);
302 * Generate query parameter for an RSA signature. The
303 * database must contain a BLOB type in the respective position
305 *@param x the query parameter to pass
306 *@return array entry for the query parameters to use
308 struct GNUNET_MY_QueryParam
309 GNUNET_MY_query_param_rsa_signature (const struct
310 GNUNET_CRYPTO_RsaSignature *x);
313 * Generate query parameter for an absolute time value.
314 * The database must store a 64-bit integer.
316 *@param x pointer to the query parameter to pass
317 *@return array entry for the query parameters to use
319 struct GNUNET_MY_QueryParam
320 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
324 * Generate query parameter for an absolute time value.
325 * The database must store a 64-bit integer.
327 *@param x pointer to the query parameter to pass
329 struct GNUNET_MY_QueryParam
330 GNUNET_MY_query_param_absolute_time_nbo (const struct
331 GNUNET_TIME_AbsoluteNBO *x);
334 * Generate query parameter for an uint16_t in host byte order.
336 * @param x pointer to the query parameter to pass
338 struct GNUNET_MY_QueryParam
339 GNUNET_MY_query_param_uint16 (const uint16_t *x);
342 * Generate query parameter for an uint32_t in host byte order
344 *@param x pointer to the query parameter to pass
346 struct GNUNET_MY_QueryParam
347 GNUNET_MY_query_param_uint32 (const uint32_t *x);
350 * Generate query parameter for an uint64_t in host byte order
352 *@param x pointer to the query parameter to pass
354 struct GNUNET_MY_QueryParam
355 GNUNET_MY_query_param_uint64 (const uint64_t *x);
358 * We expect a fixed-size result, with size determined by the type of `* dst`
360 * @spec name name of the field in the table
361 * @spec dst point to where to store the result, type fits expected result size
362 * @return array entry for the result specification to use
364 #define GNUNET_MY_result_spec_auto_from_type( \
365 dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof(*(dst)))
369 * Variable-size result expected
371 * @param[out] dst where to store the result, allocated
372 * @param[out] sptr where to store the size of @a dst
373 * @return array entru for the result specification to use
375 struct GNUNET_MY_ResultSpec
376 GNUNET_MY_result_spec_variable_size (void **dst,
380 * RSA public key expected
382 * @param name name of the field in the table
383 * @param[out] rsa where to store the result
384 * @return array entry for the result specification to use
386 struct GNUNET_MY_ResultSpec
387 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
391 * RSA signature expected.
393 * @param[out] sig where to store the result;
394 * @return array entry for the result specification to use
396 struct GNUNET_MY_ResultSpec
397 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
400 * 0- terminated string exprected.
402 * @param[out] dst where to store the result, allocated
403 * @return array entry for the result specification to use
405 struct GNUNET_MY_ResultSpec
406 GNUNET_MY_result_spec_string (char **dst);
409 * Absolute time expected
411 * @param name name of the field in the table
412 * @param[out] at where to store the result
413 * @return array entry for the result specification to use
415 struct GNUNET_MY_ResultSpec
416 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
419 * Absolute time in network byte order expected
421 * @param[out] at where to store the result
422 * @return array entry for the result specification to use
424 struct GNUNET_MY_ResultSpec
425 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
430 * @param[out] u16 where to store the result
431 * @return array entry for the result specification to use
433 struct GNUNET_MY_ResultSpec
434 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
439 * @param[out] u32 where to store the result
440 * @return array entry for the result specification to use
442 struct GNUNET_MY_ResultSpec
443 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
448 * @param[out] u64 where to store the result
449 * @return array entry for the result specification to use
451 struct GNUNET_MY_ResultSpec
452 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
456 * Extract results from a query result according to the given
457 * specification. Always fetches the next row.
459 * @param sh statement that returned results
460 * @param rs specification to extract for
462 * #GNUNET_YES if all results could be extracted
463 * #GNUNET_NO if there is no more data in the result set
464 * #GNUNET_SYSERR if a result was invalid
467 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
468 struct GNUNET_MY_ResultSpec *specs);
472 * Free all memory that was allocated in @a qp during
473 * #GNUNET_MY_exect_prepared().
475 * @param qp query specification to clean up
476 * @param qbind mysql query
479 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
484 * Free all memory that was allocated in @a rs during
485 * #GNUNET_MY_extract_result().
487 * @param rs reult specification to clean up
490 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs);
493 #if 0 /* keep Emacsens' auto-indent happy */
502 /** @} */ /* end of group */