From: Christian Grothoff Date: Fri, 17 Apr 2020 17:13:42 +0000 (+0200) Subject: add relative_time specs X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=3695a510a18d0bb9ed58fb5270856a88853b4e30;p=oweals%2Fgnunet.git add relative_time specs --- diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h index 12ec19d7e..ca549f77c 100644 --- a/src/include/gnunet_pq_lib.h +++ b/src/include/gnunet_pq_lib.h @@ -151,6 +151,16 @@ GNUNET_PQ_query_param_rsa_signature ( const struct GNUNET_CRYPTO_RsaSignature *x); +/** + * Generate query parameter for a relative time value. + * The database must store a 64-bit integer. + * + * @param x pointer to the query parameter to pass + */ +struct GNUNET_PQ_QueryParam +GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x); + + /** * Generate query parameter for an absolute time value. * The database must store a 64-bit integer. @@ -376,6 +386,18 @@ GNUNET_PQ_result_spec_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at); +/** + * Relative time expected. + * + * @param name name of the field in the table + * @param[out] rt where to store the result + * @return array entry for the result specification to use + */ +struct GNUNET_PQ_ResultSpec +GNUNET_PQ_result_spec_relative_time (const char *name, + struct GNUNET_TIME_Relative *rt); + + /** * Absolute time expected. * 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 @@ -387,6 +387,68 @@ GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) } +/** + * Function called to convert input argument into SQL parameters. + * + * @param cls closure + * @param data pointer to input argument + * @param data_len number of bytes in @a data (if applicable) + * @param[out] param_values SQL data to set + * @param[out] param_lengths SQL length data to set + * @param[out] param_formats SQL format data to set + * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays + * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc() + * @param scratch_length number of entries left in @a scratch + * @return -1 on error, number of offsets used in @a scratch otherwise + */ +static int +qconv_rel_time (void *cls, + const void *data, + size_t data_len, + void *param_values[], + int param_lengths[], + int param_formats[], + unsigned int param_length, + void *scratch[], + unsigned int scratch_length) +{ + const struct GNUNET_TIME_Relative *u = data; + struct GNUNET_TIME_Relative rel; + uint64_t *u_nbo; + + GNUNET_break (NULL == cls); + if (1 != param_length) + return -1; + rel = *u; + if (rel.rel_value_us > INT64_MAX) + rel.rel_value_us = INT64_MAX; + u_nbo = GNUNET_new (uint64_t); + scratch[0] = u_nbo; + *u_nbo = GNUNET_htonll (rel.rel_value_us); + param_values[0] = (void *) u_nbo; + param_lengths[0] = sizeof(uint64_t); + param_formats[0] = 1; + return 1; +} + + +/** + * Generate query parameter for a relative time value. + * The database must store a 64-bit integer. + * + * @param x pointer to the query parameter to pass + * @return array entry for the query parameters to use + */ +struct GNUNET_PQ_QueryParam +GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x) +{ + struct GNUNET_PQ_QueryParam res = + { &qconv_rel_time, NULL, x, sizeof(*x), 1 }; + + return res; +} + + /** * Function called to convert input argument into SQL parameters. * 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 @@ -530,6 +530,96 @@ GNUNET_PQ_result_spec_string (const char *name, } +/** + * Extract data from a Postgres database @a result at row @a row. + * + * @param cls closure + * @param result where to extract data from + * @param int row to extract data from + * @param fname name (or prefix) of the fields to extract from + * @param[in,out] dst_size where to store size of result, may be NULL + * @param[out] dst where to store the result + * @return + * #GNUNET_YES if all results could be extracted + * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) + */ +static int +extract_rel_time (void *cls, + PGresult *result, + int row, + const char *fname, + size_t *dst_size, + void *dst) +{ + struct GNUNET_TIME_Relative *udst = dst; + const int64_t *res; + int fnum; + + (void) cls; + fnum = PQfnumber (result, + fname); + if (fnum < 0) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (PQgetisnull (result, + row, + fnum)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + GNUNET_assert (NULL != dst); + if (sizeof(struct GNUNET_TIME_Relative) != *dst_size) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (sizeof(int64_t) != + PQgetlength (result, + row, + fnum)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + res = (int64_t *) PQgetvalue (result, + row, + fnum); + if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res)) + *udst = GNUNET_TIME_UNIT_FOREVER_REL; + else + udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res); + return GNUNET_OK; +} + + +/** + * Relative time expected. + * + * @param name name of the field in the table + * @param[out] at where to store the result + * @return array entry for the result specification to use + */ +struct GNUNET_PQ_ResultSpec +GNUNET_PQ_result_spec_relative_time (const char *name, + struct GNUNET_TIME_Relative *rt) +{ + struct GNUNET_PQ_ResultSpec res = { + &extract_rel_time, + NULL, + NULL, + (void *) rt, + sizeof(*rt), + name, + NULL + }; + + return res; +} + + /** * Extract data from a Postgres database @a result at row @a row. *