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 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 sq/sq_query_helper.c
22 * @brief helper functions for queries
23 * @author Christian Grothoff
26 #include "gnunet_sq_lib.h"
30 * Function called to convert input argument into SQL parameters.
33 * @param data pointer to input argument
34 * @param data_len number of bytes in @a data (if applicable)
35 * @param stmt sqlite statement to bind parameters for
36 * @param off offset of the argument to bind in @a stmt, numbered from 1,
37 * so immediately suitable for passing to `sqlite3_bind`-functions.
38 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
41 bind_fixed_blob (void *cls,
48 sqlite3_bind_blob64 (stmt,
51 (sqlite3_uint64) data_len,
59 * Generate query parameter for a buffer @a ptr of
62 * @param ptr pointer to the query parameter to pass
63 * @oaran ptr_size number of bytes in @a ptr
65 struct GNUNET_SQ_QueryParam
66 GNUNET_SQ_query_param_fixed_size (const void *ptr,
69 struct GNUNET_SQ_QueryParam qp = {
70 .conv = &bind_fixed_blob,
81 * Function called to convert input argument into SQL parameters.
84 * @param data pointer to input argument
85 * @param data_len number of bytes in @a data (if applicable)
86 * @param stmt sqlite statement to bind parameters for
87 * @param off offset of the argument to bind in @a stmt, numbered from 1,
88 * so immediately suitable for passing to `sqlite3_bind`-functions.
89 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
92 bind_string (void *cls,
101 sqlite3_bind_null (stmt,
103 return GNUNET_SYSERR;
107 sqlite3_bind_text (stmt,
112 return GNUNET_SYSERR;
118 * Generate query parameter for a string.
120 * @param ptr pointer to the string query parameter to pass
122 struct GNUNET_SQ_QueryParam
123 GNUNET_SQ_query_param_string (const char *ptr)
125 struct GNUNET_SQ_QueryParam qp = {
126 .conv = &bind_string,
136 * Function called to convert input argument into SQL parameters.
139 * @param data pointer to input argument
140 * @param data_len number of bytes in @a data (if applicable)
141 * @param stmt sqlite statement to bind parameters for
142 * @param off offset of the argument to bind in @a stmt, numbered from 1,
143 * so immediately suitable for passing to `sqlite3_bind`-functions.
144 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
147 bind_rsa_pub (void *cls,
153 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
157 GNUNET_break (NULL == cls);
158 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
161 sqlite3_bind_blob64 (stmt,
164 (sqlite3_uint64) buf_size,
168 return GNUNET_SYSERR;
176 * Generate query parameter for an RSA public key. The
177 * database must contain a BLOB type in the respective position.
179 * @param x the query parameter to pass.
181 struct GNUNET_SQ_QueryParam
182 GNUNET_SQ_query_param_rsa_public_key (const struct
183 GNUNET_CRYPTO_RsaPublicKey *x)
185 struct GNUNET_SQ_QueryParam qp = {
186 .conv = &bind_rsa_pub,
196 * Function called to convert input argument into SQL parameters.
199 * @param data pointer to input argument
200 * @param data_len number of bytes in @a data (if applicable)
201 * @param stmt sqlite statement to bind parameters for
202 * @param off offset of the argument to bind in @a stmt, numbered from 1,
203 * so immediately suitable for passing to `sqlite3_bind`-functions.
204 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
207 bind_rsa_sig (void *cls,
213 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
217 GNUNET_break (NULL == cls);
218 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
221 sqlite3_bind_blob64 (stmt,
224 (sqlite3_uint64) buf_size,
228 return GNUNET_SYSERR;
236 * Generate query parameter for an RSA signature. The
237 * database must contain a BLOB type in the respective position.
239 * @param x the query parameter to pass
241 struct GNUNET_SQ_QueryParam
242 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
244 struct GNUNET_SQ_QueryParam qp = {
245 .conv = &bind_rsa_sig,
255 * Function called to convert input argument into SQL parameters.
258 * @param data pointer to input argument
259 * @param data_len number of bytes in @a data (if applicable)
260 * @param stmt sqlite statement to bind parameters for
261 * @param off offset of the argument to bind in @a stmt, numbered from 1,
262 * so immediately suitable for passing to `sqlite3_bind`-functions.
263 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
266 bind_abstime (void *cls,
272 const struct GNUNET_TIME_Absolute *u = data;
273 struct GNUNET_TIME_Absolute abs;
276 if (abs.abs_value_us > INT64_MAX)
277 abs.abs_value_us = INT64_MAX;
278 GNUNET_assert (sizeof(uint64_t) == data_len);
280 sqlite3_bind_int64 (stmt,
282 (sqlite3_int64) abs.abs_value_us))
283 return GNUNET_SYSERR;
289 * Generate query parameter for an absolute time value.
290 * The database must store a 64-bit integer.
292 * @param x pointer to the query parameter to pass
294 struct GNUNET_SQ_QueryParam
295 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
297 struct GNUNET_SQ_QueryParam qp = {
298 .conv = &bind_abstime,
300 .size = sizeof(struct GNUNET_TIME_Absolute),
309 * Function called to convert input argument into SQL parameters.
312 * @param data pointer to input argument
313 * @param data_len number of bytes in @a data (if applicable)
314 * @param stmt sqlite statement to bind parameters for
315 * @param off offset of the argument to bind in @a stmt, numbered from 1,
316 * so immediately suitable for passing to `sqlite3_bind`-functions.
317 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
320 bind_nbotime (void *cls,
326 const struct GNUNET_TIME_AbsoluteNBO *u = data;
327 struct GNUNET_TIME_Absolute abs;
329 abs = GNUNET_TIME_absolute_ntoh (*u);
330 if (abs.abs_value_us > INT64_MAX)
331 abs.abs_value_us = INT64_MAX;
332 GNUNET_assert (sizeof(uint64_t) == data_len);
334 sqlite3_bind_int64 (stmt,
336 (sqlite3_int64) abs.abs_value_us))
337 return GNUNET_SYSERR;
343 * Generate query parameter for an absolute time value.
344 * The database must store a 64-bit integer.
346 * @param x pointer to the query parameter to pass
348 struct GNUNET_SQ_QueryParam
349 GNUNET_SQ_query_param_absolute_time_nbo (const struct
350 GNUNET_TIME_AbsoluteNBO *x)
352 struct GNUNET_SQ_QueryParam qp = {
353 .conv = &bind_nbotime,
355 .size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
364 * Function called to convert input argument into SQL parameters.
367 * @param data pointer to input argument
368 * @param data_len number of bytes in @a data (if applicable)
369 * @param stmt sqlite statement to bind parameters for
370 * @param off offset of the argument to bind in @a stmt, numbered from 1,
371 * so immediately suitable for passing to `sqlite3_bind`-functions.
372 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
381 const uint16_t *u = data;
383 GNUNET_assert (sizeof(uint16_t) == data_len);
385 sqlite3_bind_int (stmt,
388 return GNUNET_SYSERR;
394 * Generate query parameter for an uint16_t in host byte order.
396 * @param x pointer to the query parameter to pass
398 struct GNUNET_SQ_QueryParam
399 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
401 struct GNUNET_SQ_QueryParam qp = {
404 .size = sizeof(uint16_t),
413 * Function called to convert input argument into SQL parameters.
416 * @param data pointer to input argument
417 * @param data_len number of bytes in @a data (if applicable)
418 * @param stmt sqlite statement to bind parameters for
419 * @param off offset of the argument to bind in @a stmt, numbered from 1,
420 * so immediately suitable for passing to `sqlite3_bind`-functions.
421 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
430 const uint32_t *u = data;
432 GNUNET_assert (sizeof(uint32_t) == data_len);
434 sqlite3_bind_int64 (stmt,
436 (sqlite3_int64) * u))
437 return GNUNET_SYSERR;
442 * Generate query parameter for an uint32_t in host byte order.
444 * @param x pointer to the query parameter to pass
446 struct GNUNET_SQ_QueryParam
447 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
449 struct GNUNET_SQ_QueryParam qp = {
452 .size = sizeof(uint32_t),
461 * Function called to convert input argument into SQL parameters.
464 * @param data pointer to input argument
465 * @param data_len number of bytes in @a data (if applicable)
466 * @param stmt sqlite statement to bind parameters for
467 * @param off offset of the argument to bind in @a stmt, numbered from 1,
468 * so immediately suitable for passing to `sqlite3_bind`-functions.
469 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
478 const uint64_t *u = data;
480 GNUNET_assert (sizeof(uint64_t) == data_len);
482 sqlite3_bind_int64 (stmt,
484 (sqlite3_int64) * u))
485 return GNUNET_SYSERR;
491 * Generate query parameter for an uint16_t in host byte order.
493 * @param x pointer to the query parameter to pass
495 struct GNUNET_SQ_QueryParam
496 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
498 struct GNUNET_SQ_QueryParam qp = {
501 .size = sizeof(uint64_t),
508 /* end of sq_query_helper.c */