diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-06-16 17:09:22 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-06-16 17:09:22 +0200 |
commit | 1bc1fbda00a9e31d2256c52b29a2c758b7818268 (patch) | |
tree | 6480c3ed89990f845c44d51b14dc8d67fc3a79bb /src | |
parent | c66e726a8abe565e5f34313e38729f18fde4a73a (diff) | |
download | gnunet-1bc1fbda00a9e31d2256c52b29a2c758b7818268.tar.gz gnunet-1bc1fbda00a9e31d2256c52b29a2c758b7818268.zip |
PQ: update libgnunetpq implementation to avoid duplicated nullable tests and to allow for the actual field name to differ from the fname, as done for example by the Taler amounts which are split over two DB fields
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_pq_lib.h | 19 | ||||
-rw-r--r-- | src/pq/pq.c | 35 | ||||
-rw-r--r-- | src/pq/pq_result_helper.c | 88 |
3 files changed, 59 insertions, 83 deletions
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h index c8a648032..d7fa793ca 100644 --- a/src/include/gnunet_pq_lib.h +++ b/src/include/gnunet_pq_lib.h | |||
@@ -245,15 +245,16 @@ GNUNET_PQ_query_param_uint64 (const uint64_t *x); | |||
245 | * @param[out] dst where to store the result | 245 | * @param[out] dst where to store the result |
246 | * @return | 246 | * @return |
247 | * #GNUNET_YES if all results could be extracted | 247 | * #GNUNET_YES if all results could be extracted |
248 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 248 | * #GNUNET_NO if the field was NULL |
249 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | ||
249 | */ | 250 | */ |
250 | typedef int | 251 | typedef enum GNUNET_GenericReturnValue |
251 | (*GNUNET_PQ_ResultConverter) (void *cls, | 252 | (*GNUNET_PQ_ResultConverter)(void *cls, |
252 | PGresult *result, | 253 | PGresult *result, |
253 | int row, | 254 | int row, |
254 | const char *fname, | 255 | const char *fname, |
255 | size_t *dst_size, | 256 | size_t *dst_size, |
256 | void *dst); | 257 | void *dst); |
257 | 258 | ||
258 | 259 | ||
259 | /** | 260 | /** |
@@ -310,7 +311,7 @@ struct GNUNET_PQ_ResultSpec | |||
310 | * Where to store actual size of the result. | 311 | * Where to store actual size of the result. |
311 | */ | 312 | */ |
312 | size_t *result_size; | 313 | size_t *result_size; |
313 | 314 | ||
314 | /** | 315 | /** |
315 | * True if NULL is allowed for a value in the database. | 316 | * True if NULL is allowed for a value in the database. |
316 | */ | 317 | */ |
diff --git a/src/pq/pq.c b/src/pq/pq.c index e9c960e33..25202a640 100644 --- a/src/pq/pq.c +++ b/src/pq/pq.c | |||
@@ -133,34 +133,31 @@ GNUNET_PQ_extract_result (PGresult *result, | |||
133 | for (unsigned int i = 0; NULL != rs[i].conv; i++) | 133 | for (unsigned int i = 0; NULL != rs[i].conv; i++) |
134 | { | 134 | { |
135 | struct GNUNET_PQ_ResultSpec *spec; | 135 | struct GNUNET_PQ_ResultSpec *spec; |
136 | int ret; | 136 | enum GNUNET_GenericReturnValue ret; |
137 | 137 | ||
138 | spec = &rs[i]; | 138 | spec = &rs[i]; |
139 | if (spec->is_nullable) | ||
140 | { | ||
141 | int fnum; | ||
142 | |||
143 | fnum = PQfnumber (result, | ||
144 | spec->fname); | ||
145 | if (PQgetisnull (result, | ||
146 | row, | ||
147 | fnum)) | ||
148 | { | ||
149 | if (NULL != spec->is_null) | ||
150 | *spec->is_null = true; | ||
151 | continue; | ||
152 | } | ||
153 | if (NULL != spec->is_null) | ||
154 | *spec->is_null = false; | ||
155 | } | ||
156 | ret = spec->conv (spec->cls, | 139 | ret = spec->conv (spec->cls, |
157 | result, | 140 | result, |
158 | row, | 141 | row, |
159 | spec->fname, | 142 | spec->fname, |
160 | &spec->dst_size, | 143 | &spec->dst_size, |
161 | spec->dst); | 144 | spec->dst); |
162 | if (GNUNET_OK != ret) | 145 | switch (ret) |
163 | { | 146 | { |
147 | case GNUNET_OK: | ||
148 | /* canonical case, continue below */ | ||
149 | if (NULL != spec->is_null) | ||
150 | *spec->is_null = false; | ||
151 | break; | ||
152 | case GNUNET_NO: | ||
153 | if (spec->is_nullable) | ||
154 | { | ||
155 | if (NULL != spec->is_null) | ||
156 | *spec->is_null = true; | ||
157 | continue; | ||
158 | } | ||
159 | /* intentional fall-through: NULL value for field that is NOT nullable */ | ||
160 | case GNUNET_SYSERR: | ||
164 | for (unsigned int j = 0; j < i; j++) | 161 | for (unsigned int j = 0; j < i; j++) |
165 | if (NULL != rs[j].cleaner) | 162 | if (NULL != rs[j].cleaner) |
166 | rs[j].cleaner (rs[j].cls, | 163 | rs[j].cleaner (rs[j].cls, |
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c index 204ccee72..23fb4f96e 100644 --- a/src/pq/pq_result_helper.c +++ b/src/pq/pq_result_helper.c | |||
@@ -75,7 +75,7 @@ clean_varsize_blob (void *cls, | |||
75 | * #GNUNET_YES if all results could be extracted | 75 | * #GNUNET_YES if all results could be extracted |
76 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | 76 | * #GNUNET_SYSERR if a result was invalid (non-existing field) |
77 | */ | 77 | */ |
78 | static int | 78 | static enum GNUNET_GenericReturnValue |
79 | extract_varsize_blob (void *cls, | 79 | extract_varsize_blob (void *cls, |
80 | PGresult *result, | 80 | PGresult *result, |
81 | int row, | 81 | int row, |
@@ -102,10 +102,7 @@ extract_varsize_blob (void *cls, | |||
102 | if (PQgetisnull (result, | 102 | if (PQgetisnull (result, |
103 | row, | 103 | row, |
104 | fnum)) | 104 | fnum)) |
105 | { | 105 | return GNUNET_NO; |
106 | /* Let's allow this for varsize */ | ||
107 | return GNUNET_OK; | ||
108 | } | ||
109 | /* if a field is null, continue but | 106 | /* if a field is null, continue but |
110 | * remember that we now return a different result */ | 107 | * remember that we now return a different result */ |
111 | len = PQgetlength (result, | 108 | len = PQgetlength (result, |
@@ -152,7 +149,7 @@ GNUNET_PQ_result_spec_variable_size (const char *name, | |||
152 | * #GNUNET_YES if all results could be extracted | 149 | * #GNUNET_YES if all results could be extracted |
153 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 150 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
154 | */ | 151 | */ |
155 | static int | 152 | static enum GNUNET_GenericReturnValue |
156 | extract_fixed_blob (void *cls, | 153 | extract_fixed_blob (void *cls, |
157 | PGresult *result, | 154 | PGresult *result, |
158 | int row, | 155 | int row, |
@@ -178,11 +175,7 @@ extract_fixed_blob (void *cls, | |||
178 | if (PQgetisnull (result, | 175 | if (PQgetisnull (result, |
179 | row, | 176 | row, |
180 | fnum)) | 177 | fnum)) |
181 | { | 178 | return GNUNET_NO; |
182 | GNUNET_break (0); | ||
183 | return GNUNET_SYSERR; | ||
184 | } | ||
185 | |||
186 | /* if a field is null, continue but | 179 | /* if a field is null, continue but |
187 | * remember that we now return a different result */ | 180 | * remember that we now return a different result */ |
188 | len = PQgetlength (result, | 181 | len = PQgetlength (result, |
@@ -236,7 +229,7 @@ GNUNET_PQ_result_spec_fixed_size (const char *name, | |||
236 | * #GNUNET_YES if all results could be extracted | 229 | * #GNUNET_YES if all results could be extracted |
237 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 230 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
238 | */ | 231 | */ |
239 | static int | 232 | static enum GNUNET_GenericReturnValue |
240 | extract_rsa_public_key (void *cls, | 233 | extract_rsa_public_key (void *cls, |
241 | PGresult *result, | 234 | PGresult *result, |
242 | int row, | 235 | int row, |
@@ -261,10 +254,8 @@ extract_rsa_public_key (void *cls, | |||
261 | if (PQgetisnull (result, | 254 | if (PQgetisnull (result, |
262 | row, | 255 | row, |
263 | fnum)) | 256 | fnum)) |
264 | { | 257 | return GNUNET_NO; |
265 | GNUNET_break (0); | 258 | |
266 | return GNUNET_SYSERR; | ||
267 | } | ||
268 | /* if a field is null, continue but | 259 | /* if a field is null, continue but |
269 | * remember that we now return a different result */ | 260 | * remember that we now return a different result */ |
270 | len = PQgetlength (result, | 261 | len = PQgetlength (result, |
@@ -333,7 +324,7 @@ GNUNET_PQ_result_spec_rsa_public_key (const char *name, | |||
333 | * #GNUNET_YES if all results could be extracted | 324 | * #GNUNET_YES if all results could be extracted |
334 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 325 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
335 | */ | 326 | */ |
336 | static int | 327 | static enum GNUNET_GenericReturnValue |
337 | extract_rsa_signature (void *cls, | 328 | extract_rsa_signature (void *cls, |
338 | PGresult *result, | 329 | PGresult *result, |
339 | int row, | 330 | int row, |
@@ -358,10 +349,7 @@ extract_rsa_signature (void *cls, | |||
358 | if (PQgetisnull (result, | 349 | if (PQgetisnull (result, |
359 | row, | 350 | row, |
360 | fnum)) | 351 | fnum)) |
361 | { | 352 | return GNUNET_NO; |
362 | GNUNET_break (0); | ||
363 | return GNUNET_SYSERR; | ||
364 | } | ||
365 | /* if a field is null, continue but | 353 | /* if a field is null, continue but |
366 | * remember that we now return a different result */ | 354 | * remember that we now return a different result */ |
367 | len = PQgetlength (result, | 355 | len = PQgetlength (result, |
@@ -430,7 +418,7 @@ GNUNET_PQ_result_spec_rsa_signature (const char *name, | |||
430 | * #GNUNET_YES if all results could be extracted | 418 | * #GNUNET_YES if all results could be extracted |
431 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 419 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
432 | */ | 420 | */ |
433 | static int | 421 | static enum GNUNET_GenericReturnValue |
434 | extract_string (void *cls, | 422 | extract_string (void *cls, |
435 | PGresult *result, | 423 | PGresult *result, |
436 | int row, | 424 | int row, |
@@ -455,10 +443,7 @@ extract_string (void *cls, | |||
455 | if (PQgetisnull (result, | 443 | if (PQgetisnull (result, |
456 | row, | 444 | row, |
457 | fnum)) | 445 | fnum)) |
458 | { | 446 | return GNUNET_NO; |
459 | GNUNET_break (0); | ||
460 | return GNUNET_SYSERR; | ||
461 | } | ||
462 | /* if a field is null, continue but | 447 | /* if a field is null, continue but |
463 | * remember that we now return a different result */ | 448 | * remember that we now return a different result */ |
464 | len = PQgetlength (result, | 449 | len = PQgetlength (result, |
@@ -527,7 +512,7 @@ GNUNET_PQ_result_spec_string (const char *name, | |||
527 | * #GNUNET_YES if all results could be extracted | 512 | * #GNUNET_YES if all results could be extracted |
528 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 513 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
529 | */ | 514 | */ |
530 | static int | 515 | static enum GNUNET_GenericReturnValue |
531 | extract_rel_time (void *cls, | 516 | extract_rel_time (void *cls, |
532 | PGresult *result, | 517 | PGresult *result, |
533 | int row, | 518 | int row, |
@@ -550,10 +535,7 @@ extract_rel_time (void *cls, | |||
550 | if (PQgetisnull (result, | 535 | if (PQgetisnull (result, |
551 | row, | 536 | row, |
552 | fnum)) | 537 | fnum)) |
553 | { | 538 | return GNUNET_NO; |
554 | GNUNET_break (0); | ||
555 | return GNUNET_SYSERR; | ||
556 | } | ||
557 | GNUNET_assert (NULL != dst); | 539 | GNUNET_assert (NULL != dst); |
558 | if (sizeof(struct GNUNET_TIME_Relative) != *dst_size) | 540 | if (sizeof(struct GNUNET_TIME_Relative) != *dst_size) |
559 | { | 541 | { |
@@ -610,7 +592,7 @@ GNUNET_PQ_result_spec_relative_time (const char *name, | |||
610 | * #GNUNET_YES if all results could be extracted | 592 | * #GNUNET_YES if all results could be extracted |
611 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 593 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
612 | */ | 594 | */ |
613 | static int | 595 | static enum GNUNET_GenericReturnValue |
614 | extract_abs_time (void *cls, | 596 | extract_abs_time (void *cls, |
615 | PGresult *result, | 597 | PGresult *result, |
616 | int row, | 598 | int row, |
@@ -633,10 +615,7 @@ extract_abs_time (void *cls, | |||
633 | if (PQgetisnull (result, | 615 | if (PQgetisnull (result, |
634 | row, | 616 | row, |
635 | fnum)) | 617 | fnum)) |
636 | { | 618 | return GNUNET_NO; |
637 | GNUNET_break (0); | ||
638 | return GNUNET_SYSERR; | ||
639 | } | ||
640 | GNUNET_assert (NULL != dst); | 619 | GNUNET_assert (NULL != dst); |
641 | if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size) | 620 | if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size) |
642 | { | 621 | { |
@@ -700,7 +679,7 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, | |||
700 | * #GNUNET_YES if all results could be extracted | 679 | * #GNUNET_YES if all results could be extracted |
701 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 680 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
702 | */ | 681 | */ |
703 | static int | 682 | static enum GNUNET_GenericReturnValue |
704 | extract_uint16 (void *cls, | 683 | extract_uint16 (void *cls, |
705 | PGresult *result, | 684 | PGresult *result, |
706 | int row, | 685 | int row, |
@@ -723,10 +702,7 @@ extract_uint16 (void *cls, | |||
723 | if (PQgetisnull (result, | 702 | if (PQgetisnull (result, |
724 | row, | 703 | row, |
725 | fnum)) | 704 | fnum)) |
726 | { | 705 | return GNUNET_NO; |
727 | GNUNET_break (0); | ||
728 | return GNUNET_SYSERR; | ||
729 | } | ||
730 | GNUNET_assert (NULL != dst); | 706 | GNUNET_assert (NULL != dst); |
731 | if (sizeof(uint16_t) != *dst_size) | 707 | if (sizeof(uint16_t) != *dst_size) |
732 | { | 708 | { |
@@ -776,7 +752,7 @@ GNUNET_PQ_result_spec_uint16 (const char *name, | |||
776 | * #GNUNET_YES if all results could be extracted | 752 | * #GNUNET_YES if all results could be extracted |
777 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 753 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
778 | */ | 754 | */ |
779 | static int | 755 | static enum GNUNET_GenericReturnValue |
780 | extract_uint32 (void *cls, | 756 | extract_uint32 (void *cls, |
781 | PGresult *result, | 757 | PGresult *result, |
782 | int row, | 758 | int row, |
@@ -799,10 +775,7 @@ extract_uint32 (void *cls, | |||
799 | if (PQgetisnull (result, | 775 | if (PQgetisnull (result, |
800 | row, | 776 | row, |
801 | fnum)) | 777 | fnum)) |
802 | { | 778 | return GNUNET_NO; |
803 | GNUNET_break (0); | ||
804 | return GNUNET_SYSERR; | ||
805 | } | ||
806 | GNUNET_assert (NULL != dst); | 779 | GNUNET_assert (NULL != dst); |
807 | if (sizeof(uint32_t) != *dst_size) | 780 | if (sizeof(uint32_t) != *dst_size) |
808 | { | 781 | { |
@@ -829,11 +802,15 @@ struct GNUNET_PQ_ResultSpec | |||
829 | GNUNET_PQ_result_spec_uint32 (const char *name, | 802 | GNUNET_PQ_result_spec_uint32 (const char *name, |
830 | uint32_t *u32) | 803 | uint32_t *u32) |
831 | { | 804 | { |
832 | struct GNUNET_PQ_ResultSpec res = | 805 | struct GNUNET_PQ_ResultSpec res = { |
833 | { &extract_uint32, | 806 | &extract_uint32, |
834 | NULL, | 807 | NULL, |
835 | NULL, | 808 | NULL, |
836 | (void *) u32, sizeof(*u32), (name), NULL }; | 809 | (void *) u32, |
810 | sizeof(*u32), | ||
811 | (name), | ||
812 | NULL | ||
813 | }; | ||
837 | 814 | ||
838 | return res; | 815 | return res; |
839 | } | 816 | } |
@@ -852,7 +829,7 @@ GNUNET_PQ_result_spec_uint32 (const char *name, | |||
852 | * #GNUNET_YES if all results could be extracted | 829 | * #GNUNET_YES if all results could be extracted |
853 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 830 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
854 | */ | 831 | */ |
855 | static int | 832 | static enum GNUNET_GenericReturnValue |
856 | extract_uint64 (void *cls, | 833 | extract_uint64 (void *cls, |
857 | PGresult *result, | 834 | PGresult *result, |
858 | int row, | 835 | int row, |
@@ -878,10 +855,8 @@ extract_uint64 (void *cls, | |||
878 | if (PQgetisnull (result, | 855 | if (PQgetisnull (result, |
879 | row, | 856 | row, |
880 | fnum)) | 857 | fnum)) |
881 | { | 858 | return GNUNET_NO; |
882 | GNUNET_break (0); | 859 | |
883 | return GNUNET_SYSERR; | ||
884 | } | ||
885 | GNUNET_assert (NULL != dst); | 860 | GNUNET_assert (NULL != dst); |
886 | if (sizeof(uint64_t) != *dst_size) | 861 | if (sizeof(uint64_t) != *dst_size) |
887 | { | 862 | { |
@@ -912,7 +887,10 @@ GNUNET_PQ_result_spec_uint64 (const char *name, | |||
912 | &extract_uint64, | 887 | &extract_uint64, |
913 | NULL, | 888 | NULL, |
914 | NULL, | 889 | NULL, |
915 | (void *) u64, sizeof(*u64), (name), NULL | 890 | (void *) u64, |
891 | sizeof(*u64), | ||
892 | (name), | ||
893 | NULL | ||
916 | }; | 894 | }; |
917 | 895 | ||
918 | return res; | 896 | return res; |