summaryrefslogtreecommitdiff
path: root/src/pq/pq_result_helper.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-05-03 05:30:25 +0000
committerChristian Grothoff <christian@grothoff.org>2016-05-03 05:30:25 +0000
commit0cdac9e1cfdc299666cc5b89a43e5148bbcd2d2c (patch)
tree4056379b34e526e5cd6e98e95a00b61e5f39ed6c /src/pq/pq_result_helper.c
parent91c472ec6a6be7fd37ecd6b13674389431a02afb (diff)
downloadgnunet-0cdac9e1cfdc299666cc5b89a43e5148bbcd2d2c.tar.gz
gnunet-0cdac9e1cfdc299666cc5b89a43e5148bbcd2d2c.zip
add support for string results from PQ
Diffstat (limited to 'src/pq/pq_result_helper.c')
-rw-r--r--src/pq/pq_result_helper.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index c3f3cfb2f..bb6a8fa9b 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -416,6 +416,109 @@ GNUNET_PQ_result_spec_rsa_signature (const char *name,
416 416
417 417
418/** 418/**
419 * Extract data from a Postgres database @a result at row @a row.
420 *
421 * @param cls closure
422 * @param result where to extract data from
423 * @param int row to extract data from
424 * @param fname name (or prefix) of the fields to extract from
425 * @param[in,out] dst_size where to store size of result, may be NULL
426 * @param[out] dst where to store the result
427 * @return
428 * #GNUNET_YES if all results could be extracted
429 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
430 */
431static int
432extract_string (void *cls,
433 PGresult *result,
434 int row,
435 const char *fname,
436 size_t *dst_size,
437 void *dst)
438{
439 char **str = dst;
440 size_t len;
441 const char *res;
442 int fnum;
443
444 *str = NULL;
445 fnum = PQfnumber (result,
446 fname);
447 if (fnum < 0)
448 {
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
450 "Field `%s' does not exist in result\n",
451 fname);
452 return GNUNET_SYSERR;
453 }
454 if (PQgetisnull (result,
455 row,
456 fnum))
457 return GNUNET_SYSERR;
458
459 /* if a field is null, continue but
460 * remember that we now return a different result */
461 len = PQgetlength (result,
462 row,
463 fnum);
464 res = PQgetvalue (result,
465 row,
466 fnum);
467 *str = GNUNET_strndup (res,
468 len);
469 if (NULL == *str)
470 {
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472 "Field `%s' contains bogus value (fails to decode)\n",
473 fname);
474 return GNUNET_SYSERR;
475 }
476 return GNUNET_OK;
477}
478
479
480/**
481 * Function called to clean up memory allocated
482 * by a #GNUNET_PQ_ResultConverter.
483 *
484 * @param cls closure
485 * @param rd result data to clean up
486 */
487static void
488clean_string (void *cls,
489 void *rd)
490{
491 char **str = rd;
492
493 if (NULL != *str)
494 {
495 GNUNET_free (*str);
496 *str = NULL;
497 }
498}
499
500
501/**
502 * 0-terminated string expected.
503 *
504 * @param name name of the field in the table
505 * @param[out] dst where to store the result, allocated
506 * @return array entry for the result specification to use
507 */
508struct GNUNET_PQ_ResultSpec
509GNUNET_PQ_result_spec_string (const char *name,
510 char **dst)
511{
512 struct GNUNET_PQ_ResultSpec res =
513 { &extract_string,
514 &clean_string,
515 NULL,
516 (void *) dst, 0, (name), NULL };
517 return res;
518}
519
520
521/**
419 * Absolute time expected. 522 * Absolute time expected.
420 * 523 *
421 * @param name name of the field in the table 524 * @param name name of the field in the table