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 * Generate query parameter for an absolute time value.
239 * The database must store a 64-bit integer.
241 * @param x pointer to the query parameter to pass
243 struct GNUNET_SQ_QueryParam
244 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
246 return GNUNET_SQ_query_param_uint64 (&x->abs_value_us);
251 * Function called to convert input argument into SQL parameters.
254 * @param data pointer to input argument
255 * @param data_len number of bytes in @a data (if applicable)
256 * @param stmt sqlite statement to bind parameters for
257 * @param off offset of the argument to bind in @a stmt, numbered from 1,
258 * so immediately suitable for passing to `sqlite3_bind`-functions.
259 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
262 bind_nbotime (void *cls,
268 const struct GNUNET_TIME_AbsoluteNBO *u = data;
269 struct GNUNET_TIME_Absolute abs;
271 abs = GNUNET_TIME_absolute_ntoh (*u);
272 GNUNET_assert (sizeof (uint64_t) == data_len);
274 sqlite3_bind_int64 (stmt,
276 (sqlite3_int64) abs.abs_value_us))
277 return GNUNET_SYSERR;
283 * Generate query parameter for an absolute time value.
284 * The database must store a 64-bit integer.
286 * @param x pointer to the query parameter to pass
288 struct GNUNET_SQ_QueryParam
289 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
291 struct GNUNET_SQ_QueryParam qp = {
292 .conv = &bind_nbotime,
294 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
302 * Function called to convert input argument into SQL parameters.
305 * @param data pointer to input argument
306 * @param data_len number of bytes in @a data (if applicable)
307 * @param stmt sqlite statement to bind parameters for
308 * @param off offset of the argument to bind in @a stmt, numbered from 1,
309 * so immediately suitable for passing to `sqlite3_bind`-functions.
310 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
319 const uint16_t *u = data;
321 GNUNET_assert (sizeof (uint16_t) == data_len);
323 sqlite3_bind_int (stmt,
326 return GNUNET_SYSERR;
332 * Generate query parameter for an uint16_t in host byte order.
334 * @param x pointer to the query parameter to pass
336 struct GNUNET_SQ_QueryParam
337 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
339 struct GNUNET_SQ_QueryParam qp = {
342 .size = sizeof (uint16_t),
350 * Function called to convert input argument into SQL parameters.
353 * @param data pointer to input argument
354 * @param data_len number of bytes in @a data (if applicable)
355 * @param stmt sqlite statement to bind parameters for
356 * @param off offset of the argument to bind in @a stmt, numbered from 1,
357 * so immediately suitable for passing to `sqlite3_bind`-functions.
358 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
367 const uint32_t *u = data;
369 GNUNET_assert (sizeof (uint32_t) == data_len);
371 sqlite3_bind_int64 (stmt,
374 return GNUNET_SYSERR;
379 * Generate query parameter for an uint32_t in host byte order.
381 * @param x pointer to the query parameter to pass
383 struct GNUNET_SQ_QueryParam
384 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
386 struct GNUNET_SQ_QueryParam qp = {
389 .size = sizeof (uint32_t),
397 * Function called to convert input argument into SQL parameters.
400 * @param data pointer to input argument
401 * @param data_len number of bytes in @a data (if applicable)
402 * @param stmt sqlite statement to bind parameters for
403 * @param off offset of the argument to bind in @a stmt, numbered from 1,
404 * so immediately suitable for passing to `sqlite3_bind`-functions.
405 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
414 const uint64_t *u = data;
416 GNUNET_assert (sizeof (uint64_t) == data_len);
418 sqlite3_bind_int64 (stmt,
421 return GNUNET_SYSERR;
427 * Generate query parameter for an uint16_t in host byte order.
429 * @param x pointer to the query parameter to pass
431 struct GNUNET_SQ_QueryParam
432 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
434 struct GNUNET_SQ_QueryParam qp = {
437 .size = sizeof (uint64_t),
443 /* end of sq_query_helper.c */