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 GNUNET_assert (1 == qp->num_params);
119 qbind->buffer = (void *) qp->data;
120 qbind->buffer_length = sizeof (uint16_t);
121 qbind->buffer_type = MYSQL_TYPE_SHORT;
127 * Generate query parameter for an uint16_t in host byte order.
129 * @param x pointer to the query parameter to pass
131 struct GNUNET_MY_QueryParam
132 GNUNET_MY_query_param_uint16 (const uint16_t *x)
134 struct GNUNET_MY_QueryParam res = {
135 .conv = &my_conv_uint16,
140 .data_len = sizeof (*x)
148 * Function called to convert input argument into SQL parameters
151 *@param pq data about the query
152 * @param qbind array of parameters to initialize
156 my_conv_uint32 (void *cls,
157 const struct GNUNET_MY_QueryParam *qp,
160 GNUNET_assert (1 == qp->num_params);
161 qbind->buffer = (void *) qp->data;
162 qbind->buffer_length = sizeof(uint32_t);
163 qbind->buffer_type = MYSQL_TYPE_LONG;
170 * Generate query parameter for an uint32_t in host byte order
172 *@param x pointer to the query parameter to pass
174 struct GNUNET_MY_QueryParam
175 GNUNET_MY_query_param_uint32 (const uint32_t *x)
177 struct GNUNET_MY_QueryParam res = {
178 .conv = &my_conv_uint32,
183 .data_len = sizeof (*x)
191 * Function called to convert input argument into SQL parameters
194 *@param pq data about the query
195 * @param qbind array of parameters to initialize
199 my_conv_uint64 (void *cls,
200 const struct GNUNET_MY_QueryParam *qp,
203 GNUNET_assert (1 == qp->num_params);
204 qbind->buffer = (void *) qp->data;
205 qbind->buffer_length = sizeof (uint64_t);
206 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
212 * Generate query parameter for an uint64_t in host byte order
214 *@param x pointer to the query parameter to pass
216 struct GNUNET_MY_QueryParam
217 GNUNET_MY_query_param_uint64 (const uint64_t *x)
219 struct GNUNET_MY_QueryParam res = {
220 .conv = &my_conv_uint64,
225 .data_len = sizeof(*x)
233 * Function called to convert input argument into SQL parameters
236 *@param pq data about the query
237 * @param qbind array of parameters to initialize
241 my_conv_rsa_public_key (void *cls,
242 const struct GNUNET_MY_QueryParam *qp,
245 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
249 GNUNET_assert(1 == qp->num_params);
251 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
253 qbind->buffer = (void *) buf;
254 qbind->buffer_length = buf_size;
255 qbind->buffer_type = MYSQL_TYPE_BLOB;
262 * Generate query parameter for an RSA public key. The
263 * database must contain a BLOB type in the respective position.
265 * @param x the query parameter to pass
266 * @return array entry for the query parameters to use
268 struct GNUNET_MY_QueryParam
269 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
271 struct GNUNET_MY_QueryParam res = {
272 .conv = &my_conv_rsa_public_key,
273 .cleaner = &my_clean_query,
285 * Function called to convert input argument into SQL parameters
288 *@param pq data about the query
289 *@param qbind array of parameters to initialize
293 my_conv_rsa_signature (void *cls,
294 const struct GNUNET_MY_QueryParam *qp,
297 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
301 GNUNET_assert(1 == qp->num_params);
303 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
305 qbind->buffer = (void *) buf;
306 qbind->buffer_length = buf_size;
307 qbind->buffer_type = MYSQL_TYPE_BLOB;
314 * Generate query parameter for an RSA signature. The
315 * database must contain a BLOB type in the respective position
317 *@param x the query parameter to pass
318 *@return array entry for the query parameters to use
320 struct GNUNET_MY_QueryParam
321 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
323 struct GNUNET_MY_QueryParam res = {
324 .conv = &my_conv_rsa_signature,
325 .cleaner = &my_clean_query,
336 * Generate query parameter for an absolute time value.
337 * The database must store a 64-bit integer.
339 *@param x pointer to the query parameter to pass
340 *@return array entry for the query parameters to use
342 struct GNUNET_MY_QueryParam
343 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
345 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
350 * Generate query parameter for an absolute time value.
351 * The database must store a 64-bit integer.
353 *@param x pointer to the query parameter to pass
355 struct GNUNET_MY_QueryParam
356 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
358 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
362 /* end of my_query_helper.c */