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
95 * Function called to convert input argument into SQL parameters.
98 * @param pq data about the query
99 * @param qbind array of parameters to initialize
100 * @return -1 on error
103 my_conv_string(void *cls,
104 const struct GNUNET_MY_QueryParam *qp,
108 GNUNET_assert(1 == qp->num_params);
109 qbind->buffer = (void *)qp->data;
110 qbind->buffer_length = qp->data_len;
111 qbind->buffer_type = MYSQL_TYPE_STRING;
117 * Generate query parameter for a string
119 * @param ptr pointer to the string query parameter to pass
121 struct GNUNET_MY_QueryParam
122 GNUNET_MY_query_param_string(const char *ptr)
124 struct GNUNET_MY_QueryParam qp = {
125 .conv = &my_conv_string,
130 .data_len = strlen(ptr)
138 * Function called to convert input argument into SQL parameters
141 * @param pq data about the query
142 * @param qbind array of parameters to initialize
143 * @return -1 on error
146 my_conv_uint16(void *cls,
147 const struct GNUNET_MY_QueryParam *qp,
151 GNUNET_assert(1 == qp->num_params);
152 qbind->buffer = (void *)qp->data;
153 qbind->buffer_length = sizeof(uint16_t);
154 qbind->buffer_type = MYSQL_TYPE_SHORT;
155 qbind->is_unsigned = 1;
161 * Generate query parameter for an uint16_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_uint16(const uint16_t *x)
168 struct GNUNET_MY_QueryParam res = {
169 .conv = &my_conv_uint16,
174 .data_len = sizeof(*x)
182 * Function called to convert input argument into SQL parameters
185 * @param pq data about the query
186 * @param qbind array of parameters to initialize
187 * @return -1 on error
190 my_conv_uint32(void *cls,
191 const struct GNUNET_MY_QueryParam *qp,
195 GNUNET_assert(1 == qp->num_params);
196 qbind->buffer = (void *)qp->data;
197 qbind->buffer_length = sizeof(uint32_t);
198 qbind->buffer_type = MYSQL_TYPE_LONG;
199 qbind->is_unsigned = 1;
205 * Generate query parameter for an uint32_t in host byte order
207 * @param x pointer to the query parameter to pass
209 struct GNUNET_MY_QueryParam
210 GNUNET_MY_query_param_uint32(const uint32_t *x)
212 struct GNUNET_MY_QueryParam res = {
213 .conv = &my_conv_uint32,
218 .data_len = sizeof(*x)
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
231 * @return -1 on error
234 my_conv_uint64(void *cls,
235 const struct GNUNET_MY_QueryParam *qp,
239 GNUNET_assert(1 == qp->num_params);
240 qbind->buffer = (void *)qp->data;
241 qbind->buffer_length = sizeof(uint64_t);
242 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
243 qbind->is_unsigned = 1;
249 * Generate query parameter for an uint64_t in host byte order
251 * @param x pointer to the query parameter to pass
253 struct GNUNET_MY_QueryParam
254 GNUNET_MY_query_param_uint64(const uint64_t *x)
256 struct GNUNET_MY_QueryParam res = {
257 .conv = &my_conv_uint64,
262 .data_len = sizeof(*x)
270 * Function called to convert input argument into SQL parameters
273 * @param pq data about the query
274 * @param qbind array of parameters to initialize
275 * @return -1 on error
278 my_conv_rsa_public_key(void *cls,
279 const struct GNUNET_MY_QueryParam *qp,
282 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
287 GNUNET_assert(1 == qp->num_params);
288 buf_size = GNUNET_CRYPTO_rsa_public_key_encode(rsa,
290 qbind->buffer = (void *)buf;
291 qbind->buffer_length = buf_size;
292 qbind->buffer_type = MYSQL_TYPE_BLOB;
298 * Generate query parameter for an RSA public key. The
299 * database must contain a BLOB type in the respective position.
301 * @param x the query parameter to pass
302 * @return array entry for the query parameters to use
304 struct GNUNET_MY_QueryParam
305 GNUNET_MY_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
307 struct GNUNET_MY_QueryParam res = {
308 .conv = &my_conv_rsa_public_key,
309 .cleaner = &my_clean_query,
321 * Function called to convert input argument into SQL parameters
324 *@param pq data about the query
325 *@param qbind array of parameters to initialize
329 my_conv_rsa_signature(void *cls,
330 const struct GNUNET_MY_QueryParam *qp,
333 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
338 GNUNET_assert(1 == qp->num_params);
339 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
341 qbind->buffer = (void *)buf;
342 qbind->buffer_length = buf_size;
343 qbind->buffer_type = MYSQL_TYPE_BLOB;
349 * Generate query parameter for an RSA signature. The
350 * database must contain a BLOB type in the respective position
352 * @param x the query parameter to pass
353 * @return array entry for the query parameters to use
355 struct GNUNET_MY_QueryParam
356 GNUNET_MY_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
358 struct GNUNET_MY_QueryParam res = {
359 .conv = &my_conv_rsa_signature,
360 .cleaner = &my_clean_query,
372 * Generate query parameter for an absolute time value.
373 * The database must store a 64-bit integer.
375 * @param x pointer to the query parameter to pass
376 * @return array entry for the query parameters to use
378 struct GNUNET_MY_QueryParam
379 GNUNET_MY_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
381 return GNUNET_MY_query_param_uint64(&x->abs_value_us);
386 * Generate query parameter for an absolute time value.
387 * The database must store a 64-bit integer.
389 * @param x pointer to the query parameter to pass
391 struct GNUNET_MY_QueryParam
392 GNUNET_MY_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
394 return GNUNET_MY_query_param_auto_from_type(&x->abs_value_us__);
398 /* end of my_query_helper.c */