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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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,
43 GNUNET_free (qbind[0].buffer);
48 * Function called to convert input argument into SQL parameters.
51 * @param pq data about the query
52 * @param qbind array of parameters to initialize
56 my_conv_fixed_size (void *cls,
57 const struct GNUNET_MY_QueryParam *qp,
61 GNUNET_assert (1 == qp->num_params);
62 qbind->buffer = (void *) qp->data;
63 qbind->buffer_length = qp->data_len;
64 qbind->buffer_type = MYSQL_TYPE_BLOB;
71 * Generate query parameter for a buffer @a ptr of
74 * @param ptr pointer to the query parameter to pass
75 * @param ptr_size number of bytes in @a ptr
77 struct GNUNET_MY_QueryParam
78 GNUNET_MY_query_param_fixed_size (const void *ptr,
81 struct GNUNET_MY_QueryParam qp = {
82 .conv = &my_conv_fixed_size,
87 .data_len = (unsigned long) ptr_size
94 * Function called to convert input argument into SQL parameters.
97 * @param pq data about the query
98 * @param qbind array of parameters to initialize
102 my_conv_string (void *cls,
103 const struct GNUNET_MY_QueryParam *qp,
107 GNUNET_assert (1 == qp->num_params);
108 qbind->buffer = (void *) qp->data;
109 qbind->buffer_length = qp->data_len;
110 qbind->buffer_type = MYSQL_TYPE_STRING;
116 * Generate query parameter for a string
118 * @param ptr pointer to the string query parameter to pass
120 struct GNUNET_MY_QueryParam
121 GNUNET_MY_query_param_string (const char *ptr)
123 struct GNUNET_MY_QueryParam qp = {
124 .conv = &my_conv_string,
129 .data_len = strlen (ptr)
136 * Function called to convert input argument into SQL parameters
139 * @param pq data about the query
140 * @param qbind array of parameters to initialize
141 * @return -1 on error
144 my_conv_uint16 (void *cls,
145 const struct GNUNET_MY_QueryParam *qp,
149 GNUNET_assert (1 == qp->num_params);
150 qbind->buffer = (void *) qp->data;
151 qbind->buffer_length = sizeof (uint16_t);
152 qbind->buffer_type = MYSQL_TYPE_SHORT;
153 qbind->is_unsigned = 1;
159 * Generate query parameter for an uint16_t in host byte order.
161 * @param x pointer to the query parameter to pass
163 struct GNUNET_MY_QueryParam
164 GNUNET_MY_query_param_uint16 (const uint16_t *x)
166 struct GNUNET_MY_QueryParam res = {
167 .conv = &my_conv_uint16,
172 .data_len = sizeof (*x)
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
185 * @return -1 on error
188 my_conv_uint32 (void *cls,
189 const struct GNUNET_MY_QueryParam *qp,
193 GNUNET_assert (1 == qp->num_params);
194 qbind->buffer = (void *) qp->data;
195 qbind->buffer_length = sizeof(uint32_t);
196 qbind->buffer_type = MYSQL_TYPE_LONG;
197 qbind->is_unsigned = 1;
203 * Generate query parameter for an uint32_t in host byte order
205 * @param x pointer to the query parameter to pass
207 struct GNUNET_MY_QueryParam
208 GNUNET_MY_query_param_uint32 (const uint32_t *x)
210 struct GNUNET_MY_QueryParam res = {
211 .conv = &my_conv_uint32,
216 .data_len = sizeof (*x)
224 * Function called to convert input argument into SQL parameters
227 * @param pq data about the query
228 * @param qbind array of parameters to initialize
229 * @return -1 on error
232 my_conv_uint64 (void *cls,
233 const struct GNUNET_MY_QueryParam *qp,
237 GNUNET_assert (1 == qp->num_params);
238 qbind->buffer = (void *) qp->data;
239 qbind->buffer_length = sizeof (uint64_t);
240 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
241 qbind->is_unsigned = 1;
247 * Generate query parameter for an uint64_t in host byte order
249 * @param x pointer to the query parameter to pass
251 struct GNUNET_MY_QueryParam
252 GNUNET_MY_query_param_uint64 (const uint64_t *x)
254 struct GNUNET_MY_QueryParam res = {
255 .conv = &my_conv_uint64,
260 .data_len = sizeof(*x)
268 * Function called to convert input argument into SQL parameters
271 * @param pq data about the query
272 * @param qbind array of parameters to initialize
273 * @return -1 on error
276 my_conv_rsa_public_key (void *cls,
277 const struct GNUNET_MY_QueryParam *qp,
280 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
285 GNUNET_assert(1 == qp->num_params);
286 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
288 qbind->buffer = (void *) buf;
289 qbind->buffer_length = buf_size;
290 qbind->buffer_type = MYSQL_TYPE_BLOB;
296 * Generate query parameter for an RSA public key. The
297 * database must contain a BLOB type in the respective position.
299 * @param x the query parameter to pass
300 * @return array entry for the query parameters to use
302 struct GNUNET_MY_QueryParam
303 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
305 struct GNUNET_MY_QueryParam res = {
306 .conv = &my_conv_rsa_public_key,
307 .cleaner = &my_clean_query,
319 * Function called to convert input argument into SQL parameters
322 *@param pq data about the query
323 *@param qbind array of parameters to initialize
327 my_conv_rsa_signature (void *cls,
328 const struct GNUNET_MY_QueryParam *qp,
331 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
336 GNUNET_assert(1 == qp->num_params);
337 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
339 qbind->buffer = (void *) buf;
340 qbind->buffer_length = buf_size;
341 qbind->buffer_type = MYSQL_TYPE_BLOB;
347 * Generate query parameter for an RSA signature. The
348 * database must contain a BLOB type in the respective position
350 * @param x the query parameter to pass
351 * @return array entry for the query parameters to use
353 struct GNUNET_MY_QueryParam
354 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
356 struct GNUNET_MY_QueryParam res = {
357 .conv = &my_conv_rsa_signature,
358 .cleaner = &my_clean_query,
369 * Generate query parameter for an absolute time value.
370 * The database must store a 64-bit integer.
372 * @param x pointer to the query parameter to pass
373 * @return array entry for the query parameters to use
375 struct GNUNET_MY_QueryParam
376 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
378 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
383 * Generate query parameter for an absolute time value.
384 * The database must store a 64-bit integer.
386 * @param x pointer to the query parameter to pass
388 struct GNUNET_MY_QueryParam
389 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
391 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
395 /* end of my_query_helper.c */