diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-02-28 04:07:26 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-02-28 04:07:26 +0100 |
commit | 67935d7aa1090eed2eb00212741dcc4d13c0bb40 (patch) | |
tree | 6f261b7cda16c8a04ccec9d8763a3c0dbfaf5a95 /src/sq/sq_result_helper.c | |
parent | 830137e312e8e565c9113a20411aa3073fbd1cbd (diff) | |
download | gnunet-67935d7aa1090eed2eb00212741dcc4d13c0bb40.tar.gz gnunet-67935d7aa1090eed2eb00212741dcc4d13c0bb40.zip |
completing sq_result_helper
Diffstat (limited to 'src/sq/sq_result_helper.c')
-rw-r--r-- | src/sq/sq_result_helper.c | 553 |
1 files changed, 544 insertions, 9 deletions
diff --git a/src/sq/sq_result_helper.c b/src/sq/sq_result_helper.c index 36ce53317..9ac2e2533 100644 --- a/src/sq/sq_result_helper.c +++ b/src/sq/sq_result_helper.c | |||
@@ -24,26 +24,75 @@ | |||
24 | 24 | ||
25 | 25 | ||
26 | /** | 26 | /** |
27 | * Extract fixed-sized binary data from a Postgres database @a result at row @a row. | 27 | * Extract variable-sized binary data from a Postgres database @a result at row @a row. |
28 | * | 28 | * |
29 | * @param cls closure | 29 | * @param cls closure |
30 | * @param result where to extract data from | 30 | * @param result where to extract data from |
31 | * @param row row to extract data from | ||
32 | * @param column column to extract data from | 31 | * @param column column to extract data from |
33 | * @param[in,out] dst_size where to store size of result, may be NULL | 32 | * @param[in,out] dst_size where to store size of result, may be NULL |
34 | * @param[out] dst where to store the result | 33 | * @param[out] dst where to store the result (actually a `void **`) |
35 | * @return | 34 | * @return |
36 | * #GNUNET_YES if all results could be extracted | 35 | * #GNUNET_YES if all results could be extracted |
37 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 36 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
38 | */ | 37 | */ |
39 | static int | 38 | static int |
40 | extract_fixed_blob (void *cls, | 39 | extract_var_blob (void *cls, |
41 | sqlite3_stmt *result, | 40 | sqlite3_stmt *result, |
42 | int row, | 41 | unsigned int column, |
43 | unsigned int column, | 42 | size_t *dst_size, |
44 | size_t *dst_size, | 43 | void *dst) |
45 | void *dst) | ||
46 | { | 44 | { |
45 | int have; | ||
46 | const void *ret; | ||
47 | void **rdst = (void **) dst; | ||
48 | |||
49 | if (SQLITE_BLOB != | ||
50 | sqlite3_column_type (result, | ||
51 | column)) | ||
52 | { | ||
53 | GNUNET_break (0); | ||
54 | return GNUNET_SYSERR; | ||
55 | } | ||
56 | /* sqlite manual says to invoke 'sqlite3_column_blob()' | ||
57 | before calling sqlite3_column_bytes() */ | ||
58 | ret = sqlite3_column_blob (result, | ||
59 | column); | ||
60 | have = sqlite3_column_bytes (result, | ||
61 | column); | ||
62 | if (have < 0) | ||
63 | { | ||
64 | GNUNET_break (0); | ||
65 | return GNUNET_SYSERR; | ||
66 | } | ||
67 | *dst_size = have; | ||
68 | if (0 == have) | ||
69 | { | ||
70 | *rdst = NULL; | ||
71 | return GNUNET_OK; | ||
72 | } | ||
73 | *rdst = GNUNET_malloc (have); | ||
74 | GNUNET_memcpy (*rdst, | ||
75 | ret, | ||
76 | have); | ||
77 | return GNUNET_OK; | ||
78 | } | ||
79 | |||
80 | |||
81 | /** | ||
82 | * Cleanup memory allocated by #extract_var_blob(). | ||
83 | * | ||
84 | * @param cls pointer to pointer of allocation | ||
85 | */ | ||
86 | static void | ||
87 | clean_var_blob (void *cls) | ||
88 | { | ||
89 | void **dptr = (void **) cls; | ||
90 | |||
91 | if (NULL != *dptr) | ||
92 | { | ||
93 | GNUNET_free (*dptr); | ||
94 | *dptr = NULL; | ||
95 | } | ||
47 | } | 96 | } |
48 | 97 | ||
49 | 98 | ||
@@ -58,6 +107,62 @@ struct GNUNET_SQ_ResultSpec | |||
58 | GNUNET_SQ_result_spec_variable_size (void **dst, | 107 | GNUNET_SQ_result_spec_variable_size (void **dst, |
59 | size_t *sptr) | 108 | size_t *sptr) |
60 | { | 109 | { |
110 | struct GNUNET_SQ_ResultSpec rs = { | ||
111 | .conv = &extract_var_blob, | ||
112 | .cleaner = &clean_var_blob, | ||
113 | .cls = dst, | ||
114 | .result_size = sptr, | ||
115 | .num_params = 1 | ||
116 | }; | ||
117 | |||
118 | return rs; | ||
119 | } | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Extract fixed-sized binary data from a Postgres database @a result at row @a row. | ||
124 | * | ||
125 | * @param cls closure | ||
126 | * @param result where to extract data from | ||
127 | * @param column column to extract data from | ||
128 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
129 | * @param[out] dst where to store the result | ||
130 | * @return | ||
131 | * #GNUNET_YES if all results could be extracted | ||
132 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
133 | */ | ||
134 | static int | ||
135 | extract_fixed_blob (void *cls, | ||
136 | sqlite3_stmt *result, | ||
137 | unsigned int column, | ||
138 | size_t *dst_size, | ||
139 | void *dst) | ||
140 | { | ||
141 | int have; | ||
142 | const void *ret; | ||
143 | |||
144 | if (SQLITE_BLOB != | ||
145 | sqlite3_column_type (result, | ||
146 | column)) | ||
147 | { | ||
148 | GNUNET_break (0); | ||
149 | return GNUNET_SYSERR; | ||
150 | } | ||
151 | /* sqlite manual says to invoke 'sqlite3_column_blob()' | ||
152 | before calling sqlite3_column_bytes() */ | ||
153 | ret = sqlite3_column_blob (result, | ||
154 | column); | ||
155 | have = sqlite3_column_bytes (result, | ||
156 | column); | ||
157 | if (*dst_size != have) | ||
158 | { | ||
159 | GNUNET_break (0); | ||
160 | return GNUNET_SYSERR; | ||
161 | } | ||
162 | GNUNET_memcpy (dst, | ||
163 | ret, | ||
164 | have); | ||
165 | return GNUNET_OK; | ||
61 | } | 166 | } |
62 | 167 | ||
63 | 168 | ||
@@ -72,6 +177,76 @@ struct GNUNET_SQ_ResultSpec | |||
72 | GNUNET_SQ_result_spec_fixed_size (void *dst, | 177 | GNUNET_SQ_result_spec_fixed_size (void *dst, |
73 | size_t dst_size) | 178 | size_t dst_size) |
74 | { | 179 | { |
180 | struct GNUNET_SQ_ResultSpec rs = { | ||
181 | .conv = &extract_fixed_blob, | ||
182 | .dst = dst, | ||
183 | .dst_size = dst_size, | ||
184 | .num_params = 1 | ||
185 | }; | ||
186 | |||
187 | return rs; | ||
188 | } | ||
189 | |||
190 | |||
191 | /** | ||
192 | * Extract fixed-sized binary data from a Postgres database @a result at row @a row. | ||
193 | * | ||
194 | * @param cls closure | ||
195 | * @param result where to extract data from | ||
196 | * @param column column to extract data from | ||
197 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
198 | * @param[out] dst where to store the result | ||
199 | * @return | ||
200 | * #GNUNET_YES if all results could be extracted | ||
201 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
202 | */ | ||
203 | static int | ||
204 | extract_utf8_string (void *cls, | ||
205 | sqlite3_stmt *result, | ||
206 | unsigned int column, | ||
207 | size_t *dst_size, | ||
208 | void *dst) | ||
209 | { | ||
210 | const char *text; | ||
211 | char **rdst = dst; | ||
212 | |||
213 | if (SQLITE_TEXT != | ||
214 | sqlite3_column_type (result, | ||
215 | column)) | ||
216 | { | ||
217 | GNUNET_break (0); | ||
218 | return GNUNET_SYSERR; | ||
219 | } | ||
220 | /* sqlite manual guarantees that 'sqlite3_column_text()' | ||
221 | is 0-terminated */ | ||
222 | text = (const char *) sqlite3_column_text (result, | ||
223 | column); | ||
224 | if (NULL == text) | ||
225 | { | ||
226 | GNUNET_break (0); | ||
227 | return GNUNET_SYSERR; | ||
228 | } | ||
229 | *dst_size = strlen (text) + 1; | ||
230 | *rdst = GNUNET_strdup (text); | ||
231 | return GNUNET_OK; | ||
232 | } | ||
233 | |||
234 | |||
235 | /** | ||
236 | * Cleanup memory allocated by #extract_var_blob(). | ||
237 | * | ||
238 | * @param cls pointer to pointer of allocation | ||
239 | */ | ||
240 | static void | ||
241 | clean_utf8_string (void *cls) | ||
242 | { | ||
243 | char **dptr = (char **) cls; | ||
244 | |||
245 | if (NULL != *dptr) | ||
246 | { | ||
247 | GNUNET_free (*dptr); | ||
248 | *dptr = NULL; | ||
249 | } | ||
75 | } | 250 | } |
76 | 251 | ||
77 | 252 | ||
@@ -84,6 +259,87 @@ GNUNET_SQ_result_spec_fixed_size (void *dst, | |||
84 | struct GNUNET_SQ_ResultSpec | 259 | struct GNUNET_SQ_ResultSpec |
85 | GNUNET_SQ_result_spec_string (char **dst) | 260 | GNUNET_SQ_result_spec_string (char **dst) |
86 | { | 261 | { |
262 | struct GNUNET_SQ_ResultSpec rs = { | ||
263 | .conv = &extract_utf8_string, | ||
264 | .cleaner = &clean_utf8_string, | ||
265 | .cls = dst, | ||
266 | .dst = dst, | ||
267 | .num_params = 1 | ||
268 | }; | ||
269 | |||
270 | return rs; | ||
271 | } | ||
272 | |||
273 | |||
274 | /** | ||
275 | * Extract data from a Postgres database @a result at row @a row. | ||
276 | * | ||
277 | * @param cls closure | ||
278 | * @param result where to extract data from | ||
279 | * @param column column to extract data from | ||
280 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
281 | * @param[out] dst where to store the result | ||
282 | * @return | ||
283 | * #GNUNET_YES if all results could be extracted | ||
284 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
285 | */ | ||
286 | static int | ||
287 | extract_rsa_pub (void *cls, | ||
288 | sqlite3_stmt *result, | ||
289 | unsigned int column, | ||
290 | size_t *dst_size, | ||
291 | void *dst) | ||
292 | { | ||
293 | struct GNUNET_CRYPTO_RsaPublicKey **pk = dst; | ||
294 | int have; | ||
295 | const void *ret; | ||
296 | |||
297 | if (SQLITE_BLOB != | ||
298 | sqlite3_column_type (result, | ||
299 | column)) | ||
300 | { | ||
301 | GNUNET_break (0); | ||
302 | return GNUNET_SYSERR; | ||
303 | } | ||
304 | /* sqlite manual says to invoke 'sqlite3_column_blob()' | ||
305 | before calling sqlite3_column_bytes() */ | ||
306 | ret = sqlite3_column_blob (result, | ||
307 | column); | ||
308 | have = sqlite3_column_bytes (result, | ||
309 | column); | ||
310 | if (have < 0) | ||
311 | { | ||
312 | GNUNET_break (0); | ||
313 | return GNUNET_SYSERR; | ||
314 | } | ||
315 | |||
316 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (ret, | ||
317 | have); | ||
318 | if (NULL == *pk) | ||
319 | { | ||
320 | GNUNET_break (0); | ||
321 | return GNUNET_SYSERR; | ||
322 | } | ||
323 | return GNUNET_OK; | ||
324 | } | ||
325 | |||
326 | |||
327 | /** | ||
328 | * Function called to clean up memory allocated | ||
329 | * by a #GNUNET_PQ_ResultConverter. | ||
330 | * | ||
331 | * @param cls closure | ||
332 | */ | ||
333 | static void | ||
334 | clean_rsa_pub (void *cls) | ||
335 | { | ||
336 | struct GNUNET_CRYPTO_RsaPublicKey **pk = cls; | ||
337 | |||
338 | if (NULL != *pk) | ||
339 | { | ||
340 | GNUNET_CRYPTO_rsa_public_key_free (*pk); | ||
341 | *pk = NULL; | ||
342 | } | ||
87 | } | 343 | } |
88 | 344 | ||
89 | 345 | ||
@@ -96,6 +352,87 @@ GNUNET_SQ_result_spec_string (char **dst) | |||
96 | struct GNUNET_SQ_ResultSpec | 352 | struct GNUNET_SQ_ResultSpec |
97 | GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) | 353 | GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) |
98 | { | 354 | { |
355 | struct GNUNET_SQ_ResultSpec rs = { | ||
356 | .conv = &extract_rsa_pub, | ||
357 | .cleaner = &clean_rsa_pub, | ||
358 | .dst = rsa, | ||
359 | .cls = rsa, | ||
360 | .num_params = 1 | ||
361 | }; | ||
362 | |||
363 | return rs; | ||
364 | } | ||
365 | |||
366 | |||
367 | /** | ||
368 | * Extract data from a Postgres database @a result at row @a row. | ||
369 | * | ||
370 | * @param cls closure | ||
371 | * @param result where to extract data from | ||
372 | * @param column column to extract data from | ||
373 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
374 | * @param[out] dst where to store the result | ||
375 | * @return | ||
376 | * #GNUNET_YES if all results could be extracted | ||
377 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
378 | */ | ||
379 | static int | ||
380 | extract_rsa_sig (void *cls, | ||
381 | sqlite3_stmt *result, | ||
382 | unsigned int column, | ||
383 | size_t *dst_size, | ||
384 | void *dst) | ||
385 | { | ||
386 | struct GNUNET_CRYPTO_RsaSignature **sig = dst; | ||
387 | int have; | ||
388 | const void *ret; | ||
389 | |||
390 | if (SQLITE_BLOB != | ||
391 | sqlite3_column_type (result, | ||
392 | column)) | ||
393 | { | ||
394 | GNUNET_break (0); | ||
395 | return GNUNET_SYSERR; | ||
396 | } | ||
397 | /* sqlite manual says to invoke 'sqlite3_column_blob()' | ||
398 | before calling sqlite3_column_bytes() */ | ||
399 | ret = sqlite3_column_blob (result, | ||
400 | column); | ||
401 | have = sqlite3_column_bytes (result, | ||
402 | column); | ||
403 | if (have < 0) | ||
404 | { | ||
405 | GNUNET_break (0); | ||
406 | return GNUNET_SYSERR; | ||
407 | } | ||
408 | |||
409 | *sig = GNUNET_CRYPTO_rsa_signature_decode (ret, | ||
410 | have); | ||
411 | if (NULL == *sig) | ||
412 | { | ||
413 | GNUNET_break (0); | ||
414 | return GNUNET_SYSERR; | ||
415 | } | ||
416 | return GNUNET_OK; | ||
417 | } | ||
418 | |||
419 | |||
420 | /** | ||
421 | * Function called to clean up memory allocated | ||
422 | * by a #GNUNET_PQ_ResultConverter. | ||
423 | * | ||
424 | * @param cls result data to clean up | ||
425 | */ | ||
426 | static void | ||
427 | clean_rsa_sig (void *cls) | ||
428 | { | ||
429 | struct GNUNET_CRYPTO_RsaSignature **sig = cls; | ||
430 | |||
431 | if (NULL != *sig) | ||
432 | { | ||
433 | GNUNET_CRYPTO_rsa_signature_free (*sig); | ||
434 | *sig = NULL; | ||
435 | } | ||
99 | } | 436 | } |
100 | 437 | ||
101 | 438 | ||
@@ -108,6 +445,15 @@ GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) | |||
108 | struct GNUNET_SQ_ResultSpec | 445 | struct GNUNET_SQ_ResultSpec |
109 | GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) | 446 | GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) |
110 | { | 447 | { |
448 | struct GNUNET_SQ_ResultSpec rs = { | ||
449 | .conv = &extract_rsa_sig, | ||
450 | .cleaner = &clean_rsa_sig, | ||
451 | .dst = sig, | ||
452 | .cls = sig, | ||
453 | .num_params = 1 | ||
454 | }; | ||
455 | |||
456 | return rs; | ||
111 | } | 457 | } |
112 | 458 | ||
113 | 459 | ||
@@ -120,6 +466,44 @@ GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) | |||
120 | struct GNUNET_SQ_ResultSpec | 466 | struct GNUNET_SQ_ResultSpec |
121 | GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) | 467 | GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) |
122 | { | 468 | { |
469 | return GNUNET_SQ_result_spec_uint64 (&at->abs_value_us); | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * Extract absolute time value in NBO from a Postgres database @a result at row @a row. | ||
475 | * | ||
476 | * @param cls closure | ||
477 | * @param result where to extract data from | ||
478 | * @param column column to extract data from | ||
479 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
480 | * @param[out] dst where to store the result | ||
481 | * @return | ||
482 | * #GNUNET_YES if all results could be extracted | ||
483 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
484 | */ | ||
485 | static int | ||
486 | extract_abs_time_nbo (void *cls, | ||
487 | sqlite3_stmt *result, | ||
488 | unsigned int column, | ||
489 | size_t *dst_size, | ||
490 | void *dst) | ||
491 | { | ||
492 | struct GNUNET_TIME_AbsoluteNBO *u = dst; | ||
493 | struct GNUNET_TIME_Absolute t; | ||
494 | |||
495 | GNUNET_assert (sizeof (uint64_t) == *dst_size); | ||
496 | if (SQLITE_INTEGER != | ||
497 | sqlite3_column_type (result, | ||
498 | column)) | ||
499 | { | ||
500 | GNUNET_break (0); | ||
501 | return GNUNET_SYSERR; | ||
502 | } | ||
503 | t.abs_value_us = (uint64_t) sqlite3_column_int64 (result, | ||
504 | column); | ||
505 | *u = GNUNET_TIME_absolute_hton (t); | ||
506 | return GNUNET_OK; | ||
123 | } | 507 | } |
124 | 508 | ||
125 | 509 | ||
@@ -132,6 +516,56 @@ GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) | |||
132 | struct GNUNET_SQ_ResultSpec | 516 | struct GNUNET_SQ_ResultSpec |
133 | GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) | 517 | GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) |
134 | { | 518 | { |
519 | struct GNUNET_SQ_ResultSpec rs = { | ||
520 | .conv = &extract_abs_time_nbo, | ||
521 | .dst = at, | ||
522 | .dst_size = sizeof (struct GNUNET_TIME_AbsoluteNBO), | ||
523 | .num_params = 1 | ||
524 | }; | ||
525 | |||
526 | return rs; | ||
527 | } | ||
528 | |||
529 | |||
530 | /** | ||
531 | * Extract 16-bit integer from a Postgres database @a result at row @a row. | ||
532 | * | ||
533 | * @param cls closure | ||
534 | * @param result where to extract data from | ||
535 | * @param column column to extract data from | ||
536 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
537 | * @param[out] dst where to store the result | ||
538 | * @return | ||
539 | * #GNUNET_YES if all results could be extracted | ||
540 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
541 | */ | ||
542 | static int | ||
543 | extract_uint16 (void *cls, | ||
544 | sqlite3_stmt *result, | ||
545 | unsigned int column, | ||
546 | size_t *dst_size, | ||
547 | void *dst) | ||
548 | { | ||
549 | uint64_t v; | ||
550 | uint32_t *u = dst; | ||
551 | |||
552 | GNUNET_assert (sizeof (uint16_t) == *dst_size); | ||
553 | if (SQLITE_INTEGER != | ||
554 | sqlite3_column_type (result, | ||
555 | column)) | ||
556 | { | ||
557 | GNUNET_break (0); | ||
558 | return GNUNET_SYSERR; | ||
559 | } | ||
560 | v = (uint64_t) sqlite3_column_int64 (result, | ||
561 | column); | ||
562 | if (v > UINT16_MAX) | ||
563 | { | ||
564 | GNUNET_break (0); | ||
565 | return GNUNET_SYSERR; | ||
566 | } | ||
567 | *u = (uint16_t) v; | ||
568 | return GNUNET_OK; | ||
135 | } | 569 | } |
136 | 570 | ||
137 | 571 | ||
@@ -144,6 +578,56 @@ GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) | |||
144 | struct GNUNET_SQ_ResultSpec | 578 | struct GNUNET_SQ_ResultSpec |
145 | GNUNET_SQ_result_spec_uint16 (uint16_t *u16) | 579 | GNUNET_SQ_result_spec_uint16 (uint16_t *u16) |
146 | { | 580 | { |
581 | struct GNUNET_SQ_ResultSpec rs = { | ||
582 | .conv = &extract_uint16, | ||
583 | .dst = u16, | ||
584 | .dst_size = sizeof (uint16_t), | ||
585 | .num_params = 1 | ||
586 | }; | ||
587 | |||
588 | return rs; | ||
589 | } | ||
590 | |||
591 | |||
592 | /** | ||
593 | * Extract 32-bit integer from a Postgres database @a result at row @a row. | ||
594 | * | ||
595 | * @param cls closure | ||
596 | * @param result where to extract data from | ||
597 | * @param column column to extract data from | ||
598 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
599 | * @param[out] dst where to store the result | ||
600 | * @return | ||
601 | * #GNUNET_YES if all results could be extracted | ||
602 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
603 | */ | ||
604 | static int | ||
605 | extract_uint32 (void *cls, | ||
606 | sqlite3_stmt *result, | ||
607 | unsigned int column, | ||
608 | size_t *dst_size, | ||
609 | void *dst) | ||
610 | { | ||
611 | uint64_t v; | ||
612 | uint32_t *u = dst; | ||
613 | |||
614 | GNUNET_assert (sizeof (uint32_t) == *dst_size); | ||
615 | if (SQLITE_INTEGER != | ||
616 | sqlite3_column_type (result, | ||
617 | column)) | ||
618 | { | ||
619 | GNUNET_break (0); | ||
620 | return GNUNET_SYSERR; | ||
621 | } | ||
622 | v = (uint64_t) sqlite3_column_int64 (result, | ||
623 | column); | ||
624 | if (v > UINT32_MAX) | ||
625 | { | ||
626 | GNUNET_break (0); | ||
627 | return GNUNET_SYSERR; | ||
628 | } | ||
629 | *u = (uint32_t) v; | ||
630 | return GNUNET_OK; | ||
147 | } | 631 | } |
148 | 632 | ||
149 | 633 | ||
@@ -156,6 +640,49 @@ GNUNET_SQ_result_spec_uint16 (uint16_t *u16) | |||
156 | struct GNUNET_SQ_ResultSpec | 640 | struct GNUNET_SQ_ResultSpec |
157 | GNUNET_SQ_result_spec_uint32 (uint32_t *u32) | 641 | GNUNET_SQ_result_spec_uint32 (uint32_t *u32) |
158 | { | 642 | { |
643 | struct GNUNET_SQ_ResultSpec rs = { | ||
644 | .conv = &extract_uint32, | ||
645 | .dst = u32, | ||
646 | .dst_size = sizeof (uint32_t), | ||
647 | .num_params = 1 | ||
648 | }; | ||
649 | |||
650 | return rs; | ||
651 | } | ||
652 | |||
653 | |||
654 | /** | ||
655 | * Extract 64-bit integer from a Postgres database @a result at row @a row. | ||
656 | * | ||
657 | * @param cls closure | ||
658 | * @param result where to extract data from | ||
659 | * @param column column to extract data from | ||
660 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
661 | * @param[out] dst where to store the result | ||
662 | * @return | ||
663 | * #GNUNET_YES if all results could be extracted | ||
664 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
665 | */ | ||
666 | static int | ||
667 | extract_uint64 (void *cls, | ||
668 | sqlite3_stmt *result, | ||
669 | unsigned int column, | ||
670 | size_t *dst_size, | ||
671 | void *dst) | ||
672 | { | ||
673 | uint64_t *u = dst; | ||
674 | |||
675 | GNUNET_assert (sizeof (uint64_t) == *dst_size); | ||
676 | if (SQLITE_INTEGER != | ||
677 | sqlite3_column_type (result, | ||
678 | column)) | ||
679 | { | ||
680 | GNUNET_break (0); | ||
681 | return GNUNET_SYSERR; | ||
682 | } | ||
683 | *u = (uint64_t) sqlite3_column_int64 (result, | ||
684 | column); | ||
685 | return GNUNET_OK; | ||
159 | } | 686 | } |
160 | 687 | ||
161 | 688 | ||
@@ -168,6 +695,14 @@ GNUNET_SQ_result_spec_uint32 (uint32_t *u32) | |||
168 | struct GNUNET_SQ_ResultSpec | 695 | struct GNUNET_SQ_ResultSpec |
169 | GNUNET_SQ_result_spec_uint64 (uint64_t *u64) | 696 | GNUNET_SQ_result_spec_uint64 (uint64_t *u64) |
170 | { | 697 | { |
698 | struct GNUNET_SQ_ResultSpec rs = { | ||
699 | .conv = &extract_uint64, | ||
700 | .dst = u64, | ||
701 | .dst_size = sizeof (uint64_t), | ||
702 | .num_params = 1 | ||
703 | }; | ||
704 | |||
705 | return rs; | ||
171 | } | 706 | } |
172 | 707 | ||
173 | 708 | ||