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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
17 * @file sq/sq_query_helper.c
18 * @brief helper functions for queries
19 * @author Christian Grothoff
22 #include "gnunet_sq_lib.h"
26 * Function called to convert input argument into SQL parameters.
29 * @param data pointer to input argument
30 * @param data_len number of bytes in @a data (if applicable)
31 * @param stmt sqlite statement to bind parameters for
32 * @param off offset of the argument to bind in @a stmt, numbered from 1,
33 * so immediately suitable for passing to `sqlite3_bind`-functions.
34 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
37 bind_fixed_blob (void *cls,
44 sqlite3_bind_blob64 (stmt,
47 (sqlite3_uint64) data_len,
55 * Generate query parameter for a buffer @a ptr of
58 * @param ptr pointer to the query parameter to pass
59 * @oaran ptr_size number of bytes in @a ptr
61 struct GNUNET_SQ_QueryParam
62 GNUNET_SQ_query_param_fixed_size (const void *ptr,
65 struct GNUNET_SQ_QueryParam qp = {
66 .conv = &bind_fixed_blob,
76 * Function called to convert input argument into SQL parameters.
79 * @param data pointer to input argument
80 * @param data_len number of bytes in @a data (if applicable)
81 * @param stmt sqlite statement to bind parameters for
82 * @param off offset of the argument to bind in @a stmt, numbered from 1,
83 * so immediately suitable for passing to `sqlite3_bind`-functions.
84 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
87 bind_string (void *cls,
96 sqlite3_bind_null (stmt,
102 sqlite3_bind_text (stmt,
107 return GNUNET_SYSERR;
113 * Generate query parameter for a string.
115 * @param ptr pointer to the string query parameter to pass
117 struct GNUNET_SQ_QueryParam
118 GNUNET_SQ_query_param_string (const char *ptr)
120 struct GNUNET_SQ_QueryParam qp = {
121 .conv = &bind_string,
130 * Function called to convert input argument into SQL parameters.
133 * @param data pointer to input argument
134 * @param data_len number of bytes in @a data (if applicable)
135 * @param stmt sqlite statement to bind parameters for
136 * @param off offset of the argument to bind in @a stmt, numbered from 1,
137 * so immediately suitable for passing to `sqlite3_bind`-functions.
138 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
141 bind_rsa_pub (void *cls,
147 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
151 GNUNET_break (NULL == cls);
152 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
155 sqlite3_bind_blob64 (stmt,
158 (sqlite3_uint64) buf_size,
162 return GNUNET_SYSERR;
170 * Generate query parameter for an RSA public key. The
171 * database must contain a BLOB type in the respective position.
173 * @param x the query parameter to pass.
175 struct GNUNET_SQ_QueryParam
176 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
178 struct GNUNET_SQ_QueryParam qp = {
179 .conv = &bind_rsa_pub,
188 * Function called to convert input argument into SQL parameters.
191 * @param data pointer to input argument
192 * @param data_len number of bytes in @a data (if applicable)
193 * @param stmt sqlite statement to bind parameters for
194 * @param off offset of the argument to bind in @a stmt, numbered from 1,
195 * so immediately suitable for passing to `sqlite3_bind`-functions.
196 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
199 bind_rsa_sig (void *cls,
205 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
209 GNUNET_break (NULL == cls);
210 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
213 sqlite3_bind_blob64 (stmt,
216 (sqlite3_uint64) buf_size,
220 return GNUNET_SYSERR;
228 * Generate query parameter for an RSA signature. The
229 * database must contain a BLOB type in the respective position.
231 * @param x the query parameter to pass
233 struct GNUNET_SQ_QueryParam
234 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
236 struct GNUNET_SQ_QueryParam qp = {
237 .conv = &bind_rsa_sig,
246 * Function called to convert input argument into SQL parameters.
249 * @param data pointer to input argument
250 * @param data_len number of bytes in @a data (if applicable)
251 * @param stmt sqlite statement to bind parameters for
252 * @param off offset of the argument to bind in @a stmt, numbered from 1,
253 * so immediately suitable for passing to `sqlite3_bind`-functions.
254 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
257 bind_abstime (void *cls,
263 const struct GNUNET_TIME_Absolute *u = data;
264 struct GNUNET_TIME_Absolute abs;
267 if (abs.abs_value_us > INT64_MAX)
268 abs.abs_value_us = INT64_MAX;
269 GNUNET_assert (sizeof (uint64_t) == data_len);
271 sqlite3_bind_int64 (stmt,
273 (sqlite3_int64) abs.abs_value_us))
274 return GNUNET_SYSERR;
280 * Generate query parameter for an absolute time value.
281 * The database must store a 64-bit integer.
283 * @param x pointer to the query parameter to pass
285 struct GNUNET_SQ_QueryParam
286 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
288 struct GNUNET_SQ_QueryParam qp = {
289 .conv = &bind_abstime,
291 .size = sizeof (struct GNUNET_TIME_Absolute),
299 * Function called to convert input argument into SQL parameters.
302 * @param data pointer to input argument
303 * @param data_len number of bytes in @a data (if applicable)
304 * @param stmt sqlite statement to bind parameters for
305 * @param off offset of the argument to bind in @a stmt, numbered from 1,
306 * so immediately suitable for passing to `sqlite3_bind`-functions.
307 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
310 bind_nbotime (void *cls,
316 const struct GNUNET_TIME_AbsoluteNBO *u = data;
317 struct GNUNET_TIME_Absolute abs;
319 abs = GNUNET_TIME_absolute_ntoh (*u);
320 if (abs.abs_value_us > INT64_MAX)
321 abs.abs_value_us = INT64_MAX;
322 GNUNET_assert (sizeof (uint64_t) == data_len);
324 sqlite3_bind_int64 (stmt,
326 (sqlite3_int64) abs.abs_value_us))
327 return GNUNET_SYSERR;
333 * Generate query parameter for an absolute time value.
334 * The database must store a 64-bit integer.
336 * @param x pointer to the query parameter to pass
338 struct GNUNET_SQ_QueryParam
339 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
341 struct GNUNET_SQ_QueryParam qp = {
342 .conv = &bind_nbotime,
344 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
352 * Function called to convert input argument into SQL parameters.
355 * @param data pointer to input argument
356 * @param data_len number of bytes in @a data (if applicable)
357 * @param stmt sqlite statement to bind parameters for
358 * @param off offset of the argument to bind in @a stmt, numbered from 1,
359 * so immediately suitable for passing to `sqlite3_bind`-functions.
360 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
369 const uint16_t *u = data;
371 GNUNET_assert (sizeof (uint16_t) == data_len);
373 sqlite3_bind_int (stmt,
376 return GNUNET_SYSERR;
382 * Generate query parameter for an uint16_t in host byte order.
384 * @param x pointer to the query parameter to pass
386 struct GNUNET_SQ_QueryParam
387 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
389 struct GNUNET_SQ_QueryParam qp = {
392 .size = sizeof (uint16_t),
400 * Function called to convert input argument into SQL parameters.
403 * @param data pointer to input argument
404 * @param data_len number of bytes in @a data (if applicable)
405 * @param stmt sqlite statement to bind parameters for
406 * @param off offset of the argument to bind in @a stmt, numbered from 1,
407 * so immediately suitable for passing to `sqlite3_bind`-functions.
408 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
417 const uint32_t *u = data;
419 GNUNET_assert (sizeof (uint32_t) == data_len);
421 sqlite3_bind_int64 (stmt,
424 return GNUNET_SYSERR;
429 * Generate query parameter for an uint32_t in host byte order.
431 * @param x pointer to the query parameter to pass
433 struct GNUNET_SQ_QueryParam
434 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
436 struct GNUNET_SQ_QueryParam qp = {
439 .size = sizeof (uint32_t),
447 * Function called to convert input argument into SQL parameters.
450 * @param data pointer to input argument
451 * @param data_len number of bytes in @a data (if applicable)
452 * @param stmt sqlite statement to bind parameters for
453 * @param off offset of the argument to bind in @a stmt, numbered from 1,
454 * so immediately suitable for passing to `sqlite3_bind`-functions.
455 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
464 const uint64_t *u = data;
466 GNUNET_assert (sizeof (uint64_t) == data_len);
468 sqlite3_bind_int64 (stmt,
471 return GNUNET_SYSERR;
477 * Generate query parameter for an uint16_t in host byte order.
479 * @param x pointer to the query parameter to pass
481 struct GNUNET_SQ_QueryParam
482 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
484 struct GNUNET_SQ_QueryParam qp = {
487 .size = sizeof (uint64_t),
493 /* end of sq_query_helper.c */