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,
80 * Function called to convert input argument into SQL parameters.
83 * @param data pointer to input argument
84 * @param data_len number of bytes in @a data (if applicable)
85 * @param stmt sqlite statement to bind parameters for
86 * @param off offset of the argument to bind in @a stmt, numbered from 1,
87 * so immediately suitable for passing to `sqlite3_bind`-functions.
88 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
91 bind_string (void *cls,
100 sqlite3_bind_null (stmt,
102 return GNUNET_SYSERR;
106 sqlite3_bind_text (stmt,
111 return GNUNET_SYSERR;
117 * Generate query parameter for a string.
119 * @param ptr pointer to the string query parameter to pass
121 struct GNUNET_SQ_QueryParam
122 GNUNET_SQ_query_param_string (const char *ptr)
124 struct GNUNET_SQ_QueryParam qp = {
125 .conv = &bind_string,
134 * Function called to convert input argument into SQL parameters.
137 * @param data pointer to input argument
138 * @param data_len number of bytes in @a data (if applicable)
139 * @param stmt sqlite statement to bind parameters for
140 * @param off offset of the argument to bind in @a stmt, numbered from 1,
141 * so immediately suitable for passing to `sqlite3_bind`-functions.
142 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
145 bind_rsa_pub (void *cls,
151 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
155 GNUNET_break (NULL == cls);
156 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
159 sqlite3_bind_blob64 (stmt,
162 (sqlite3_uint64) buf_size,
166 return GNUNET_SYSERR;
174 * Generate query parameter for an RSA public key. The
175 * database must contain a BLOB type in the respective position.
177 * @param x the query parameter to pass.
179 struct GNUNET_SQ_QueryParam
180 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
182 struct GNUNET_SQ_QueryParam qp = {
183 .conv = &bind_rsa_pub,
192 * Function called to convert input argument into SQL parameters.
195 * @param data pointer to input argument
196 * @param data_len number of bytes in @a data (if applicable)
197 * @param stmt sqlite statement to bind parameters for
198 * @param off offset of the argument to bind in @a stmt, numbered from 1,
199 * so immediately suitable for passing to `sqlite3_bind`-functions.
200 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
203 bind_rsa_sig (void *cls,
209 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
213 GNUNET_break (NULL == cls);
214 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
217 sqlite3_bind_blob64 (stmt,
220 (sqlite3_uint64) buf_size,
224 return GNUNET_SYSERR;
232 * Generate query parameter for an RSA signature. The
233 * database must contain a BLOB type in the respective position.
235 * @param x the query parameter to pass
237 struct GNUNET_SQ_QueryParam
238 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
240 struct GNUNET_SQ_QueryParam qp = {
241 .conv = &bind_rsa_sig,
250 * Function called to convert input argument into SQL parameters.
253 * @param data pointer to input argument
254 * @param data_len number of bytes in @a data (if applicable)
255 * @param stmt sqlite statement to bind parameters for
256 * @param off offset of the argument to bind in @a stmt, numbered from 1,
257 * so immediately suitable for passing to `sqlite3_bind`-functions.
258 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
261 bind_abstime (void *cls,
267 const struct GNUNET_TIME_Absolute *u = data;
268 struct GNUNET_TIME_Absolute abs;
271 if (abs.abs_value_us > INT64_MAX)
272 abs.abs_value_us = INT64_MAX;
273 GNUNET_assert (sizeof (uint64_t) == data_len);
275 sqlite3_bind_int64 (stmt,
277 (sqlite3_int64) abs.abs_value_us))
278 return GNUNET_SYSERR;
284 * Generate query parameter for an absolute time value.
285 * The database must store a 64-bit integer.
287 * @param x pointer to the query parameter to pass
289 struct GNUNET_SQ_QueryParam
290 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
292 struct GNUNET_SQ_QueryParam qp = {
293 .conv = &bind_abstime,
295 .size = sizeof (struct GNUNET_TIME_Absolute),
303 * Function called to convert input argument into SQL parameters.
306 * @param data pointer to input argument
307 * @param data_len number of bytes in @a data (if applicable)
308 * @param stmt sqlite statement to bind parameters for
309 * @param off offset of the argument to bind in @a stmt, numbered from 1,
310 * so immediately suitable for passing to `sqlite3_bind`-functions.
311 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
314 bind_nbotime (void *cls,
320 const struct GNUNET_TIME_AbsoluteNBO *u = data;
321 struct GNUNET_TIME_Absolute abs;
323 abs = GNUNET_TIME_absolute_ntoh (*u);
324 if (abs.abs_value_us > INT64_MAX)
325 abs.abs_value_us = INT64_MAX;
326 GNUNET_assert (sizeof (uint64_t) == data_len);
328 sqlite3_bind_int64 (stmt,
330 (sqlite3_int64) abs.abs_value_us))
331 return GNUNET_SYSERR;
337 * Generate query parameter for an absolute time value.
338 * The database must store a 64-bit integer.
340 * @param x pointer to the query parameter to pass
342 struct GNUNET_SQ_QueryParam
343 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
345 struct GNUNET_SQ_QueryParam qp = {
346 .conv = &bind_nbotime,
348 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
356 * Function called to convert input argument into SQL parameters.
359 * @param data pointer to input argument
360 * @param data_len number of bytes in @a data (if applicable)
361 * @param stmt sqlite statement to bind parameters for
362 * @param off offset of the argument to bind in @a stmt, numbered from 1,
363 * so immediately suitable for passing to `sqlite3_bind`-functions.
364 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
373 const uint16_t *u = data;
375 GNUNET_assert (sizeof (uint16_t) == data_len);
377 sqlite3_bind_int (stmt,
380 return GNUNET_SYSERR;
386 * Generate query parameter for an uint16_t in host byte order.
388 * @param x pointer to the query parameter to pass
390 struct GNUNET_SQ_QueryParam
391 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
393 struct GNUNET_SQ_QueryParam qp = {
396 .size = sizeof (uint16_t),
404 * Function called to convert input argument into SQL parameters.
407 * @param data pointer to input argument
408 * @param data_len number of bytes in @a data (if applicable)
409 * @param stmt sqlite statement to bind parameters for
410 * @param off offset of the argument to bind in @a stmt, numbered from 1,
411 * so immediately suitable for passing to `sqlite3_bind`-functions.
412 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
421 const uint32_t *u = data;
423 GNUNET_assert (sizeof (uint32_t) == data_len);
425 sqlite3_bind_int64 (stmt,
428 return GNUNET_SYSERR;
433 * Generate query parameter for an uint32_t in host byte order.
435 * @param x pointer to the query parameter to pass
437 struct GNUNET_SQ_QueryParam
438 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
440 struct GNUNET_SQ_QueryParam qp = {
443 .size = sizeof (uint32_t),
451 * Function called to convert input argument into SQL parameters.
454 * @param data pointer to input argument
455 * @param data_len number of bytes in @a data (if applicable)
456 * @param stmt sqlite statement to bind parameters for
457 * @param off offset of the argument to bind in @a stmt, numbered from 1,
458 * so immediately suitable for passing to `sqlite3_bind`-functions.
459 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
468 const uint64_t *u = data;
470 GNUNET_assert (sizeof (uint64_t) == data_len);
472 sqlite3_bind_int64 (stmt,
475 return GNUNET_SYSERR;
481 * Generate query parameter for an uint16_t in host byte order.
483 * @param x pointer to the query parameter to pass
485 struct GNUNET_SQ_QueryParam
486 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
488 struct GNUNET_SQ_QueryParam qp = {
491 .size = sizeof (uint64_t),
497 /* end of sq_query_helper.c */