From 571d43cef3732ed6f491d91a9e767a80008edeb1 Mon Sep 17 00:00:00 2001 From: Özgür Kesim Date: Thu, 27 Jul 2023 23:57:07 +0200 Subject: [WiP] added TALER_AMOUNT type to Postgres - first in age_withdraw - Added a type TALER_AMOUNT (val INT8, frac INT4) to Postgres. - Added PLSQL functions/procedures - amount_normalize(a) - amount_add(a, b) - amount_left_minus_right(l, r, diff, ok bool) - Added PQ-helper functions - TALER_PQ_query_param_amount_tuple() - TALER_PQ_result_spec_amount_tuple() - In table 'age_withdraw', changed fields 'amount_with_fee_val' and '..._frac' into single field 'amount_with_fee' be of type TALER_AMOUNT - Changed functions/stored procedures 'do_age_withdraw' and 'get_age_withdraw' to use new APIs. => make check runs through without errors, age-withdraw and -reveal test passes. --- src/pq/pq_query_helper.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) (limited to 'src/pq/pq_query_helper.c') diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c index 9a02cdda..9ada2357 100644 --- a/src/pq/pq_query_helper.c +++ b/src/pq/pq_query_helper.c @@ -21,6 +21,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include #include #include #include "taler_pq_lib.h" @@ -149,6 +150,84 @@ TALER_PQ_query_param_amount (const struct TALER_Amount *x) } +/** + * Function called to convert input amount into SQL parameter as tuple. + * + * @param cls closure + * @param data pointer to input argument, here a `struct TALER_Amount` + * @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_amount_tuple (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_PQ_Context *db = cls; + const struct TALER_Amount *amount = data; + size_t sz; + + GNUNET_assert (NULL != db); + GNUNET_assert (NULL != amount); + GNUNET_assert (1 == param_length); + GNUNET_assert (1 <= scratch_length); + GNUNET_assert (sizeof (struct TALER_Amount) == data_len); + GNUNET_static_assert (sizeof(uint32_t) == sizeof(Oid)); + + { + char *out; + struct TALER_PQ_Amount_P d = MAKE_TALER_PQ_AMOUNT_P (db, amount); + + sz = sizeof(uint32_t); /* number of elements in tuple */ + sz += sizeof(d); + + out = GNUNET_malloc (sz); + scratch[0] = out; + + *(uint32_t *) out = htonl (2); + out += sizeof(uint32_t); + + *(struct TALER_PQ_Amount_P*) out = d; + + } + + param_values[0] = scratch[0]; + param_lengths[0] = sz; + param_formats[0] = 1; + + return 1; +} + + +struct GNUNET_PQ_QueryParam +TALER_PQ_query_param_amount_tuple ( + const struct GNUNET_PQ_Context *db, + const struct TALER_Amount *amount) +{ + struct GNUNET_PQ_QueryParam res = { + .conv_cls = (void *) db, + .conv = &qconv_amount_tuple, + .data = amount, + .size = sizeof (*amount), + .num_params = 1, + }; + + return res; +} + + /** * Function called to convert input argument into SQL parameters. * @@ -793,7 +872,7 @@ qconv_array ( RETURN_UNLESS ((0 == num) || (y / num == x)); /* size of header */ - total_size = x = sizeof(struct TALER_PQ_ArrayHeader); + total_size = x = sizeof(struct TALER_PQ_ArrayHeader_P); total_size += y; RETURN_UNLESS (total_size >= x); @@ -862,7 +941,7 @@ qconv_array ( /* Write data */ { char *out = elements; - struct TALER_PQ_ArrayHeader h = { + struct TALER_PQ_ArrayHeader_P h = { .ndim = htonl (1), /* We only support one-dimensional arrays */ .has_null = htonl (0), /* We do not support NULL entries in arrays */ .lbound = htonl (1), /* Default start index value */ -- cgit v1.2.3