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/>.
19 * @file my/my_query_helper.c
20 * @brief library to help with access to a MySQL database
21 * @author Christian Grothoff
22 * @author Christophe Genevey
25 #include <mysql/mysql.h>
26 #include "gnunet_my_lib.h"
30 * Function called to clean up memory allocated
31 * by a #GNUNET_MY_QueryConverter.
34 * @param qbind array of parameter to clean up
37 my_clean_query (void *cls,
40 GNUNET_free (qbind[0].buffer);
45 * Function called to convert input argument into SQL parameters.
48 * @param pq data about the query
49 * @param qbind array of parameters to initialize
53 my_conv_fixed_size (void *cls,
54 const struct GNUNET_MY_QueryParam *qp,
57 GNUNET_assert (1 == qp->num_params);
58 qbind->buffer = (void *) qp->data;
59 qbind->buffer_length = qp->data_len;
60 qbind->buffer_type = MYSQL_TYPE_BLOB;
67 * Generate query parameter for a buffer @a ptr of
70 * @param ptr pointer to the query parameter to pass
71 * @param ptr_size number of bytes in @a ptr
73 struct GNUNET_MY_QueryParam
74 GNUNET_MY_query_param_fixed_size (const void *ptr,
77 struct GNUNET_MY_QueryParam qp = {
78 .conv = &my_conv_fixed_size,
83 .data_len = (unsigned long) ptr_size
90 * Function called to convert input argument into SQL parameters.
93 * @param pq data about the query
94 * @param qbind array of parameters to initialize
98 my_conv_string (void *cls,
99 const struct GNUNET_MY_QueryParam *qp,
102 GNUNET_assert (1 == qp->num_params);
104 qbind->buffer = (void *) qp->data;
105 qbind->buffer_length = qp->data_len;
106 qbind->buffer_type = MYSQL_TYPE_STRING;
113 * Generate query parameter for a string
115 * @param ptr pointer to the string query parameter to pass
117 struct GNUNET_MY_QueryParam
118 GNUNET_MY_query_param_string (const char *ptr)
120 struct GNUNET_MY_QueryParam qp = {
121 .conv = &my_conv_string,
126 .data_len = strlen (ptr)
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
138 * @return -1 on error
141 my_conv_uint16 (void *cls,
142 const struct GNUNET_MY_QueryParam *qp,
145 GNUNET_assert (1 == qp->num_params);
146 qbind->buffer = (void *) qp->data;
147 qbind->buffer_length = sizeof (uint16_t);
148 qbind->buffer_type = MYSQL_TYPE_SHORT;
154 * Generate query parameter for an uint16_t in host byte order.
156 * @param x pointer to the query parameter to pass
158 struct GNUNET_MY_QueryParam
159 GNUNET_MY_query_param_uint16 (const uint16_t *x)
161 struct GNUNET_MY_QueryParam res = {
162 .conv = &my_conv_uint16,
167 .data_len = sizeof (*x)
175 * Function called to convert input argument into SQL parameters
178 * @param pq data about the query
179 * @param qbind array of parameters to initialize
180 * @return -1 on error
183 my_conv_uint32 (void *cls,
184 const struct GNUNET_MY_QueryParam *qp,
187 GNUNET_assert (1 == qp->num_params);
188 qbind->buffer = (void *) qp->data;
189 qbind->buffer_length = sizeof(uint32_t);
190 qbind->buffer_type = MYSQL_TYPE_LONG;
197 * Generate query parameter for an uint32_t in host byte order
199 * @param x pointer to the query parameter to pass
201 struct GNUNET_MY_QueryParam
202 GNUNET_MY_query_param_uint32 (const uint32_t *x)
204 struct GNUNET_MY_QueryParam res = {
205 .conv = &my_conv_uint32,
210 .data_len = sizeof (*x)
218 * Function called to convert input argument into SQL parameters
221 * @param pq data about the query
222 * @param qbind array of parameters to initialize
223 * @return -1 on error
226 my_conv_uint64 (void *cls,
227 const struct GNUNET_MY_QueryParam *qp,
230 GNUNET_assert (1 == qp->num_params);
231 qbind->buffer = (void *) qp->data;
232 qbind->buffer_length = sizeof (uint64_t);
233 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
239 * Generate query parameter for an uint64_t in host byte order
241 * @param x pointer to the query parameter to pass
243 struct GNUNET_MY_QueryParam
244 GNUNET_MY_query_param_uint64 (const uint64_t *x)
246 struct GNUNET_MY_QueryParam res = {
247 .conv = &my_conv_uint64,
252 .data_len = sizeof(*x)
260 * Function called to convert input argument into SQL parameters
263 * @param pq data about the query
264 * @param qbind array of parameters to initialize
265 * @return -1 on error
268 my_conv_rsa_public_key (void *cls,
269 const struct GNUNET_MY_QueryParam *qp,
272 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
276 GNUNET_assert(1 == qp->num_params);
278 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
280 qbind->buffer = (void *) buf;
281 qbind->buffer_length = buf_size;
282 qbind->buffer_type = MYSQL_TYPE_BLOB;
289 * Generate query parameter for an RSA public key. The
290 * database must contain a BLOB type in the respective position.
292 * @param x the query parameter to pass
293 * @return array entry for the query parameters to use
295 struct GNUNET_MY_QueryParam
296 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
298 struct GNUNET_MY_QueryParam res = {
299 .conv = &my_conv_rsa_public_key,
300 .cleaner = &my_clean_query,
312 * Function called to convert input argument into SQL parameters
315 *@param pq data about the query
316 *@param qbind array of parameters to initialize
320 my_conv_rsa_signature (void *cls,
321 const struct GNUNET_MY_QueryParam *qp,
324 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
328 GNUNET_assert(1 == qp->num_params);
330 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
332 qbind->buffer = (void *) buf;
333 qbind->buffer_length = buf_size;
334 qbind->buffer_type = MYSQL_TYPE_BLOB;
341 * Generate query parameter for an RSA signature. The
342 * database must contain a BLOB type in the respective position
344 * @param x the query parameter to pass
345 * @return array entry for the query parameters to use
347 struct GNUNET_MY_QueryParam
348 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
350 struct GNUNET_MY_QueryParam res = {
351 .conv = &my_conv_rsa_signature,
352 .cleaner = &my_clean_query,
363 * Generate query parameter for an absolute time value.
364 * The database must store a 64-bit integer.
366 * @param x pointer to the query parameter to pass
367 * @return array entry for the query parameters to use
369 struct GNUNET_MY_QueryParam
370 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
372 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
377 * Generate query parameter for an absolute time value.
378 * The database must store a 64-bit integer.
380 * @param x pointer to the query parameter to pass
382 struct GNUNET_MY_QueryParam
383 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
385 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
389 /* end of my_query_helper.c */