aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2024-03-08 22:17:45 +0100
committerChristian Grothoff <christian@grothoff.org>2024-03-08 22:19:08 +0100
commit6c81dc288274027abcd8903764be401e3e43339c (patch)
tree29884bb2c8d276264bf100e8d3f6df83892a75bf
parent517cad11b8f63beb001ad0d3634fad88269e679f (diff)
downloadgnunet-6c81dc288274027abcd8903764be401e3e43339c.tar.gz
gnunet-6c81dc288274027abcd8903764be401e3e43339c.zip
PQ: add int64 data type support
m---------contrib/handbook0
-rw-r--r--src/include/gnunet_pq_lib.h25
-rw-r--r--src/lib/pq/pq_query_helper.c57
-rw-r--r--src/lib/pq/pq_result_helper.c100
-rw-r--r--src/lib/util/gnunet_error_codes.c1
5 files changed, 173 insertions, 10 deletions
diff --git a/contrib/handbook b/contrib/handbook
Subproject f9368db2bf8ce9eb6a9e951891142309c9c898d Subproject f88a389426a665df222486b0563e5a91e77a704
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index 3d9c12cb2..b5d39ed41 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -577,7 +577,7 @@ GNUNET_PQ_query_param_uint32 (const uint32_t *x);
577 577
578 578
579/** 579/**
580 * Generate query parameter for an uint16_t in host byte order. 580 * Generate query parameter for an uint64_t in host byte order.
581 * 581 *
582 * @param x pointer to the query parameter to pass 582 * @param x pointer to the query parameter to pass
583 * @return query parameter to use 583 * @return query parameter to use
@@ -586,6 +586,16 @@ struct GNUNET_PQ_QueryParam
586GNUNET_PQ_query_param_uint64 (const uint64_t *x); 586GNUNET_PQ_query_param_uint64 (const uint64_t *x);
587 587
588 588
589/**
590 * Generate query parameter for an int64_t in host byte order.
591 *
592 * @param x pointer to the query parameter to pass
593 * @return query parameter to use
594 */
595struct GNUNET_PQ_QueryParam
596GNUNET_PQ_query_param_int64 (const int64_t *x);
597
598
589/* ************************* pq_result_helper.c functions ************************ */ 599/* ************************* pq_result_helper.c functions ************************ */
590 600
591 601
@@ -895,6 +905,19 @@ struct GNUNET_PQ_ResultSpec
895GNUNET_PQ_result_spec_uint64 (const char *name, 905GNUNET_PQ_result_spec_uint64 (const char *name,
896 uint64_t *u64); 906 uint64_t *u64);
897 907
908
909/**
910 * int64_t expected.
911 *
912 * @param name name of the field in the table
913 * @param[out] i64 where to store the result
914 * @return array entry for the result specification to use
915 */
916struct GNUNET_PQ_ResultSpec
917GNUNET_PQ_result_spec_int64 (const char *name,
918 int64_t *i64);
919
920
898/** 921/**
899 * array of bool expected. 922 * array of bool expected.
900 * 923 *
diff --git a/src/lib/pq/pq_query_helper.c b/src/lib/pq/pq_query_helper.c
index 1e2f7a965..211c35076 100644
--- a/src/lib/pq/pq_query_helper.c
+++ b/src/lib/pq/pq_query_helper.c
@@ -338,6 +338,63 @@ GNUNET_PQ_query_param_uint64 (const uint64_t *x)
338 * @return -1 on error, number of offsets used in @a scratch otherwise 338 * @return -1 on error, number of offsets used in @a scratch otherwise
339 */ 339 */
340static int 340static int
341qconv_int64 (void *cls,
342 const void *data,
343 size_t data_len,
344 void *param_values[],
345 int param_lengths[],
346 int param_formats[],
347 unsigned int param_length,
348 void *scratch[],
349 unsigned int scratch_length)
350{
351 const int64_t *u_hbo = data;
352 int64_t *u_nbo;
353
354 (void) scratch;
355 (void) scratch_length;
356 GNUNET_break (NULL == cls);
357 if (1 != param_length)
358 return -1;
359 u_nbo = GNUNET_new (int64_t);
360 scratch[0] = u_nbo;
361 *u_nbo = GNUNET_htonll (*u_hbo);
362 param_values[0] = (void *) u_nbo;
363 param_lengths[0] = sizeof(int64_t);
364 param_formats[0] = 1;
365 return 1;
366}
367
368
369struct GNUNET_PQ_QueryParam
370GNUNET_PQ_query_param_int64 (const int64_t *x)
371{
372 struct GNUNET_PQ_QueryParam res = {
373 .conv = &qconv_int64,
374 .data = x,
375 .size = sizeof(*x),
376 .num_params = 1
377 };
378
379 return res;
380}
381
382
383/**
384 * Function called to convert input argument into SQL parameters.
385 *
386 * @param cls closure
387 * @param data pointer to input argument
388 * @param data_len number of bytes in @a data (if applicable)
389 * @param[out] param_values SQL data to set
390 * @param[out] param_lengths SQL length data to set
391 * @param[out] param_formats SQL format data to set
392 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
393 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
394 * @param scratch_length number of entries left in @a scratch
395 * @return -1 on error, number of offsets used in @a scratch otherwise
396 */
397static int
341qconv_rsa_public_key (void *cls, 398qconv_rsa_public_key (void *cls,
342 const void *data, 399 const void *data,
343 size_t data_len, 400 size_t data_len,
diff --git a/src/lib/pq/pq_result_helper.c b/src/lib/pq/pq_result_helper.c
index f230826cb..6210746bb 100644
--- a/src/lib/pq/pq_result_helper.c
+++ b/src/lib/pq/pq_result_helper.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016, 2020 GNUnet e.V. 3 Copyright (C) 2014-2024 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -1122,6 +1122,90 @@ GNUNET_PQ_result_spec_uint64 (const char *name,
1122 1122
1123 1123
1124/** 1124/**
1125 * Extract data from a Postgres database @a result at row @a row.
1126 *
1127 * @param cls closure
1128 * @param result where to extract data from
1129 * @param row row to extract data from
1130 * @param fname name (or prefix) of the fields to extract from
1131 * @param[in,out] dst_size where to store size of result, may be NULL
1132 * @param[out] dst where to store the result
1133 * @return
1134 * #GNUNET_YES if all results could be extracted
1135 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
1136 */
1137static enum GNUNET_GenericReturnValue
1138extract_int64 (void *cls,
1139 PGresult *result,
1140 int row,
1141 const char *fname,
1142 size_t *dst_size,
1143 void *dst)
1144{
1145 int64_t *udst = dst;
1146 const int64_t *res;
1147 int fnum;
1148
1149 (void) cls;
1150 fnum = PQfnumber (result,
1151 fname);
1152 if (fnum < 0)
1153 {
1154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1155 "Field %s missing in result\n",
1156 fname);
1157 GNUNET_break (0);
1158 return GNUNET_SYSERR;
1159 }
1160 if (PQgetisnull (result,
1161 row,
1162 fnum))
1163 return GNUNET_NO;
1164
1165 GNUNET_assert (NULL != dst);
1166 if (sizeof(int64_t) != *dst_size)
1167 {
1168 GNUNET_break (0);
1169 return GNUNET_SYSERR;
1170 }
1171 if (sizeof(int64_t) !=
1172 PQgetlength (result,
1173 row,
1174 fnum))
1175 {
1176 GNUNET_break (0);
1177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1178 "Got length %u for field `%s'\n",
1179 PQgetlength (result,
1180 row,
1181 fnum),
1182 fname);
1183 return GNUNET_SYSERR;
1184 }
1185 res = (int64_t *) PQgetvalue (result,
1186 row,
1187 fnum);
1188 *udst = GNUNET_ntohll (*res);
1189 return GNUNET_OK;
1190}
1191
1192
1193struct GNUNET_PQ_ResultSpec
1194GNUNET_PQ_result_spec_int64 (const char *name,
1195 int64_t *i64)
1196{
1197 struct GNUNET_PQ_ResultSpec res = {
1198 .conv = &extract_int64,
1199 .dst = (void *) i64,
1200 .dst_size = sizeof(*i64),
1201 .fname = name
1202 };
1203
1204 return res;
1205}
1206
1207
1208/**
1125 * Closure for the array result specifications. Contains type information 1209 * Closure for the array result specifications. Contains type information
1126 * for the generic parser extract_array_generic and out-pointers for the results. 1210 * for the generic parser extract_array_generic and out-pointers for the results.
1127 */ 1211 */
@@ -1180,13 +1264,13 @@ extract_array_generic (
1180 *((void **) dst) = NULL; 1264 *((void **) dst) = NULL;
1181 1265
1182 #define FAIL_IF(cond) \ 1266 #define FAIL_IF(cond) \
1183 do { \ 1267 do { \
1184 if ((cond)) \ 1268 if ((cond)) \
1185 { \ 1269 { \
1186 GNUNET_break (! (cond)); \ 1270 GNUNET_break (! (cond)); \
1187 goto FAIL; \ 1271 goto FAIL; \
1188 } \ 1272 } \
1189 } while (0) 1273 } while (0)
1190 1274
1191 col_num = PQfnumber (result, fname); 1275 col_num = PQfnumber (result, fname);
1192 FAIL_IF (0 > col_num); 1276 FAIL_IF (0 > col_num);
diff --git a/src/lib/util/gnunet_error_codes.c b/src/lib/util/gnunet_error_codes.c
index c286f2e52..11ce2d0c8 100644
--- a/src/lib/util/gnunet_error_codes.c
+++ b/src/lib/util/gnunet_error_codes.c
@@ -17,7 +17,6 @@
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20#include "platform.h"
21#include "gnunet_error_codes.h" 20#include "gnunet_error_codes.h"
22#include <stddef.h> 21#include <stddef.h>
23#include <microhttpd.h> 22#include <microhttpd.h>