aboutsummaryrefslogtreecommitdiff
path: root/src/pq
diff options
context:
space:
mode:
authorÖzgür Kesim <oec-taler@kesim.org>2023-11-28 15:38:07 +0100
committerÖzgür Kesim <oec-taler@kesim.org>2023-11-28 15:38:07 +0100
commitd15fd5add80707b30b874ecb207e2dcad29692cc (patch)
tree5408975ce554b801cebb471670b113bda40c1481 /src/pq
parentfd2f18d4fbf8b84eb9444972b1660bb7712d192e (diff)
[pq] add support for array of struct GNUNET_HashCode
Diffstat (limited to 'src/pq')
-rw-r--r--src/pq/pq_common.h1
-rw-r--r--src/pq/pq_query_helper.c45
-rw-r--r--src/pq/pq_result_helper.c54
-rw-r--r--src/pq/test_pq.c52
4 files changed, 143 insertions, 9 deletions
diff --git a/src/pq/pq_common.h b/src/pq/pq_common.h
index 6172c0bfb..735528ae1 100644
--- a/src/pq/pq_common.h
+++ b/src/pq/pq_common.h
@@ -44,6 +44,7 @@ enum TALER_PQ_ArrayType
TALER_PQ_array_of_blinded_denom_sig,
TALER_PQ_array_of_blinded_coin_hash,
TALER_PQ_array_of_denom_hash,
+ TALER_PQ_array_of_hash_code,
/**
* Amounts *without* currency.
*/
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
index f66844515..24bf054da 100644
--- a/src/pq/pq_query_helper.c
+++ b/src/pq/pq_query_helper.c
@@ -846,14 +846,14 @@ qconv_array (
same_sized = (0 != meta->same_size);
#define RETURN_UNLESS(cond) \
- do { \
- if (! (cond)) \
- { \
- GNUNET_break ((cond)); \
- noerror = false; \
- goto DONE; \
- } \
- } while (0)
+ do { \
+ if (! (cond)) \
+ { \
+ GNUNET_break ((cond)); \
+ noerror = false; \
+ goto DONE; \
+ } \
+ } while (0)
/* Calculate sizes and check bounds */
{
@@ -1037,6 +1037,15 @@ qconv_array (
sizeof(struct TALER_DenominationHashP));
break;
}
+ case TALER_PQ_array_of_hash_code:
+ {
+ const struct GNUNET_HashCode *hashes = data;
+
+ GNUNET_memcpy (out,
+ &hashes[i],
+ sizeof(struct GNUNET_HashCode));
+ break;
+ }
default:
{
GNUNET_assert (0);
@@ -1171,6 +1180,26 @@ TALER_PQ_query_param_array_denom_hash (
struct GNUNET_PQ_QueryParam
+TALER_PQ_query_param_array_hash_code (
+ size_t num,
+ const struct GNUNET_HashCode *hashes,
+ struct GNUNET_PQ_Context *db)
+{
+ Oid oid;
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_PQ_get_oid_by_name (db, "gnunet_hashcode", &oid));
+ return query_param_array_generic (num,
+ true,
+ hashes,
+ NULL,
+ sizeof(struct GNUNET_HashCode),
+ TALER_PQ_array_of_hash_code,
+ oid,
+ NULL);
+}
+
+
+struct GNUNET_PQ_QueryParam
TALER_PQ_query_param_array_amount (
size_t num,
const struct TALER_Amount *amounts,
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index 9bd3fdb49..915aad144 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -1249,6 +1249,30 @@ extract_array_generic (
}
return GNUNET_OK;
+ case TALER_PQ_array_of_hash_code:
+ if (NULL != dst_size)
+ *dst_size = sizeof(struct GNUNET_HashCode) * (header.dim);
+ out = GNUNET_new_array (header.dim,
+ struct GNUNET_HashCode);
+ *((void **) dst) = out;
+ for (uint32_t i = 0; i < header.dim; i++)
+ {
+ uint32_t val;
+ size_t sz;
+
+ GNUNET_memcpy (&val,
+ in,
+ sizeof(val));
+ sz = ntohl (val);
+ FAIL_IF (sz != sizeof(struct GNUNET_HashCode));
+ in += sizeof(uint32_t);
+ *(struct GNUNET_HashCode *) out =
+ *(struct GNUNET_HashCode *) in;
+ in += sz;
+ out += sz;
+ }
+ return GNUNET_OK;
+
case TALER_PQ_array_of_blinded_coin_hash:
if (NULL != dst_size)
*dst_size = sizeof(struct TALER_BlindedCoinHashP) * (header.dim);
@@ -1504,4 +1528,34 @@ TALER_PQ_result_spec_array_amount (
}
+struct GNUNET_PQ_ResultSpec
+TALER_PQ_result_spec_array_hash_code (
+ struct GNUNET_PQ_Context *db,
+ const char *name,
+ size_t *num,
+ struct GNUNET_HashCode **hashes)
+{
+ struct ArrayResultCls *info = GNUNET_new (struct ArrayResultCls);
+
+ info->num = num;
+ info->typ = TALER_PQ_array_of_hash_code;
+ info->db = db;
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_PQ_get_oid_by_name (db,
+ "gnunet_hashcode",
+ &info->oid));
+
+ struct GNUNET_PQ_ResultSpec res = {
+ .conv = extract_array_generic,
+ .cleaner = array_cleanup,
+ .dst = (void *) hashes,
+ .fname = name,
+ .cls = info,
+ };
+ return res;
+
+
+}
+
+
/* end of pq_result_helper.c */
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 237c8a9ef..0fd2bfddf 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -21,6 +21,7 @@
#include "platform.h"
#include "taler_util.h"
#include "taler_pq_lib.h"
+#include <gnunet/gnunet_pq_lib.h>
/**
@@ -39,14 +40,18 @@ postgres_prepare (struct GNUNET_PQ_Context *db)
",json"
",aamount"
",tamountc"
+ ",hash"
+ ",hashes"
") VALUES "
- "($1, $2, $3, $4);"),
+ "($1, $2, $3, $4, $5, $6);"),
GNUNET_PQ_make_prepare ("test_select",
"SELECT"
" tamount"
",json"
",aamount"
",tamountc"
+ ",hash"
+ ",hashes"
" FROM test_pq;"),
GNUNET_PQ_PREPARED_STATEMENT_END
};
@@ -67,6 +72,20 @@ run_queries (struct GNUNET_PQ_Context *conn)
struct TALER_Amount tamount;
struct TALER_Amount aamount[3];
struct TALER_Amount tamountc;
+ struct GNUNET_HashCode hc =
+ {{0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef,
+ 0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef,
+ 0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef,
+ 0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef, }};
+ struct GNUNET_HashCode hcs[2] =
+ {{{0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f,
+ 0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f,
+ 0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f,
+ 0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f,}},
+ {{0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,
+ 0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,
+ 0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,
+ 0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,}}};
json_t *json;
GNUNET_assert (GNUNET_OK ==
@@ -100,6 +119,11 @@ run_queries (struct GNUNET_PQ_Context *conn)
conn),
TALER_PQ_query_param_amount_with_currency (conn,
&tamountc),
+ GNUNET_PQ_query_param_fixed_size (&hc,
+ sizeof (hc)),
+ TALER_PQ_query_param_array_hash_code (2,
+ hcs,
+ conn),
GNUNET_PQ_query_param_end
};
PGresult *result;
@@ -122,7 +146,10 @@ run_queries (struct GNUNET_PQ_Context *conn)
struct TALER_Amount tamount2;
struct TALER_Amount tamountc2;
struct TALER_Amount *pamount;
+ struct GNUNET_HashCode hc2;
+ struct GNUNET_HashCode *hcs2;
size_t npamount;
+ size_t nhcs;
json_t *json2;
struct GNUNET_PQ_QueryParam params_select[] = {
GNUNET_PQ_query_param_end
@@ -140,6 +167,12 @@ run_queries (struct GNUNET_PQ_Context *conn)
&pamount),
TALER_PQ_result_spec_amount_with_currency ("tamountc",
&tamountc2),
+ GNUNET_PQ_result_spec_auto_from_type ("hash",
+ &hc2),
+ TALER_PQ_result_spec_array_hash_code (conn,
+ "hashes",
+ &nhcs,
+ &hcs2),
GNUNET_PQ_result_spec_end
};
@@ -168,6 +201,13 @@ run_queries (struct GNUNET_PQ_Context *conn)
GNUNET_break (0 ==
TALER_amount_cmp (&tamountc,
&tamountc2));
+ GNUNET_break (0 == GNUNET_memcmp (&hc,&hc2));
+ for (size_t i = 0; i < 2; i++)
+ {
+ GNUNET_break (0 ==
+ GNUNET_memcmp (&hcs[i],
+ &hcs2[i]));
+ }
GNUNET_PQ_cleanup_result (results_select);
}
return 0;
@@ -181,6 +221,14 @@ main (int argc,
struct GNUNET_PQ_ExecuteStatement es[] = {
GNUNET_PQ_make_execute ("DO $$ "
" BEGIN"
+ " CREATE DOMAIN gnunet_hashcode AS BYTEA"
+ " CHECK(length(VALUE)=64);"
+ " EXCEPTION"
+ " WHEN duplicate_object THEN null;"
+ " END "
+ "$$;"),
+ GNUNET_PQ_make_execute ("DO $$ "
+ " BEGIN"
" CREATE TYPE taler_amount AS"
" (val INT8, frac INT4);"
" EXCEPTION"
@@ -200,6 +248,8 @@ main (int argc,
",json VARCHAR NOT NULL"
",aamount taler_amount[]"
",tamountc taler_amount_currency"
+ ",hash gnunet_hashcode"
+ ",hashes gnunet_hashcode[]"
")"),
GNUNET_PQ_EXECUTE_STATEMENT_END
};