aboutsummaryrefslogtreecommitdiff
path: root/src/pq/pq_result_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq_result_helper.c')
-rw-r--r--src/pq/pq_result_helper.c142
1 files changed, 142 insertions, 0 deletions
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,