-handle NULL results
[oweals/gnunet.git] / src / my / my_result_helper.c
1  /*
2   This file is part of GNUnet
3   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
5   GNUnet is free software; you can redistribute it and/or modify it under the
6   terms of the GNU General Public License as published by the Free Software
7   Foundation; either version 3, or (at your option) any later version.
8
9   GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12
13   You should have received a copy of the GNU General Public License along with
14   GNUnet; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
15 */
16 /**
17  * @file my/my_result_helper.c
18  * @brief functions to extract result values
19  * @author Christophe Genevey
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_my_lib.h"
25
26
27 /**
28  * extract data from a Mysql database @a result at row @a row
29  *
30  * @param cls closure
31  * @param[in,out] rs
32  * @param stmt the mysql statement that is being run
33  * @param column the column that is being processed
34  * @param[out] result mysql result
35  * @return
36  *   #GNUNET_OK if all results could be extracted
37  *   #GNUNET_SYSERR if a result was invalid
38  */
39 static int
40 pre_extract_varsize_blob (void *cls,
41                           struct GNUNET_MY_ResultSpec *rs,
42                           MYSQL_STMT *stmt,
43                           unsigned int column,
44                           MYSQL_BIND *results)
45 {
46   results[0].buffer = NULL;
47   results[0].buffer_length = 0;
48   results[0].length = &rs->mysql_bind_output_length;
49   results[0].is_null = &rs->is_null;
50   rs->is_null = 0;
51
52   return GNUNET_OK;
53 }
54
55
56 /**
57  * extract data from a Mysql database @a result at row @a row
58  *
59  * @param cls closure
60  * @param[in,out] rs
61  * @param stmt the mysql statement that is being run
62  * @param column the column that is being processed
63  * @param[out] results
64  * @return
65  *   #GNUNET_OK if all results could be extracted
66  *   #GNUNET_SYSERR if a result was invalid
67  */
68 static int
69 post_extract_varsize_blob (void *cls,
70                            struct GNUNET_MY_ResultSpec *rs,
71                            MYSQL_STMT *stmt,
72                            unsigned int column,
73                            MYSQL_BIND *results)
74 {
75   void *buf;
76   size_t size;
77
78   if (*results->is_null)
79     return GNUNET_SYSERR;
80   size = (size_t) rs->mysql_bind_output_length;
81
82   if (rs->mysql_bind_output_length != size)
83     return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
84
85   buf = GNUNET_malloc (size);
86
87   results[0].buffer = buf;
88   results[0].buffer_length = size;
89   results[0].buffer_type = MYSQL_TYPE_BLOB;
90
91   if (0 !=
92       mysql_stmt_fetch_column (stmt,
93                                results,
94                                column,
95                                0))
96   {
97     GNUNET_free (buf);
98     return GNUNET_SYSERR;
99   }
100
101   *(void **) rs->dst = buf;
102   *rs->result_size = size;
103
104   return GNUNET_OK;
105 }
106
107
108 /**
109  * extract data from a Mysql database @a result at row @a row
110  *
111  * @param cls closure
112  * @param[in,out] rs
113  */
114 static void
115 cleanup_varsize_blob (void *cls,
116                       struct GNUNET_MY_ResultSpec *rs)
117 {
118   void **ptr = (void **)rs->dst;
119
120   if (NULL != *ptr)
121   {
122     GNUNET_free (*ptr);
123     *ptr = NULL;
124   }
125 }
126
127
128 /**
129  * Variable-size result expected
130  *
131  * @param[out] dst where to store the result, allocated
132  * @param[out] ptr_size where to store the size of @a dst
133  * @return array entru for the result specification to use
134  */
135 struct GNUNET_MY_ResultSpec
136 GNUNET_MY_result_spec_variable_size (void **dst,
137                                     size_t *ptr_size)
138 {
139   struct GNUNET_MY_ResultSpec res =
140   {
141     .pre_conv = &pre_extract_varsize_blob,
142     .post_conv = &post_extract_varsize_blob,
143     .cleaner = &cleanup_varsize_blob,
144     .dst =  (void *)(dst),
145     .result_size = ptr_size,
146     .num_fields = 1
147   };
148
149   return res;
150 }
151
152
153 /**
154  * Extract data from a Mysql database @a result at row @a row
155  *
156  * @param cls closure
157  * @param[in,out] rs
158  * @param stmt the mysql statement that is being run
159  * @param column the column that is being processed
160  * @param[out] results
161  * @return
162  *  #GNUNET_OK if all results could be extracted
163  *  #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
164  */
165 static int
166 pre_extract_fixed_blob (void *cls,
167                         struct GNUNET_MY_ResultSpec *rs,
168                         MYSQL_STMT *stmt,
169                         unsigned int column,
170                         MYSQL_BIND *results)
171 {
172   results[0].buffer = rs->dst;
173   results[0].buffer_length = rs->dst_size;
174   results[0].length = &rs->mysql_bind_output_length;
175   results[0].buffer_type = MYSQL_TYPE_BLOB;
176   results[0].is_null = &rs->is_null;
177   rs->is_null = 0;
178
179   return GNUNET_OK;
180 }
181
182
183 /**
184  * Check size of extracted fixed size data from a Mysql database @a
185  * result at row @a row
186  *
187  * @param cls closure
188  * @param[in,out] rs
189  * @param stmt the mysql statement that is being run
190  * @param column the column that is being processed
191  * @param[out] results
192  * @return
193  *  #GNUNET_OK if all results could be extracted
194  *  #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
195  */
196 static int
197 post_extract_fixed_blob (void *cls,
198                          struct GNUNET_MY_ResultSpec *rs,
199                          MYSQL_STMT *stmt,
200                          unsigned int column,
201                          MYSQL_BIND *results)
202 {
203   if (*results->is_null)
204     return GNUNET_SYSERR;
205   if (rs->dst_size != rs->mysql_bind_output_length)
206     return GNUNET_SYSERR;
207   return GNUNET_OK;
208 }
209
210
211 /**
212  * Fixed-size result expected.
213  *
214  * @param name name of the field in the table
215  * @param[out] dst where to store the result
216  * @param ptr_size number of bytes in @a dst
217  * @return array entry for the result specification to use
218  */
219 struct GNUNET_MY_ResultSpec
220 GNUNET_MY_result_spec_fixed_size (void *ptr,
221                                   size_t ptr_size)
222 {
223   struct GNUNET_MY_ResultSpec res =
224   {
225     .pre_conv = &pre_extract_fixed_blob,
226     .post_conv = &post_extract_fixed_blob,
227     .cleaner = NULL,
228     .dst = (void *)(ptr),
229     .dst_size = ptr_size,
230     .num_fields = 1
231   };
232
233   return res;
234 }
235
236
237 /**
238   * Extract data from a Mysql database @a result at row @a row
239   *
240   * @param cls closure
241   * @param[in,out] rs
242   * @param stmt the mysql statement that is being run
243   * @param column the column that is being processed
244   * @param[out] results
245   * @return
246   *   #GNUNET_OK if all results could be extracted
247   *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
248   */
249 static int
250 pre_extract_rsa_public_key (void *cls,
251                             struct GNUNET_MY_ResultSpec *rs,
252                             MYSQL_STMT *stmt,
253                             unsigned int column,
254                             MYSQL_BIND *results)
255 {
256   results[0].buffer = NULL;
257   results[0].buffer_length = 0;
258   results[0].length = &rs->mysql_bind_output_length;
259   results[0].buffer_type = MYSQL_TYPE_BLOB;
260   results[0].is_null = &rs->is_null;
261   rs->is_null = 0;
262
263   return GNUNET_OK;
264 }
265
266
267 /**
268   * Check size of extracted fixed size data from a Mysql database @a
269   * result at row @a row
270   *
271   * @param cls closure
272   * @param[in,out] rs
273   * @param stmt the mysql statement that is being run
274   * @param column the column that is being processed
275   * @param[out] results
276   * @return
277   *   #GNUNET_OK if all results could be extracted
278   *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
279   */
280 static int
281 post_extract_rsa_public_key  (void *cls,
282                               struct GNUNET_MY_ResultSpec *rs,
283                               MYSQL_STMT *stmt,
284                               unsigned int column,
285                               MYSQL_BIND *results)
286
287 {
288   struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
289   void *buf;
290   size_t size;
291
292   if (*results->is_null)
293     return GNUNET_SYSERR;
294   size = (size_t) rs->mysql_bind_output_length;
295
296   if (rs->mysql_bind_output_length != size)
297     return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
298   buf = GNUNET_malloc (size);
299
300   results[0].buffer = buf;
301   results[0].buffer_length = size;
302   results[0].buffer_type = MYSQL_TYPE_BLOB;
303   if (0 !=
304       mysql_stmt_fetch_column (stmt,
305                                results,
306                                column,
307                                0))
308   {
309     GNUNET_free (buf);
310     return GNUNET_SYSERR;
311   }
312
313   *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
314                                              size);
315   GNUNET_free (buf);
316   if (NULL == *pk)
317   {
318     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
319                 "Results contains bogus public key value (fail to decode)\n");
320     return GNUNET_SYSERR;
321   }
322
323   return GNUNET_OK;
324 }
325
326
327 /**
328  * Function called to clean up memory allocated
329  * by a #GNUNET_MY_ResultConverter.
330  *
331  * @param cls closure
332  * @param rs result data to clean up
333  */
334 static void
335 clean_rsa_public_key (void *cls,
336                       struct GNUNET_MY_ResultSpec *rs)
337 {
338   struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
339
340   if (NULL != *pk)
341   {
342     GNUNET_CRYPTO_rsa_public_key_free (*pk);
343     *pk = NULL;
344   }
345 }
346
347
348 /**
349   * RSA public key expected
350   *
351   * @param name name of the field in the table
352   * @param[out] rsa where to store the result
353   * @return array entry for the result specification to use
354   */
355 struct GNUNET_MY_ResultSpec
356 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
357 {
358   struct GNUNET_MY_ResultSpec res = {
359     .pre_conv = &pre_extract_rsa_public_key,
360     .post_conv = &post_extract_rsa_public_key,
361     .cleaner = &clean_rsa_public_key,
362     .dst = (void *) rsa,
363     .dst_size = 0,
364     .num_fields = 1
365   };
366
367   return res;
368 }
369
370
371 /**
372   * Extract data from a Mysql database @a result at row @a row.
373   *
374   * @param cls closure
375   * @param[in,out] rs
376   * @param stmt the mysql statement that is being run
377   * @param column the column that is being processed
378   * @param[out] results
379   * @return
380   *    #GNUNET_OK if all results could be extracted
381   *    #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
382   */
383 static int
384 pre_extract_rsa_signature (void *cls,
385                       struct GNUNET_MY_ResultSpec *rs,
386                       MYSQL_STMT *stmt,
387                       unsigned int column,
388                       MYSQL_BIND *results)
389 {
390   results[0].buffer = 0;
391   results[0].buffer_length = 0;
392   results[0].length = &rs->mysql_bind_output_length;
393   results[0].buffer_type = MYSQL_TYPE_BLOB;
394   results[0].is_null = &rs->is_null;
395   rs->is_null = 0;
396
397   return GNUNET_OK;
398 }
399
400
401 /**
402   * Extract data from a Mysql database @a result at row @a row.
403   *
404   * @param cls closure
405   * @param[in,out] rs
406   * @param stmt the mysql statement that is being run
407   * @param column the column that is being processed
408   * @param[out] results
409   * @return
410   *    #GNUNET_OK if all results could be extracted
411   *    #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
412   */
413 static int
414 post_extract_rsa_signature (void *cls,
415                             struct GNUNET_MY_ResultSpec *rs,
416                             MYSQL_STMT *stmt,
417                             unsigned int column,
418                             MYSQL_BIND *results)
419 {
420   struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
421   void *buf;
422   size_t size;
423
424   if (*results->is_null)
425     return GNUNET_SYSERR;
426   size = (size_t) rs->mysql_bind_output_length;
427
428   if (rs->mysql_bind_output_length != size)
429     return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
430   buf = GNUNET_malloc (size);
431
432   results[0].buffer = buf;
433   results[0].buffer_length = size;
434   results[0].buffer_type = MYSQL_TYPE_BLOB;
435   if (0 !=
436       mysql_stmt_fetch_column (stmt,
437                                results,
438                                column,
439                                0))
440   {
441     GNUNET_free (buf);
442     return GNUNET_SYSERR;
443   }
444
445   *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
446                                              size);
447   GNUNET_free (buf);
448   if (NULL == *sig)
449   {
450     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451                 "Resuls contains bogus signature value (fails to decode)\n");
452     return GNUNET_SYSERR;
453   }
454   return GNUNET_OK;
455 }
456
457
458 /**
459  * Function called to clean up memory allocated
460  * by a #GNUNET_MY_ResultConverter.
461  *
462  * @param cls closure
463  * @param rd result data to clean up
464  */
465 static void
466 clean_rsa_signature (void *cls,
467                      struct GNUNET_MY_ResultSpec *rs)
468 {
469   struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
470
471   if (NULL != *sig)
472   {
473     GNUNET_CRYPTO_rsa_signature_free (*sig);
474     *sig = NULL;
475   }
476 }
477
478
479 /**
480   * RSA signature expected.
481   *
482   * @param[out] sig where to store the result;
483   * @return array entry for the result specification to use
484   */
485 struct GNUNET_MY_ResultSpec
486 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
487 {
488   struct GNUNET_MY_ResultSpec res =
489   {
490     .pre_conv = &pre_extract_rsa_signature,
491     .post_conv = &post_extract_rsa_signature,
492     .cleaner = &clean_rsa_signature,
493     .dst = (void *)sig,
494     .dst_size = 0,
495     .num_fields = 1
496   };
497   return res;
498 }
499
500
501 /**
502  * Extract data from a Mysql database @a result at row @a row
503  *
504  * @param cls closure
505  * @param[in,out] rs
506  * @param stmt the mysql statement that is being run
507  * @param column the column that is being processed
508  * @param[out] results
509  * @return
510  *    #GNUNET_OK if all results could be extracted
511  *    #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
512  */
513 static int
514 pre_extract_string (void * cls,
515                     struct GNUNET_MY_ResultSpec *rs,
516                     MYSQL_STMT *stmt,
517                     unsigned int column,
518                     MYSQL_BIND *results)
519 {
520   results[0].buffer = NULL;
521   results[0].buffer_length = 0;
522   results[0].length = &rs->mysql_bind_output_length;
523   results[0].buffer_type = MYSQL_TYPE_BLOB;
524   results[0].is_null = &rs->is_null;
525   rs->is_null = 0;
526
527   return GNUNET_OK;
528 }
529
530
531 /**
532  * Check size of extracted fixed size data from a Mysql database @a
533  *
534  * @param cls closure
535  * @param[in,out] rs
536  * @param stmt the mysql statement that is being run
537  * @param column the column that is being processed
538  * @param[out] results
539  * @return
540  *    #GNUNET_OK if all results could be extracted
541  *    #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
542  */
543 static int
544 post_extract_string (void * cls,
545                      struct GNUNET_MY_ResultSpec *rs,
546                      MYSQL_STMT *stmt,
547                      unsigned int column,
548                      MYSQL_BIND *results)
549 {
550   size_t size = (size_t) rs->mysql_bind_output_length;
551   char *buf;
552
553   if (rs->mysql_bind_output_length != size)
554     return GNUNET_SYSERR;
555   if (*results->is_null)
556   {
557     rs->dst = NULL;
558     return GNUNET_OK;
559   }
560
561   buf = GNUNET_malloc (size);
562   results[0].buffer = buf;
563   results[0].buffer_length = size;
564   results[0].buffer_type = MYSQL_TYPE_BLOB;
565
566   if (0 !=
567       mysql_stmt_fetch_column (stmt,
568                                results,
569                                column,
570                                0))
571   {
572     GNUNET_free (buf);
573     return GNUNET_SYSERR;
574   }
575   rs->dst = buf;
576   return GNUNET_OK;
577 }
578
579
580 /**
581  * 0- terminated string exprected.
582  *
583  * @param[out] dst where to store the result, allocated
584  * @return array entry for the result specification to use
585  */
586 struct GNUNET_MY_ResultSpec
587 GNUNET_MY_result_spec_string (char **dst)
588 {
589   struct GNUNET_MY_ResultSpec res = {
590     .pre_conv = &pre_extract_string,
591     .post_conv = &post_extract_string,
592     .cleaner = NULL,
593     .dst = (void *) dst,
594     .dst_size = 0,
595     .num_fields = 1
596   };
597   return res;
598 }
599
600
601 /**
602  * Absolute time expected
603  *
604  * @param name name of the field in the table
605  * @param[out] at where to store the result
606  * @return array entry for the result specification to use
607   */
608 struct GNUNET_MY_ResultSpec
609 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
610 {
611   return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
612 }
613
614
615 /**
616   * Absolute time in network byte order expected
617   *
618   * @param[out] at where to store the result
619   * @return array entry for the result specification to use
620   */
621 struct GNUNET_MY_ResultSpec
622 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
623 {
624   struct GNUNET_MY_ResultSpec res =
625     GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
626   return res;
627 }
628
629
630 /**
631  * Extract data from a Postgres database @a result at row @a row.
632  *
633  * @param cls closure
634  * @param[in,out] rs
635  * @param stmt the mysql statement that is being run
636  * @param column the column that is being processed
637  * @param[out] results
638  * @return
639  *   #GNUNET_YES if all results could be extracted
640  *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
641  */
642 static int
643 pre_extract_uint16 (void *cls,
644                     struct GNUNET_MY_ResultSpec *rs,
645                     MYSQL_STMT *stmt,
646                     unsigned int column,
647                     MYSQL_BIND *results)
648 {
649   results[0].buffer = rs->dst;
650   results[0].buffer_length = rs->dst_size;
651   results[0].length = &rs->mysql_bind_output_length;
652   results[0].buffer_type = MYSQL_TYPE_SHORT;
653   results[0].is_null = &rs->is_null;
654   rs->is_null = 0;
655
656   return GNUNET_OK;
657 }
658
659
660 /**
661  * Check size of extracted fixed size data from a Mysql datbase.
662  *
663  * @param cls closure
664  * @param[in,out] rs
665  * @param stmt the mysql statement that is being run
666  * @param column the column that is being processed
667  * @param[out] results
668  * @return
669  *   #GNUNET_YES if all results could be extracted
670  *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
671  */
672 static int
673 post_extract_uint16 (void *cls,
674                      struct GNUNET_MY_ResultSpec *rs,
675                      MYSQL_STMT *stmt,
676                      unsigned int column,
677                      MYSQL_BIND *results)
678 {
679   if (rs->dst_size != rs->mysql_bind_output_length)
680     return GNUNET_SYSERR;
681   if (*results->is_null)
682     return GNUNET_SYSERR;
683   return GNUNET_OK;
684 }
685
686
687 /**
688  * uint16_t expected
689  *
690  * @param[out] u16 where to store the result
691  * @return array entry for the result specification to use
692  */
693 struct GNUNET_MY_ResultSpec
694 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
695 {
696   struct GNUNET_MY_ResultSpec res = {
697     .pre_conv = &pre_extract_uint16,
698     .post_conv = &post_extract_uint16,
699     .cleaner = NULL,
700     .dst = (void *) u16,
701     .dst_size = sizeof (*u16),
702     .num_fields = 1
703   };
704   return res;
705 }
706
707
708 /**
709   * Extrac data from a  MYSQL database @a result at row @a row
710   *
711   * @param cls closure
712   * @param cls closure
713   * @param[in,out] rs
714   * @param stmt the mysql statement that is being run
715   * @param column the column that is being processed
716   * @param[out] results
717   * @return
718   *      #GNUNET_OK if all results could be extracted
719   *      #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
720   */
721 static int
722 pre_extract_uint32 (void *cls,
723                     struct GNUNET_MY_ResultSpec *rs,
724                     MYSQL_STMT *stmt,
725                     unsigned int column,
726                     MYSQL_BIND *results)
727 {
728   results[0].buffer = rs->dst;
729   results[0].buffer_length = rs->dst_size;
730   results[0].length = &rs->mysql_bind_output_length;
731   results[0].buffer_type = MYSQL_TYPE_LONG;
732   results[0].is_null = &rs->is_null;
733   rs->is_null = 0;
734
735   return GNUNET_OK;
736 }
737
738
739 /**
740   * Extrac data from a  MYSQL database @a result at row @a row
741   *
742   * @param cls closure
743   * @param cls closure
744   * @param[in,out] rs
745   * @param stmt the mysql statement that is being run
746   * @param column the column that is being processed
747   * @param[out] results
748   * @return
749   *      #GNUNET_OK if all results could be extracted
750   *      #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
751   */
752 static int
753 post_extract_uint32 (void *cls,
754                      struct GNUNET_MY_ResultSpec *rs,
755                      MYSQL_STMT * stmt,
756                      unsigned int column,
757                      MYSQL_BIND *results)
758 {
759   if (rs->dst_size != rs->mysql_bind_output_length)
760     return GNUNET_SYSERR;
761   if (*results->is_null)
762     return GNUNET_SYSERR;
763   return GNUNET_OK;
764 }
765
766
767 /**
768   * uint32_t expected
769   *
770   * @param[out] u32 where to store the result
771   * @return array entry for the result specification to use
772   */
773 struct GNUNET_MY_ResultSpec
774 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
775 {
776   struct GNUNET_MY_ResultSpec res = {
777     .pre_conv = &pre_extract_uint32,
778     .post_conv = &post_extract_uint32,
779     .cleaner = NULL,
780     .dst = (void *) u32,
781     .dst_size = sizeof (*u32),
782     .num_fields = 1
783   };
784   return res;
785 }
786
787
788 /**
789  * Extract data from a MYSQL database @a result at row @a row
790  *
791  * @param cls closure
792  * @param[in,out] rs
793  * @param stmt the mysql statement that is being run
794  * @param column the column that is being processed
795  * @param[out] results
796  * @return
797  *    #GNUNET_OK if all results could be extracted
798  *    #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
799  */
800 static int
801 pre_extract_uint64 (void *cls,
802                     struct GNUNET_MY_ResultSpec *rs,
803                     MYSQL_STMT *stmt,
804                     unsigned int column,
805                     MYSQL_BIND *results)
806 {
807   if (sizeof (uint64_t) != rs->dst_size)
808     return GNUNET_SYSERR;
809   results[0].buffer = rs->dst;
810   results[0].buffer_length = rs->dst_size;
811   results[0].length = &rs->mysql_bind_output_length;
812   results[0].buffer_type = MYSQL_TYPE_LONGLONG;
813   results[0].is_null = &rs->is_null;
814   rs->is_null = 0;
815
816   return GNUNET_OK;
817 }
818
819
820 /**
821  * Check size of extracted fixe size data from a Mysql database
822  *
823  * @param cls closure
824  * @param[in,out] rs
825  * @param stmt the mysql statement that is being run
826  * @param column the column that is being processed
827  * @param[out] results
828  * @return
829  *    #GNUNET_OK if all results could be extracted
830  *    #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
831  */
832 static int
833 post_extract_uint64 (void *cls,
834                      struct GNUNET_MY_ResultSpec *rs,
835                      MYSQL_STMT *stmt,
836                      unsigned int column,
837                      MYSQL_BIND *results)
838 {
839   if (sizeof (uint64_t) != rs->dst_size)
840     return GNUNET_SYSERR;
841   if (*results->is_null)
842     return GNUNET_SYSERR;
843   return GNUNET_OK;
844 }
845
846
847 /**
848  * uint64_t expected.
849  *
850  * @param[out] u64 where to store the result
851  * @return array entry for the result specification to use
852  */
853 struct GNUNET_MY_ResultSpec
854 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
855 {
856   struct GNUNET_MY_ResultSpec res = {
857     .pre_conv = &pre_extract_uint64,
858     .post_conv = &post_extract_uint64,
859     .cleaner = NULL,
860     .dst = (void *) u64,
861     .dst_size = sizeof (*u64),
862     .num_fields = 1
863   };
864   return res;
865 }
866
867
868 /* end of pq_result_helper.c */