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"
30 * Function called to convert input argument into SQL parameters.
33 * @param pq data about the query
34 * @param qbind array of parameters to initialize
38 my_conv_fixed_size (void *cls,
39 const struct GNUNET_MY_QueryParam *qp,
42 GNUNET_assert (1 == qp->num_params);
44 qbind->buffer = (void *) qp->data;
45 qbind->buffer_length = qp->data_len;
46 qbind->buffer_type = MYSQL_TYPE_BLOB;
53 * Generate query parameter for a buffer @a ptr of
56 * @param ptr pointer to the query parameter to pass
57 * @param ptr_size number of bytes in @a ptr
59 struct GNUNET_MY_QueryParam
60 GNUNET_MY_query_param_fixed_size (const void *ptr,
63 struct GNUNET_MY_QueryParam qp = {
68 (unsigned long) ptr_size
75 * Generate query parameter for a string
77 *@param ptr pointer to the string query parameter to pass
79 struct GNUNET_MY_QueryParam
80 GNUNET_MY_query_param_string (const char *ptr)
82 return GNUNET_MY_query_param_fixed_size(ptr,
87 * Function called to convert input argument into SQL parameters
90 *@param pq data about the query
91 * @param qbind array of parameters to initialize
95 my_conv_uint16 (void *cls,
96 const struct GNUNET_MY_QueryParam * qp,
99 const uint16_t *u_hbo = qp->data;
102 GNUNET_assert (1 == qp->num_params);
105 u_nbo = GNUNET_new (uint16_t);
109 // *u_nbo = htons (*u_hbo);
112 qbind->buffer = (void *) u_nbo;
113 qbind->buffer_length = sizeof(uint16_t);
114 qbind->buffer_type = MYSQL_TYPE_SHORT;
120 * Generate query parameter for an uint16_t in host byte order.
122 * @param x pointer to the query parameter to pass
124 struct GNUNET_MY_QueryParam
125 GNUNET_MY_query_param_uint16 (const uint16_t *x)
127 struct GNUNET_MY_QueryParam res = {
139 * Function called to convert input argument into SQL parameters
142 *@param pq data about the query
143 * @param qbind array of parameters to initialize
147 my_conv_uint32 (void *cls,
148 const struct GNUNET_MY_QueryParam *qp,
151 const uint32_t *u_hbo = qp->data;
154 GNUNET_assert (1 == qp->num_params);
156 u_nbo = GNUNET_new (uint32_t);
157 // *u_nbo = htonl (*u_hbo);
160 qbind->buffer = (void *) u_nbo;
161 qbind->buffer_length = sizeof(uint32_t);
162 qbind->buffer_type = MYSQL_TYPE_LONG;
168 * Generate query parameter for an uint32_t in host byte order
170 *@param x pointer to the query parameter to pass
172 struct GNUNET_MY_QueryParam
173 GNUNET_MY_query_param_uint32 (const uint32_t *x)
175 struct GNUNET_MY_QueryParam res = {
187 * Function called to convert input argument into SQL parameters
190 *@param pq data about the query
191 * @param qbind array of parameters to initialize
195 my_conv_uint64 (void *cls,
196 const struct GNUNET_MY_QueryParam *qp,
199 const uint64_t * u_hbo = qp->data;
202 GNUNET_assert (1 == qp->num_params);
204 u_nbo = GNUNET_new(uint64_t);
205 //*u_nbo = GNUNET_htonll (*u_hbo);
208 qbind->buffer = (void *) u_nbo;
209 qbind->buffer_length = sizeof (uint64_t);
210 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
216 * Generate query parameter for an uint64_t in host byte order
218 *@param x pointer to the query parameter to pass
220 struct GNUNET_MY_QueryParam
221 GNUNET_MY_query_param_uint64 (const uint64_t *x)
223 struct GNUNET_MY_QueryParam res = {
235 * Function called to convert input argument into SQL parameters
238 *@param pq data about the query
239 * @param qbind array of parameters to initialize
243 my_conv_rsa_public_key (void *cls,
244 const struct GNUNET_MY_QueryParam *qp,
247 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
251 GNUNET_assert(1 == qp->num_params);
253 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
255 qbind->buffer = (void *)buf;
256 qbind->buffer_length = buf_size - 1;
257 qbind->buffer_type = MYSQL_TYPE_LONG;
263 * Generate query parameter for an RSA public key. The
264 * database must contain a BLOB type in the respective position.
266 * @param x the query parameter to pass
267 * @return array entry for the query parameters to use
269 struct GNUNET_MY_QueryParam
270 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
272 struct GNUNET_MY_QueryParam res = {
273 &my_conv_rsa_public_key,
284 * Function called to convert input argument into SQL parameters
287 *@param pq data about the query
288 * @param qbind array of parameters to initialize
292 my_conv_rsa_signature (void *cls,
293 const struct GNUNET_MY_QueryParam *qp,
296 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
300 GNUNET_assert(1 == qp->num_params);
302 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
305 qbind->buffer = (void *)buf;
306 qbind->buffer_length = buf_size - 1;
307 qbind->buffer_type = MYSQL_TYPE_LONG;
313 * Generate query parameter for an RSA signature. The
314 * database must contain a BLOB type in the respective position
316 *@param x 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_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
322 struct GNUNET_MY_QueryParam res = {
323 &my_conv_rsa_signature,
333 * Generate query parameter for an absolute time value.
334 * The database must store a 64-bit integer.
336 *@param x pointer to the query parameter to pass
337 *@return array entry for the query parameters to use
339 struct GNUNET_MY_QueryParam
340 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
342 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
346 * Generate query parameter for an absolute time value.
347 * The database must store a 64-bit integer.
349 *@param x pointer to the query parameter to pass
351 struct GNUNET_MY_QueryParam
352 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
354 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
357 /* end of my_query_helper.c */