diff options
Diffstat (limited to 'src/pq/pq_result_helper.c')
-rw-r--r-- | src/pq/pq_result_helper.c | 907 |
1 files changed, 0 insertions, 907 deletions
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c deleted file mode 100644 index f264603f4..000000000 --- a/src/pq/pq_result_helper.c +++ /dev/null | |||
@@ -1,907 +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 | { &extract_string, | ||
501 | &clean_string, | ||
502 | NULL, | ||
503 | (void *) dst, 0, (name), NULL }; | ||
504 | |||
505 | return res; | ||
506 | } | ||
507 | |||
508 | |||
509 | /** | ||
510 | * Extract data from a Postgres database @a result at row @a row. | ||
511 | * | ||
512 | * @param cls closure | ||
513 | * @param result where to extract data from | ||
514 | * @param int row to extract data from | ||
515 | * @param fname name (or prefix) of the fields to extract from | ||
516 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
517 | * @param[out] dst where to store the result | ||
518 | * @return | ||
519 | * #GNUNET_YES if all results could be extracted | ||
520 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
521 | */ | ||
522 | static enum GNUNET_GenericReturnValue | ||
523 | extract_rel_time (void *cls, | ||
524 | PGresult *result, | ||
525 | int row, | ||
526 | const char *fname, | ||
527 | size_t *dst_size, | ||
528 | void *dst) | ||
529 | { | ||
530 | struct GNUNET_TIME_Relative *udst = dst; | ||
531 | const int64_t *res; | ||
532 | int fnum; | ||
533 | |||
534 | (void) cls; | ||
535 | fnum = PQfnumber (result, | ||
536 | fname); | ||
537 | if (fnum < 0) | ||
538 | { | ||
539 | GNUNET_break (0); | ||
540 | return GNUNET_SYSERR; | ||
541 | } | ||
542 | if (PQgetisnull (result, | ||
543 | row, | ||
544 | fnum)) | ||
545 | return GNUNET_NO; | ||
546 | GNUNET_assert (NULL != dst); | ||
547 | if (sizeof(struct GNUNET_TIME_Relative) != *dst_size) | ||
548 | { | ||
549 | GNUNET_break (0); | ||
550 | return GNUNET_SYSERR; | ||
551 | } | ||
552 | if (sizeof(int64_t) != | ||
553 | PQgetlength (result, | ||
554 | row, | ||
555 | fnum)) | ||
556 | { | ||
557 | GNUNET_break (0); | ||
558 | return GNUNET_SYSERR; | ||
559 | } | ||
560 | res = (int64_t *) PQgetvalue (result, | ||
561 | row, | ||
562 | fnum); | ||
563 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
564 | *udst = GNUNET_TIME_UNIT_FOREVER_REL; | ||
565 | else | ||
566 | udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
567 | return GNUNET_OK; | ||
568 | } | ||
569 | |||
570 | |||
571 | struct GNUNET_PQ_ResultSpec | ||
572 | GNUNET_PQ_result_spec_relative_time (const char *name, | ||
573 | struct GNUNET_TIME_Relative *rt) | ||
574 | { | ||
575 | struct GNUNET_PQ_ResultSpec res = { | ||
576 | &extract_rel_time, | ||
577 | NULL, | ||
578 | NULL, | ||
579 | (void *) rt, | ||
580 | sizeof(*rt), | ||
581 | name, | ||
582 | NULL | ||
583 | }; | ||
584 | |||
585 | return res; | ||
586 | } | ||
587 | |||
588 | |||
589 | /** | ||
590 | * Extract data from a Postgres database @a result at row @a row. | ||
591 | * | ||
592 | * @param cls closure | ||
593 | * @param result where to extract data from | ||
594 | * @param int row to extract data from | ||
595 | * @param fname name (or prefix) of the fields to extract from | ||
596 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
597 | * @param[out] dst where to store the result | ||
598 | * @return | ||
599 | * #GNUNET_YES if all results could be extracted | ||
600 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
601 | */ | ||
602 | static enum GNUNET_GenericReturnValue | ||
603 | extract_abs_time (void *cls, | ||
604 | PGresult *result, | ||
605 | int row, | ||
606 | const char *fname, | ||
607 | size_t *dst_size, | ||
608 | void *dst) | ||
609 | { | ||
610 | struct GNUNET_TIME_Absolute *udst = dst; | ||
611 | const int64_t *res; | ||
612 | int fnum; | ||
613 | |||
614 | (void) cls; | ||
615 | fnum = PQfnumber (result, | ||
616 | fname); | ||
617 | if (fnum < 0) | ||
618 | { | ||
619 | GNUNET_break (0); | ||
620 | return GNUNET_SYSERR; | ||
621 | } | ||
622 | if (PQgetisnull (result, | ||
623 | row, | ||
624 | fnum)) | ||
625 | return GNUNET_NO; | ||
626 | GNUNET_assert (NULL != dst); | ||
627 | if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size) | ||
628 | { | ||
629 | GNUNET_break (0); | ||
630 | return GNUNET_SYSERR; | ||
631 | } | ||
632 | if (sizeof(int64_t) != | ||
633 | PQgetlength (result, | ||
634 | row, | ||
635 | fnum)) | ||
636 | { | ||
637 | GNUNET_break (0); | ||
638 | return GNUNET_SYSERR; | ||
639 | } | ||
640 | res = (int64_t *) PQgetvalue (result, | ||
641 | row, | ||
642 | fnum); | ||
643 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
644 | *udst = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
645 | else | ||
646 | udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
647 | return GNUNET_OK; | ||
648 | } | ||
649 | |||
650 | |||
651 | struct GNUNET_PQ_ResultSpec | ||
652 | GNUNET_PQ_result_spec_absolute_time (const char *name, | ||
653 | struct GNUNET_TIME_Absolute *at) | ||
654 | { | ||
655 | struct GNUNET_PQ_ResultSpec res = | ||
656 | { &extract_abs_time, | ||
657 | NULL, | ||
658 | NULL, | ||
659 | (void *) at, sizeof(*at), (name), NULL }; | ||
660 | |||
661 | return res; | ||
662 | } | ||
663 | |||
664 | |||
665 | struct GNUNET_PQ_ResultSpec | ||
666 | GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, | ||
667 | struct GNUNET_TIME_AbsoluteNBO *at) | ||
668 | { | ||
669 | struct GNUNET_PQ_ResultSpec res = | ||
670 | GNUNET_PQ_result_spec_auto_from_type (name, &at->abs_value_us__); | ||
671 | |||
672 | return res; | ||
673 | } | ||
674 | |||
675 | |||
676 | /** | ||
677 | * Extract data from a Postgres database @a result at row @a row. | ||
678 | * | ||
679 | * @param cls closure | ||
680 | * @param result where to extract data from | ||
681 | * @param int row to extract data from | ||
682 | * @param fname name (or prefix) of the fields to extract from | ||
683 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
684 | * @param[out] dst where to store the result | ||
685 | * @return | ||
686 | * #GNUNET_YES if all results could be extracted | ||
687 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
688 | */ | ||
689 | static enum GNUNET_GenericReturnValue | ||
690 | extract_uint16 (void *cls, | ||
691 | PGresult *result, | ||
692 | int row, | ||
693 | const char *fname, | ||
694 | size_t *dst_size, | ||
695 | void *dst) | ||
696 | { | ||
697 | uint16_t *udst = dst; | ||
698 | const uint16_t *res; | ||
699 | int fnum; | ||
700 | |||
701 | (void) cls; | ||
702 | fnum = PQfnumber (result, | ||
703 | fname); | ||
704 | if (fnum < 0) | ||
705 | { | ||
706 | GNUNET_break (0); | ||
707 | return GNUNET_SYSERR; | ||
708 | } | ||
709 | if (PQgetisnull (result, | ||
710 | row, | ||
711 | fnum)) | ||
712 | return GNUNET_NO; | ||
713 | GNUNET_assert (NULL != dst); | ||
714 | if (sizeof(uint16_t) != *dst_size) | ||
715 | { | ||
716 | GNUNET_break (0); | ||
717 | return GNUNET_SYSERR; | ||
718 | } | ||
719 | if (sizeof(uint16_t) != | ||
720 | PQgetlength (result, | ||
721 | row, | ||
722 | fnum)) | ||
723 | { | ||
724 | GNUNET_break (0); | ||
725 | return GNUNET_SYSERR; | ||
726 | } | ||
727 | res = (uint16_t *) PQgetvalue (result, | ||
728 | row, | ||
729 | fnum); | ||
730 | *udst = ntohs (*res); | ||
731 | return GNUNET_OK; | ||
732 | } | ||
733 | |||
734 | |||
735 | struct GNUNET_PQ_ResultSpec | ||
736 | GNUNET_PQ_result_spec_uint16 (const char *name, | ||
737 | uint16_t *u16) | ||
738 | { | ||
739 | struct GNUNET_PQ_ResultSpec res = | ||
740 | { &extract_uint16, | ||
741 | NULL, | ||
742 | NULL, | ||
743 | (void *) u16, sizeof(*u16), (name), NULL }; | ||
744 | |||
745 | return res; | ||
746 | } | ||
747 | |||
748 | |||
749 | /** | ||
750 | * Extract data from a Postgres database @a result at row @a row. | ||
751 | * | ||
752 | * @param cls closure | ||
753 | * @param result where to extract data from | ||
754 | * @param int row to extract data from | ||
755 | * @param fname name (or prefix) of the fields to extract from | ||
756 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
757 | * @param[out] dst where to store the result | ||
758 | * @return | ||
759 | * #GNUNET_YES if all results could be extracted | ||
760 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
761 | */ | ||
762 | static enum GNUNET_GenericReturnValue | ||
763 | extract_uint32 (void *cls, | ||
764 | PGresult *result, | ||
765 | int row, | ||
766 | const char *fname, | ||
767 | size_t *dst_size, | ||
768 | void *dst) | ||
769 | { | ||
770 | uint32_t *udst = dst; | ||
771 | const uint32_t *res; | ||
772 | int fnum; | ||
773 | |||
774 | (void) cls; | ||
775 | fnum = PQfnumber (result, | ||
776 | fname); | ||
777 | if (fnum < 0) | ||
778 | { | ||
779 | GNUNET_break (0); | ||
780 | return GNUNET_SYSERR; | ||
781 | } | ||
782 | if (PQgetisnull (result, | ||
783 | row, | ||
784 | fnum)) | ||
785 | return GNUNET_NO; | ||
786 | GNUNET_assert (NULL != dst); | ||
787 | if (sizeof(uint32_t) != *dst_size) | ||
788 | { | ||
789 | GNUNET_break (0); | ||
790 | return GNUNET_SYSERR; | ||
791 | } | ||
792 | if (sizeof(uint32_t) != | ||
793 | PQgetlength (result, | ||
794 | row, | ||
795 | fnum)) | ||
796 | { | ||
797 | GNUNET_break (0); | ||
798 | return GNUNET_SYSERR; | ||
799 | } | ||
800 | res = (uint32_t *) PQgetvalue (result, | ||
801 | row, | ||
802 | fnum); | ||
803 | *udst = ntohl (*res); | ||
804 | return GNUNET_OK; | ||
805 | } | ||
806 | |||
807 | |||
808 | struct GNUNET_PQ_ResultSpec | ||
809 | GNUNET_PQ_result_spec_uint32 (const char *name, | ||
810 | uint32_t *u32) | ||
811 | { | ||
812 | struct GNUNET_PQ_ResultSpec res = { | ||
813 | &extract_uint32, | ||
814 | NULL, | ||
815 | NULL, | ||
816 | (void *) u32, | ||
817 | sizeof(*u32), | ||
818 | (name), | ||
819 | NULL | ||
820 | }; | ||
821 | |||
822 | return res; | ||
823 | } | ||
824 | |||
825 | |||
826 | /** | ||
827 | * Extract data from a Postgres database @a result at row @a row. | ||
828 | * | ||
829 | * @param cls closure | ||
830 | * @param result where to extract data from | ||
831 | * @param int row to extract data from | ||
832 | * @param fname name (or prefix) of the fields to extract from | ||
833 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
834 | * @param[out] dst where to store the result | ||
835 | * @return | ||
836 | * #GNUNET_YES if all results could be extracted | ||
837 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
838 | */ | ||
839 | static enum GNUNET_GenericReturnValue | ||
840 | extract_uint64 (void *cls, | ||
841 | PGresult *result, | ||
842 | int row, | ||
843 | const char *fname, | ||
844 | size_t *dst_size, | ||
845 | void *dst) | ||
846 | { | ||
847 | uint64_t *udst = dst; | ||
848 | const uint64_t *res; | ||
849 | int fnum; | ||
850 | |||
851 | (void) cls; | ||
852 | fnum = PQfnumber (result, | ||
853 | fname); | ||
854 | if (fnum < 0) | ||
855 | { | ||
856 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
857 | "Field %s missing in result\n", | ||
858 | fname); | ||
859 | GNUNET_break (0); | ||
860 | return GNUNET_SYSERR; | ||
861 | } | ||
862 | if (PQgetisnull (result, | ||
863 | row, | ||
864 | fnum)) | ||
865 | return GNUNET_NO; | ||
866 | |||
867 | GNUNET_assert (NULL != dst); | ||
868 | if (sizeof(uint64_t) != *dst_size) | ||
869 | { | ||
870 | GNUNET_break (0); | ||
871 | return GNUNET_SYSERR; | ||
872 | } | ||
873 | if (sizeof(uint64_t) != | ||
874 | PQgetlength (result, | ||
875 | row, | ||
876 | fnum)) | ||
877 | { | ||
878 | GNUNET_break (0); | ||
879 | return GNUNET_SYSERR; | ||
880 | } | ||
881 | res = (uint64_t *) PQgetvalue (result, | ||
882 | row, | ||
883 | fnum); | ||
884 | *udst = GNUNET_ntohll (*res); | ||
885 | return GNUNET_OK; | ||
886 | } | ||
887 | |||
888 | |||
889 | struct GNUNET_PQ_ResultSpec | ||
890 | GNUNET_PQ_result_spec_uint64 (const char *name, | ||
891 | uint64_t *u64) | ||
892 | { | ||
893 | struct GNUNET_PQ_ResultSpec res = { | ||
894 | &extract_uint64, | ||
895 | NULL, | ||
896 | NULL, | ||
897 | (void *) u64, | ||
898 | sizeof(*u64), | ||
899 | (name), | ||
900 | NULL | ||
901 | }; | ||
902 | |||
903 | return res; | ||
904 | } | ||
905 | |||
906 | |||
907 | /* end of pq_result_helper.c */ | ||