aboutsummaryrefslogtreecommitdiff
path: root/src/sq/sq_query_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sq/sq_query_helper.c')
-rw-r--r--src/sq/sq_query_helper.c305
1 files changed, 157 insertions, 148 deletions
diff --git a/src/sq/sq_query_helper.c b/src/sq/sq_query_helper.c
index 2c7a9028a..1addef499 100644
--- a/src/sq/sq_query_helper.c
+++ b/src/sq/sq_query_helper.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 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, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file sq/sq_query_helper.c 21 * @file sq/sq_query_helper.c
22 * @brief helper functions for queries 22 * @brief helper functions for queries
@@ -38,18 +38,18 @@
38 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 38 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
39 */ 39 */
40static int 40static int
41bind_fixed_blob (void *cls, 41bind_fixed_blob(void *cls,
42 const void *data, 42 const void *data,
43 size_t data_len, 43 size_t data_len,
44 sqlite3_stmt *stmt, 44 sqlite3_stmt *stmt,
45 unsigned int off) 45 unsigned int off)
46{ 46{
47 if (SQLITE_OK != 47 if (SQLITE_OK !=
48 sqlite3_bind_blob64 (stmt, 48 sqlite3_bind_blob64(stmt,
49 (int) off, 49 (int)off,
50 data, 50 data,
51 (sqlite3_uint64) data_len, 51 (sqlite3_uint64)data_len,
52 SQLITE_TRANSIENT)) 52 SQLITE_TRANSIENT))
53 return GNUNET_SYSERR; 53 return GNUNET_SYSERR;
54 return GNUNET_OK; 54 return GNUNET_OK;
55} 55}
@@ -63,8 +63,8 @@ bind_fixed_blob (void *cls,
63 * @oaran ptr_size number of bytes in @a ptr 63 * @oaran ptr_size number of bytes in @a ptr
64 */ 64 */
65struct GNUNET_SQ_QueryParam 65struct GNUNET_SQ_QueryParam
66GNUNET_SQ_query_param_fixed_size (const void *ptr, 66GNUNET_SQ_query_param_fixed_size(const void *ptr,
67 size_t ptr_size) 67 size_t ptr_size)
68{ 68{
69 struct GNUNET_SQ_QueryParam qp = { 69 struct GNUNET_SQ_QueryParam qp = {
70 .conv = &bind_fixed_blob, 70 .conv = &bind_fixed_blob,
@@ -72,6 +72,7 @@ GNUNET_SQ_query_param_fixed_size (const void *ptr,
72 .size = ptr_size, 72 .size = ptr_size,
73 .num_params = 1 73 .num_params = 1
74 }; 74 };
75
75 return qp; 76 return qp;
76} 77}
77 78
@@ -88,26 +89,26 @@ GNUNET_SQ_query_param_fixed_size (const void *ptr,
88 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 89 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
89 */ 90 */
90static int 91static int
91bind_string (void *cls, 92bind_string(void *cls,
92 const void *data, 93 const void *data,
93 size_t data_len, 94 size_t data_len,
94 sqlite3_stmt *stmt, 95 sqlite3_stmt *stmt,
95 unsigned int off) 96 unsigned int off)
96{ 97{
97 if (NULL == data) 98 if (NULL == data)
98 { 99 {
99 if (SQLITE_OK != 100 if (SQLITE_OK !=
100 sqlite3_bind_null (stmt, 101 sqlite3_bind_null(stmt,
101 (int) off)) 102 (int)off))
102 return GNUNET_SYSERR; 103 return GNUNET_SYSERR;
103 return GNUNET_OK; 104 return GNUNET_OK;
104 } 105 }
105 if (SQLITE_OK != 106 if (SQLITE_OK !=
106 sqlite3_bind_text (stmt, 107 sqlite3_bind_text(stmt,
107 (int) off, 108 (int)off,
108 (const char *) data, 109 (const char *)data,
109 -1, 110 -1,
110 SQLITE_TRANSIENT)) 111 SQLITE_TRANSIENT))
111 return GNUNET_SYSERR; 112 return GNUNET_SYSERR;
112 return GNUNET_OK; 113 return GNUNET_OK;
113} 114}
@@ -119,13 +120,14 @@ bind_string (void *cls,
119 * @param ptr pointer to the string query parameter to pass 120 * @param ptr pointer to the string query parameter to pass
120 */ 121 */
121struct GNUNET_SQ_QueryParam 122struct GNUNET_SQ_QueryParam
122GNUNET_SQ_query_param_string (const char *ptr) 123GNUNET_SQ_query_param_string(const char *ptr)
123{ 124{
124 struct GNUNET_SQ_QueryParam qp = { 125 struct GNUNET_SQ_QueryParam qp = {
125 .conv = &bind_string, 126 .conv = &bind_string,
126 .data = ptr, 127 .data = ptr,
127 .num_params = 1 128 .num_params = 1
128 }; 129 };
130
129 return qp; 131 return qp;
130} 132}
131 133
@@ -142,30 +144,30 @@ GNUNET_SQ_query_param_string (const char *ptr)
142 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 144 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
143 */ 145 */
144static int 146static int
145bind_rsa_pub (void *cls, 147bind_rsa_pub(void *cls,
146 const void *data, 148 const void *data,
147 size_t data_len, 149 size_t data_len,
148 sqlite3_stmt *stmt, 150 sqlite3_stmt *stmt,
149 unsigned int off) 151 unsigned int off)
150{ 152{
151 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data; 153 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
152 char *buf; 154 char *buf;
153 size_t buf_size; 155 size_t buf_size;
154 156
155 GNUNET_break (NULL == cls); 157 GNUNET_break(NULL == cls);
156 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, 158 buf_size = GNUNET_CRYPTO_rsa_public_key_encode(rsa,
157 &buf); 159 &buf);
158 if (SQLITE_OK != 160 if (SQLITE_OK !=
159 sqlite3_bind_blob64 (stmt, 161 sqlite3_bind_blob64(stmt,
160 (int) off, 162 (int)off,
161 buf, 163 buf,
162 (sqlite3_uint64) buf_size, 164 (sqlite3_uint64)buf_size,
163 SQLITE_TRANSIENT)) 165 SQLITE_TRANSIENT))
164 { 166 {
165 GNUNET_free (buf); 167 GNUNET_free(buf);
166 return GNUNET_SYSERR; 168 return GNUNET_SYSERR;
167 } 169 }
168 GNUNET_free (buf); 170 GNUNET_free(buf);
169 return GNUNET_OK; 171 return GNUNET_OK;
170} 172}
171 173
@@ -177,14 +179,15 @@ bind_rsa_pub (void *cls,
177 * @param x the query parameter to pass. 179 * @param x the query parameter to pass.
178 */ 180 */
179struct GNUNET_SQ_QueryParam 181struct GNUNET_SQ_QueryParam
180GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x) 182GNUNET_SQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
181{ 183{
182 struct GNUNET_SQ_QueryParam qp = { 184 struct GNUNET_SQ_QueryParam qp = {
183 .conv = &bind_rsa_pub, 185 .conv = &bind_rsa_pub,
184 .data = x, 186 .data = x,
185 .num_params = 1 187 .num_params = 1
186 }; 188 };
187 return qp; 189
190 return qp;
188} 191}
189 192
190 193
@@ -200,30 +203,30 @@ GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x
200 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 203 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
201 */ 204 */
202static int 205static int
203bind_rsa_sig (void *cls, 206bind_rsa_sig(void *cls,
204 const void *data, 207 const void *data,
205 size_t data_len, 208 size_t data_len,
206 sqlite3_stmt *stmt, 209 sqlite3_stmt *stmt,
207 unsigned int off) 210 unsigned int off)
208{ 211{
209 const struct GNUNET_CRYPTO_RsaSignature *sig = data; 212 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
210 char *buf; 213 char *buf;
211 size_t buf_size; 214 size_t buf_size;
212 215
213 GNUNET_break (NULL == cls); 216 GNUNET_break(NULL == cls);
214 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig, 217 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
215 &buf); 218 &buf);
216 if (SQLITE_OK != 219 if (SQLITE_OK !=
217 sqlite3_bind_blob64 (stmt, 220 sqlite3_bind_blob64(stmt,
218 (int) off, 221 (int)off,
219 buf, 222 buf,
220 (sqlite3_uint64) buf_size, 223 (sqlite3_uint64)buf_size,
221 SQLITE_TRANSIENT)) 224 SQLITE_TRANSIENT))
222 { 225 {
223 GNUNET_free (buf); 226 GNUNET_free(buf);
224 return GNUNET_SYSERR; 227 return GNUNET_SYSERR;
225 } 228 }
226 GNUNET_free (buf); 229 GNUNET_free(buf);
227 return GNUNET_OK; 230 return GNUNET_OK;
228} 231}
229 232
@@ -235,14 +238,15 @@ bind_rsa_sig (void *cls,
235 * @param x the query parameter to pass 238 * @param x the query parameter to pass
236 */ 239 */
237struct GNUNET_SQ_QueryParam 240struct GNUNET_SQ_QueryParam
238GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) 241GNUNET_SQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
239{ 242{
240 struct GNUNET_SQ_QueryParam qp = { 243 struct GNUNET_SQ_QueryParam qp = {
241 .conv = &bind_rsa_sig, 244 .conv = &bind_rsa_sig,
242 .data = x, 245 .data = x,
243 .num_params = 1 246 .num_params = 1
244 }; 247 };
245 return qp; 248
249 return qp;
246} 250}
247 251
248 252
@@ -258,11 +262,11 @@ GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
258 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 262 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
259 */ 263 */
260static int 264static int
261bind_abstime (void *cls, 265bind_abstime(void *cls,
262 const void *data, 266 const void *data,
263 size_t data_len, 267 size_t data_len,
264 sqlite3_stmt *stmt, 268 sqlite3_stmt *stmt,
265 unsigned int off) 269 unsigned int off)
266{ 270{
267 const struct GNUNET_TIME_Absolute *u = data; 271 const struct GNUNET_TIME_Absolute *u = data;
268 struct GNUNET_TIME_Absolute abs; 272 struct GNUNET_TIME_Absolute abs;
@@ -270,11 +274,11 @@ bind_abstime (void *cls,
270 abs = *u; 274 abs = *u;
271 if (abs.abs_value_us > INT64_MAX) 275 if (abs.abs_value_us > INT64_MAX)
272 abs.abs_value_us = INT64_MAX; 276 abs.abs_value_us = INT64_MAX;
273 GNUNET_assert (sizeof (uint64_t) == data_len); 277 GNUNET_assert(sizeof(uint64_t) == data_len);
274 if (SQLITE_OK != 278 if (SQLITE_OK !=
275 sqlite3_bind_int64 (stmt, 279 sqlite3_bind_int64(stmt,
276 (int) off, 280 (int)off,
277 (sqlite3_int64) abs.abs_value_us)) 281 (sqlite3_int64)abs.abs_value_us))
278 return GNUNET_SYSERR; 282 return GNUNET_SYSERR;
279 return GNUNET_OK; 283 return GNUNET_OK;
280} 284}
@@ -287,14 +291,15 @@ bind_abstime (void *cls,
287 * @param x pointer to the query parameter to pass 291 * @param x pointer to the query parameter to pass
288 */ 292 */
289struct GNUNET_SQ_QueryParam 293struct GNUNET_SQ_QueryParam
290GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) 294GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
291{ 295{
292 struct GNUNET_SQ_QueryParam qp = { 296 struct GNUNET_SQ_QueryParam qp = {
293 .conv = &bind_abstime, 297 .conv = &bind_abstime,
294 .data = x, 298 .data = x,
295 .size = sizeof (struct GNUNET_TIME_Absolute), 299 .size = sizeof(struct GNUNET_TIME_Absolute),
296 .num_params = 1 300 .num_params = 1
297 }; 301 };
302
298 return qp; 303 return qp;
299} 304}
300 305
@@ -311,23 +316,23 @@ GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
311 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 316 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
312 */ 317 */
313static int 318static int
314bind_nbotime (void *cls, 319bind_nbotime(void *cls,
315 const void *data, 320 const void *data,
316 size_t data_len, 321 size_t data_len,
317 sqlite3_stmt *stmt, 322 sqlite3_stmt *stmt,
318 unsigned int off) 323 unsigned int off)
319{ 324{
320 const struct GNUNET_TIME_AbsoluteNBO *u = data; 325 const struct GNUNET_TIME_AbsoluteNBO *u = data;
321 struct GNUNET_TIME_Absolute abs; 326 struct GNUNET_TIME_Absolute abs;
322 327
323 abs = GNUNET_TIME_absolute_ntoh (*u); 328 abs = GNUNET_TIME_absolute_ntoh(*u);
324 if (abs.abs_value_us > INT64_MAX) 329 if (abs.abs_value_us > INT64_MAX)
325 abs.abs_value_us = INT64_MAX; 330 abs.abs_value_us = INT64_MAX;
326 GNUNET_assert (sizeof (uint64_t) == data_len); 331 GNUNET_assert(sizeof(uint64_t) == data_len);
327 if (SQLITE_OK != 332 if (SQLITE_OK !=
328 sqlite3_bind_int64 (stmt, 333 sqlite3_bind_int64(stmt,
329 (int) off, 334 (int)off,
330 (sqlite3_int64) abs.abs_value_us)) 335 (sqlite3_int64)abs.abs_value_us))
331 return GNUNET_SYSERR; 336 return GNUNET_SYSERR;
332 return GNUNET_OK; 337 return GNUNET_OK;
333} 338}
@@ -340,14 +345,15 @@ bind_nbotime (void *cls,
340 * @param x pointer to the query parameter to pass 345 * @param x pointer to the query parameter to pass
341 */ 346 */
342struct GNUNET_SQ_QueryParam 347struct GNUNET_SQ_QueryParam
343GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x) 348GNUNET_SQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
344{ 349{
345 struct GNUNET_SQ_QueryParam qp = { 350 struct GNUNET_SQ_QueryParam qp = {
346 .conv = &bind_nbotime, 351 .conv = &bind_nbotime,
347 .data = x, 352 .data = x,
348 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO), 353 .size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
349 .num_params = 1 354 .num_params = 1
350 }; 355 };
356
351 return qp; 357 return qp;
352} 358}
353 359
@@ -364,19 +370,19 @@ GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x
364 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 370 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
365 */ 371 */
366static int 372static int
367bind_u16 (void *cls, 373bind_u16(void *cls,
368 const void *data, 374 const void *data,
369 size_t data_len, 375 size_t data_len,
370 sqlite3_stmt *stmt, 376 sqlite3_stmt *stmt,
371 unsigned int off) 377 unsigned int off)
372{ 378{
373 const uint16_t *u = data; 379 const uint16_t *u = data;
374 380
375 GNUNET_assert (sizeof (uint16_t) == data_len); 381 GNUNET_assert(sizeof(uint16_t) == data_len);
376 if (SQLITE_OK != 382 if (SQLITE_OK !=
377 sqlite3_bind_int (stmt, 383 sqlite3_bind_int(stmt,
378 (int) off, 384 (int)off,
379 (int) *u)) 385 (int)*u))
380 return GNUNET_SYSERR; 386 return GNUNET_SYSERR;
381 return GNUNET_OK; 387 return GNUNET_OK;
382} 388}
@@ -388,14 +394,15 @@ bind_u16 (void *cls,
388 * @param x pointer to the query parameter to pass 394 * @param x pointer to the query parameter to pass
389 */ 395 */
390struct GNUNET_SQ_QueryParam 396struct GNUNET_SQ_QueryParam
391GNUNET_SQ_query_param_uint16 (const uint16_t *x) 397GNUNET_SQ_query_param_uint16(const uint16_t *x)
392{ 398{
393 struct GNUNET_SQ_QueryParam qp = { 399 struct GNUNET_SQ_QueryParam qp = {
394 .conv = &bind_u16, 400 .conv = &bind_u16,
395 .data = x, 401 .data = x,
396 .size = sizeof (uint16_t), 402 .size = sizeof(uint16_t),
397 .num_params = 1 403 .num_params = 1
398 }; 404 };
405
399 return qp; 406 return qp;
400} 407}
401 408
@@ -412,19 +419,19 @@ GNUNET_SQ_query_param_uint16 (const uint16_t *x)
412 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 419 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
413 */ 420 */
414static int 421static int
415bind_u32 (void *cls, 422bind_u32(void *cls,
416 const void *data, 423 const void *data,
417 size_t data_len, 424 size_t data_len,
418 sqlite3_stmt *stmt, 425 sqlite3_stmt *stmt,
419 unsigned int off) 426 unsigned int off)
420{ 427{
421 const uint32_t *u = data; 428 const uint32_t *u = data;
422 429
423 GNUNET_assert (sizeof (uint32_t) == data_len); 430 GNUNET_assert(sizeof(uint32_t) == data_len);
424 if (SQLITE_OK != 431 if (SQLITE_OK !=
425 sqlite3_bind_int64 (stmt, 432 sqlite3_bind_int64(stmt,
426 (int) off, 433 (int)off,
427 (sqlite3_int64) *u)) 434 (sqlite3_int64) * u))
428 return GNUNET_SYSERR; 435 return GNUNET_SYSERR;
429 return GNUNET_OK; 436 return GNUNET_OK;
430} 437}
@@ -435,14 +442,15 @@ bind_u32 (void *cls,
435 * @param x pointer to the query parameter to pass 442 * @param x pointer to the query parameter to pass
436 */ 443 */
437struct GNUNET_SQ_QueryParam 444struct GNUNET_SQ_QueryParam
438GNUNET_SQ_query_param_uint32 (const uint32_t *x) 445GNUNET_SQ_query_param_uint32(const uint32_t *x)
439{ 446{
440 struct GNUNET_SQ_QueryParam qp = { 447 struct GNUNET_SQ_QueryParam qp = {
441 .conv = &bind_u32, 448 .conv = &bind_u32,
442 .data = x, 449 .data = x,
443 .size = sizeof (uint32_t), 450 .size = sizeof(uint32_t),
444 .num_params = 1 451 .num_params = 1
445 }; 452 };
453
446 return qp; 454 return qp;
447} 455}
448 456
@@ -459,19 +467,19 @@ GNUNET_SQ_query_param_uint32 (const uint32_t *x)
459 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 467 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
460 */ 468 */
461static int 469static int
462bind_u64 (void *cls, 470bind_u64(void *cls,
463 const void *data, 471 const void *data,
464 size_t data_len, 472 size_t data_len,
465 sqlite3_stmt *stmt, 473 sqlite3_stmt *stmt,
466 unsigned int off) 474 unsigned int off)
467{ 475{
468 const uint64_t *u = data; 476 const uint64_t *u = data;
469 477
470 GNUNET_assert (sizeof (uint64_t) == data_len); 478 GNUNET_assert(sizeof(uint64_t) == data_len);
471 if (SQLITE_OK != 479 if (SQLITE_OK !=
472 sqlite3_bind_int64 (stmt, 480 sqlite3_bind_int64(stmt,
473 (int) off, 481 (int)off,
474 (sqlite3_int64) *u)) 482 (sqlite3_int64) * u))
475 return GNUNET_SYSERR; 483 return GNUNET_SYSERR;
476 return GNUNET_OK; 484 return GNUNET_OK;
477} 485}
@@ -483,14 +491,15 @@ bind_u64 (void *cls,
483 * @param x pointer to the query parameter to pass 491 * @param x pointer to the query parameter to pass
484 */ 492 */
485struct GNUNET_SQ_QueryParam 493struct GNUNET_SQ_QueryParam
486GNUNET_SQ_query_param_uint64 (const uint64_t *x) 494GNUNET_SQ_query_param_uint64(const uint64_t *x)
487{ 495{
488 struct GNUNET_SQ_QueryParam qp = { 496 struct GNUNET_SQ_QueryParam qp = {
489 .conv = &bind_u64, 497 .conv = &bind_u64,
490 .data = x, 498 .data = x,
491 .size = sizeof (uint64_t), 499 .size = sizeof(uint64_t),
492 .num_params = 1 500 .num_params = 1
493 }; 501 };
502
494 return qp; 503 return qp;
495} 504}
496 505