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
26 #include <mysql/mysql.h>
27 #include "gnunet_my_lib.h"
31 * Function called to convert input argument into SQL parameters.
34 * @param pq data about the query
35 * @param qbind array of parameters to initialize
39 my_conv_fixed_size (void *cls,
40 const struct GNUNET_MY_QueryParam *qp,
43 GNUNET_assert (1 == qp->num_params);
44 qbind->buffer = (void *) qp->data;
45 qbind->buffer_length = qp->data_len;
46 qbind->length = (unsigned long *) &qp->data_len;
52 * Generate query parameter for a buffer @a ptr of
55 * @param ptr pointer to the query parameter to pass
56 * @param ptr_size number of bytes in @a ptr
58 struct GNUNET_MY_QueryParam
59 GNUNET_MY_query_param_fixed_size (const void *ptr,
62 struct GNUNET_MY_QueryParam qp = {
67 (unsigned long) ptr_size
74 * Generate query parameter for a string
76 *@param ptr pointer to the string query parameter to pass
78 struct GNUNET_MY_QueryParam
79 GNUNET_MY_query_param_string (const char *ptr)
81 return GNUNET_MY_query_param_fixed_size(ptr,
86 * Function called to convert input argument into SQL parameters
89 *@param pq data about the query
90 * @param qbind array of parameters to initialize
94 my_conv_uint16 (void *cls,
95 const struct GNUNET_MY_QueryParam * qp,
98 const uint16_t *u_hbo = qp->data;
101 GNUNET_assert (1 == qp->num_params);
103 u_nbo = GNUNET_new (uint16_t);
104 *u_nbo = htons (*u_hbo);
105 qbind->buffer = (void *) u_nbo;
106 qbind->buffer_length = sizeof(uint16_t);
107 qbind->buffer_type = 1;
113 * Generate query parameter for an uint16_t in host byte order.
115 * @param x pointer to the query parameter to pass
117 struct GNUNET_MY_QueryParam
118 GNUNET_MY_query_param_uint16 (const uint16_t *x)
120 struct GNUNET_MY_QueryParam res = {
132 * Function called to convert input argument into SQL parameters
135 *@param pq data about the query
136 * @param qbind array of parameters to initialize
140 my_conv_uint32 (void *cls,
141 const struct GNUNET_MY_QueryParam *qp,
144 const uint32_t *u_hbo = qp->data;
147 GNUNET_assert (1 == qp->num_params);
149 u_nbo = GNUNET_new (uint32_t);
150 *u_nbo = htonl (*u_hbo);
152 qbind->buffer = (void *) u_nbo;
153 qbind->buffer_length = sizeof(uint32_t);
154 qbind->buffer_type = 1;
160 * Generate query parameter for an uint32_t in host byte order
162 *@param x pointer to the query parameter to pass
164 struct GNUNET_MY_QueryParam
165 GNUNET_MY_query_param_uint32 (const uint32_t *x)
167 struct GNUNET_MY_QueryParam res = {
179 * Function called to convert input argument into SQL parameters
182 *@param pq data about the query
183 * @param qbind array of parameters to initialize
187 my_conv_uint64 (void *cls,
188 const struct GNUNET_MY_QueryParam *qp,
191 const uint64_t * u_hbo = qp->data;
194 GNUNET_assert (1 == qp->num_params);
196 u_nbo = GNUNET_new(uint64_t);
197 *u_nbo = GNUNET_htonll (*u_hbo);
199 qbind->buffer = (void *) u_nbo;
200 qbind->buffer_length = sizeof (uint64_t);
201 qbind->buffer_type = 1;
207 * Generate query parameter for an uint64_t in host byte order
209 *@param x pointer to the query parameter to pass
211 struct GNUNET_MY_QueryParam
212 GNUNET_MY_query_param_uint64 (const uint64_t *x)
214 struct GNUNET_MY_QueryParam res = {
226 * Function called to convert input argument into SQL parameters
229 *@param pq data about the query
230 * @param qbind array of parameters to initialize
234 my_conv_rsa_public_key (void *cls,
235 const struct GNUNET_MY_QueryParam *qp,
238 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
242 GNUNET_assert(1 == qp->num_params);
244 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
246 qbind->buffer = (void *)buf;
247 qbind->buffer_length = buf_size - 1;
248 qbind->buffer_type = 1;
254 * Generate query parameter for an RSA public key. The
255 * database must contain a BLOB type in the respective position.
257 * @param x the query parameter to pass
258 * @return array entry for the query parameters to use
260 struct GNUNET_MY_QueryParam
261 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
263 struct GNUNET_MY_QueryParam res = {
264 &my_conv_rsa_public_key,
275 * Function called to convert input argument into SQL parameters
278 *@param pq data about the query
279 * @param qbind array of parameters to initialize
283 my_conv_rsa_signature (void *cls,
284 const struct GNUNET_MY_QueryParam *qp,
287 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
291 GNUNET_assert(1 == qp->num_params);
293 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
296 qbind->buffer = (void *)buf;
297 qbind->buffer_length = buf_size - 1;
298 qbind->buffer_type = 1;
304 * Generate query parameter for an RSA signature. The
305 * database must contain a BLOB type in the respective position
307 *@param x the query parameter to pass
308 *@return array entry for the query parameters to use
310 struct GNUNET_MY_QueryParam
311 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
313 struct GNUNET_MY_QueryParam res = {
314 &my_conv_rsa_signature,
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
328 *@return array entry for the query parameters to use
330 struct GNUNET_MY_QueryParam
331 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
333 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
337 * Generate query parameter for an absolute time value.
338 * The database must store a 64-bit integer.
340 *@param x pointer to the query parameter to pass
342 struct GNUNET_MY_QueryParam
343 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
345 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
348 /* end of my_query_helper.c */