diff options
Diffstat (limited to 'src/pq/pq_result_helper.c')
-rw-r--r-- | src/pq/pq_result_helper.c | 1120 |
1 files changed, 0 insertions, 1120 deletions
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c deleted file mode 100644 index f3d246c36..000000000 --- a/src/pq/pq_result_helper.c +++ /dev/null | |||
@@ -1,1120 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016, 2020 GNUnet e.V. | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file pq/pq_result_helper.c | ||
22 | * @brief functions to extract result values | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "gnunet_pq_lib.h" | ||
28 | |||
29 | |||
30 | struct GNUNET_PQ_ResultSpec | ||
31 | GNUNET_PQ_result_spec_allow_null (struct GNUNET_PQ_ResultSpec rs, | ||
32 | bool *is_null) | ||
33 | { | ||
34 | struct GNUNET_PQ_ResultSpec rsr; | ||
35 | |||
36 | rsr = rs; | ||
37 | rsr.is_nullable = true; | ||
38 | rsr.is_null = is_null; | ||
39 | return rsr; | ||
40 | } | ||
41 | |||
42 | |||
43 | /** | ||
44 | * Function called to clean up memory allocated | ||
45 | * by a #GNUNET_PQ_ResultConverter. | ||
46 | * | ||
47 | * @param cls closure | ||
48 | * @param rd result data to clean up | ||
49 | */ | ||
50 | static void | ||
51 | clean_varsize_blob (void *cls, | ||
52 | void *rd) | ||
53 | { | ||
54 | void **dst = rd; | ||
55 | |||
56 | (void) cls; | ||
57 | if (NULL != *dst) | ||
58 | { | ||
59 | GNUNET_free (*dst); | ||
60 | *dst = NULL; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | |||
65 | /** | ||
66 | * Extract data from a Postgres database @a result at row @a row. | ||
67 | * | ||
68 | * @param cls closure | ||
69 | * @param result where to extract data from | ||
70 | * @param int row to extract data from | ||
71 | * @param fname name (or prefix) of the fields to extract from | ||
72 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
73 | * @param[out] dst where to store the result | ||
74 | * @return | ||
75 | * #GNUNET_YES if all results could be extracted | ||
76 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | ||
77 | */ | ||
78 | static enum GNUNET_GenericReturnValue | ||
79 | extract_varsize_blob (void *cls, | ||
80 | PGresult *result, | ||
81 | int row, | ||
82 | const char *fname, | ||
83 | size_t *dst_size, | ||
84 | void *dst) | ||
85 | { | ||
86 | size_t len; | ||
87 | const char *res; | ||
88 | void *idst; | ||
89 | int fnum; | ||
90 | |||
91 | (void) cls; | ||
92 | *dst_size = 0; | ||
93 | *((void **) dst) = NULL; | ||
94 | |||
95 | fnum = PQfnumber (result, | ||
96 | fname); | ||
97 | if (fnum < 0) | ||
98 | { | ||
99 | GNUNET_break (0); | ||
100 | return GNUNET_SYSERR; | ||
101 | } | ||
102 | if (PQgetisnull (result, | ||
103 | row, | ||
104 | fnum)) | ||
105 | return GNUNET_NO; | ||
106 | /* if a field is null, continue but | ||
107 | * remember that we now return a different result */ | ||
108 | len = PQgetlength (result, | ||
109 | row, | ||
110 | fnum); | ||
111 | res = PQgetvalue (result, | ||
112 | row, | ||
113 | fnum); | ||
114 | GNUNET_assert (NULL != res); | ||
115 | *dst_size = len; | ||
116 | idst = GNUNET_malloc (len); | ||
117 | *((void **) dst) = idst; | ||
118 | GNUNET_memcpy (idst, | ||
119 | res, | ||
120 | len); | ||
121 | return GNUNET_OK; | ||
122 | } | ||
123 | |||
124 | |||
125 | struct GNUNET_PQ_ResultSpec | ||
126 | GNUNET_PQ_result_spec_variable_size (const char *name, | ||
127 | void **dst, | ||
128 | size_t *sptr) | ||
129 | { | ||
130 | struct GNUNET_PQ_ResultSpec res = { | ||
131 | .conv = &extract_varsize_blob, | ||
132 | .cleaner = &clean_varsize_blob, | ||
133 | .dst = (void *) (dst), | ||
134 | .fname = name, | ||
135 | .result_size = sptr | ||
136 | }; | ||
137 | |||
138 | return res; | ||
139 | } | ||
140 | |||
141 | |||
142 | /** | ||
143 | * Extract data from a Postgres database @a result at row @a row. | ||
144 | * | ||
145 | * @param cls closure | ||
146 | * @param result where to extract data from | ||
147 | * @param int row to extract data from | ||
148 | * @param fname name (or prefix) of the fields to extract from | ||
149 | * @param[in] dst_size desired size, never NULL | ||
150 | * @param[out] dst where to store the result | ||
151 | * @return | ||
152 | * #GNUNET_YES if all results could be extracted | ||
153 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
154 | */ | ||
155 | static enum GNUNET_GenericReturnValue | ||
156 | extract_fixed_blob (void *cls, | ||
157 | PGresult *result, | ||
158 | int row, | ||
159 | const char *fname, | ||
160 | size_t *dst_size, | ||
161 | void *dst) | ||
162 | { | ||
163 | size_t len; | ||
164 | const char *res; | ||
165 | int fnum; | ||
166 | |||
167 | (void) cls; | ||
168 | fnum = PQfnumber (result, | ||
169 | fname); | ||
170 | if (fnum < 0) | ||
171 | { | ||
172 | GNUNET_break (0); | ||
173 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
174 | "Result does not have field %s\n", | ||
175 | fname); | ||
176 | return GNUNET_SYSERR; | ||
177 | } | ||
178 | if (PQgetisnull (result, | ||
179 | row, | ||
180 | fnum)) | ||
181 | return GNUNET_NO; | ||
182 | /* if a field is null, continue but | ||
183 | * remember that we now return a different result */ | ||
184 | len = PQgetlength (result, | ||
185 | row, | ||
186 | fnum); | ||
187 | if (*dst_size != len) | ||
188 | { | ||
189 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
190 | "Expected %u bytes for field `%s', got %u\n", | ||
191 | (unsigned int) *dst_size, | ||
192 | fname, | ||
193 | (unsigned int) len); | ||
194 | GNUNET_break (0); | ||
195 | return GNUNET_SYSERR; | ||
196 | } | ||
197 | res = PQgetvalue (result, | ||
198 | row, | ||
199 | fnum); | ||
200 | GNUNET_assert (NULL != res); | ||
201 | GNUNET_memcpy (dst, | ||
202 | res, | ||
203 | len); | ||
204 | return GNUNET_OK; | ||
205 | } | ||
206 | |||
207 | |||
208 | struct GNUNET_PQ_ResultSpec | ||
209 | GNUNET_PQ_result_spec_fixed_size (const char *name, | ||
210 | void *dst, | ||
211 | size_t dst_size) | ||
212 | { | ||
213 | struct GNUNET_PQ_ResultSpec res = { | ||
214 | .conv = &extract_fixed_blob, | ||
215 | .dst = (dst), | ||
216 | .dst_size = dst_size, | ||
217 | .fname = name | ||
218 | }; | ||
219 | |||
220 | return res; | ||
221 | } | ||
222 | |||
223 | |||
224 | /** | ||
225 | * Extract data from a Postgres database @a result at row @a row. | ||
226 | * | ||
227 | * @param cls closure | ||
228 | * @param result where to extract data from | ||
229 | * @param int row to extract data from | ||
230 | * @param fname name (or prefix) of the fields to extract from | ||
231 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
232 | * @param[out] dst where to store the result | ||
233 | * @return | ||
234 | * #GNUNET_YES if all results could be extracted | ||
235 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
236 | */ | ||
237 | static enum GNUNET_GenericReturnValue | ||
238 | extract_rsa_public_key (void *cls, | ||
239 | PGresult *result, | ||
240 | int row, | ||
241 | const char *fname, | ||
242 | size_t *dst_size, | ||
243 | void *dst) | ||
244 | { | ||
245 | struct GNUNET_CRYPTO_RsaPublicKey **pk = dst; | ||
246 | size_t len; | ||
247 | const char *res; | ||
248 | int fnum; | ||
249 | |||
250 | (void) cls; | ||
251 | *pk = NULL; | ||
252 | fnum = PQfnumber (result, | ||
253 | fname); | ||
254 | if (fnum < 0) | ||
255 | { | ||
256 | GNUNET_break (0); | ||
257 | return GNUNET_SYSERR; | ||
258 | } | ||
259 | if (PQgetisnull (result, | ||
260 | row, | ||
261 | fnum)) | ||
262 | return GNUNET_NO; | ||
263 | |||
264 | /* if a field is null, continue but | ||
265 | * remember that we now return a different result */ | ||
266 | len = PQgetlength (result, | ||
267 | row, | ||
268 | fnum); | ||
269 | res = PQgetvalue (result, | ||
270 | row, | ||
271 | fnum); | ||
272 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, | ||
273 | len); | ||
274 | if (NULL == *pk) | ||
275 | { | ||
276 | GNUNET_break (0); | ||
277 | return GNUNET_SYSERR; | ||
278 | } | ||
279 | return GNUNET_OK; | ||
280 | } | ||
281 | |||
282 | |||
283 | /** | ||
284 | * Function called to clean up memory allocated | ||
285 | * by a #GNUNET_PQ_ResultConverter. | ||
286 | * | ||
287 | * @param cls closure | ||
288 | * @param rd result data to clean up | ||
289 | */ | ||
290 | static void | ||
291 | clean_rsa_public_key (void *cls, | ||
292 | void *rd) | ||
293 | { | ||
294 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rd; | ||
295 | |||
296 | (void) cls; | ||
297 | if (NULL != *pk) | ||
298 | { | ||
299 | GNUNET_CRYPTO_rsa_public_key_free (*pk); | ||
300 | *pk = NULL; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | |||
305 | struct GNUNET_PQ_ResultSpec | ||
306 | GNUNET_PQ_result_spec_rsa_public_key (const char *name, | ||
307 | struct GNUNET_CRYPTO_RsaPublicKey **rsa) | ||
308 | { | ||
309 | struct GNUNET_PQ_ResultSpec res = { | ||
310 | .conv = &extract_rsa_public_key, | ||
311 | .cleaner = &clean_rsa_public_key, | ||
312 | .dst = (void *) rsa, | ||
313 | .fname = name | ||
314 | }; | ||
315 | |||
316 | return res; | ||
317 | } | ||
318 | |||
319 | |||
320 | /** | ||
321 | * Extract data from a Postgres database @a result at row @a row. | ||
322 | * | ||
323 | * @param cls closure | ||
324 | * @param result where to extract data from | ||
325 | * @param int row to extract data from | ||
326 | * @param fname name (or prefix) of the fields to extract from | ||
327 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
328 | * @param[out] dst where to store the result | ||
329 | * @return | ||
330 | * #GNUNET_YES if all results could be extracted | ||
331 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
332 | */ | ||
333 | static enum GNUNET_GenericReturnValue | ||
334 | extract_rsa_signature (void *cls, | ||
335 | PGresult *result, | ||
336 | int row, | ||
337 | const char *fname, | ||
338 | size_t *dst_size, | ||
339 | void *dst) | ||
340 | { | ||
341 | struct GNUNET_CRYPTO_RsaSignature **sig = dst; | ||
342 | size_t len; | ||
343 | const void *res; | ||
344 | int fnum; | ||
345 | |||
346 | (void) cls; | ||
347 | *sig = NULL; | ||
348 | fnum = PQfnumber (result, | ||
349 | fname); | ||
350 | if (fnum < 0) | ||
351 | { | ||
352 | GNUNET_break (0); | ||
353 | return GNUNET_SYSERR; | ||
354 | } | ||
355 | if (PQgetisnull (result, | ||
356 | row, | ||
357 | fnum)) | ||
358 | return GNUNET_NO; | ||
359 | /* if a field is null, continue but | ||
360 | * remember that we now return a different result */ | ||
361 | len = PQgetlength (result, | ||
362 | row, | ||
363 | fnum); | ||
364 | res = PQgetvalue (result, | ||
365 | row, | ||
366 | fnum); | ||
367 | *sig = GNUNET_CRYPTO_rsa_signature_decode (res, | ||
368 | len); | ||
369 | if (NULL == *sig) | ||
370 | { | ||
371 | GNUNET_break (0); | ||
372 | return GNUNET_SYSERR; | ||
373 | } | ||
374 | return GNUNET_OK; | ||
375 | } | ||
376 | |||
377 | |||
378 | /** | ||
379 | * Function called to clean up memory allocated | ||
380 | * by a #GNUNET_PQ_ResultConverter. | ||
381 | * | ||
382 | * @param cls closure | ||
383 | * @param rd result data to clean up | ||
384 | */ | ||
385 | static void | ||
386 | clean_rsa_signature (void *cls, | ||
387 | void *rd) | ||
388 | { | ||
389 | struct GNUNET_CRYPTO_RsaSignature **sig = rd; | ||
390 | |||
391 | (void) cls; | ||
392 | if (NULL != *sig) | ||
393 | { | ||
394 | GNUNET_CRYPTO_rsa_signature_free (*sig); | ||
395 | *sig = NULL; | ||
396 | } | ||
397 | } | ||
398 | |||
399 | |||
400 | struct GNUNET_PQ_ResultSpec | ||
401 | GNUNET_PQ_result_spec_rsa_signature (const char *name, | ||
402 | struct GNUNET_CRYPTO_RsaSignature **sig) | ||
403 | { | ||
404 | struct GNUNET_PQ_ResultSpec res = { | ||
405 | .conv = &extract_rsa_signature, | ||
406 | .cleaner = &clean_rsa_signature, | ||
407 | .dst = (void *) sig, | ||
408 | .fname = name | ||
409 | }; | ||
410 | |||
411 | return res; | ||
412 | } | ||
413 | |||
414 | |||
415 | /** | ||
416 | * Extract data from a Postgres database @a result at row @a row. | ||
417 | * | ||
418 | * @param cls closure | ||
419 | * @param result where to extract data from | ||
420 | * @param int row to extract data from | ||
421 | * @param fname name (or prefix) of the fields to extract from | ||
422 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
423 | * @param[out] dst where to store the result | ||
424 | * @return | ||
425 | * #GNUNET_YES if all results could be extracted | ||
426 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
427 | */ | ||
428 | static enum GNUNET_GenericReturnValue | ||
429 | extract_string (void *cls, | ||
430 | PGresult *result, | ||
431 | int row, | ||
432 | const char *fname, | ||
433 | size_t *dst_size, | ||
434 | void *dst) | ||
435 | { | ||
436 | char **str = dst; | ||
437 | size_t len; | ||
438 | const char *res; | ||
439 | int fnum; | ||
440 | |||
441 | (void) cls; | ||
442 | *str = NULL; | ||
443 | fnum = PQfnumber (result, | ||
444 | fname); | ||
445 | if (fnum < 0) | ||
446 | { | ||
447 | GNUNET_break (0); | ||
448 | return GNUNET_SYSERR; | ||
449 | } | ||
450 | if (PQgetisnull (result, | ||
451 | row, | ||
452 | fnum)) | ||
453 | return GNUNET_NO; | ||
454 | /* if a field is null, continue but | ||
455 | * remember that we now return a different result */ | ||
456 | len = PQgetlength (result, | ||
457 | row, | ||
458 | fnum); | ||
459 | res = PQgetvalue (result, | ||
460 | row, | ||
461 | fnum); | ||
462 | *str = GNUNET_strndup (res, | ||
463 | len); | ||
464 | if (NULL == *str) | ||
465 | { | ||
466 | GNUNET_break (0); | ||
467 | return GNUNET_SYSERR; | ||
468 | } | ||
469 | return GNUNET_OK; | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * Function called to clean up memory allocated | ||
475 | * by a #GNUNET_PQ_ResultConverter. | ||
476 | * | ||
477 | * @param cls closure | ||
478 | * @param rd result data to clean up | ||
479 | */ | ||
480 | static void | ||
481 | clean_string (void *cls, | ||
482 | void *rd) | ||
483 | { | ||
484 | char **str = rd; | ||
485 | |||
486 | (void) cls; | ||
487 | if (NULL != *str) | ||
488 | { | ||
489 | GNUNET_free (*str); | ||
490 | *str = NULL; | ||
491 | } | ||
492 | } | ||
493 | |||
494 | |||
495 | struct GNUNET_PQ_ResultSpec | ||
496 | GNUNET_PQ_result_spec_string (const char *name, | ||
497 | char **dst) | ||
498 | { | ||
499 | struct GNUNET_PQ_ResultSpec res = { | ||
500 | .conv = &extract_string, | ||
501 | .cleaner = &clean_string, | ||
502 | .dst = (void *) dst, | ||
503 | .fname = (name) | ||
504 | }; | ||
505 | |||
506 | return res; | ||
507 | } | ||
508 | |||
509 | |||
510 | /** | ||
511 | * Extract data from a Postgres database @a result at row @a row. | ||
512 | * | ||
513 | * @param cls closure | ||
514 | * @param result where to extract data from | ||
515 | * @param int row to extract data from | ||
516 | * @param fname name (or prefix) of the fields to extract from | ||
517 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
518 | * @param[out] dst where to store the result | ||
519 | * @return | ||
520 | * #GNUNET_YES if all results could be extracted | ||
521 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
522 | */ | ||
523 | static enum GNUNET_GenericReturnValue | ||
524 | extract_bool (void *cls, | ||
525 | PGresult *result, | ||
526 | int row, | ||
527 | const char *fname, | ||
528 | size_t *dst_size, | ||
529 | void *dst) | ||
530 | { | ||
531 | bool *b = dst; | ||
532 | const uint8_t *res; | ||
533 | int fnum; | ||
534 | size_t len; | ||
535 | |||
536 | (void) cls; | ||
537 | fnum = PQfnumber (result, | ||
538 | fname); | ||
539 | if (fnum < 0) | ||
540 | { | ||
541 | GNUNET_break (0); | ||
542 | return GNUNET_SYSERR; | ||
543 | } | ||
544 | if (PQgetisnull (result, | ||
545 | row, | ||
546 | fnum)) | ||
547 | return GNUNET_NO; | ||
548 | /* if a field is null, continue but | ||
549 | * remember that we now return a different result */ | ||
550 | len = PQgetlength (result, | ||
551 | row, | ||
552 | fnum); | ||
553 | if (sizeof (uint8_t) != len) | ||
554 | { | ||
555 | GNUNET_break (0); | ||
556 | return GNUNET_SYSERR; | ||
557 | } | ||
558 | res = (const uint8_t *) PQgetvalue (result, | ||
559 | row, | ||
560 | fnum); | ||
561 | *b = (0 != *res); | ||
562 | return GNUNET_OK; | ||
563 | } | ||
564 | |||
565 | |||
566 | struct GNUNET_PQ_ResultSpec | ||
567 | GNUNET_PQ_result_spec_bool (const char *name, | ||
568 | bool *dst) | ||
569 | { | ||
570 | struct GNUNET_PQ_ResultSpec res = { | ||
571 | .conv = &extract_bool, | ||
572 | .dst = (void *) dst, | ||
573 | .fname = name | ||
574 | }; | ||
575 | |||
576 | return res; | ||
577 | } | ||
578 | |||
579 | |||
580 | /** | ||
581 | * Extract data from a Postgres database @a result at row @a row. | ||
582 | * | ||
583 | * @param cls closure | ||
584 | * @param result where to extract data from | ||
585 | * @param int row to extract data from | ||
586 | * @param fname name (or prefix) of the fields to extract from | ||
587 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
588 | * @param[out] dst where to store the result | ||
589 | * @return | ||
590 | * #GNUNET_YES if all results could be extracted | ||
591 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
592 | */ | ||
593 | static enum GNUNET_GenericReturnValue | ||
594 | extract_rel_time (void *cls, | ||
595 | PGresult *result, | ||
596 | int row, | ||
597 | const char *fname, | ||
598 | size_t *dst_size, | ||
599 | void *dst) | ||
600 | { | ||
601 | struct GNUNET_TIME_Relative *udst = dst; | ||
602 | const int64_t *res; | ||
603 | int fnum; | ||
604 | |||
605 | (void) cls; | ||
606 | fnum = PQfnumber (result, | ||
607 | fname); | ||
608 | if (fnum < 0) | ||
609 | { | ||
610 | GNUNET_break (0); | ||
611 | return GNUNET_SYSERR; | ||
612 | } | ||
613 | if (PQgetisnull (result, | ||
614 | row, | ||
615 | fnum)) | ||
616 | return GNUNET_NO; | ||
617 | GNUNET_assert (NULL != dst); | ||
618 | if (sizeof(struct GNUNET_TIME_Relative) != *dst_size) | ||
619 | { | ||
620 | GNUNET_break (0); | ||
621 | return GNUNET_SYSERR; | ||
622 | } | ||
623 | if (sizeof(int64_t) != | ||
624 | PQgetlength (result, | ||
625 | row, | ||
626 | fnum)) | ||
627 | { | ||
628 | GNUNET_break (0); | ||
629 | return GNUNET_SYSERR; | ||
630 | } | ||
631 | res = (int64_t *) PQgetvalue (result, | ||
632 | row, | ||
633 | fnum); | ||
634 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
635 | *udst = GNUNET_TIME_UNIT_FOREVER_REL; | ||
636 | else | ||
637 | udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
638 | return GNUNET_OK; | ||
639 | } | ||
640 | |||
641 | |||
642 | struct GNUNET_PQ_ResultSpec | ||
643 | GNUNET_PQ_result_spec_relative_time (const char *name, | ||
644 | struct GNUNET_TIME_Relative *rt) | ||
645 | { | ||
646 | struct GNUNET_PQ_ResultSpec res = { | ||
647 | .conv = &extract_rel_time, | ||
648 | .dst = (void *) rt, | ||
649 | .dst_size = sizeof(*rt), | ||
650 | .fname = name | ||
651 | }; | ||
652 | |||
653 | return res; | ||
654 | } | ||
655 | |||
656 | |||
657 | /** | ||
658 | * Extract data from a Postgres database @a result at row @a row. | ||
659 | * | ||
660 | * @param cls closure | ||
661 | * @param result where to extract data from | ||
662 | * @param int row to extract data from | ||
663 | * @param fname name (or prefix) of the fields to extract from | ||
664 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
665 | * @param[out] dst where to store the result | ||
666 | * @return | ||
667 | * #GNUNET_YES if all results could be extracted | ||
668 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
669 | */ | ||
670 | static enum GNUNET_GenericReturnValue | ||
671 | extract_abs_time (void *cls, | ||
672 | PGresult *result, | ||
673 | int row, | ||
674 | const char *fname, | ||
675 | size_t *dst_size, | ||
676 | void *dst) | ||
677 | { | ||
678 | struct GNUNET_TIME_Absolute *udst = dst; | ||
679 | const int64_t *res; | ||
680 | int fnum; | ||
681 | |||
682 | (void) cls; | ||
683 | fnum = PQfnumber (result, | ||
684 | fname); | ||
685 | if (fnum < 0) | ||
686 | { | ||
687 | GNUNET_break (0); | ||
688 | return GNUNET_SYSERR; | ||
689 | } | ||
690 | if (PQgetisnull (result, | ||
691 | row, | ||
692 | fnum)) | ||
693 | return GNUNET_NO; | ||
694 | GNUNET_assert (NULL != dst); | ||
695 | if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size) | ||
696 | { | ||
697 | GNUNET_break (0); | ||
698 | return GNUNET_SYSERR; | ||
699 | } | ||
700 | if (sizeof(int64_t) != | ||
701 | PQgetlength (result, | ||
702 | row, | ||
703 | fnum)) | ||
704 | { | ||
705 | GNUNET_break (0); | ||
706 | return GNUNET_SYSERR; | ||
707 | } | ||
708 | res = (int64_t *) PQgetvalue (result, | ||
709 | row, | ||
710 | fnum); | ||
711 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
712 | *udst = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
713 | else | ||
714 | udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
715 | return GNUNET_OK; | ||
716 | } | ||
717 | |||
718 | |||
719 | struct GNUNET_PQ_ResultSpec | ||
720 | GNUNET_PQ_result_spec_absolute_time (const char *name, | ||
721 | struct GNUNET_TIME_Absolute *at) | ||
722 | { | ||
723 | struct GNUNET_PQ_ResultSpec res = { | ||
724 | .conv = &extract_abs_time, | ||
725 | .dst = (void *) at, | ||
726 | .dst_size = sizeof(*at), | ||
727 | .fname = name | ||
728 | }; | ||
729 | |||
730 | return res; | ||
731 | } | ||
732 | |||
733 | |||
734 | struct GNUNET_PQ_ResultSpec | ||
735 | GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, | ||
736 | struct GNUNET_TIME_AbsoluteNBO *at) | ||
737 | { | ||
738 | struct GNUNET_PQ_ResultSpec res = | ||
739 | GNUNET_PQ_result_spec_auto_from_type (name, | ||
740 | &at->abs_value_us__); | ||
741 | |||
742 | return res; | ||
743 | } | ||
744 | |||
745 | |||
746 | /** | ||
747 | * Extract data from a Postgres database @a result at row @a row. | ||
748 | * | ||
749 | * @param cls closure | ||
750 | * @param result where to extract data from | ||
751 | * @param int row to extract data from | ||
752 | * @param fname name (or prefix) of the fields to extract from | ||
753 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
754 | * @param[out] dst where to store the result | ||
755 | * @return | ||
756 | * #GNUNET_YES if all results could be extracted | ||
757 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
758 | */ | ||
759 | static enum GNUNET_GenericReturnValue | ||
760 | extract_timestamp (void *cls, | ||
761 | PGresult *result, | ||
762 | int row, | ||
763 | const char *fname, | ||
764 | size_t *dst_size, | ||
765 | void *dst) | ||
766 | { | ||
767 | struct GNUNET_TIME_Timestamp *udst = dst; | ||
768 | struct GNUNET_TIME_Absolute abs; | ||
769 | const int64_t *res; | ||
770 | int fnum; | ||
771 | |||
772 | (void) cls; | ||
773 | fnum = PQfnumber (result, | ||
774 | fname); | ||
775 | if (fnum < 0) | ||
776 | { | ||
777 | GNUNET_break (0); | ||
778 | return GNUNET_SYSERR; | ||
779 | } | ||
780 | if (PQgetisnull (result, | ||
781 | row, | ||
782 | fnum)) | ||
783 | return GNUNET_NO; | ||
784 | GNUNET_assert (NULL != dst); | ||
785 | if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size) | ||
786 | { | ||
787 | GNUNET_break (0); | ||
788 | return GNUNET_SYSERR; | ||
789 | } | ||
790 | if (sizeof(int64_t) != | ||
791 | PQgetlength (result, | ||
792 | row, | ||
793 | fnum)) | ||
794 | { | ||
795 | GNUNET_break (0); | ||
796 | return GNUNET_SYSERR; | ||
797 | } | ||
798 | res = (int64_t *) PQgetvalue (result, | ||
799 | row, | ||
800 | fnum); | ||
801 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
802 | { | ||
803 | abs = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
804 | } | ||
805 | else | ||
806 | { | ||
807 | abs.abs_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
808 | if (0 != abs.abs_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us) | ||
809 | { | ||
810 | /* timestamps must be multiple of seconds! */ | ||
811 | GNUNET_break (0); | ||
812 | return GNUNET_SYSERR; | ||
813 | } | ||
814 | } | ||
815 | udst->abs_time = abs; | ||
816 | return GNUNET_OK; | ||
817 | } | ||
818 | |||
819 | |||
820 | struct GNUNET_PQ_ResultSpec | ||
821 | GNUNET_PQ_result_spec_timestamp (const char *name, | ||
822 | struct GNUNET_TIME_Timestamp *at) | ||
823 | { | ||
824 | struct GNUNET_PQ_ResultSpec res = { | ||
825 | .conv = &extract_timestamp, | ||
826 | .dst = (void *) at, | ||
827 | .dst_size = sizeof(*at), | ||
828 | .fname = name | ||
829 | }; | ||
830 | |||
831 | return res; | ||
832 | } | ||
833 | |||
834 | |||
835 | /** | ||
836 | * Extract data from a Postgres database @a result at row @a row. | ||
837 | * | ||
838 | * @param cls closure | ||
839 | * @param result where to extract data from | ||
840 | * @param int row to extract data from | ||
841 | * @param fname name (or prefix) of the fields to extract from | ||
842 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
843 | * @param[out] dst where to store the result | ||
844 | * @return | ||
845 | * #GNUNET_YES if all results could be extracted | ||
846 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
847 | */ | ||
848 | static enum GNUNET_GenericReturnValue | ||
849 | extract_timestamp_nbo (void *cls, | ||
850 | PGresult *result, | ||
851 | int row, | ||
852 | const char *fname, | ||
853 | size_t *dst_size, | ||
854 | void *dst) | ||
855 | { | ||
856 | struct GNUNET_TIME_TimestampNBO *udst = dst; | ||
857 | struct GNUNET_TIME_Timestamp t; | ||
858 | enum GNUNET_GenericReturnValue r; | ||
859 | |||
860 | r = extract_timestamp (NULL, | ||
861 | result, | ||
862 | row, | ||
863 | fname, | ||
864 | dst_size, | ||
865 | &t); | ||
866 | if (GNUNET_OK != r) | ||
867 | return r; | ||
868 | *udst = GNUNET_TIME_timestamp_hton (t); | ||
869 | return r; | ||
870 | } | ||
871 | |||
872 | |||
873 | struct GNUNET_PQ_ResultSpec | ||
874 | GNUNET_PQ_result_spec_timestamp_nbo (const char *name, | ||
875 | struct GNUNET_TIME_TimestampNBO *at) | ||
876 | { | ||
877 | struct GNUNET_PQ_ResultSpec res = { | ||
878 | .conv = &extract_timestamp_nbo, | ||
879 | .dst = (void *) at, | ||
880 | .dst_size = sizeof(*at), | ||
881 | .fname = name | ||
882 | }; | ||
883 | |||
884 | return res; | ||
885 | } | ||
886 | |||
887 | |||
888 | /** | ||
889 | * Extract data from a Postgres database @a result at row @a row. | ||
890 | * | ||
891 | * @param cls closure | ||
892 | * @param result where to extract data from | ||
893 | * @param int row to extract data from | ||
894 | * @param fname name (or prefix) of the fields to extract from | ||
895 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
896 | * @param[out] dst where to store the result | ||
897 | * @return | ||
898 | * #GNUNET_YES if all results could be extracted | ||
899 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
900 | */ | ||
901 | static enum GNUNET_GenericReturnValue | ||
902 | extract_uint16 (void *cls, | ||
903 | PGresult *result, | ||
904 | int row, | ||
905 | const char *fname, | ||
906 | size_t *dst_size, | ||
907 | void *dst) | ||
908 | { | ||
909 | uint16_t *udst = dst; | ||
910 | const uint16_t *res; | ||
911 | int fnum; | ||
912 | |||
913 | (void) cls; | ||
914 | fnum = PQfnumber (result, | ||
915 | fname); | ||
916 | if (fnum < 0) | ||
917 | { | ||
918 | GNUNET_break (0); | ||
919 | return GNUNET_SYSERR; | ||
920 | } | ||
921 | if (PQgetisnull (result, | ||
922 | row, | ||
923 | fnum)) | ||
924 | return GNUNET_NO; | ||
925 | GNUNET_assert (NULL != dst); | ||
926 | if (sizeof(uint16_t) != *dst_size) | ||
927 | { | ||
928 | GNUNET_break (0); | ||
929 | return GNUNET_SYSERR; | ||
930 | } | ||
931 | if (sizeof(uint16_t) != | ||
932 | PQgetlength (result, | ||
933 | row, | ||
934 | fnum)) | ||
935 | { | ||
936 | GNUNET_break (0); | ||
937 | return GNUNET_SYSERR; | ||
938 | } | ||
939 | res = (uint16_t *) PQgetvalue (result, | ||
940 | row, | ||
941 | fnum); | ||
942 | *udst = ntohs (*res); | ||
943 | return GNUNET_OK; | ||
944 | } | ||
945 | |||
946 | |||
947 | struct GNUNET_PQ_ResultSpec | ||
948 | GNUNET_PQ_result_spec_uint16 (const char *name, | ||
949 | uint16_t *u16) | ||
950 | { | ||
951 | struct GNUNET_PQ_ResultSpec res = { | ||
952 | .conv = &extract_uint16, | ||
953 | .dst = (void *) u16, | ||
954 | .dst_size = sizeof(*u16), | ||
955 | .fname = name | ||
956 | }; | ||
957 | |||
958 | return res; | ||
959 | } | ||
960 | |||
961 | |||
962 | /** | ||
963 | * Extract data from a Postgres database @a result at row @a row. | ||
964 | * | ||
965 | * @param cls closure | ||
966 | * @param result where to extract data from | ||
967 | * @param int row to extract data from | ||
968 | * @param fname name (or prefix) of the fields to extract from | ||
969 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
970 | * @param[out] dst where to store the result | ||
971 | * @return | ||
972 | * #GNUNET_YES if all results could be extracted | ||
973 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
974 | */ | ||
975 | static enum GNUNET_GenericReturnValue | ||
976 | extract_uint32 (void *cls, | ||
977 | PGresult *result, | ||
978 | int row, | ||
979 | const char *fname, | ||
980 | size_t *dst_size, | ||
981 | void *dst) | ||
982 | { | ||
983 | uint32_t *udst = dst; | ||
984 | const uint32_t *res; | ||
985 | int fnum; | ||
986 | |||
987 | (void) cls; | ||
988 | fnum = PQfnumber (result, | ||
989 | fname); | ||
990 | if (fnum < 0) | ||
991 | { | ||
992 | GNUNET_break (0); | ||
993 | return GNUNET_SYSERR; | ||
994 | } | ||
995 | if (PQgetisnull (result, | ||
996 | row, | ||
997 | fnum)) | ||
998 | return GNUNET_NO; | ||
999 | GNUNET_assert (NULL != dst); | ||
1000 | if (sizeof(uint32_t) != *dst_size) | ||
1001 | { | ||
1002 | GNUNET_break (0); | ||
1003 | return GNUNET_SYSERR; | ||
1004 | } | ||
1005 | if (sizeof(uint32_t) != | ||
1006 | PQgetlength (result, | ||
1007 | row, | ||
1008 | fnum)) | ||
1009 | { | ||
1010 | GNUNET_break (0); | ||
1011 | return GNUNET_SYSERR; | ||
1012 | } | ||
1013 | res = (uint32_t *) PQgetvalue (result, | ||
1014 | row, | ||
1015 | fnum); | ||
1016 | *udst = ntohl (*res); | ||
1017 | return GNUNET_OK; | ||
1018 | } | ||
1019 | |||
1020 | |||
1021 | struct GNUNET_PQ_ResultSpec | ||
1022 | GNUNET_PQ_result_spec_uint32 (const char *name, | ||
1023 | uint32_t *u32) | ||
1024 | { | ||
1025 | struct GNUNET_PQ_ResultSpec res = { | ||
1026 | .conv = &extract_uint32, | ||
1027 | .dst = (void *) u32, | ||
1028 | .dst_size = sizeof(*u32), | ||
1029 | .fname = name | ||
1030 | }; | ||
1031 | |||
1032 | return res; | ||
1033 | } | ||
1034 | |||
1035 | |||
1036 | /** | ||
1037 | * Extract data from a Postgres database @a result at row @a row. | ||
1038 | * | ||
1039 | * @param cls closure | ||
1040 | * @param result where to extract data from | ||
1041 | * @param int row to extract data from | ||
1042 | * @param fname name (or prefix) of the fields to extract from | ||
1043 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
1044 | * @param[out] dst where to store the result | ||
1045 | * @return | ||
1046 | * #GNUNET_YES if all results could be extracted | ||
1047 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
1048 | */ | ||
1049 | static enum GNUNET_GenericReturnValue | ||
1050 | extract_uint64 (void *cls, | ||
1051 | PGresult *result, | ||
1052 | int row, | ||
1053 | const char *fname, | ||
1054 | size_t *dst_size, | ||
1055 | void *dst) | ||
1056 | { | ||
1057 | uint64_t *udst = dst; | ||
1058 | const uint64_t *res; | ||
1059 | int fnum; | ||
1060 | |||
1061 | (void) cls; | ||
1062 | fnum = PQfnumber (result, | ||
1063 | fname); | ||
1064 | if (fnum < 0) | ||
1065 | { | ||
1066 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1067 | "Field %s missing in result\n", | ||
1068 | fname); | ||
1069 | GNUNET_break (0); | ||
1070 | return GNUNET_SYSERR; | ||
1071 | } | ||
1072 | if (PQgetisnull (result, | ||
1073 | row, | ||
1074 | fnum)) | ||
1075 | return GNUNET_NO; | ||
1076 | |||
1077 | GNUNET_assert (NULL != dst); | ||
1078 | if (sizeof(uint64_t) != *dst_size) | ||
1079 | { | ||
1080 | GNUNET_break (0); | ||
1081 | return GNUNET_SYSERR; | ||
1082 | } | ||
1083 | if (sizeof(uint64_t) != | ||
1084 | PQgetlength (result, | ||
1085 | row, | ||
1086 | fnum)) | ||
1087 | { | ||
1088 | GNUNET_break (0); | ||
1089 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1090 | "Got length %u for field `%s'\n", | ||
1091 | PQgetlength (result, | ||
1092 | row, | ||
1093 | fnum), | ||
1094 | fname); | ||
1095 | return GNUNET_SYSERR; | ||
1096 | } | ||
1097 | res = (uint64_t *) PQgetvalue (result, | ||
1098 | row, | ||
1099 | fnum); | ||
1100 | *udst = GNUNET_ntohll (*res); | ||
1101 | return GNUNET_OK; | ||
1102 | } | ||
1103 | |||
1104 | |||
1105 | struct GNUNET_PQ_ResultSpec | ||
1106 | GNUNET_PQ_result_spec_uint64 (const char *name, | ||
1107 | uint64_t *u64) | ||
1108 | { | ||
1109 | struct GNUNET_PQ_ResultSpec res = { | ||
1110 | .conv = &extract_uint64, | ||
1111 | .dst = (void *) u64, | ||
1112 | .dst_size = sizeof(*u64), | ||
1113 | .fname = name | ||
1114 | }; | ||
1115 | |||
1116 | return res; | ||
1117 | } | ||
1118 | |||
1119 | |||
1120 | /* end of pq_result_helper.c */ | ||