aboutsummaryrefslogtreecommitdiff
path: root/src/pq
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-04-17 19:13:42 +0200
committerChristian Grothoff <christian@grothoff.org>2020-04-17 19:13:42 +0200
commit3695a510a18d0bb9ed58fb5270856a88853b4e30 (patch)
tree9aa9fe53d3b3f16a086a242b660504b793c3aa54 /src/pq
parent6b89b84d2781a774adbadf272eb90785889b8407 (diff)
downloadgnunet-3695a510a18d0bb9ed58fb5270856a88853b4e30.tar.gz
gnunet-3695a510a18d0bb9ed58fb5270856a88853b4e30.zip
add relative_time specs
Diffstat (limited to 'src/pq')
-rw-r--r--src/pq/pq_query_helper.c62
-rw-r--r--src/pq/pq_result_helper.c90
2 files changed, 152 insertions, 0 deletions
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
index 1290a6fb2..a36848f3a 100644
--- a/src/pq/pq_query_helper.c
+++ b/src/pq/pq_query_helper.c
@@ -402,6 +402,68 @@ GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
402 * @return -1 on error, number of offsets used in @a scratch otherwise 402 * @return -1 on error, number of offsets used in @a scratch otherwise
403 */ 403 */
404static int 404static int
405qconv_rel_time (void *cls,
406 const void *data,
407 size_t data_len,
408 void *param_values[],
409 int param_lengths[],
410 int param_formats[],
411 unsigned int param_length,
412 void *scratch[],
413 unsigned int scratch_length)
414{
415 const struct GNUNET_TIME_Relative *u = data;
416 struct GNUNET_TIME_Relative rel;
417 uint64_t *u_nbo;
418
419 GNUNET_break (NULL == cls);
420 if (1 != param_length)
421 return -1;
422 rel = *u;
423 if (rel.rel_value_us > INT64_MAX)
424 rel.rel_value_us = INT64_MAX;
425 u_nbo = GNUNET_new (uint64_t);
426 scratch[0] = u_nbo;
427 *u_nbo = GNUNET_htonll (rel.rel_value_us);
428 param_values[0] = (void *) u_nbo;
429 param_lengths[0] = sizeof(uint64_t);
430 param_formats[0] = 1;
431 return 1;
432}
433
434
435/**
436 * Generate query parameter for a relative time value.
437 * The database must store a 64-bit integer.
438 *
439 * @param x pointer to the query parameter to pass
440 * @return array entry for the query parameters to use
441 */
442struct GNUNET_PQ_QueryParam
443GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
444{
445 struct GNUNET_PQ_QueryParam res =
446 { &qconv_rel_time, NULL, x, sizeof(*x), 1 };
447
448 return res;
449}
450
451
452/**
453 * Function called to convert input argument into SQL parameters.
454 *
455 * @param cls closure
456 * @param data pointer to input argument
457 * @param data_len number of bytes in @a data (if applicable)
458 * @param[out] param_values SQL data to set
459 * @param[out] param_lengths SQL length data to set
460 * @param[out] param_formats SQL format data to set
461 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
462 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
463 * @param scratch_length number of entries left in @a scratch
464 * @return -1 on error, number of offsets used in @a scratch otherwise
465 */
466static int
405qconv_abs_time (void *cls, 467qconv_abs_time (void *cls,
406 const void *data, 468 const void *data,
407 size_t data_len, 469 size_t data_len,
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index dc64597f8..f764593b0 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -544,6 +544,96 @@ GNUNET_PQ_result_spec_string (const char *name,
544 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 544 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
545 */ 545 */
546static int 546static int
547extract_rel_time (void *cls,
548 PGresult *result,
549 int row,
550 const char *fname,
551 size_t *dst_size,
552 void *dst)
553{
554 struct GNUNET_TIME_Relative *udst = dst;
555 const int64_t *res;
556 int fnum;
557
558 (void) cls;
559 fnum = PQfnumber (result,
560 fname);
561 if (fnum < 0)
562 {
563 GNUNET_break (0);
564 return GNUNET_SYSERR;
565 }
566 if (PQgetisnull (result,
567 row,
568 fnum))
569 {
570 GNUNET_break (0);
571 return GNUNET_SYSERR;
572 }
573 GNUNET_assert (NULL != dst);
574 if (sizeof(struct GNUNET_TIME_Relative) != *dst_size)
575 {
576 GNUNET_break (0);
577 return GNUNET_SYSERR;
578 }
579 if (sizeof(int64_t) !=
580 PQgetlength (result,
581 row,
582 fnum))
583 {
584 GNUNET_break (0);
585 return GNUNET_SYSERR;
586 }
587 res = (int64_t *) PQgetvalue (result,
588 row,
589 fnum);
590 if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res))
591 *udst = GNUNET_TIME_UNIT_FOREVER_REL;
592 else
593 udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res);
594 return GNUNET_OK;
595}
596
597
598/**
599 * Relative time expected.
600 *
601 * @param name name of the field in the table
602 * @param[out] at where to store the result
603 * @return array entry for the result specification to use
604 */
605struct GNUNET_PQ_ResultSpec
606GNUNET_PQ_result_spec_relative_time (const char *name,
607 struct GNUNET_TIME_Relative *rt)
608{
609 struct GNUNET_PQ_ResultSpec res = {
610 &extract_rel_time,
611 NULL,
612 NULL,
613 (void *) rt,
614 sizeof(*rt),
615 name,
616 NULL
617 };
618
619 return res;
620}
621
622
623/**
624 * Extract data from a Postgres database @a result at row @a row.
625 *
626 * @param cls closure
627 * @param result where to extract data from
628 * @param int row to extract data from
629 * @param fname name (or prefix) of the fields to extract from
630 * @param[in,out] dst_size where to store size of result, may be NULL
631 * @param[out] dst where to store the result
632 * @return
633 * #GNUNET_YES if all results could be extracted
634 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
635 */
636static int
547extract_abs_time (void *cls, 637extract_abs_time (void *cls,
548 PGresult *result, 638 PGresult *result,
549 int row, 639 int row,