2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
16 * @file sq/sq_query_helper.c
17 * @brief helper functions for queries
18 * @author Christian Grothoff
21 #include "gnunet_sq_lib.h"
25 * Function called to convert input argument into SQL parameters.
28 * @param data pointer to input argument
29 * @param data_len number of bytes in @a data (if applicable)
30 * @param stmt sqlite statement to bind parameters for
31 * @param off offset of the argument to bind in @a stmt, numbered from 1,
32 * so immediately suitable for passing to `sqlite3_bind`-functions.
33 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
36 bind_fixed_blob (void *cls,
43 sqlite3_bind_blob64 (stmt,
46 (sqlite3_uint64) data_len,
54 * Generate query parameter for a buffer @a ptr of
57 * @param ptr pointer to the query parameter to pass
58 * @oaran ptr_size number of bytes in @a ptr
60 struct GNUNET_SQ_QueryParam
61 GNUNET_SQ_query_param_fixed_size (const void *ptr,
64 struct GNUNET_SQ_QueryParam qp = {
65 .conv = &bind_fixed_blob,
75 * Function called to convert input argument into SQL parameters.
78 * @param data pointer to input argument
79 * @param data_len number of bytes in @a data (if applicable)
80 * @param stmt sqlite statement to bind parameters for
81 * @param off offset of the argument to bind in @a stmt, numbered from 1,
82 * so immediately suitable for passing to `sqlite3_bind`-functions.
83 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
86 bind_string (void *cls,
95 sqlite3_bind_null (stmt,
101 sqlite3_bind_text (stmt,
106 return GNUNET_SYSERR;
112 * Generate query parameter for a string.
114 * @param ptr pointer to the string query parameter to pass
116 struct GNUNET_SQ_QueryParam
117 GNUNET_SQ_query_param_string (const char *ptr)
119 struct GNUNET_SQ_QueryParam qp = {
120 .conv = &bind_string,
129 * Function called to convert input argument into SQL parameters.
132 * @param data pointer to input argument
133 * @param data_len number of bytes in @a data (if applicable)
134 * @param stmt sqlite statement to bind parameters for
135 * @param off offset of the argument to bind in @a stmt, numbered from 1,
136 * so immediately suitable for passing to `sqlite3_bind`-functions.
137 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
140 bind_rsa_pub (void *cls,
146 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
150 GNUNET_break (NULL == cls);
151 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
154 sqlite3_bind_blob64 (stmt,
157 (sqlite3_uint64) buf_size,
161 return GNUNET_SYSERR;
169 * Generate query parameter for an RSA public key. The
170 * database must contain a BLOB type in the respective position.
172 * @param x the query parameter to pass.
174 struct GNUNET_SQ_QueryParam
175 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
177 struct GNUNET_SQ_QueryParam qp = {
178 .conv = &bind_rsa_pub,
187 * Function called to convert input argument into SQL parameters.
190 * @param data pointer to input argument
191 * @param data_len number of bytes in @a data (if applicable)
192 * @param stmt sqlite statement to bind parameters for
193 * @param off offset of the argument to bind in @a stmt, numbered from 1,
194 * so immediately suitable for passing to `sqlite3_bind`-functions.
195 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
198 bind_rsa_sig (void *cls,
204 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
208 GNUNET_break (NULL == cls);
209 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
212 sqlite3_bind_blob64 (stmt,
215 (sqlite3_uint64) buf_size,
219 return GNUNET_SYSERR;
227 * Generate query parameter for an RSA signature. The
228 * database must contain a BLOB type in the respective position.
230 * @param x the query parameter to pass
232 struct GNUNET_SQ_QueryParam
233 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
235 struct GNUNET_SQ_QueryParam qp = {
236 .conv = &bind_rsa_sig,
245 * Function called to convert input argument into SQL parameters.
248 * @param data pointer to input argument
249 * @param data_len number of bytes in @a data (if applicable)
250 * @param stmt sqlite statement to bind parameters for
251 * @param off offset of the argument to bind in @a stmt, numbered from 1,
252 * so immediately suitable for passing to `sqlite3_bind`-functions.
253 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
256 bind_abstime (void *cls,
262 const struct GNUNET_TIME_Absolute *u = data;
263 struct GNUNET_TIME_Absolute abs;
266 if (abs.abs_value_us > INT64_MAX)
267 abs.abs_value_us = INT64_MAX;
268 GNUNET_assert (sizeof (uint64_t) == data_len);
270 sqlite3_bind_int64 (stmt,
272 (sqlite3_int64) abs.abs_value_us))
273 return GNUNET_SYSERR;
279 * Generate query parameter for an absolute time value.
280 * The database must store a 64-bit integer.
282 * @param x pointer to the query parameter to pass
284 struct GNUNET_SQ_QueryParam
285 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
287 struct GNUNET_SQ_QueryParam qp = {
288 .conv = &bind_abstime,
290 .size = sizeof (struct GNUNET_TIME_Absolute),
298 * Function called to convert input argument into SQL parameters.
301 * @param data pointer to input argument
302 * @param data_len number of bytes in @a data (if applicable)
303 * @param stmt sqlite statement to bind parameters for
304 * @param off offset of the argument to bind in @a stmt, numbered from 1,
305 * so immediately suitable for passing to `sqlite3_bind`-functions.
306 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
309 bind_nbotime (void *cls,
315 const struct GNUNET_TIME_AbsoluteNBO *u = data;
316 struct GNUNET_TIME_Absolute abs;
318 abs = GNUNET_TIME_absolute_ntoh (*u);
319 if (abs.abs_value_us > INT64_MAX)
320 abs.abs_value_us = INT64_MAX;
321 GNUNET_assert (sizeof (uint64_t) == data_len);
323 sqlite3_bind_int64 (stmt,
325 (sqlite3_int64) abs.abs_value_us))
326 return GNUNET_SYSERR;
332 * Generate query parameter for an absolute time value.
333 * The database must store a 64-bit integer.
335 * @param x pointer to the query parameter to pass
337 struct GNUNET_SQ_QueryParam
338 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
340 struct GNUNET_SQ_QueryParam qp = {
341 .conv = &bind_nbotime,
343 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
351 * Function called to convert input argument into SQL parameters.
354 * @param data pointer to input argument
355 * @param data_len number of bytes in @a data (if applicable)
356 * @param stmt sqlite statement to bind parameters for
357 * @param off offset of the argument to bind in @a stmt, numbered from 1,
358 * so immediately suitable for passing to `sqlite3_bind`-functions.
359 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
368 const uint16_t *u = data;
370 GNUNET_assert (sizeof (uint16_t) == data_len);
372 sqlite3_bind_int (stmt,
375 return GNUNET_SYSERR;
381 * Generate query parameter for an uint16_t in host byte order.
383 * @param x pointer to the query parameter to pass
385 struct GNUNET_SQ_QueryParam
386 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
388 struct GNUNET_SQ_QueryParam qp = {
391 .size = sizeof (uint16_t),
399 * Function called to convert input argument into SQL parameters.
402 * @param data pointer to input argument
403 * @param data_len number of bytes in @a data (if applicable)
404 * @param stmt sqlite statement to bind parameters for
405 * @param off offset of the argument to bind in @a stmt, numbered from 1,
406 * so immediately suitable for passing to `sqlite3_bind`-functions.
407 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
416 const uint32_t *u = data;
418 GNUNET_assert (sizeof (uint32_t) == data_len);
420 sqlite3_bind_int64 (stmt,
423 return GNUNET_SYSERR;
428 * Generate query parameter for an uint32_t in host byte order.
430 * @param x pointer to the query parameter to pass
432 struct GNUNET_SQ_QueryParam
433 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
435 struct GNUNET_SQ_QueryParam qp = {
438 .size = sizeof (uint32_t),
446 * Function called to convert input argument into SQL parameters.
449 * @param data pointer to input argument
450 * @param data_len number of bytes in @a data (if applicable)
451 * @param stmt sqlite statement to bind parameters for
452 * @param off offset of the argument to bind in @a stmt, numbered from 1,
453 * so immediately suitable for passing to `sqlite3_bind`-functions.
454 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
463 const uint64_t *u = data;
465 GNUNET_assert (sizeof (uint64_t) == data_len);
467 sqlite3_bind_int64 (stmt,
470 return GNUNET_SYSERR;
476 * Generate query parameter for an uint16_t in host byte order.
478 * @param x pointer to the query parameter to pass
480 struct GNUNET_SQ_QueryParam
481 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
483 struct GNUNET_SQ_QueryParam qp = {
486 .size = sizeof (uint64_t),
492 /* end of sq_query_helper.c */