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);
61 qbind->buffer = (void *) qp->data;
62 qbind->buffer_length = qp->data_len;
63 qbind->buffer_type = MYSQL_TYPE_BLOB;
70 * Generate query parameter for a buffer @a ptr of
73 * @param ptr pointer to the query parameter to pass
74 * @param ptr_size number of bytes in @a ptr
76 struct GNUNET_MY_QueryParam
77 GNUNET_MY_query_param_fixed_size (const void *ptr,
80 struct GNUNET_MY_QueryParam qp = {
81 .conv = &my_conv_fixed_size,
86 .data_len = (unsigned long) ptr_size
93 * Generate query parameter for a string
95 *@param ptr pointer to the string query parameter to pass
97 struct GNUNET_MY_QueryParam
98 GNUNET_MY_query_param_string (const char *ptr)
100 return GNUNET_MY_query_param_fixed_size(ptr,
106 * Function called to convert input argument into SQL parameters
109 *@param pq data about the query
110 * @param qbind array of parameters to initialize
114 my_conv_uint16 (void *cls,
115 const struct GNUNET_MY_QueryParam * qp,
118 const uint16_t *u_hbo = qp->data;
121 GNUNET_assert (1 == qp->num_params);
123 u_nbo = GNUNET_new (uint16_t);
129 qbind->buffer = (void *) u_nbo;
130 qbind->buffer_length = sizeof(uint16_t);
131 qbind->buffer_type = MYSQL_TYPE_SHORT;
138 * Generate query parameter for an uint16_t in host byte order.
140 * @param x pointer to the query parameter to pass
142 struct GNUNET_MY_QueryParam
143 GNUNET_MY_query_param_uint16 (const uint16_t *x)
145 struct GNUNET_MY_QueryParam res = {
146 .conv = &my_conv_uint16,
147 .cleaner = &my_clean_query,
151 .data_len = sizeof (*x)
159 * Function called to convert input argument into SQL parameters
162 *@param pq data about the query
163 * @param qbind array of parameters to initialize
167 my_conv_uint32 (void *cls,
168 const struct GNUNET_MY_QueryParam *qp,
171 const uint32_t *u_hbo = qp->data;
174 GNUNET_assert (1 == qp->num_params);
176 u_nbo = GNUNET_new (uint32_t);
180 qbind->buffer = (void *) u_nbo;
181 qbind->buffer_length = sizeof(uint32_t);
182 qbind->buffer_type = MYSQL_TYPE_LONG;
189 * Generate query parameter for an uint32_t in host byte order
191 *@param x pointer to the query parameter to pass
193 struct GNUNET_MY_QueryParam
194 GNUNET_MY_query_param_uint32 (const uint32_t *x)
196 struct GNUNET_MY_QueryParam res = {
197 .conv = &my_conv_uint32,
198 .cleaner = &my_clean_query,
202 .data_len = sizeof (*x)
210 * Function called to convert input argument into SQL parameters
213 *@param pq data about the query
214 * @param qbind array of parameters to initialize
218 my_conv_uint64 (void *cls,
219 const struct GNUNET_MY_QueryParam *qp,
222 const uint64_t * u_hbo = qp->data;
225 GNUNET_assert (1 == qp->num_params);
227 u_nbo = GNUNET_new(uint64_t);
231 qbind->buffer = (void *) u_nbo;
232 qbind->buffer_length = sizeof (uint64_t);
233 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
240 * Generate query parameter for an uint64_t in host byte order
242 *@param x pointer to the query parameter to pass
244 struct GNUNET_MY_QueryParam
245 GNUNET_MY_query_param_uint64 (const uint64_t *x)
247 struct GNUNET_MY_QueryParam res = {
248 .conv = &my_conv_uint64,
249 .cleaner = &my_clean_query,
253 .data_len = sizeof(*x)
261 * Function called to convert input argument into SQL parameters
264 *@param pq data about the query
265 * @param qbind array of parameters to initialize
269 my_conv_rsa_public_key (void *cls,
270 const struct GNUNET_MY_QueryParam *qp,
273 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
277 GNUNET_assert(1 == qp->num_params);
279 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
281 qbind->buffer = (void *) buf;
282 qbind->buffer_length = buf_size;
283 qbind->buffer_type = MYSQL_TYPE_BLOB;
290 * Generate query parameter for an RSA public key. The
291 * database must contain a BLOB type in the respective position.
293 * @param x the query parameter to pass
294 * @return array entry for the query parameters to use
296 struct GNUNET_MY_QueryParam
297 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
299 struct GNUNET_MY_QueryParam res = {
300 .conv = &my_conv_rsa_public_key,
301 .cleaner = &my_clean_query,
313 * Function called to convert input argument into SQL parameters
316 *@param pq data about the query
317 *@param qbind array of parameters to initialize
321 my_conv_rsa_signature (void *cls,
322 const struct GNUNET_MY_QueryParam *qp,
325 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
329 GNUNET_assert(1 == qp->num_params);
331 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
333 qbind->buffer = (void *) buf;
334 qbind->buffer_length = buf_size;
335 qbind->buffer_type = MYSQL_TYPE_BLOB;
342 * Generate query parameter for an RSA signature. The
343 * database must contain a BLOB type in the respective position
345 *@param x the query parameter to pass
346 *@return array entry for the query parameters to use
348 struct GNUNET_MY_QueryParam
349 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
351 struct GNUNET_MY_QueryParam res = {
352 .conv = &my_conv_rsa_signature,
353 .cleaner = &my_clean_query,
364 * Generate query parameter for an absolute time value.
365 * The database must store a 64-bit integer.
367 *@param x pointer to the query parameter to pass
368 *@return array entry for the query parameters to use
370 struct GNUNET_MY_QueryParam
371 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
373 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
378 * Generate query parameter for an absolute time value.
379 * The database must store a 64-bit integer.
381 *@param x pointer to the query parameter to pass
383 struct GNUNET_MY_QueryParam
384 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
386 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
390 /* end of my_query_helper.c */