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/>.
19 * @file sq/sq_query_helper.c
20 * @brief helper functions for queries
21 * @author Christian Grothoff
24 #include "gnunet_sq_lib.h"
28 * Function called to convert input argument into SQL parameters.
31 * @param data pointer to input argument
32 * @param data_len number of bytes in @a data (if applicable)
33 * @param stmt sqlite statement to bind parameters for
34 * @param off offset of the argument to bind in @a stmt, numbered from 1,
35 * so immediately suitable for passing to `sqlite3_bind`-functions.
36 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
39 bind_fixed_blob (void *cls,
46 sqlite3_bind_blob64 (stmt,
49 (sqlite3_uint64) data_len,
57 * Generate query parameter for a buffer @a ptr of
60 * @param ptr pointer to the query parameter to pass
61 * @oaran ptr_size number of bytes in @a ptr
63 struct GNUNET_SQ_QueryParam
64 GNUNET_SQ_query_param_fixed_size (const void *ptr,
67 struct GNUNET_SQ_QueryParam qp = {
68 .conv = &bind_fixed_blob,
78 * Function called to convert input argument into SQL parameters.
81 * @param data pointer to input argument
82 * @param data_len number of bytes in @a data (if applicable)
83 * @param stmt sqlite statement to bind parameters for
84 * @param off offset of the argument to bind in @a stmt, numbered from 1,
85 * so immediately suitable for passing to `sqlite3_bind`-functions.
86 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
89 bind_string (void *cls,
98 sqlite3_bind_null (stmt,
100 return GNUNET_SYSERR;
104 sqlite3_bind_text (stmt,
109 return GNUNET_SYSERR;
115 * Generate query parameter for a string.
117 * @param ptr pointer to the string query parameter to pass
119 struct GNUNET_SQ_QueryParam
120 GNUNET_SQ_query_param_string (const char *ptr)
122 struct GNUNET_SQ_QueryParam qp = {
123 .conv = &bind_string,
132 * Function called to convert input argument into SQL parameters.
135 * @param data pointer to input argument
136 * @param data_len number of bytes in @a data (if applicable)
137 * @param stmt sqlite statement to bind parameters for
138 * @param off offset of the argument to bind in @a stmt, numbered from 1,
139 * so immediately suitable for passing to `sqlite3_bind`-functions.
140 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
143 bind_rsa_pub (void *cls,
149 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
153 GNUNET_break (NULL == cls);
154 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
157 sqlite3_bind_blob64 (stmt,
160 (sqlite3_uint64) buf_size,
164 return GNUNET_SYSERR;
172 * Generate query parameter for an RSA public key. The
173 * database must contain a BLOB type in the respective position.
175 * @param x the query parameter to pass.
177 struct GNUNET_SQ_QueryParam
178 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
180 struct GNUNET_SQ_QueryParam qp = {
181 .conv = &bind_rsa_pub,
190 * Function called to convert input argument into SQL parameters.
193 * @param data pointer to input argument
194 * @param data_len number of bytes in @a data (if applicable)
195 * @param stmt sqlite statement to bind parameters for
196 * @param off offset of the argument to bind in @a stmt, numbered from 1,
197 * so immediately suitable for passing to `sqlite3_bind`-functions.
198 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
201 bind_rsa_sig (void *cls,
207 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
211 GNUNET_break (NULL == cls);
212 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
215 sqlite3_bind_blob64 (stmt,
218 (sqlite3_uint64) buf_size,
222 return GNUNET_SYSERR;
230 * Generate query parameter for an RSA signature. The
231 * database must contain a BLOB type in the respective position.
233 * @param x the query parameter to pass
235 struct GNUNET_SQ_QueryParam
236 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
238 struct GNUNET_SQ_QueryParam qp = {
239 .conv = &bind_rsa_sig,
248 * Function called to convert input argument into SQL parameters.
251 * @param data pointer to input argument
252 * @param data_len number of bytes in @a data (if applicable)
253 * @param stmt sqlite statement to bind parameters for
254 * @param off offset of the argument to bind in @a stmt, numbered from 1,
255 * so immediately suitable for passing to `sqlite3_bind`-functions.
256 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
259 bind_abstime (void *cls,
265 const struct GNUNET_TIME_Absolute *u = data;
266 struct GNUNET_TIME_Absolute abs;
269 if (abs.abs_value_us > INT64_MAX)
270 abs.abs_value_us = INT64_MAX;
271 GNUNET_assert (sizeof (uint64_t) == data_len);
273 sqlite3_bind_int64 (stmt,
275 (sqlite3_int64) abs.abs_value_us))
276 return GNUNET_SYSERR;
282 * Generate query parameter for an absolute time value.
283 * The database must store a 64-bit integer.
285 * @param x pointer to the query parameter to pass
287 struct GNUNET_SQ_QueryParam
288 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
290 struct GNUNET_SQ_QueryParam qp = {
291 .conv = &bind_abstime,
293 .size = sizeof (struct GNUNET_TIME_Absolute),
301 * Function called to convert input argument into SQL parameters.
304 * @param data pointer to input argument
305 * @param data_len number of bytes in @a data (if applicable)
306 * @param stmt sqlite statement to bind parameters for
307 * @param off offset of the argument to bind in @a stmt, numbered from 1,
308 * so immediately suitable for passing to `sqlite3_bind`-functions.
309 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
312 bind_nbotime (void *cls,
318 const struct GNUNET_TIME_AbsoluteNBO *u = data;
319 struct GNUNET_TIME_Absolute abs;
321 abs = GNUNET_TIME_absolute_ntoh (*u);
322 if (abs.abs_value_us > INT64_MAX)
323 abs.abs_value_us = INT64_MAX;
324 GNUNET_assert (sizeof (uint64_t) == data_len);
326 sqlite3_bind_int64 (stmt,
328 (sqlite3_int64) abs.abs_value_us))
329 return GNUNET_SYSERR;
335 * Generate query parameter for an absolute time value.
336 * The database must store a 64-bit integer.
338 * @param x pointer to the query parameter to pass
340 struct GNUNET_SQ_QueryParam
341 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
343 struct GNUNET_SQ_QueryParam qp = {
344 .conv = &bind_nbotime,
346 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
354 * Function called to convert input argument into SQL parameters.
357 * @param data pointer to input argument
358 * @param data_len number of bytes in @a data (if applicable)
359 * @param stmt sqlite statement to bind parameters for
360 * @param off offset of the argument to bind in @a stmt, numbered from 1,
361 * so immediately suitable for passing to `sqlite3_bind`-functions.
362 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
371 const uint16_t *u = data;
373 GNUNET_assert (sizeof (uint16_t) == data_len);
375 sqlite3_bind_int (stmt,
378 return GNUNET_SYSERR;
384 * Generate query parameter for an uint16_t in host byte order.
386 * @param x pointer to the query parameter to pass
388 struct GNUNET_SQ_QueryParam
389 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
391 struct GNUNET_SQ_QueryParam qp = {
394 .size = sizeof (uint16_t),
402 * Function called to convert input argument into SQL parameters.
405 * @param data pointer to input argument
406 * @param data_len number of bytes in @a data (if applicable)
407 * @param stmt sqlite statement to bind parameters for
408 * @param off offset of the argument to bind in @a stmt, numbered from 1,
409 * so immediately suitable for passing to `sqlite3_bind`-functions.
410 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
419 const uint32_t *u = data;
421 GNUNET_assert (sizeof (uint32_t) == data_len);
423 sqlite3_bind_int64 (stmt,
426 return GNUNET_SYSERR;
431 * Generate query parameter for an uint32_t in host byte order.
433 * @param x pointer to the query parameter to pass
435 struct GNUNET_SQ_QueryParam
436 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
438 struct GNUNET_SQ_QueryParam qp = {
441 .size = sizeof (uint32_t),
449 * Function called to convert input argument into SQL parameters.
452 * @param data pointer to input argument
453 * @param data_len number of bytes in @a data (if applicable)
454 * @param stmt sqlite statement to bind parameters for
455 * @param off offset of the argument to bind in @a stmt, numbered from 1,
456 * so immediately suitable for passing to `sqlite3_bind`-functions.
457 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
466 const uint64_t *u = data;
468 GNUNET_assert (sizeof (uint64_t) == data_len);
470 sqlite3_bind_int64 (stmt,
473 return GNUNET_SYSERR;
479 * Generate query parameter for an uint16_t in host byte order.
481 * @param x pointer to the query parameter to pass
483 struct GNUNET_SQ_QueryParam
484 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
486 struct GNUNET_SQ_QueryParam qp = {
489 .size = sizeof (uint64_t),
495 /* end of sq_query_helper.c */