diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-12-05 11:58:45 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-12-05 11:58:45 +0100 |
commit | 2266d461461384be87dd4385e1fc2043c61bc99e (patch) | |
tree | 6ee3826fc7189fef2192f3dcc5bcb8d444a1fd8d | |
parent | ac749fb8ab73ff86366099226a76f1c7d2980394 (diff) | |
download | gnunet-2266d461461384be87dd4385e1fc2043c61bc99e.tar.gz gnunet-2266d461461384be87dd4385e1fc2043c61bc99e.zip |
add bool type to libgnunetpq
-rw-r--r-- | src/include/gnunet_pq_lib.h | 22 | ||||
-rw-r--r-- | src/pq/pq_query_helper.c | 47 | ||||
-rw-r--r-- | src/pq/pq_result_helper.c | 139 |
3 files changed, 156 insertions, 52 deletions
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h index 6a374f7e2..05d373f88 100644 --- a/src/include/gnunet_pq_lib.h +++ b/src/include/gnunet_pq_lib.h | |||
@@ -133,6 +133,16 @@ GNUNET_PQ_query_param_string (const char *ptr); | |||
133 | 133 | ||
134 | 134 | ||
135 | /** | 135 | /** |
136 | * Pass a boolean into a query. | ||
137 | * | ||
138 | * @param b boolean bit to pass | ||
139 | * @return query parameter to use | ||
140 | */ | ||
141 | struct GNUNET_PQ_QueryParam | ||
142 | GNUNET_PQ_query_param_bool (bool b); | ||
143 | |||
144 | |||
145 | /** | ||
136 | * Generate fixed-size query parameter with size determined | 146 | * Generate fixed-size query parameter with size determined |
137 | * by variable type. | 147 | * by variable type. |
138 | * | 148 | * |
@@ -414,6 +424,18 @@ GNUNET_PQ_result_spec_string (const char *name, | |||
414 | 424 | ||
415 | 425 | ||
416 | /** | 426 | /** |
427 | * boolean expected. | ||
428 | * | ||
429 | * @param name name of the field in the table | ||
430 | * @param[out] dst where to store the result | ||
431 | * @return array entry for the result specification to use | ||
432 | */ | ||
433 | struct GNUNET_PQ_ResultSpec | ||
434 | GNUNET_PQ_result_spec_bool (const char *name, | ||
435 | bool *dst); | ||
436 | |||
437 | |||
438 | /** | ||
417 | * RSA public key expected. | 439 | * RSA public key expected. |
418 | * | 440 | * |
419 | * @param name name of the field in the table | 441 | * @param name name of the field in the table |
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c index cee84d203..78c324512 100644 --- a/src/pq/pq_query_helper.c +++ b/src/pq/pq_query_helper.c | |||
@@ -61,7 +61,7 @@ qconv_null (void *cls, | |||
61 | return -1; | 61 | return -1; |
62 | param_values[0] = NULL; | 62 | param_values[0] = NULL; |
63 | param_lengths[0] = 0; | 63 | param_lengths[0] = 0; |
64 | param_formats[0] = 1; | 64 | param_formats[0] = 1; |
65 | return 0; | 65 | return 0; |
66 | } | 66 | } |
67 | 67 | ||
@@ -134,6 +134,17 @@ GNUNET_PQ_query_param_string (const char *ptr) | |||
134 | } | 134 | } |
135 | 135 | ||
136 | 136 | ||
137 | struct GNUNET_PQ_QueryParam | ||
138 | GNUNET_PQ_query_param_bool (bool b) | ||
139 | { | ||
140 | static uint8_t bt = 1; | ||
141 | static uint8_t bf = 0; | ||
142 | |||
143 | return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf, | ||
144 | sizeof (uint8_t)); | ||
145 | } | ||
146 | |||
147 | |||
137 | /** | 148 | /** |
138 | * Function called to convert input argument into SQL parameters. | 149 | * Function called to convert input argument into SQL parameters. |
139 | * | 150 | * |
@@ -180,8 +191,9 @@ qconv_uint16 (void *cls, | |||
180 | struct GNUNET_PQ_QueryParam | 191 | struct GNUNET_PQ_QueryParam |
181 | GNUNET_PQ_query_param_uint16 (const uint16_t *x) | 192 | GNUNET_PQ_query_param_uint16 (const uint16_t *x) |
182 | { | 193 | { |
183 | struct GNUNET_PQ_QueryParam res = | 194 | struct GNUNET_PQ_QueryParam res = { |
184 | { &qconv_uint16, NULL, x, sizeof(*x), 1 }; | 195 | &qconv_uint16, NULL, x, sizeof(*x), 1 |
196 | }; | ||
185 | 197 | ||
186 | return res; | 198 | return res; |
187 | } | 199 | } |
@@ -233,8 +245,9 @@ qconv_uint32 (void *cls, | |||
233 | struct GNUNET_PQ_QueryParam | 245 | struct GNUNET_PQ_QueryParam |
234 | GNUNET_PQ_query_param_uint32 (const uint32_t *x) | 246 | GNUNET_PQ_query_param_uint32 (const uint32_t *x) |
235 | { | 247 | { |
236 | struct GNUNET_PQ_QueryParam res = | 248 | struct GNUNET_PQ_QueryParam res = { |
237 | { &qconv_uint32, NULL, x, sizeof(*x), 1 }; | 249 | &qconv_uint32, NULL, x, sizeof(*x), 1 |
250 | }; | ||
238 | 251 | ||
239 | return res; | 252 | return res; |
240 | } | 253 | } |
@@ -286,8 +299,9 @@ qconv_uint64 (void *cls, | |||
286 | struct GNUNET_PQ_QueryParam | 299 | struct GNUNET_PQ_QueryParam |
287 | GNUNET_PQ_query_param_uint64 (const uint64_t *x) | 300 | GNUNET_PQ_query_param_uint64 (const uint64_t *x) |
288 | { | 301 | { |
289 | struct GNUNET_PQ_QueryParam res = | 302 | struct GNUNET_PQ_QueryParam res = { |
290 | { &qconv_uint64, NULL, x, sizeof(*x), 1 }; | 303 | &qconv_uint64, NULL, x, sizeof(*x), 1 |
304 | }; | ||
291 | 305 | ||
292 | return res; | 306 | return res; |
293 | } | 307 | } |
@@ -339,8 +353,9 @@ struct GNUNET_PQ_QueryParam | |||
339 | GNUNET_PQ_query_param_rsa_public_key (const struct | 353 | GNUNET_PQ_query_param_rsa_public_key (const struct |
340 | GNUNET_CRYPTO_RsaPublicKey *x) | 354 | GNUNET_CRYPTO_RsaPublicKey *x) |
341 | { | 355 | { |
342 | struct GNUNET_PQ_QueryParam res = | 356 | struct GNUNET_PQ_QueryParam res = { |
343 | { &qconv_rsa_public_key, NULL, (x), 0, 1 }; | 357 | &qconv_rsa_public_key, NULL, (x), 0, 1 |
358 | }; | ||
344 | 359 | ||
345 | return res; | 360 | return res; |
346 | } | 361 | } |
@@ -391,8 +406,9 @@ qconv_rsa_signature (void *cls, | |||
391 | struct GNUNET_PQ_QueryParam | 406 | struct GNUNET_PQ_QueryParam |
392 | GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) | 407 | GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) |
393 | { | 408 | { |
394 | struct GNUNET_PQ_QueryParam res = | 409 | struct GNUNET_PQ_QueryParam res = { |
395 | { &qconv_rsa_signature, NULL, (x), 0, 1 }; | 410 | &qconv_rsa_signature, NULL, (x), 0, 1 |
411 | }; | ||
396 | 412 | ||
397 | return res; | 413 | return res; |
398 | } | 414 | } |
@@ -446,8 +462,9 @@ qconv_rel_time (void *cls, | |||
446 | struct GNUNET_PQ_QueryParam | 462 | struct GNUNET_PQ_QueryParam |
447 | GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x) | 463 | GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x) |
448 | { | 464 | { |
449 | struct GNUNET_PQ_QueryParam res = | 465 | struct GNUNET_PQ_QueryParam res = { |
450 | { &qconv_rel_time, NULL, x, sizeof(*x), 1 }; | 466 | &qconv_rel_time, NULL, x, sizeof(*x), 1 |
467 | }; | ||
451 | 468 | ||
452 | return res; | 469 | return res; |
453 | } | 470 | } |
@@ -510,8 +527,8 @@ GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) | |||
510 | 527 | ||
511 | 528 | ||
512 | struct GNUNET_PQ_QueryParam | 529 | struct GNUNET_PQ_QueryParam |
513 | GNUNET_PQ_query_param_absolute_time_nbo (const struct | 530 | GNUNET_PQ_query_param_absolute_time_nbo ( |
514 | GNUNET_TIME_AbsoluteNBO *x) | 531 | const struct GNUNET_TIME_AbsoluteNBO *x) |
515 | { | 532 | { |
516 | return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__); | 533 | return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__); |
517 | } | 534 | } |
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c index f264603f4..4057772ec 100644 --- a/src/pq/pq_result_helper.c +++ b/src/pq/pq_result_helper.c | |||
@@ -496,11 +496,82 @@ struct GNUNET_PQ_ResultSpec | |||
496 | GNUNET_PQ_result_spec_string (const char *name, | 496 | GNUNET_PQ_result_spec_string (const char *name, |
497 | char **dst) | 497 | char **dst) |
498 | { | 498 | { |
499 | struct GNUNET_PQ_ResultSpec res = | 499 | struct GNUNET_PQ_ResultSpec res = { |
500 | { &extract_string, | 500 | .conv = &extract_string, |
501 | &clean_string, | 501 | .cleaner = &clean_string, |
502 | NULL, | 502 | .dst = (void *) dst, |
503 | (void *) dst, 0, (name), NULL }; | 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 | }; | ||
504 | 575 | ||
505 | return res; | 576 | return res; |
506 | } | 577 | } |
@@ -573,13 +644,10 @@ GNUNET_PQ_result_spec_relative_time (const char *name, | |||
573 | struct GNUNET_TIME_Relative *rt) | 644 | struct GNUNET_TIME_Relative *rt) |
574 | { | 645 | { |
575 | struct GNUNET_PQ_ResultSpec res = { | 646 | struct GNUNET_PQ_ResultSpec res = { |
576 | &extract_rel_time, | 647 | .conv = &extract_rel_time, |
577 | NULL, | 648 | .dst = (void *) rt, |
578 | NULL, | 649 | .dst_size = sizeof(*rt), |
579 | (void *) rt, | 650 | .fname = name |
580 | sizeof(*rt), | ||
581 | name, | ||
582 | NULL | ||
583 | }; | 651 | }; |
584 | 652 | ||
585 | return res; | 653 | return res; |
@@ -652,11 +720,12 @@ struct GNUNET_PQ_ResultSpec | |||
652 | GNUNET_PQ_result_spec_absolute_time (const char *name, | 720 | GNUNET_PQ_result_spec_absolute_time (const char *name, |
653 | struct GNUNET_TIME_Absolute *at) | 721 | struct GNUNET_TIME_Absolute *at) |
654 | { | 722 | { |
655 | struct GNUNET_PQ_ResultSpec res = | 723 | struct GNUNET_PQ_ResultSpec res = { |
656 | { &extract_abs_time, | 724 | .conv = &extract_abs_time, |
657 | NULL, | 725 | .dst = (void *) at, |
658 | NULL, | 726 | .dst_size = sizeof(*at), |
659 | (void *) at, sizeof(*at), (name), NULL }; | 727 | .fname = name |
728 | }; | ||
660 | 729 | ||
661 | return res; | 730 | return res; |
662 | } | 731 | } |
@@ -667,7 +736,8 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, | |||
667 | struct GNUNET_TIME_AbsoluteNBO *at) | 736 | struct GNUNET_TIME_AbsoluteNBO *at) |
668 | { | 737 | { |
669 | struct GNUNET_PQ_ResultSpec res = | 738 | struct GNUNET_PQ_ResultSpec res = |
670 | GNUNET_PQ_result_spec_auto_from_type (name, &at->abs_value_us__); | 739 | GNUNET_PQ_result_spec_auto_from_type (name, |
740 | &at->abs_value_us__); | ||
671 | 741 | ||
672 | return res; | 742 | return res; |
673 | } | 743 | } |
@@ -736,11 +806,12 @@ struct GNUNET_PQ_ResultSpec | |||
736 | GNUNET_PQ_result_spec_uint16 (const char *name, | 806 | GNUNET_PQ_result_spec_uint16 (const char *name, |
737 | uint16_t *u16) | 807 | uint16_t *u16) |
738 | { | 808 | { |
739 | struct GNUNET_PQ_ResultSpec res = | 809 | struct GNUNET_PQ_ResultSpec res = { |
740 | { &extract_uint16, | 810 | .conv = &extract_uint16, |
741 | NULL, | 811 | .dst = (void *) u16, |
742 | NULL, | 812 | .dst_size = sizeof(*u16), |
743 | (void *) u16, sizeof(*u16), (name), NULL }; | 813 | .fname = name |
814 | }; | ||
744 | 815 | ||
745 | return res; | 816 | return res; |
746 | } | 817 | } |
@@ -810,13 +881,10 @@ GNUNET_PQ_result_spec_uint32 (const char *name, | |||
810 | uint32_t *u32) | 881 | uint32_t *u32) |
811 | { | 882 | { |
812 | struct GNUNET_PQ_ResultSpec res = { | 883 | struct GNUNET_PQ_ResultSpec res = { |
813 | &extract_uint32, | 884 | .conv = &extract_uint32, |
814 | NULL, | 885 | .dst = (void *) u32, |
815 | NULL, | 886 | .dst_size = sizeof(*u32), |
816 | (void *) u32, | 887 | .fname = name |
817 | sizeof(*u32), | ||
818 | (name), | ||
819 | NULL | ||
820 | }; | 888 | }; |
821 | 889 | ||
822 | return res; | 890 | return res; |
@@ -891,13 +959,10 @@ GNUNET_PQ_result_spec_uint64 (const char *name, | |||
891 | uint64_t *u64) | 959 | uint64_t *u64) |
892 | { | 960 | { |
893 | struct GNUNET_PQ_ResultSpec res = { | 961 | struct GNUNET_PQ_ResultSpec res = { |
894 | &extract_uint64, | 962 | .conv = &extract_uint64, |
895 | NULL, | 963 | .dst = (void *) u64, |
896 | NULL, | 964 | .dst_size = sizeof(*u64), |
897 | (void *) u64, | 965 | .fname = name |
898 | sizeof(*u64), | ||
899 | (name), | ||
900 | NULL | ||
901 | }; | 966 | }; |
902 | 967 | ||
903 | return res; | 968 | return res; |