aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_pq_lib.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/gnunet_pq_lib.h')
-rw-r--r--src/include/gnunet_pq_lib.h676
1 files changed, 636 insertions, 40 deletions
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index 2263704aa..7501bee12 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2016, 2017, 2020, 2021 GNUnet e.V. 3 Copyright (C) 2016, 2017, 2020-2022 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
@@ -25,9 +25,19 @@
25#ifndef GNUNET_PQ_LIB_H 25#ifndef GNUNET_PQ_LIB_H
26#define GNUNET_PQ_LIB_H 26#define GNUNET_PQ_LIB_H
27 27
28
28#include <libpq-fe.h> 29#include <libpq-fe.h>
30#include <stdint.h>
31#include "gnunet_common.h"
32#include "gnunet_time_lib.h"
29#include "gnunet_util_lib.h" 33#include "gnunet_util_lib.h"
30#include "gnunet_db_lib.h" 34#include "gnunet_db_lib.h"
35#include "postgres_ext.h"
36
37/**
38 * Postgres context.
39 */
40struct GNUNET_PQ_Context;
31 41
32/* ************************* pq_query_helper.c functions ************************ */ 42/* ************************* pq_query_helper.c functions ************************ */
33 43
@@ -57,6 +67,14 @@ typedef int
57 void *scratch[], 67 void *scratch[],
58 unsigned int scratch_length); 68 unsigned int scratch_length);
59 69
70/**
71 * Function called to cleanup the closure of SQL parameters converter. Will be
72 * called with GNUNET_PQ_query_params_cleanup
73 *
74 * @param cls closure
75 */
76typedef void
77(*GNUNET_PQ_QueryConverter_Cleanup)(void *cls);
60 78
61/** 79/**
62 * @brief Description of a DB query parameter. 80 * @brief Description of a DB query parameter.
@@ -70,10 +88,20 @@ struct GNUNET_PQ_QueryParam
70 88
71 /** 89 /**
72 * Closure for @e conv. 90 * Closure for @e conv.
91 * The function @conv_cls_cleanup can be set to cleanup the closure.
92 * The cleanup of the closure MUST be triggered manually by a call to
93 * GNUNET_PQ_query_params_cleanup.
73 */ 94 */
74 void *conv_cls; 95 void *conv_cls;
75 96
76 /** 97 /**
98 * Function to cleanup the closure @a conv_cls, may be NULL.
99 * The cleanup of the closure MUST be triggered manually by a call to
100 * GNUNET_PQ_query_params_cleanup.
101 */
102 GNUNET_PQ_QueryConverter_Cleanup conv_cls_cleanup;
103
104 /**
77 * Data or NULL. 105 * Data or NULL.
78 */ 106 */
79 const void *data; 107 const void *data;
@@ -90,6 +118,15 @@ struct GNUNET_PQ_QueryParam
90 118
91}; 119};
92 120
121/**
122 * Must be called to cleanup memory from closures after the query parameters
123 * have been used as much as needed.
124 *
125 * @param params Array of GNUNET_PQ_QueryParam which must terminate with a GNUNET_PQ_query_param_end
126 */
127void
128GNUNET_PQ_cleanup_query_params_closures (
129 const struct GNUNET_PQ_QueryParam *params);
93 130
94/** 131/**
95 * End of query parameter specification. 132 * End of query parameter specification.
@@ -118,7 +155,7 @@ GNUNET_PQ_query_param_null (void);
118 * @a ptr_size bytes. 155 * @a ptr_size bytes.
119 * 156 *
120 * @param ptr pointer to the query parameter to pass 157 * @param ptr pointer to the query parameter to pass
121 * @oaran ptr_size number of bytes in @a ptr 158 * @param ptr_size number of bytes in @a ptr
122 * @return query parameter to use 159 * @return query parameter to use
123 */ 160 */
124struct GNUNET_PQ_QueryParam 161struct GNUNET_PQ_QueryParam
@@ -145,6 +182,210 @@ GNUNET_PQ_query_param_string (const char *ptr);
145struct GNUNET_PQ_QueryParam 182struct GNUNET_PQ_QueryParam
146GNUNET_PQ_query_param_bool (bool b); 183GNUNET_PQ_query_param_bool (bool b);
147 184
185/**
186 * Returns the oid for a given datatype by name.
187 *
188 * @param db The db-connection
189 * @param name The name of the datatype
190 * @param[out] oid The OID of the datatype.
191 * @return GNUNET_OK when the datatype was found, GNUNET_SYSERR otherwise
192 */
193enum GNUNET_GenericReturnValue
194GNUNET_PQ_get_oid_by_name (
195 struct GNUNET_PQ_Context *db,
196 const char *name,
197 Oid *oid);
198
199
200/**
201 * The header for a postgresql array in binary format. note that this a
202 * simplified special case of the general structure (which contains pointers),
203 * as we only support one-dimensional arrays.
204 *
205 * Note that all values must be in network-byte-order.
206 */
207struct GNUNET_PQ_ArrayHeader_P
208{
209 uint32_t ndim; /* number of dimensions. we only support ndim = 1 */
210 uint32_t has_null;
211 uint32_t oid; /* oid of the elements */
212 uint32_t dim; /* size of the array */
213 uint32_t lbound; /* index value of first element in the db (default: 1). */
214} __attribute__((packed));
215
216
217/**
218 * Generate query parameter for an array of bool in host byte order.
219 *
220 * @param num Number of elements in @a elements
221 * @param elements Continuous array of @a num boolean elements
222 * @param db Database context, needed for database-depending encoding of @a elements
223 * @return query parameter to use
224 */
225struct GNUNET_PQ_QueryParam
226GNUNET_PQ_query_param_array_bool (
227 unsigned int num,
228 const bool *elements,
229 struct GNUNET_PQ_Context *db);
230
231/**
232 * Generate query parameter for an array of uint16_t in host byte order.
233 * Note that the (unsigend) elements are not checked to wrap over INT2_MAX
234 *
235 * @param num Number of elements in @a elements
236 * @param elements Continuous array of @a num uint16 elements
237 * @param db Database context, needed for database-depending encoding of @a elements
238 * @return query parameter to use
239 */
240struct GNUNET_PQ_QueryParam
241GNUNET_PQ_query_param_array_uint16 (
242 unsigned int num,
243 const uint16_t *elements,
244 struct GNUNET_PQ_Context *db);
245
246/**
247 * Generate query parameter for an array of uint32_t in host byte order.
248 * Note that the (unsigend) elements are not checked to wrap over INT4_MAX
249 *
250 * @param num Number of elements in @a elements
251 * @param elements Continuous Array of @a num uint32_t elements
252 * @param db Database context, needed for database-depending encoding of @a elements
253 * @return query parameter to use
254 */
255struct GNUNET_PQ_QueryParam
256GNUNET_PQ_query_param_array_uint32 (
257 unsigned int num,
258 const uint32_t *elements,
259 struct GNUNET_PQ_Context *db);
260
261/**
262 * Generate query parameter for an array of uint64 in host byte order.
263 * Note that the (unsigend) elements are not checked to wrap over INT8_MAX
264 *
265 * @param num Number of elements in @a elements
266 * @param elements Continuous array of @a num uint64_t elements
267 * @param db Database context, needed for database-depending encoding of @a elements
268 * @return query parameter to use
269 */
270struct GNUNET_PQ_QueryParam
271GNUNET_PQ_query_param_array_uint64 (
272 unsigned int num,
273 const uint64_t *elements,
274 struct GNUNET_PQ_Context *db);
275
276/**
277 * Generate query parameter for an array of buffers @a elements, each of
278 * corresponding size given in @a sizes.
279 *
280 * @param num Number of elements in @a elements
281 * @param elements Continous array of @a num buffers, each of corresponding size given in @a sizes.
282 * @param sizes Pointer to sizes in bytes of each element in @a elements
283 * @param db Database context, needed for database-depending encoding of @a elements
284 * @return query parameter to use
285 */
286struct GNUNET_PQ_QueryParam
287GNUNET_PQ_query_param_array_bytes (
288 unsigned int num,
289 const void *elements,
290 const size_t *sizes,
291 struct GNUNET_PQ_Context *db);
292
293/**
294 * Generate query parameter for an array of buffers @a elements,
295 * each of the same size @a size.
296 *
297 * @param num Number of elements in @a elements
298 * @param elements Continous array of @a num buffers, each with the same size @a same_size
299 * @param same_size Size in bytes of each element in @a elements
300 * @param db Database context, needed for database-depending encoding of @a elements
301 * @return query parameter to use
302 */
303struct GNUNET_PQ_QueryParam
304GNUNET_PQ_query_param_array_bytes_same_size (
305 unsigned int num,
306 const void *elements,
307 size_t same_size,
308 struct GNUNET_PQ_Context *db);
309
310/**
311 * Generate array of equal-sized query parameter with size determined
312 * by variable type from a pointer to an continuous array.
313 *
314 * @param num Number of elements in @a elements
315 * @param elements Continuous array of the query parameter to pass.
316 * @param db Database context, needed for database-depending encoding of @a elements
317 * @return query parameter to use
318 */
319#define GNUNET_PQ_query_param_array_auto_from_type(num, elements, db) \
320 GNUNET_PQ_query_param_array_bytes_same_size ((num), \
321 (elements), \
322 sizeof(*(elements)), \
323 (db))
324
325/**
326 * Generate query parameter for an array of pointers to buffers @a elements,
327 * each of the same size @a size.
328 *
329 * @param num Number of elements in @a elements
330 * @param elements Array of @a num pointers to buffers, each with the same size @a same_size
331 * @param same_size Size in bytes of each element in @a elements
332 * @param db Database context, needed for database-depending encoding of @a elements
333 * @return query parameter to use
334 */
335struct GNUNET_PQ_QueryParam
336GNUNET_PQ_query_param_array_ptrs_bytes_same_size (
337 unsigned int num,
338 const void *elements[static num],
339 size_t same_size,
340 struct GNUNET_PQ_Context *db);
341
342
343/**
344 * Generate array of equal-sized query parameter with size determined by
345 * variable type from a pointer to an array of pointers to objects of the same
346 * type.
347 *
348 * @param num Number of elements in @a elements
349 * @param elements Continuous array of the query parameter to pass.
350 * @param db Database context, needed for database-depending encoding of @a elements
351 * @return query parameter to use
352 */
353#define GNUNET_PQ_query_param_array_ptrs_auto_from_type(num, elements, db) \
354 GNUNET_PQ_query_param_array_ptrs_bytes_same_size ((num), \
355 (const void **) (elements), \
356 sizeof(*(elements[0])), \
357 (db))
358
359
360/**
361 * Generate query parameter for an array of strings (continuous)
362 *
363 * @param num Number of elements in @a elements
364 * @param elements Array of @a num continuous strings (zero-terminated each)
365 * @param db Database context, needed for database-depending encoding of @a elements
366 * @return query parameter to use
367 */
368struct GNUNET_PQ_QueryParam
369GNUNET_PQ_query_param_array_string (
370 unsigned int num,
371 const char *elements,
372 struct GNUNET_PQ_Context *db);
373
374
375/**
376 * Generate query parameter for an array of strings (pointers)
377 *
378 * @param num Number of elements in @a elements
379 * @param elements Array of @a num pointer to strings
380 * @param db Database context, needed for database-depending encoding of @a elements
381 * @return query parameter to use
382 */
383struct GNUNET_PQ_QueryParam
384GNUNET_PQ_query_param_array_ptrs_string (
385 unsigned int num,
386 const char *elements[static num],
387 struct GNUNET_PQ_Context *db);
388
148 389
149/** 390/**
150 * Generate fixed-size query parameter with size determined 391 * Generate fixed-size query parameter with size determined
@@ -156,6 +397,83 @@ GNUNET_PQ_query_param_bool (bool b);
156#define GNUNET_PQ_query_param_auto_from_type(x) \ 397#define GNUNET_PQ_query_param_auto_from_type(x) \
157 GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x))) 398 GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
158 399
400/**
401 * Generate query parameter for an array of absolute time stamps (continuous)
402 *
403 * @param num Number of elements in @a elements
404 * @param elements Array of @a num absolute timestamps
405 * @return query parameter to use
406 */
407struct GNUNET_PQ_QueryParam
408GNUNET_PQ_query_param_array_abs_time (
409 unsigned int num,
410 const struct GNUNET_TIME_Absolute *elements,
411 struct GNUNET_PQ_Context *db);
412
413/**
414 * Generate query parameter for an array of absolute time stamps (pointers)
415 *
416 * @param num Number of elements in @a elements
417 * @param elements Array of @a num pointers to absolute timestamps
418 * @return query parameter to use
419 */
420struct GNUNET_PQ_QueryParam
421GNUNET_PQ_query_param_array_ptrs_abs_time (
422 unsigned int num,
423 const struct GNUNET_TIME_Absolute *elements[],
424 struct GNUNET_PQ_Context *db);
425
426/**
427 * Generate query parameter for an array of relative time stamps (continuous)
428 *
429 * @param num Number of elements in @a elements
430 * @param elements Array of @a num relative timestamps
431 * @return query parameter to use
432 */
433struct GNUNET_PQ_QueryParam
434GNUNET_PQ_query_param_array_rel_time (
435 unsigned int num,
436 const struct GNUNET_TIME_Relative *elements,
437 struct GNUNET_PQ_Context *db);
438
439/**
440 * Generate query parameter for an array of relative time stamps (pointers)
441 *
442 * @param num Number of elements in @a elements
443 * @param elements Array of @a num pointers to relative timestamps
444 * @return query parameter to use
445 */
446struct GNUNET_PQ_QueryParam
447GNUNET_PQ_query_param_array_ptrs_rel_time (
448 unsigned int num,
449 const struct GNUNET_TIME_Relative *elements[],
450 struct GNUNET_PQ_Context *db);
451
452/**
453 * Generate query parameter for an array of time stamps (continuous)
454 *
455 * @param num Number of elements in @a elements
456 * @param elements Array of @a num timestamps
457 * @return query parameter to use
458 */
459struct GNUNET_PQ_QueryParam
460GNUNET_PQ_query_param_array_timestamp (
461 unsigned int num,
462 const struct GNUNET_TIME_Timestamp *elements,
463 struct GNUNET_PQ_Context *db);
464
465/**
466 * Generate query parameter for an array of time stamps (pointers)
467 *
468 * @param num Number of elements in @a elements
469 * @param elements Array of @a num pointers to relative timestamps
470 * @return query parameter to use
471 */
472struct GNUNET_PQ_QueryParam
473GNUNET_PQ_query_param_array_ptrs_timestamp (
474 unsigned int num,
475 const struct GNUNET_TIME_Timestamp *elements[],
476 struct GNUNET_PQ_Context *db);
159 477
160/** 478/**
161 * Generate query parameter for an RSA public key. The 479 * Generate query parameter for an RSA public key. The
@@ -235,7 +553,7 @@ GNUNET_PQ_query_param_absolute_time_nbo (
235 */ 553 */
236struct GNUNET_PQ_QueryParam 554struct GNUNET_PQ_QueryParam
237GNUNET_PQ_query_param_timestamp_nbo ( 555GNUNET_PQ_query_param_timestamp_nbo (
238 const struct GNUNET_TIME_TimestampNBO *t); 556 const struct GNUNET_TIME_TimestampNBO *x);
239 557
240 558
241/** 559/**
@@ -259,7 +577,7 @@ GNUNET_PQ_query_param_uint32 (const uint32_t *x);
259 577
260 578
261/** 579/**
262 * Generate query parameter for an uint16_t in host byte order. 580 * Generate query parameter for an uint64_t in host byte order.
263 * 581 *
264 * @param x pointer to the query parameter to pass 582 * @param x pointer to the query parameter to pass
265 * @return query parameter to use 583 * @return query parameter to use
@@ -268,6 +586,37 @@ struct GNUNET_PQ_QueryParam
268GNUNET_PQ_query_param_uint64 (const uint64_t *x); 586GNUNET_PQ_query_param_uint64 (const uint64_t *x);
269 587
270 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/**
599 * Generate query parameter for a blind sign public key.
600 * Internally, the various attributes of the public key
601 * will be serialized into on variable-size BLOB.
602 *
603 * @param pub pointer to the query parameter to pass
604 *
605 */
606struct GNUNET_PQ_QueryParam
607GNUNET_PQ_query_param_blind_sign_pub (
608 const struct GNUNET_CRYPTO_BlindSignPublicKey *pub);
609
610/**
611 * Generate query parameter for a blind sign private key of variable size.
612 *
613 * @param priv pointer to the query parameter to pass
614 *
615 */
616struct GNUNET_PQ_QueryParam
617GNUNET_PQ_query_param_blind_sign_priv (
618 const struct GNUNET_CRYPTO_BlindSignPrivateKey *priv);
619
271/* ************************* pq_result_helper.c functions ************************ */ 620/* ************************* pq_result_helper.c functions ************************ */
272 621
273 622
@@ -317,8 +666,7 @@ struct GNUNET_PQ_ResultSpec
317 GNUNET_PQ_ResultConverter conv; 666 GNUNET_PQ_ResultConverter conv;
318 667
319 /** 668 /**
320 * Function to clean up result data, NULL if cleanup is 669 * Function to clean up result data, NULL if cleanup is not necessary.
321 * not necessary.
322 */ 670 */
323 GNUNET_PQ_ResultCleanup cleaner; 671 GNUNET_PQ_ResultCleanup cleaner;
324 672
@@ -333,9 +681,9 @@ struct GNUNET_PQ_ResultSpec
333 void *dst; 681 void *dst;
334 682
335 /** 683 /**
336 * Allowed size for the data, 0 for variable-size 684 * Allowed size for the data, 0 for variable-size (in this case, the type of
337 * (in this case, the type of @e dst is a `void **` 685 * @e dst is a `void **` and we need to allocate a buffer of the right
338 * and we need to allocate a buffer of the right size). 686 * size).
339 */ 687 */
340 size_t dst_size; 688 size_t dst_size;
341 689
@@ -355,10 +703,8 @@ struct GNUNET_PQ_ResultSpec
355 bool is_nullable; 703 bool is_nullable;
356 704
357 /** 705 /**
358 * Points to a location where we should store 706 * Points to a location where we should store "true" if the result found is
359 * "true" if the result found is NULL, and 707 * NULL, and otherwise "false". Only used if @e is_nullable is true.
360 * otherwise "false". Only used if @e is_nullable
361 * is true.
362 */ 708 */
363 bool *is_null; 709 bool *is_null;
364 710
@@ -385,8 +731,7 @@ struct GNUNET_PQ_ResultSpec
385 731
386 732
387/** 733/**
388 * Allow NULL value to be found in the database 734 * Allow NULL value to be found in the database for the given value.
389 * for the given value.
390 * 735 *
391 * @param rs result spec entry to modify 736 * @param rs result spec entry to modify
392 * @param[out] is_null location set to 'true' if the 737 * @param[out] is_null location set to 'true' if the
@@ -582,13 +927,231 @@ GNUNET_PQ_result_spec_uint64 (const char *name,
582 uint64_t *u64); 927 uint64_t *u64);
583 928
584 929
585/* ************************* pq.c functions ************************ */ 930/**
931 * int64_t expected.
932 *
933 * @param name name of the field in the table
934 * @param[out] i64 where to store the result
935 * @return array entry for the result specification to use
936 */
937struct GNUNET_PQ_ResultSpec
938GNUNET_PQ_result_spec_int64 (const char *name,
939 int64_t *i64);
940
586 941
587/** 942/**
588 * Postgres context. 943 * array of bool expected.
944 *
945 * @param db Database context, needed for OID lookup for the correct type
946 * @param name name of the field in the table
947 * @param[out] num where to store the number of elements in the array @a bools.
948 * @param[out] bools pointer to where to store the result, an array of @a num bool's. Allocated by the function, MUST be freed with GNUNET_free.
949 * @return array entry for the result specification to use
589 */ 950 */
590struct GNUNET_PQ_Context; 951struct GNUNET_PQ_ResultSpec
952GNUNET_PQ_result_spec_array_bool (
953 struct GNUNET_PQ_Context *db,
954 const char *name,
955 size_t *num,
956 bool **bools);
957
958/**
959 * array of uint16_t expected.
960 *
961 * @param db Database context, needed for OID lookup for the correct type
962 * @param name name of the field in the table
963 * @param[out] num where to store the number of elements in the array @a u16s.
964 * @param[out] dst pointer to where to store the an array of @a num uint16_t's. Allocated by the function, MUST be freed with GNUNET_free.
965 * @return array entry for the result specification to use
966 */
967struct GNUNET_PQ_ResultSpec
968GNUNET_PQ_result_spec_array_uint16 (
969 struct GNUNET_PQ_Context *db,
970 const char *name,
971 size_t *num,
972 uint16_t **dst);
973
974/**
975 * array of uint32_t expected.
976 *
977 * @param db Database context, needed for OID lookup for the correct type
978 * @param name name of the field in the table
979 * @param[out] num where to store the number of elements in the array @a u32s.
980 * @param[out] dst pointer to where to store the array of @a num uint32_t's. Allocated by the function, MUST be freed with GNUNET_free.
981 * @return array entry for the result specification to use
982 */
983struct GNUNET_PQ_ResultSpec
984GNUNET_PQ_result_spec_array_uint32 (
985 struct GNUNET_PQ_Context *db,
986 const char *name,
987 size_t *num,
988 uint32_t **dst);
989
990/**
991 * array of uint64_t expected.
992 *
993 * @param db Database context, needed for OID lookup for the correct type
994 * @param name name of the field in the table
995 * @param[out] num where to store the number of elements in the array @a u64s.
996 * @param[out] dst pointer to where to store the array of @a num uint64_t's. Allocated by the function, MUST be freed with GNUNET_free.
997 * @return array entry for the result specification to use
998 */
999struct GNUNET_PQ_ResultSpec
1000GNUNET_PQ_result_spec_array_uint64 (
1001 struct GNUNET_PQ_Context *db,
1002 const char *name,
1003 size_t *num,
1004 uint64_t **dst);
1005
1006
1007/**
1008 * array of absolute time expected.
1009 *
1010 * @param db Database context, needed for OID lookup for the correct type
1011 * @param name name of the field in the table
1012 * @param[out] num where to store the number of elements in the array @a u64s.
1013 * @param[out] dst pointer to where to store the array of @a num absolute time. Allocated by the function, MUST be freed with GNUNET_free.
1014 * @return array entry for the result specification to use
1015 */
1016struct GNUNET_PQ_ResultSpec
1017GNUNET_PQ_result_spec_array_abs_time (
1018 struct GNUNET_PQ_Context *db,
1019 const char *name,
1020 size_t *num,
1021 struct GNUNET_TIME_Absolute **dst);
1022
1023/**
1024 * array of relative time expected.
1025 *
1026 * @param db Database context, needed for OID lookup for the correct type
1027 * @param name name of the field in the table
1028 * @param[out] num where to store the number of elements in the array @a u64s.
1029 * @param[out] dst pointer to where to store the array of @a num relate time. Allocated by the function, MUST be freed with GNUNET_free.
1030 * @return array entry for the result specification to use
1031 */
1032struct GNUNET_PQ_ResultSpec
1033GNUNET_PQ_result_spec_array_rel_time (
1034 struct GNUNET_PQ_Context *db,
1035 const char *name,
1036 size_t *num,
1037 struct GNUNET_TIME_Relative **dst);
1038
1039/**
1040 * array of relative time expected.
1041 *
1042 * @param db Database context, needed for OID lookup for the correct type
1043 * @param name name of the field in the table
1044 * @param[out] num where to store the number of elements in the array @a u64s.
1045 * @param[out] dst pointer to where to store the array of @a num timestamps. Allocated by the function, MUST be freed with GNUNET_free.
1046 * @return array entry for the result specification to use
1047 */
1048
1049struct GNUNET_PQ_ResultSpec
1050GNUNET_PQ_result_spec_array_timestamp (
1051 struct GNUNET_PQ_Context *db,
1052 const char *name,
1053 size_t *num,
1054 struct GNUNET_TIME_Timestamp **dst);
1055
1056/**
1057 * Array of variable-size result expected.
1058 *
1059 * @param db Database context, needed for OID lookup for the correct type
1060 * @param name name of the field in the table
1061 * @param[out] num where to store the number of elements
1062 * @param[out] sizes where to store the @a num size's of byte-buffers in @a dst
1063 * @param[out] dst where to store the continuous array of @a num byte-buffers , allocated
1064 * @return array entry for the result specification to use
1065 */
1066struct GNUNET_PQ_ResultSpec
1067GNUNET_PQ_result_spec_array_variable_size (
1068 struct GNUNET_PQ_Context *db,
1069 const char *name,
1070 size_t *num,
1071 size_t **sizes,
1072 void **dst);
1073
1074
1075/**
1076 * Array of fixed-size result expected.
1077 *
1078 * @param db Database context, needed for OID lookup for the correct type
1079 * @param name name of the field in the table
1080 * @param size number of bytes expected in each element of @a dst
1081 * @param[out] num where to store the number of elements
1082 * @param[out] dst where to store the results, an continuous array of fixed-size elements
1083 * @return array entry for the result specification to use
1084 */
1085struct GNUNET_PQ_ResultSpec
1086GNUNET_PQ_result_spec_array_fixed_size (
1087 struct GNUNET_PQ_Context *db,
1088 const char *name,
1089 size_t size,
1090 size_t *num,
1091 void **dst);
1092
1093
1094/**
1095 * We expect a fixed-size result, with size determined by the type of `* dst`
1096 *
1097 * @param db Database context, needed for OID lookup for the correct type
1098 * @param name name of the field in the table
1099 * @param num pointer to where to store the number of elements
1100 * @param dst pointer to where to store the results, type fits expected result size
1101 * @return array entry for the result specification to use
1102 */
1103#define GNUNET_PQ_result_spec_auto_array_from_type(db, name, num, dst) \
1104 GNUNET_PQ_result_spec_array_fixed_size ( \
1105 (db), \
1106 (name), \
1107 sizeof(*(dst)), \
1108 (num), \
1109 (void *) &(dst))
1110
1111
1112/**
1113 * Array of 0-terminated strings expected.
1114 *
1115 * @param db Database context, needed for OID lookup for the correct type
1116 * @param name name of the field in the table
1117 * @param[out] num where to store the number of elements
1118 * @param[out] dst where to store the allocated continous array of @a num 0-terminated strings
1119 * @return array entry for the result specification to use
1120 */
1121struct GNUNET_PQ_ResultSpec
1122GNUNET_PQ_result_spec_array_string (
1123 struct GNUNET_PQ_Context *db,
1124 const char *name,
1125 size_t *num,
1126 char **dst);
1127
1128
1129/**
1130 * Blind sign public key expected.
1131 *
1132 * @param name name of the field in the table
1133 * @param[out] public_key where to store the denomination signature
1134 * @return array entry for the result specification to use
1135 */
1136struct GNUNET_PQ_ResultSpec
1137GNUNET_PQ_result_spec_blind_sign_pub (
1138 const char *name,
1139 struct GNUNET_CRYPTO_BlindSignPublicKey **public_key);
1140
1141
1142/**
1143 * Blind sign private key expected.
1144 *
1145 * @param name name of the field in the table
1146 * @param[out] private_key where to store the denomination signature
1147 * @return array entry for the result specification to use
1148 */
1149struct GNUNET_PQ_ResultSpec
1150GNUNET_PQ_result_spec_blind_sign_priv (
1151 const char *name,
1152 struct GNUNET_CRYPTO_BlindSignPrivateKey **private_key);
591 1153
1154/* ************************* pq.c functions ************************ */
592 1155
593/** 1156/**
594 * Execute a prepared statement. 1157 * Execute a prepared statement.
@@ -754,11 +1317,6 @@ struct GNUNET_PQ_PreparedStatement
754 */ 1317 */
755 const char *sql; 1318 const char *sql;
756 1319
757 /**
758 * Number of arguments included in @e sql.
759 */
760 unsigned int num_arguments;
761
762}; 1320};
763 1321
764 1322
@@ -767,7 +1325,7 @@ struct GNUNET_PQ_PreparedStatement
767 */ 1325 */
768#define GNUNET_PQ_PREPARED_STATEMENT_END \ 1326#define GNUNET_PQ_PREPARED_STATEMENT_END \
769 { \ 1327 { \
770 NULL, NULL, 0 \ 1328 NULL, NULL \
771 } 1329 }
772 1330
773 1331
@@ -776,13 +1334,11 @@ struct GNUNET_PQ_PreparedStatement
776 * 1334 *
777 * @param name name of the statement 1335 * @param name name of the statement
778 * @param sql actual SQL statement 1336 * @param sql actual SQL statement
779 * @param num_args number of arguments in the statement
780 * @return initialized struct 1337 * @return initialized struct
781 */ 1338 */
782struct GNUNET_PQ_PreparedStatement 1339struct GNUNET_PQ_PreparedStatement
783GNUNET_PQ_make_prepare (const char *name, 1340GNUNET_PQ_make_prepare (const char *name,
784 const char *sql, 1341 const char *sql);
785 unsigned int num_args);
786 1342
787 1343
788/** 1344/**
@@ -799,6 +1355,22 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
799 const struct GNUNET_PQ_PreparedStatement *ps); 1355 const struct GNUNET_PQ_PreparedStatement *ps);
800 1356
801 1357
1358/**
1359 * Request creation of prepared statements @a ps from Postgres, but do not
1360 * automatically re-prepare the statement if we are disconnected from the
1361 * database.
1362 *
1363 * @param db database to prepare the statements for
1364 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
1365 * statements.
1366 * @return #GNUNET_OK on success,
1367 * #GNUNET_SYSERR on error
1368 */
1369enum GNUNET_GenericReturnValue
1370GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db,
1371 const struct GNUNET_PQ_PreparedStatement *ps);
1372
1373
802/* ******************** pq_exec.c functions ************** */ 1374/* ******************** pq_exec.c functions ************** */
803 1375
804 1376
@@ -853,7 +1425,7 @@ GNUNET_PQ_make_try_execute (const char *sql);
853/** 1425/**
854 * Request execution of an array of statements @a es from Postgres. 1426 * Request execution of an array of statements @a es from Postgres.
855 * 1427 *
856 * @param pq database to execute the statements in 1428 * @param db database to execute the statements in
857 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared 1429 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
858 * statements. 1430 * statements.
859 * @return #GNUNET_OK on success (modulo statements where errors can be ignored) 1431 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
@@ -895,14 +1467,14 @@ enum GNUNET_PQ_Options
895 * configuration. Initialize logging via GNUnet's log routines and disable 1467 * configuration. Initialize logging via GNUnet's log routines and disable
896 * Postgres's logger. Also ensures that the statements in @a load_path and @a 1468 * Postgres's logger. Also ensures that the statements in @a load_path and @a
897 * es are executed whenever we (re)connect to the database, and that the 1469 * es are executed whenever we (re)connect to the database, and that the
898 * prepared statements in @a ps are "ready". If statements in @es fail that 1470 * prepared statements in @a ps are "ready". If statements in @a es fail that
899 * were created with #GNUNET_PQ_make_execute(), then the entire operation 1471 * were created with #GNUNET_PQ_make_execute(), then the entire operation
900 * fails. 1472 * fails.
901 * 1473 *
902 * In @a load_path, a list of "$XXXX.sql" files is expected where $XXXX 1474 * In @a load_path, a list of "$XXXX.sql" files is expected where $XXXX
903 * must be a sequence of contiguous integer values starting at 0000. 1475 * must be a sequence of contiguous integer values starting at 0000.
904 * These files are then loaded in sequence using "psql $config_str" before 1476 * These files are then loaded in sequence using "psql $config_str" before
905 * running statements from @e es. The directory is inspected again on 1477 * running statements from @a es. The directory is inspected again on
906 * reconnect. 1478 * reconnect.
907 * 1479 *
908 * @param config_str configuration to use 1480 * @param config_str configuration to use
@@ -939,7 +1511,7 @@ GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db,
939 * configuration. Initialize logging via GNUnet's log routines and disable 1511 * configuration. Initialize logging via GNUnet's log routines and disable
940 * Postgres's logger. Also ensures that the statements in @a load_path and @a 1512 * Postgres's logger. Also ensures that the statements in @a load_path and @a
941 * es are executed whenever we (re)connect to the database, and that the 1513 * es are executed whenever we (re)connect to the database, and that the
942 * prepared statements in @a ps are "ready". If statements in @es fail that 1514 * prepared statements in @a ps are "ready". If statements in @a es fail that
943 * were created with #GNUNET_PQ_make_execute(), then the entire operation 1515 * were created with #GNUNET_PQ_make_execute(), then the entire operation
944 * fails. 1516 * fails.
945 * 1517 *
@@ -968,10 +1540,10 @@ GNUNET_PQ_connect2 (const char *config_str,
968 1540
969 1541
970/** 1542/**
971 * Connect to a postgres database using the configuration 1543 * Connect to a postgres database using the configuration option "CONFIG" in
972 * option "CONFIG" in @a section. Also ensures that the 1544 * @a section. Also ensures that the statements in @a es are executed
973 * statements in @a es are executed whenever we (re)connect to the 1545 * whenever we (re)connect to the database, and that the prepared statements
974 * database, and that the prepared statements in @a ps are "ready". 1546 * in @a ps are "ready".
975 * 1547 *
976 * The caller does not have to ensure that @a es and @a ps remain allocated 1548 * The caller does not have to ensure that @a es and @a ps remain allocated
977 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a copy will be made. 1549 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a copy will be made.
@@ -999,7 +1571,8 @@ GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
999 * database, and that the prepared statements in @a ps are "ready". 1571 * database, and that the prepared statements in @a ps are "ready".
1000 * 1572 *
1001 * The caller does not have to ensure that @a es and @a ps remain allocated 1573 * The caller does not have to ensure that @a es and @a ps remain allocated
1002 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a copy will be made. 1574 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a
1575 * copy will be made.
1003 * 1576 *
1004 * @param cfg configuration 1577 * @param cfg configuration
1005 * @param section configuration section to use to get Postgres configuration options 1578 * @param section configuration section to use to get Postgres configuration options
@@ -1078,12 +1651,24 @@ GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh);
1078 1651
1079 1652
1080/** 1653/**
1654 * Poll for events right now. Useful if we may have
1655 * triggered an event for ourselves. Not needed when
1656 * using GNUNET_PQ_event_notify(), but useful when
1657 * stored procedures may have triggered events.
1658 * Does nothing if there are no events.
1659 *
1660 * @param[in,out] db database to check for events
1661 */
1662void
1663GNUNET_PQ_event_do_poll (struct GNUNET_PQ_Context *db);
1664
1665
1666/**
1081 * Notify all that listen on @a es of an event. 1667 * Notify all that listen on @a es of an event.
1082 * 1668 *
1083 * Unlike many other calls, this function is thread-safe 1669 * Unlike many other calls, this function is thread-safe and may be called
1084 * and may be called from threads that are different 1670 * from threads that are different from the one that setup @a db. However, the
1085 * from the one that setup @a db. However, the @a cb 1671 * @a cb will always be called from the thread that runs
1086 * will always be called from the thread that runs
1087 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler. 1672 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler.
1088 * 1673 *
1089 * @param db database context to use 1674 * @param db database context to use
@@ -1099,6 +1684,17 @@ GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db,
1099 1684
1100 1685
1101/** 1686/**
1687 * Compute the channel that one should notify upon for the given event
1688 * specification.
1689 *
1690 * @param es event specification
1691 * @return channel to notify upon
1692 */
1693char *
1694GNUNET_PQ_get_event_notify_channel (const struct GNUNET_DB_EventHeaderP *es);
1695
1696
1697/**
1102 * Within the @a db context, run all the SQL files 1698 * Within the @a db context, run all the SQL files
1103 * from the @a load_path from 0000-9999.sql (as long 1699 * from the @a load_path from 0000-9999.sql (as long
1104 * as the files exist contiguously). 1700 * as the files exist contiguously).