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;
47 qbind->buffer_type = 1;
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);
104 u_nbo = GNUNET_new (uint16_t);
105 *u_nbo = htons (*u_hbo);
106 qbind->buffer = (void *) u_nbo;
107 qbind->buffer_length = sizeof(uint16_t);
108 qbind->buffer_type = 1;
114 * Generate query parameter for an uint16_t in host byte order.
116 * @param x pointer to the query parameter to pass
118 struct GNUNET_MY_QueryParam
119 GNUNET_MY_query_param_uint16 (const uint16_t *x)
121 struct GNUNET_MY_QueryParam res = {
133 * Function called to convert input argument into SQL parameters
136 *@param pq data about the query
137 * @param qbind array of parameters to initialize
141 my_conv_uint32 (void *cls,
142 const struct GNUNET_MY_QueryParam *qp,
145 const uint32_t *u_hbo = qp->data;
148 GNUNET_assert (1 == qp->num_params);
150 u_nbo = GNUNET_new (uint32_t);
151 *u_nbo = htonl (*u_hbo);
153 qbind->buffer = (void *) u_nbo;
154 qbind->buffer_length = sizeof(uint32_t);
155 qbind->buffer_type = 1;
161 * Generate query parameter for an uint32_t in host byte order
163 *@param x pointer to the query parameter to pass
165 struct GNUNET_MY_QueryParam
166 GNUNET_MY_query_param_uint32 (const uint32_t *x)
168 struct GNUNET_MY_QueryParam res = {
180 * Function called to convert input argument into SQL parameters
183 *@param pq data about the query
184 * @param qbind array of parameters to initialize
188 my_conv_uint64 (void *cls,
189 const struct GNUNET_MY_QueryParam *qp,
192 const uint64_t * u_hbo = qp->data;
195 GNUNET_assert (1 == qp->num_params);
197 u_nbo = GNUNET_new(uint64_t);
198 *u_nbo = GNUNET_htonll (*u_hbo);
200 qbind->buffer = (void *) u_nbo;
201 qbind->buffer_length = sizeof (uint64_t);
202 qbind->buffer_type = 1;
208 * Generate query parameter for an uint64_t in host byte order
210 *@param x pointer to the query parameter to pass
212 struct GNUNET_MY_QueryParam
213 GNUNET_MY_query_param_uint64 (const uint64_t *x)
215 struct GNUNET_MY_QueryParam res = {
227 * Function called to convert input argument into SQL parameters
230 *@param pq data about the query
231 * @param qbind array of parameters to initialize
235 my_conv_rsa_public_key (void *cls,
236 const struct GNUNET_MY_QueryParam *qp,
239 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
243 GNUNET_assert(1 == qp->num_params);
245 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
247 qbind->buffer = (void *)buf;
248 qbind->buffer_length = buf_size - 1;
249 qbind->buffer_type = 1;
255 * Generate query parameter for an RSA public key. The
256 * database must contain a BLOB type in the respective position.
258 * @param x the query parameter to pass
259 * @return array entry for the query parameters to use
261 struct GNUNET_MY_QueryParam
262 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
264 struct GNUNET_MY_QueryParam res = {
265 &my_conv_rsa_public_key,
276 * Function called to convert input argument into SQL parameters
279 *@param pq data about the query
280 * @param qbind array of parameters to initialize
284 my_conv_rsa_signature (void *cls,
285 const struct GNUNET_MY_QueryParam *qp,
288 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
292 GNUNET_assert(1 == qp->num_params);
294 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
297 qbind->buffer = (void *)buf;
298 qbind->buffer_length = buf_size - 1;
299 qbind->buffer_type = 1;
305 * Generate query parameter for an RSA signature. The
306 * database must contain a BLOB type in the respective position
308 *@param x the query parameter to pass
309 *@return array entry for the query parameters to use
311 struct GNUNET_MY_QueryParam
312 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
314 struct GNUNET_MY_QueryParam res = {
315 &my_conv_rsa_signature,
325 * Generate query parameter for an absolute time value.
326 * The database must store a 64-bit integer.
328 *@param x pointer to the query parameter to pass
329 *@return array entry for the query parameters to use
331 struct GNUNET_MY_QueryParam
332 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
334 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
338 * Generate query parameter for an absolute time value.
339 * The database must store a 64-bit integer.
341 *@param x pointer to the query parameter to pass
343 struct GNUNET_MY_QueryParam
344 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
346 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
349 /* end of my_query_helper.c */