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,
94 sqlite3_bind_text (stmt,
105 * Generate query parameter for a string.
107 * @param ptr pointer to the string query parameter to pass
109 struct GNUNET_SQ_QueryParam
110 GNUNET_SQ_query_param_string (const char *ptr)
112 struct GNUNET_SQ_QueryParam qp = {
113 .conv = &bind_string,
122 * Function called to convert input argument into SQL parameters.
125 * @param data pointer to input argument
126 * @param data_len number of bytes in @a data (if applicable)
127 * @param stmt sqlite statement to bind parameters for
128 * @param off offset of the argument to bind in @a stmt, numbered from 1,
129 * so immediately suitable for passing to `sqlite3_bind`-functions.
130 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
133 bind_rsa_pub (void *cls,
139 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
143 GNUNET_break (NULL == cls);
144 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
147 sqlite3_bind_blob64 (stmt,
150 (sqlite3_uint64) buf_size,
154 return GNUNET_SYSERR;
162 * Generate query parameter for an RSA public key. The
163 * database must contain a BLOB type in the respective position.
165 * @param x the query parameter to pass.
167 struct GNUNET_SQ_QueryParam
168 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
170 struct GNUNET_SQ_QueryParam qp = {
171 .conv = &bind_rsa_pub,
180 * Function called to convert input argument into SQL parameters.
183 * @param data pointer to input argument
184 * @param data_len number of bytes in @a data (if applicable)
185 * @param stmt sqlite statement to bind parameters for
186 * @param off offset of the argument to bind in @a stmt, numbered from 1,
187 * so immediately suitable for passing to `sqlite3_bind`-functions.
188 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
191 bind_rsa_sig (void *cls,
197 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
201 GNUNET_break (NULL == cls);
202 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
205 sqlite3_bind_blob64 (stmt,
208 (sqlite3_uint64) buf_size,
212 return GNUNET_SYSERR;
220 * Generate query parameter for an RSA signature. The
221 * database must contain a BLOB type in the respective position.
223 * @param x the query parameter to pass
225 struct GNUNET_SQ_QueryParam
226 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
228 struct GNUNET_SQ_QueryParam qp = {
229 .conv = &bind_rsa_sig,
238 * Function called to convert input argument into SQL parameters.
241 * @param data pointer to input argument
242 * @param data_len number of bytes in @a data (if applicable)
243 * @param stmt sqlite statement to bind parameters for
244 * @param off offset of the argument to bind in @a stmt, numbered from 1,
245 * so immediately suitable for passing to `sqlite3_bind`-functions.
246 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
249 bind_abstime (void *cls,
255 const struct GNUNET_TIME_Absolute *u = data;
256 struct GNUNET_TIME_Absolute abs;
259 if (abs.abs_value_us > INT64_MAX)
260 abs.abs_value_us = INT64_MAX;
261 GNUNET_assert (sizeof (uint64_t) == data_len);
263 sqlite3_bind_int64 (stmt,
265 (sqlite3_int64) abs.abs_value_us))
266 return GNUNET_SYSERR;
272 * Generate query parameter for an absolute time value.
273 * The database must store a 64-bit integer.
275 * @param x pointer to the query parameter to pass
277 struct GNUNET_SQ_QueryParam
278 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
280 struct GNUNET_SQ_QueryParam qp = {
281 .conv = &bind_abstime,
283 .size = sizeof (struct GNUNET_TIME_Absolute),
291 * Function called to convert input argument into SQL parameters.
294 * @param data pointer to input argument
295 * @param data_len number of bytes in @a data (if applicable)
296 * @param stmt sqlite statement to bind parameters for
297 * @param off offset of the argument to bind in @a stmt, numbered from 1,
298 * so immediately suitable for passing to `sqlite3_bind`-functions.
299 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
302 bind_nbotime (void *cls,
308 const struct GNUNET_TIME_AbsoluteNBO *u = data;
309 struct GNUNET_TIME_Absolute abs;
311 abs = GNUNET_TIME_absolute_ntoh (*u);
312 if (abs.abs_value_us > INT64_MAX)
313 abs.abs_value_us = INT64_MAX;
314 GNUNET_assert (sizeof (uint64_t) == data_len);
316 sqlite3_bind_int64 (stmt,
318 (sqlite3_int64) abs.abs_value_us))
319 return GNUNET_SYSERR;
325 * Generate query parameter for an absolute time value.
326 * The database must store a 64-bit integer.
328 * @param x pointer to the query parameter to pass
330 struct GNUNET_SQ_QueryParam
331 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
333 struct GNUNET_SQ_QueryParam qp = {
334 .conv = &bind_nbotime,
336 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
344 * Function called to convert input argument into SQL parameters.
347 * @param data pointer to input argument
348 * @param data_len number of bytes in @a data (if applicable)
349 * @param stmt sqlite statement to bind parameters for
350 * @param off offset of the argument to bind in @a stmt, numbered from 1,
351 * so immediately suitable for passing to `sqlite3_bind`-functions.
352 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
361 const uint16_t *u = data;
363 GNUNET_assert (sizeof (uint16_t) == data_len);
365 sqlite3_bind_int (stmt,
368 return GNUNET_SYSERR;
374 * Generate query parameter for an uint16_t in host byte order.
376 * @param x pointer to the query parameter to pass
378 struct GNUNET_SQ_QueryParam
379 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
381 struct GNUNET_SQ_QueryParam qp = {
384 .size = sizeof (uint16_t),
392 * Function called to convert input argument into SQL parameters.
395 * @param data pointer to input argument
396 * @param data_len number of bytes in @a data (if applicable)
397 * @param stmt sqlite statement to bind parameters for
398 * @param off offset of the argument to bind in @a stmt, numbered from 1,
399 * so immediately suitable for passing to `sqlite3_bind`-functions.
400 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
409 const uint32_t *u = data;
411 GNUNET_assert (sizeof (uint32_t) == data_len);
413 sqlite3_bind_int64 (stmt,
416 return GNUNET_SYSERR;
421 * Generate query parameter for an uint32_t in host byte order.
423 * @param x pointer to the query parameter to pass
425 struct GNUNET_SQ_QueryParam
426 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
428 struct GNUNET_SQ_QueryParam qp = {
431 .size = sizeof (uint32_t),
439 * Function called to convert input argument into SQL parameters.
442 * @param data pointer to input argument
443 * @param data_len number of bytes in @a data (if applicable)
444 * @param stmt sqlite statement to bind parameters for
445 * @param off offset of the argument to bind in @a stmt, numbered from 1,
446 * so immediately suitable for passing to `sqlite3_bind`-functions.
447 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
456 const uint64_t *u = data;
458 GNUNET_assert (sizeof (uint64_t) == data_len);
460 sqlite3_bind_int64 (stmt,
463 return GNUNET_SYSERR;
469 * Generate query parameter for an uint16_t in host byte order.
471 * @param x pointer to the query parameter to pass
473 struct GNUNET_SQ_QueryParam
474 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
476 struct GNUNET_SQ_QueryParam qp = {
479 .size = sizeof (uint64_t),
485 /* end of sq_query_helper.c */