aboutsummaryrefslogtreecommitdiff
path: root/src/pq
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq')
-rw-r--r--src/pq/pq_query_helper.c107
-rw-r--r--src/pq/pq_result_helper.c142
2 files changed, 239 insertions, 10 deletions
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
index 78c324512..f5b4f38a2 100644
--- a/src/pq/pq_query_helper.c
+++ b/src/pq/pq_query_helper.c
@@ -70,7 +70,8 @@ struct GNUNET_PQ_QueryParam
70GNUNET_PQ_query_param_null (void) 70GNUNET_PQ_query_param_null (void)
71{ 71{
72 struct GNUNET_PQ_QueryParam res = { 72 struct GNUNET_PQ_QueryParam res = {
73 &qconv_null, NULL, NULL, 0, 1 73 .conv = &qconv_null,
74 .num_params = 1
74 }; 75 };
75 76
76 return res; 77 return res;
@@ -192,7 +193,10 @@ struct GNUNET_PQ_QueryParam
192GNUNET_PQ_query_param_uint16 (const uint16_t *x) 193GNUNET_PQ_query_param_uint16 (const uint16_t *x)
193{ 194{
194 struct GNUNET_PQ_QueryParam res = { 195 struct GNUNET_PQ_QueryParam res = {
195 &qconv_uint16, NULL, x, sizeof(*x), 1 196 .conv = &qconv_uint16,
197 .data = x,
198 .size = sizeof(*x),
199 .num_params = 1
196 }; 200 };
197 201
198 return res; 202 return res;
@@ -246,7 +250,10 @@ struct GNUNET_PQ_QueryParam
246GNUNET_PQ_query_param_uint32 (const uint32_t *x) 250GNUNET_PQ_query_param_uint32 (const uint32_t *x)
247{ 251{
248 struct GNUNET_PQ_QueryParam res = { 252 struct GNUNET_PQ_QueryParam res = {
249 &qconv_uint32, NULL, x, sizeof(*x), 1 253 .conv = &qconv_uint32,
254 .data = x,
255 .size = sizeof(*x),
256 .num_params = 1
250 }; 257 };
251 258
252 return res; 259 return res;
@@ -300,7 +307,10 @@ struct GNUNET_PQ_QueryParam
300GNUNET_PQ_query_param_uint64 (const uint64_t *x) 307GNUNET_PQ_query_param_uint64 (const uint64_t *x)
301{ 308{
302 struct GNUNET_PQ_QueryParam res = { 309 struct GNUNET_PQ_QueryParam res = {
303 &qconv_uint64, NULL, x, sizeof(*x), 1 310 .conv = &qconv_uint64,
311 .data = x,
312 .size = sizeof(*x),
313 .num_params = 1
304 }; 314 };
305 315
306 return res; 316 return res;
@@ -350,11 +360,13 @@ qconv_rsa_public_key (void *cls,
350 360
351 361
352struct GNUNET_PQ_QueryParam 362struct GNUNET_PQ_QueryParam
353GNUNET_PQ_query_param_rsa_public_key (const struct 363GNUNET_PQ_query_param_rsa_public_key (
354 GNUNET_CRYPTO_RsaPublicKey *x) 364 const struct GNUNET_CRYPTO_RsaPublicKey *x)
355{ 365{
356 struct GNUNET_PQ_QueryParam res = { 366 struct GNUNET_PQ_QueryParam res = {
357 &qconv_rsa_public_key, NULL, (x), 0, 1 367 .conv = &qconv_rsa_public_key,
368 .data = x,
369 .num_params = 1
358 }; 370 };
359 371
360 return res; 372 return res;
@@ -407,7 +419,9 @@ struct GNUNET_PQ_QueryParam
407GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) 419GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
408{ 420{
409 struct GNUNET_PQ_QueryParam res = { 421 struct GNUNET_PQ_QueryParam res = {
410 &qconv_rsa_signature, NULL, (x), 0, 1 422 .conv = &qconv_rsa_signature,
423 .data = x,
424 .num_params = 1
411 }; 425 };
412 426
413 return res; 427 return res;
@@ -463,7 +477,10 @@ struct GNUNET_PQ_QueryParam
463GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x) 477GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
464{ 478{
465 struct GNUNET_PQ_QueryParam res = { 479 struct GNUNET_PQ_QueryParam res = {
466 &qconv_rel_time, NULL, x, sizeof(*x), 1 480 .conv = &qconv_rel_time,
481 .data = x,
482 .size = sizeof(*x),
483 .num_params = 1
467 }; 484 };
468 485
469 return res; 486 return res;
@@ -519,7 +536,10 @@ struct GNUNET_PQ_QueryParam
519GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) 536GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
520{ 537{
521 struct GNUNET_PQ_QueryParam res = { 538 struct GNUNET_PQ_QueryParam res = {
522 &qconv_abs_time, NULL, x, sizeof(*x), 1 539 .conv = &qconv_abs_time,
540 .data = x,
541 .size = sizeof(*x),
542 .num_params = 1
523 }; 543 };
524 544
525 return res; 545 return res;
@@ -534,4 +554,71 @@ GNUNET_PQ_query_param_absolute_time_nbo (
534} 554}
535 555
536 556
557/**
558 * Function called to convert input argument into SQL parameters.
559 *
560 * @param cls closure
561 * @param data pointer to input argument
562 * @param data_len number of bytes in @a data (if applicable)
563 * @param[out] param_values SQL data to set
564 * @param[out] param_lengths SQL length data to set
565 * @param[out] param_formats SQL format data to set
566 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
567 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
568 * @param scratch_length number of entries left in @a scratch
569 * @return -1 on error, number of offsets used in @a scratch otherwise
570 */
571static int
572qconv_timestamp (void *cls,
573 const void *data,
574 size_t data_len,
575 void *param_values[],
576 int param_lengths[],
577 int param_formats[],
578 unsigned int param_length,
579 void *scratch[],
580 unsigned int scratch_length)
581{
582 const struct GNUNET_TIME_Timestamp *u = data;
583 struct GNUNET_TIME_Absolute abs;
584 uint64_t *u_nbo;
585
586 GNUNET_break (NULL == cls);
587 if (1 != param_length)
588 return -1;
589 abs = u->abs_time;
590 if (abs.abs_value_us > INT64_MAX)
591 abs.abs_value_us = INT64_MAX;
592 u_nbo = GNUNET_new (uint64_t);
593 scratch[0] = u_nbo;
594 *u_nbo = GNUNET_htonll (abs.abs_value_us);
595 param_values[0] = (void *) u_nbo;
596 param_lengths[0] = sizeof(uint64_t);
597 param_formats[0] = 1;
598 return 1;
599}
600
601
602struct GNUNET_PQ_QueryParam
603GNUNET_PQ_query_param_timestamp (const struct GNUNET_TIME_Timestamp *x)
604{
605 struct GNUNET_PQ_QueryParam res = {
606 .conv = &qconv_timestamp,
607 .data = x,
608 .size = sizeof(*x),
609 .num_params = 1
610 };
611
612 return res;
613}
614
615
616struct GNUNET_PQ_QueryParam
617GNUNET_PQ_query_param_timestamp_nbo (
618 const struct GNUNET_TIME_TimestampNBO *x)
619{
620 return GNUNET_PQ_query_param_absolute_time_nbo (&x->abs_time_nbo);
621}
622
623
537/* end of pq_query_helper.c */ 624/* end of pq_query_helper.c */
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index 4057772ec..be7c85ce9 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -757,6 +757,148 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
757 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 757 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
758 */ 758 */
759static enum GNUNET_GenericReturnValue 759static enum GNUNET_GenericReturnValue
760extract_timestamp (void *cls,
761 PGresult *result,
762 int row,
763 const char *fname,
764 size_t *dst_size,
765 void *dst)
766{
767 struct GNUNET_TIME_Timestamp *udst = dst;
768 struct GNUNET_TIME_Absolute abs;
769 const int64_t *res;
770 int fnum;
771
772 (void) cls;
773 fnum = PQfnumber (result,
774 fname);
775 if (fnum < 0)
776 {
777 GNUNET_break (0);
778 return GNUNET_SYSERR;
779 }
780 if (PQgetisnull (result,
781 row,
782 fnum))
783 return GNUNET_NO;
784 GNUNET_assert (NULL != dst);
785 if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size)
786 {
787 GNUNET_break (0);
788 return GNUNET_SYSERR;
789 }
790 if (sizeof(int64_t) !=
791 PQgetlength (result,
792 row,
793 fnum))
794 {
795 GNUNET_break (0);
796 return GNUNET_SYSERR;
797 }
798 res = (int64_t *) PQgetvalue (result,
799 row,
800 fnum);
801 if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res))
802 {
803 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
804 }
805 else
806 {
807 abs.abs_value_us = GNUNET_ntohll ((uint64_t) *res);
808 if (0 != abs.abs_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us)
809 {
810 /* timestamps must be multiple of seconds! */
811 GNUNET_break (0);
812 return GNUNET_SYSERR;
813 }
814 }
815 udst->abs_time = abs;
816 return GNUNET_OK;
817}
818
819
820struct GNUNET_PQ_ResultSpec
821GNUNET_PQ_result_spec_timestamp (const char *name,
822 struct GNUNET_TIME_Timestamp *at)
823{
824 struct GNUNET_PQ_ResultSpec res = {
825 .conv = &extract_timestamp,
826 .dst = (void *) at,
827 .dst_size = sizeof(*at),
828 .fname = name
829 };
830
831 return res;
832}
833
834
835/**
836 * Extract data from a Postgres database @a result at row @a row.
837 *
838 * @param cls closure
839 * @param result where to extract data from
840 * @param int row to extract data from
841 * @param fname name (or prefix) of the fields to extract from
842 * @param[in,out] dst_size where to store size of result, may be NULL
843 * @param[out] dst where to store the result
844 * @return
845 * #GNUNET_YES if all results could be extracted
846 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
847 */
848static enum GNUNET_GenericReturnValue
849extract_timestamp_nbo (void *cls,
850 PGresult *result,
851 int row,
852 const char *fname,
853 size_t *dst_size,
854 void *dst)
855{
856 struct GNUNET_TIME_TimestampNBO *udst = dst;
857 struct GNUNET_TIME_Timestamp t;
858 enum GNUNET_GenericReturnValue r;
859
860 r = extract_timestamp (&t,
861 result,
862 row,
863 fname,
864 dst_size,
865 dst);
866 if (GNUNET_OK != r)
867 return r;
868 *udst = GNUNET_TIME_timestamp_hton (t);
869 return r;
870}
871
872
873struct GNUNET_PQ_ResultSpec
874GNUNET_PQ_result_spec_timestamp_nbo (const char *name,
875 struct GNUNET_TIME_TimestampNBO *at)
876{
877 struct GNUNET_PQ_ResultSpec res = {
878 .conv = &extract_timestamp_nbo,
879 .dst = (void *) at,
880 .dst_size = sizeof(*at),
881 .fname = name
882 };
883
884 return res;
885}
886
887
888/**
889 * Extract data from a Postgres database @a result at row @a row.
890 *
891 * @param cls closure
892 * @param result where to extract data from
893 * @param int row to extract data from
894 * @param fname name (or prefix) of the fields to extract from
895 * @param[in,out] dst_size where to store size of result, may be NULL
896 * @param[out] dst where to store the result
897 * @return
898 * #GNUNET_YES if all results could be extracted
899 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
900 */
901static enum GNUNET_GenericReturnValue
760extract_uint16 (void *cls, 902extract_uint16 (void *cls,
761 PGresult *result, 903 PGresult *result,
762 int row, 904 int row,