diff options
Diffstat (limited to 'src/pq/pq_result_helper.c')
-rw-r--r-- | src/pq/pq_result_helper.c | 900 |
1 files changed, 0 insertions, 900 deletions
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c deleted file mode 100644 index 23fb4f96e..000000000 --- a/src/pq/pq_result_helper.c +++ /dev/null | |||
@@ -1,900 +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 | { &extract_varsize_blob, | ||
132 | &clean_varsize_blob, NULL, | ||
133 | (void *) (dst), 0, name, sptr }; | ||
134 | |||
135 | return res; | ||
136 | } | ||
137 | |||
138 | |||
139 | /** | ||
140 | * Extract data from a Postgres database @a result at row @a row. | ||
141 | * | ||
142 | * @param cls closure | ||
143 | * @param result where to extract data from | ||
144 | * @param int row to extract data from | ||
145 | * @param fname name (or prefix) of the fields to extract from | ||
146 | * @param[in] dst_size desired size, never NULL | ||
147 | * @param[out] dst where to store the result | ||
148 | * @return | ||
149 | * #GNUNET_YES if all results could be extracted | ||
150 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
151 | */ | ||
152 | static enum GNUNET_GenericReturnValue | ||
153 | extract_fixed_blob (void *cls, | ||
154 | PGresult *result, | ||
155 | int row, | ||
156 | const char *fname, | ||
157 | size_t *dst_size, | ||
158 | void *dst) | ||
159 | { | ||
160 | size_t len; | ||
161 | const char *res; | ||
162 | int fnum; | ||
163 | |||
164 | (void) cls; | ||
165 | fnum = PQfnumber (result, | ||
166 | fname); | ||
167 | if (fnum < 0) | ||
168 | { | ||
169 | GNUNET_break (0); | ||
170 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
171 | "Result does not have field %s\n", | ||
172 | fname); | ||
173 | return GNUNET_SYSERR; | ||
174 | } | ||
175 | if (PQgetisnull (result, | ||
176 | row, | ||
177 | fnum)) | ||
178 | return GNUNET_NO; | ||
179 | /* if a field is null, continue but | ||
180 | * remember that we now return a different result */ | ||
181 | len = PQgetlength (result, | ||
182 | row, | ||
183 | fnum); | ||
184 | if (*dst_size != len) | ||
185 | { | ||
186 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
187 | "Expected %u bytes for field `%s', got %u\n", | ||
188 | (unsigned int) *dst_size, | ||
189 | fname, | ||
190 | (unsigned int) len); | ||
191 | GNUNET_break (0); | ||
192 | return GNUNET_SYSERR; | ||
193 | } | ||
194 | res = PQgetvalue (result, | ||
195 | row, | ||
196 | fnum); | ||
197 | GNUNET_assert (NULL != res); | ||
198 | GNUNET_memcpy (dst, | ||
199 | res, | ||
200 | len); | ||
201 | return GNUNET_OK; | ||
202 | } | ||
203 | |||
204 | |||
205 | struct GNUNET_PQ_ResultSpec | ||
206 | GNUNET_PQ_result_spec_fixed_size (const char *name, | ||
207 | void *dst, | ||
208 | size_t dst_size) | ||
209 | { | ||
210 | struct GNUNET_PQ_ResultSpec res = | ||
211 | { &extract_fixed_blob, | ||
212 | NULL, NULL, | ||
213 | (dst), dst_size, name, NULL }; | ||
214 | |||
215 | return res; | ||
216 | } | ||
217 | |||
218 | |||
219 | /** | ||
220 | * Extract data from a Postgres database @a result at row @a row. | ||
221 | * | ||
222 | * @param cls closure | ||
223 | * @param result where to extract data from | ||
224 | * @param int row to extract data from | ||
225 | * @param fname name (or prefix) of the fields to extract from | ||
226 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
227 | * @param[out] dst where to store the result | ||
228 | * @return | ||
229 | * #GNUNET_YES if all results could be extracted | ||
230 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
231 | */ | ||
232 | static enum GNUNET_GenericReturnValue | ||
233 | extract_rsa_public_key (void *cls, | ||
234 | PGresult *result, | ||
235 | int row, | ||
236 | const char *fname, | ||
237 | size_t *dst_size, | ||
238 | void *dst) | ||
239 | { | ||
240 | struct GNUNET_CRYPTO_RsaPublicKey **pk = dst; | ||
241 | size_t len; | ||
242 | const char *res; | ||
243 | int fnum; | ||
244 | |||
245 | (void) cls; | ||
246 | *pk = NULL; | ||
247 | fnum = PQfnumber (result, | ||
248 | fname); | ||
249 | if (fnum < 0) | ||
250 | { | ||
251 | GNUNET_break (0); | ||
252 | return GNUNET_SYSERR; | ||
253 | } | ||
254 | if (PQgetisnull (result, | ||
255 | row, | ||
256 | fnum)) | ||
257 | return GNUNET_NO; | ||
258 | |||
259 | /* if a field is null, continue but | ||
260 | * remember that we now return a different result */ | ||
261 | len = PQgetlength (result, | ||
262 | row, | ||
263 | fnum); | ||
264 | res = PQgetvalue (result, | ||
265 | row, | ||
266 | fnum); | ||
267 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, | ||
268 | len); | ||
269 | if (NULL == *pk) | ||
270 | { | ||
271 | GNUNET_break (0); | ||
272 | return GNUNET_SYSERR; | ||
273 | } | ||
274 | return GNUNET_OK; | ||
275 | } | ||
276 | |||
277 | |||
278 | /** | ||
279 | * Function called to clean up memory allocated | ||
280 | * by a #GNUNET_PQ_ResultConverter. | ||
281 | * | ||
282 | * @param cls closure | ||
283 | * @param rd result data to clean up | ||
284 | */ | ||
285 | static void | ||
286 | clean_rsa_public_key (void *cls, | ||
287 | void *rd) | ||
288 | { | ||
289 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rd; | ||
290 | |||
291 | (void) cls; | ||
292 | if (NULL != *pk) | ||
293 | { | ||
294 | GNUNET_CRYPTO_rsa_public_key_free (*pk); | ||
295 | *pk = NULL; | ||
296 | } | ||
297 | } | ||
298 | |||
299 | |||
300 | struct GNUNET_PQ_ResultSpec | ||
301 | GNUNET_PQ_result_spec_rsa_public_key (const char *name, | ||
302 | struct GNUNET_CRYPTO_RsaPublicKey **rsa) | ||
303 | { | ||
304 | struct GNUNET_PQ_ResultSpec res = | ||
305 | { &extract_rsa_public_key, | ||
306 | &clean_rsa_public_key, | ||
307 | NULL, | ||
308 | (void *) rsa, 0, name, NULL }; | ||
309 | |||
310 | return res; | ||
311 | } | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Extract data from a Postgres database @a result at row @a row. | ||
316 | * | ||
317 | * @param cls closure | ||
318 | * @param result where to extract data from | ||
319 | * @param int row to extract data from | ||
320 | * @param fname name (or prefix) of the fields to extract from | ||
321 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
322 | * @param[out] dst where to store the result | ||
323 | * @return | ||
324 | * #GNUNET_YES if all results could be extracted | ||
325 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
326 | */ | ||
327 | static enum GNUNET_GenericReturnValue | ||
328 | extract_rsa_signature (void *cls, | ||
329 | PGresult *result, | ||
330 | int row, | ||
331 | const char *fname, | ||
332 | size_t *dst_size, | ||
333 | void *dst) | ||
334 | { | ||
335 | struct GNUNET_CRYPTO_RsaSignature **sig = dst; | ||
336 | size_t len; | ||
337 | const void *res; | ||
338 | int fnum; | ||
339 | |||
340 | (void) cls; | ||
341 | *sig = NULL; | ||
342 | fnum = PQfnumber (result, | ||
343 | fname); | ||
344 | if (fnum < 0) | ||
345 | { | ||
346 | GNUNET_break (0); | ||
347 | return GNUNET_SYSERR; | ||
348 | } | ||
349 | if (PQgetisnull (result, | ||
350 | row, | ||
351 | fnum)) | ||
352 | return GNUNET_NO; | ||
353 | /* if a field is null, continue but | ||
354 | * remember that we now return a different result */ | ||
355 | len = PQgetlength (result, | ||
356 | row, | ||
357 | fnum); | ||
358 | res = PQgetvalue (result, | ||
359 | row, | ||
360 | fnum); | ||
361 | *sig = GNUNET_CRYPTO_rsa_signature_decode (res, | ||
362 | len); | ||
363 | if (NULL == *sig) | ||
364 | { | ||
365 | GNUNET_break (0); | ||
366 | return GNUNET_SYSERR; | ||
367 | } | ||
368 | return GNUNET_OK; | ||
369 | } | ||
370 | |||
371 | |||
372 | /** | ||
373 | * Function called to clean up memory allocated | ||
374 | * by a #GNUNET_PQ_ResultConverter. | ||
375 | * | ||
376 | * @param cls closure | ||
377 | * @param rd result data to clean up | ||
378 | */ | ||
379 | static void | ||
380 | clean_rsa_signature (void *cls, | ||
381 | void *rd) | ||
382 | { | ||
383 | struct GNUNET_CRYPTO_RsaSignature **sig = rd; | ||
384 | |||
385 | (void) cls; | ||
386 | if (NULL != *sig) | ||
387 | { | ||
388 | GNUNET_CRYPTO_rsa_signature_free (*sig); | ||
389 | *sig = NULL; | ||
390 | } | ||
391 | } | ||
392 | |||
393 | |||
394 | struct GNUNET_PQ_ResultSpec | ||
395 | GNUNET_PQ_result_spec_rsa_signature (const char *name, | ||
396 | struct GNUNET_CRYPTO_RsaSignature **sig) | ||
397 | { | ||
398 | struct GNUNET_PQ_ResultSpec res = | ||
399 | { &extract_rsa_signature, | ||
400 | &clean_rsa_signature, | ||
401 | NULL, | ||
402 | (void *) sig, 0, (name), NULL }; | ||
403 | |||
404 | return res; | ||
405 | } | ||
406 | |||
407 | |||
408 | /** | ||
409 | * Extract data from a Postgres database @a result at row @a row. | ||
410 | * | ||
411 | * @param cls closure | ||
412 | * @param result where to extract data from | ||
413 | * @param int row to extract data from | ||
414 | * @param fname name (or prefix) of the fields to extract from | ||
415 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
416 | * @param[out] dst where to store the result | ||
417 | * @return | ||
418 | * #GNUNET_YES if all results could be extracted | ||
419 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
420 | */ | ||
421 | static enum GNUNET_GenericReturnValue | ||
422 | extract_string (void *cls, | ||
423 | PGresult *result, | ||
424 | int row, | ||
425 | const char *fname, | ||
426 | size_t *dst_size, | ||
427 | void *dst) | ||
428 | { | ||
429 | char **str = dst; | ||
430 | size_t len; | ||
431 | const char *res; | ||
432 | int fnum; | ||
433 | |||
434 | (void) cls; | ||
435 | *str = NULL; | ||
436 | fnum = PQfnumber (result, | ||
437 | fname); | ||
438 | if (fnum < 0) | ||
439 | { | ||
440 | GNUNET_break (0); | ||
441 | return GNUNET_SYSERR; | ||
442 | } | ||
443 | if (PQgetisnull (result, | ||
444 | row, | ||
445 | fnum)) | ||
446 | return GNUNET_NO; | ||
447 | /* if a field is null, continue but | ||
448 | * remember that we now return a different result */ | ||
449 | len = PQgetlength (result, | ||
450 | row, | ||
451 | fnum); | ||
452 | res = PQgetvalue (result, | ||
453 | row, | ||
454 | fnum); | ||
455 | *str = GNUNET_strndup (res, | ||
456 | len); | ||
457 | if (NULL == *str) | ||
458 | { | ||
459 | GNUNET_break (0); | ||
460 | return GNUNET_SYSERR; | ||
461 | } | ||
462 | return GNUNET_OK; | ||
463 | } | ||
464 | |||
465 | |||
466 | /** | ||
467 | * Function called to clean up memory allocated | ||
468 | * by a #GNUNET_PQ_ResultConverter. | ||
469 | * | ||
470 | * @param cls closure | ||
471 | * @param rd result data to clean up | ||
472 | */ | ||
473 | static void | ||
474 | clean_string (void *cls, | ||
475 | void *rd) | ||
476 | { | ||
477 | char **str = rd; | ||
478 | |||
479 | (void) cls; | ||
480 | if (NULL != *str) | ||
481 | { | ||
482 | GNUNET_free (*str); | ||
483 | *str = NULL; | ||
484 | } | ||
485 | } | ||
486 | |||
487 | |||
488 | struct GNUNET_PQ_ResultSpec | ||
489 | GNUNET_PQ_result_spec_string (const char *name, | ||
490 | char **dst) | ||
491 | { | ||
492 | struct GNUNET_PQ_ResultSpec res = | ||
493 | { &extract_string, | ||
494 | &clean_string, | ||
495 | NULL, | ||
496 | (void *) dst, 0, (name), NULL }; | ||
497 | |||
498 | return res; | ||
499 | } | ||
500 | |||
501 | |||
502 | /** | ||
503 | * Extract data from a Postgres database @a result at row @a row. | ||
504 | * | ||
505 | * @param cls closure | ||
506 | * @param result where to extract data from | ||
507 | * @param int row to extract data from | ||
508 | * @param fname name (or prefix) of the fields to extract from | ||
509 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
510 | * @param[out] dst where to store the result | ||
511 | * @return | ||
512 | * #GNUNET_YES if all results could be extracted | ||
513 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
514 | */ | ||
515 | static enum GNUNET_GenericReturnValue | ||
516 | extract_rel_time (void *cls, | ||
517 | PGresult *result, | ||
518 | int row, | ||
519 | const char *fname, | ||
520 | size_t *dst_size, | ||
521 | void *dst) | ||
522 | { | ||
523 | struct GNUNET_TIME_Relative *udst = dst; | ||
524 | const int64_t *res; | ||
525 | int fnum; | ||
526 | |||
527 | (void) cls; | ||
528 | fnum = PQfnumber (result, | ||
529 | fname); | ||
530 | if (fnum < 0) | ||
531 | { | ||
532 | GNUNET_break (0); | ||
533 | return GNUNET_SYSERR; | ||
534 | } | ||
535 | if (PQgetisnull (result, | ||
536 | row, | ||
537 | fnum)) | ||
538 | return GNUNET_NO; | ||
539 | GNUNET_assert (NULL != dst); | ||
540 | if (sizeof(struct GNUNET_TIME_Relative) != *dst_size) | ||
541 | { | ||
542 | GNUNET_break (0); | ||
543 | return GNUNET_SYSERR; | ||
544 | } | ||
545 | if (sizeof(int64_t) != | ||
546 | PQgetlength (result, | ||
547 | row, | ||
548 | fnum)) | ||
549 | { | ||
550 | GNUNET_break (0); | ||
551 | return GNUNET_SYSERR; | ||
552 | } | ||
553 | res = (int64_t *) PQgetvalue (result, | ||
554 | row, | ||
555 | fnum); | ||
556 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
557 | *udst = GNUNET_TIME_UNIT_FOREVER_REL; | ||
558 | else | ||
559 | udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
560 | return GNUNET_OK; | ||
561 | } | ||
562 | |||
563 | |||
564 | struct GNUNET_PQ_ResultSpec | ||
565 | GNUNET_PQ_result_spec_relative_time (const char *name, | ||
566 | struct GNUNET_TIME_Relative *rt) | ||
567 | { | ||
568 | struct GNUNET_PQ_ResultSpec res = { | ||
569 | &extract_rel_time, | ||
570 | NULL, | ||
571 | NULL, | ||
572 | (void *) rt, | ||
573 | sizeof(*rt), | ||
574 | name, | ||
575 | NULL | ||
576 | }; | ||
577 | |||
578 | return res; | ||
579 | } | ||
580 | |||
581 | |||
582 | /** | ||
583 | * Extract data from a Postgres database @a result at row @a row. | ||
584 | * | ||
585 | * @param cls closure | ||
586 | * @param result where to extract data from | ||
587 | * @param int row to extract data from | ||
588 | * @param fname name (or prefix) of the fields to extract from | ||
589 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
590 | * @param[out] dst where to store the result | ||
591 | * @return | ||
592 | * #GNUNET_YES if all results could be extracted | ||
593 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
594 | */ | ||
595 | static enum GNUNET_GenericReturnValue | ||
596 | extract_abs_time (void *cls, | ||
597 | PGresult *result, | ||
598 | int row, | ||
599 | const char *fname, | ||
600 | size_t *dst_size, | ||
601 | void *dst) | ||
602 | { | ||
603 | struct GNUNET_TIME_Absolute *udst = dst; | ||
604 | const int64_t *res; | ||
605 | int fnum; | ||
606 | |||
607 | (void) cls; | ||
608 | fnum = PQfnumber (result, | ||
609 | fname); | ||
610 | if (fnum < 0) | ||
611 | { | ||
612 | GNUNET_break (0); | ||
613 | return GNUNET_SYSERR; | ||
614 | } | ||
615 | if (PQgetisnull (result, | ||
616 | row, | ||
617 | fnum)) | ||
618 | return GNUNET_NO; | ||
619 | GNUNET_assert (NULL != dst); | ||
620 | if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size) | ||
621 | { | ||
622 | GNUNET_break (0); | ||
623 | return GNUNET_SYSERR; | ||
624 | } | ||
625 | if (sizeof(int64_t) != | ||
626 | PQgetlength (result, | ||
627 | row, | ||
628 | fnum)) | ||
629 | { | ||
630 | GNUNET_break (0); | ||
631 | return GNUNET_SYSERR; | ||
632 | } | ||
633 | res = (int64_t *) PQgetvalue (result, | ||
634 | row, | ||
635 | fnum); | ||
636 | if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) | ||
637 | *udst = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
638 | else | ||
639 | udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res); | ||
640 | return GNUNET_OK; | ||
641 | } | ||
642 | |||
643 | |||
644 | struct GNUNET_PQ_ResultSpec | ||
645 | GNUNET_PQ_result_spec_absolute_time (const char *name, | ||
646 | struct GNUNET_TIME_Absolute *at) | ||
647 | { | ||
648 | struct GNUNET_PQ_ResultSpec res = | ||
649 | { &extract_abs_time, | ||
650 | NULL, | ||
651 | NULL, | ||
652 | (void *) at, sizeof(*at), (name), NULL }; | ||
653 | |||
654 | return res; | ||
655 | } | ||
656 | |||
657 | |||
658 | struct GNUNET_PQ_ResultSpec | ||
659 | GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, | ||
660 | struct GNUNET_TIME_AbsoluteNBO *at) | ||
661 | { | ||
662 | struct GNUNET_PQ_ResultSpec res = | ||
663 | GNUNET_PQ_result_spec_auto_from_type (name, &at->abs_value_us__); | ||
664 | |||
665 | return res; | ||
666 | } | ||
667 | |||
668 | |||
669 | /** | ||
670 | * Extract data from a Postgres database @a result at row @a row. | ||
671 | * | ||
672 | * @param cls closure | ||
673 | * @param result where to extract data from | ||
674 | * @param int row to extract data from | ||
675 | * @param fname name (or prefix) of the fields to extract from | ||
676 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
677 | * @param[out] dst where to store the result | ||
678 | * @return | ||
679 | * #GNUNET_YES if all results could be extracted | ||
680 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
681 | */ | ||
682 | static enum GNUNET_GenericReturnValue | ||
683 | extract_uint16 (void *cls, | ||
684 | PGresult *result, | ||
685 | int row, | ||
686 | const char *fname, | ||
687 | size_t *dst_size, | ||
688 | void *dst) | ||
689 | { | ||
690 | uint16_t *udst = dst; | ||
691 | const uint16_t *res; | ||
692 | int fnum; | ||
693 | |||
694 | (void) cls; | ||
695 | fnum = PQfnumber (result, | ||
696 | fname); | ||
697 | if (fnum < 0) | ||
698 | { | ||
699 | GNUNET_break (0); | ||
700 | return GNUNET_SYSERR; | ||
701 | } | ||
702 | if (PQgetisnull (result, | ||
703 | row, | ||
704 | fnum)) | ||
705 | return GNUNET_NO; | ||
706 | GNUNET_assert (NULL != dst); | ||
707 | if (sizeof(uint16_t) != *dst_size) | ||
708 | { | ||
709 | GNUNET_break (0); | ||
710 | return GNUNET_SYSERR; | ||
711 | } | ||
712 | if (sizeof(uint16_t) != | ||
713 | PQgetlength (result, | ||
714 | row, | ||
715 | fnum)) | ||
716 | { | ||
717 | GNUNET_break (0); | ||
718 | return GNUNET_SYSERR; | ||
719 | } | ||
720 | res = (uint16_t *) PQgetvalue (result, | ||
721 | row, | ||
722 | fnum); | ||
723 | *udst = ntohs (*res); | ||
724 | return GNUNET_OK; | ||
725 | } | ||
726 | |||
727 | |||
728 | struct GNUNET_PQ_ResultSpec | ||
729 | GNUNET_PQ_result_spec_uint16 (const char *name, | ||
730 | uint16_t *u16) | ||
731 | { | ||
732 | struct GNUNET_PQ_ResultSpec res = | ||
733 | { &extract_uint16, | ||
734 | NULL, | ||
735 | NULL, | ||
736 | (void *) u16, sizeof(*u16), (name), NULL }; | ||
737 | |||
738 | return res; | ||
739 | } | ||
740 | |||
741 | |||
742 | /** | ||
743 | * Extract data from a Postgres database @a result at row @a row. | ||
744 | * | ||
745 | * @param cls closure | ||
746 | * @param result where to extract data from | ||
747 | * @param int row to extract data from | ||
748 | * @param fname name (or prefix) of the fields to extract from | ||
749 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
750 | * @param[out] dst where to store the result | ||
751 | * @return | ||
752 | * #GNUNET_YES if all results could be extracted | ||
753 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
754 | */ | ||
755 | static enum GNUNET_GenericReturnValue | ||
756 | extract_uint32 (void *cls, | ||
757 | PGresult *result, | ||
758 | int row, | ||
759 | const char *fname, | ||
760 | size_t *dst_size, | ||
761 | void *dst) | ||
762 | { | ||
763 | uint32_t *udst = dst; | ||
764 | const uint32_t *res; | ||
765 | int fnum; | ||
766 | |||
767 | (void) cls; | ||
768 | fnum = PQfnumber (result, | ||
769 | fname); | ||
770 | if (fnum < 0) | ||
771 | { | ||
772 | GNUNET_break (0); | ||
773 | return GNUNET_SYSERR; | ||
774 | } | ||
775 | if (PQgetisnull (result, | ||
776 | row, | ||
777 | fnum)) | ||
778 | return GNUNET_NO; | ||
779 | GNUNET_assert (NULL != dst); | ||
780 | if (sizeof(uint32_t) != *dst_size) | ||
781 | { | ||
782 | GNUNET_break (0); | ||
783 | return GNUNET_SYSERR; | ||
784 | } | ||
785 | if (sizeof(uint32_t) != | ||
786 | PQgetlength (result, | ||
787 | row, | ||
788 | fnum)) | ||
789 | { | ||
790 | GNUNET_break (0); | ||
791 | return GNUNET_SYSERR; | ||
792 | } | ||
793 | res = (uint32_t *) PQgetvalue (result, | ||
794 | row, | ||
795 | fnum); | ||
796 | *udst = ntohl (*res); | ||
797 | return GNUNET_OK; | ||
798 | } | ||
799 | |||
800 | |||
801 | struct GNUNET_PQ_ResultSpec | ||
802 | GNUNET_PQ_result_spec_uint32 (const char *name, | ||
803 | uint32_t *u32) | ||
804 | { | ||
805 | struct GNUNET_PQ_ResultSpec res = { | ||
806 | &extract_uint32, | ||
807 | NULL, | ||
808 | NULL, | ||
809 | (void *) u32, | ||
810 | sizeof(*u32), | ||
811 | (name), | ||
812 | NULL | ||
813 | }; | ||
814 | |||
815 | return res; | ||
816 | } | ||
817 | |||
818 | |||
819 | /** | ||
820 | * Extract data from a Postgres database @a result at row @a row. | ||
821 | * | ||
822 | * @param cls closure | ||
823 | * @param result where to extract data from | ||
824 | * @param int row to extract data from | ||
825 | * @param fname name (or prefix) of the fields to extract from | ||
826 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
827 | * @param[out] dst where to store the result | ||
828 | * @return | ||
829 | * #GNUNET_YES if all results could be extracted | ||
830 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
831 | */ | ||
832 | static enum GNUNET_GenericReturnValue | ||
833 | extract_uint64 (void *cls, | ||
834 | PGresult *result, | ||
835 | int row, | ||
836 | const char *fname, | ||
837 | size_t *dst_size, | ||
838 | void *dst) | ||
839 | { | ||
840 | uint64_t *udst = dst; | ||
841 | const uint64_t *res; | ||
842 | int fnum; | ||
843 | |||
844 | (void) cls; | ||
845 | fnum = PQfnumber (result, | ||
846 | fname); | ||
847 | if (fnum < 0) | ||
848 | { | ||
849 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
850 | "Field %s missing in result\n", | ||
851 | fname); | ||
852 | GNUNET_break (0); | ||
853 | return GNUNET_SYSERR; | ||
854 | } | ||
855 | if (PQgetisnull (result, | ||
856 | row, | ||
857 | fnum)) | ||
858 | return GNUNET_NO; | ||
859 | |||
860 | GNUNET_assert (NULL != dst); | ||
861 | if (sizeof(uint64_t) != *dst_size) | ||
862 | { | ||
863 | GNUNET_break (0); | ||
864 | return GNUNET_SYSERR; | ||
865 | } | ||
866 | if (sizeof(uint64_t) != | ||
867 | PQgetlength (result, | ||
868 | row, | ||
869 | fnum)) | ||
870 | { | ||
871 | GNUNET_break (0); | ||
872 | return GNUNET_SYSERR; | ||
873 | } | ||
874 | res = (uint64_t *) PQgetvalue (result, | ||
875 | row, | ||
876 | fnum); | ||
877 | *udst = GNUNET_ntohll (*res); | ||
878 | return GNUNET_OK; | ||
879 | } | ||
880 | |||
881 | |||
882 | struct GNUNET_PQ_ResultSpec | ||
883 | GNUNET_PQ_result_spec_uint64 (const char *name, | ||
884 | uint64_t *u64) | ||
885 | { | ||
886 | struct GNUNET_PQ_ResultSpec res = { | ||
887 | &extract_uint64, | ||
888 | NULL, | ||
889 | NULL, | ||
890 | (void *) u64, | ||
891 | sizeof(*u64), | ||
892 | (name), | ||
893 | NULL | ||
894 | }; | ||
895 | |||
896 | return res; | ||
897 | } | ||
898 | |||
899 | |||
900 | /* end of pq_result_helper.c */ | ||