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
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 * @file my/my_query_helper.c
22 * @brief library to help with access to a MySQL database
23 * @author Christian Grothoff
24 * @author Christophe Genevey
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
32 * Function called to clean up memory allocated
33 * by a #GNUNET_MY_QueryConverter.
36 * @param qbind array of parameter to clean up
39 my_clean_query (void *cls,
42 GNUNET_free (qbind[0].buffer);
47 * Function called to convert input argument into SQL parameters.
50 * @param pq data about the query
51 * @param qbind array of parameters to initialize
55 my_conv_fixed_size (void *cls,
56 const struct GNUNET_MY_QueryParam *qp,
59 GNUNET_assert (1 == qp->num_params);
60 qbind->buffer = (void *) qp->data;
61 qbind->buffer_length = qp->data_len;
62 qbind->buffer_type = MYSQL_TYPE_BLOB;
69 * Generate query parameter for a buffer @a ptr of
72 * @param ptr pointer to the query parameter to pass
73 * @param ptr_size number of bytes in @a ptr
75 struct GNUNET_MY_QueryParam
76 GNUNET_MY_query_param_fixed_size (const void *ptr,
79 struct GNUNET_MY_QueryParam qp = {
80 .conv = &my_conv_fixed_size,
85 .data_len = (unsigned long) ptr_size
92 * Function called to convert input argument into SQL parameters.
95 * @param pq data about the query
96 * @param qbind array of parameters to initialize
100 my_conv_string (void *cls,
101 const struct GNUNET_MY_QueryParam *qp,
104 GNUNET_assert (1 == qp->num_params);
106 qbind->buffer = (void *) qp->data;
107 qbind->buffer_length = qp->data_len;
108 qbind->buffer_type = MYSQL_TYPE_STRING;
115 * Generate query parameter for a string
117 * @param ptr pointer to the string query parameter to pass
119 struct GNUNET_MY_QueryParam
120 GNUNET_MY_query_param_string (const char *ptr)
122 struct GNUNET_MY_QueryParam qp = {
123 .conv = &my_conv_string,
128 .data_len = strlen (ptr)
135 * Function called to convert input argument into SQL parameters
138 * @param pq data about the query
139 * @param qbind array of parameters to initialize
140 * @return -1 on error
143 my_conv_uint16 (void *cls,
144 const struct GNUNET_MY_QueryParam *qp,
147 GNUNET_assert (1 == qp->num_params);
148 qbind->buffer = (void *) qp->data;
149 qbind->buffer_length = sizeof (uint16_t);
150 qbind->buffer_type = MYSQL_TYPE_SHORT;
156 * Generate query parameter for an uint16_t in host byte order.
158 * @param x pointer to the query parameter to pass
160 struct GNUNET_MY_QueryParam
161 GNUNET_MY_query_param_uint16 (const uint16_t *x)
163 struct GNUNET_MY_QueryParam res = {
164 .conv = &my_conv_uint16,
169 .data_len = sizeof (*x)
177 * Function called to convert input argument into SQL parameters
180 * @param pq data about the query
181 * @param qbind array of parameters to initialize
182 * @return -1 on error
185 my_conv_uint32 (void *cls,
186 const struct GNUNET_MY_QueryParam *qp,
189 GNUNET_assert (1 == qp->num_params);
190 qbind->buffer = (void *) qp->data;
191 qbind->buffer_length = sizeof(uint32_t);
192 qbind->buffer_type = MYSQL_TYPE_LONG;
199 * Generate query parameter for an uint32_t in host byte order
201 * @param x pointer to the query parameter to pass
203 struct GNUNET_MY_QueryParam
204 GNUNET_MY_query_param_uint32 (const uint32_t *x)
206 struct GNUNET_MY_QueryParam res = {
207 .conv = &my_conv_uint32,
212 .data_len = sizeof (*x)
220 * Function called to convert input argument into SQL parameters
223 * @param pq data about the query
224 * @param qbind array of parameters to initialize
225 * @return -1 on error
228 my_conv_uint64 (void *cls,
229 const struct GNUNET_MY_QueryParam *qp,
232 GNUNET_assert (1 == qp->num_params);
233 qbind->buffer = (void *) qp->data;
234 qbind->buffer_length = sizeof (uint64_t);
235 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
241 * Generate query parameter for an uint64_t in host byte order
243 * @param x pointer to the query parameter to pass
245 struct GNUNET_MY_QueryParam
246 GNUNET_MY_query_param_uint64 (const uint64_t *x)
248 struct GNUNET_MY_QueryParam res = {
249 .conv = &my_conv_uint64,
254 .data_len = sizeof(*x)
262 * Function called to convert input argument into SQL parameters
265 * @param pq data about the query
266 * @param qbind array of parameters to initialize
267 * @return -1 on error
270 my_conv_rsa_public_key (void *cls,
271 const struct GNUNET_MY_QueryParam *qp,
274 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
278 GNUNET_assert(1 == qp->num_params);
280 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
282 qbind->buffer = (void *) buf;
283 qbind->buffer_length = buf_size;
284 qbind->buffer_type = MYSQL_TYPE_BLOB;
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 GNUNET_CRYPTO_RsaPublicKey *x)
300 struct GNUNET_MY_QueryParam res = {
301 .conv = &my_conv_rsa_public_key,
302 .cleaner = &my_clean_query,
314 * Function called to convert input argument into SQL parameters
317 *@param pq data about the query
318 *@param qbind array of parameters to initialize
322 my_conv_rsa_signature (void *cls,
323 const struct GNUNET_MY_QueryParam *qp,
326 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
330 GNUNET_assert(1 == qp->num_params);
332 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
334 qbind->buffer = (void *) buf;
335 qbind->buffer_length = buf_size;
336 qbind->buffer_type = MYSQL_TYPE_BLOB;
343 * Generate query parameter for an RSA signature. The
344 * database must contain a BLOB type in the respective position
346 * @param x the query parameter to pass
347 * @return array entry for the query parameters to use
349 struct GNUNET_MY_QueryParam
350 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
352 struct GNUNET_MY_QueryParam res = {
353 .conv = &my_conv_rsa_signature,
354 .cleaner = &my_clean_query,
365 * Generate query parameter for an absolute time value.
366 * The database must store a 64-bit integer.
368 * @param x pointer to the query parameter to pass
369 * @return array entry for the query parameters to use
371 struct GNUNET_MY_QueryParam
372 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
374 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
379 * Generate query parameter for an absolute time value.
380 * The database must store a 64-bit integer.
382 * @param x pointer to the query parameter to pass
384 struct GNUNET_MY_QueryParam
385 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
387 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
391 /* end of my_query_helper.c */