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.h883
1 files changed, 832 insertions, 51 deletions
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index fe3fabbea..8b4077d0e 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,13 +118,26 @@ 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.
96 */ 133 */
97#define GNUNET_PQ_query_param_end \ 134#define GNUNET_PQ_query_param_end \
98 { \ 135 { \
99 NULL, NULL, NULL, 0, 0 \ 136 .conv = NULL, \
137 .conv_cls = NULL, \
138 .data = NULL, \
139 .size = 0, \
140 .num_params = 0 \
100 } 141 }
101 142
102 143
@@ -114,7 +155,7 @@ GNUNET_PQ_query_param_null (void);
114 * @a ptr_size bytes. 155 * @a ptr_size bytes.
115 * 156 *
116 * @param ptr pointer to the query parameter to pass 157 * @param ptr pointer to the query parameter to pass
117 * @oaran ptr_size number of bytes in @a ptr 158 * @param ptr_size number of bytes in @a ptr
118 * @return query parameter to use 159 * @return query parameter to use
119 */ 160 */
120struct GNUNET_PQ_QueryParam 161struct GNUNET_PQ_QueryParam
@@ -133,6 +174,220 @@ GNUNET_PQ_query_param_string (const char *ptr);
133 174
134 175
135/** 176/**
177 * Pass a boolean into a query.
178 *
179 * @param b boolean bit to pass
180 * @return query parameter to use
181 */
182struct GNUNET_PQ_QueryParam
183GNUNET_PQ_query_param_bool (bool b);
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
389
390/**
136 * Generate fixed-size query parameter with size determined 391 * Generate fixed-size query parameter with size determined
137 * by variable type. 392 * by variable type.
138 * 393 *
@@ -142,6 +397,83 @@ GNUNET_PQ_query_param_string (const char *ptr);
142#define GNUNET_PQ_query_param_auto_from_type(x) \ 397#define GNUNET_PQ_query_param_auto_from_type(x) \
143 GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x))) 398 GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
144 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);
145 477
146/** 478/**
147 * Generate query parameter for an RSA public key. The 479 * Generate query parameter for an RSA public key. The
@@ -190,6 +522,17 @@ GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
190 522
191 523
192/** 524/**
525 * Generate query parameter for a timestamp.
526 * The database must store a 64-bit integer.
527 *
528 * @param x pointer to the query parameter to pass
529 * @return query parameter to use
530 */
531struct GNUNET_PQ_QueryParam
532GNUNET_PQ_query_param_timestamp (const struct GNUNET_TIME_Timestamp *x);
533
534
535/**
193 * Generate query parameter for an absolute time value. 536 * Generate query parameter for an absolute time value.
194 * The database must store a 64-bit integer. 537 * The database must store a 64-bit integer.
195 * 538 *
@@ -202,6 +545,18 @@ GNUNET_PQ_query_param_absolute_time_nbo (
202 545
203 546
204/** 547/**
548 * Generate query parameter for a timestamp in NBO.
549 * The database must store a 64-bit integer.
550 *
551 * @param x pointer to the query parameter to pass
552 * @return query parameter to use
553 */
554struct GNUNET_PQ_QueryParam
555GNUNET_PQ_query_param_timestamp_nbo (
556 const struct GNUNET_TIME_TimestampNBO *x);
557
558
559/**
205 * Generate query parameter for an uint16_t in host byte order. 560 * Generate query parameter for an uint16_t in host byte order.
206 * 561 *
207 * @param x pointer to the query parameter to pass 562 * @param x pointer to the query parameter to pass
@@ -222,7 +577,7 @@ GNUNET_PQ_query_param_uint32 (const uint32_t *x);
222 577
223 578
224/** 579/**
225 * Generate query parameter for an uint16_t in host byte order. 580 * Generate query parameter for an uint64_t in host byte order.
226 * 581 *
227 * @param x pointer to the query parameter to pass 582 * @param x pointer to the query parameter to pass
228 * @return query parameter to use 583 * @return query parameter to use
@@ -231,6 +586,37 @@ struct GNUNET_PQ_QueryParam
231GNUNET_PQ_query_param_uint64 (const uint64_t *x); 586GNUNET_PQ_query_param_uint64 (const uint64_t *x);
232 587
233 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
234/* ************************* pq_result_helper.c functions ************************ */ 620/* ************************* pq_result_helper.c functions ************************ */
235 621
236 622
@@ -280,8 +666,7 @@ struct GNUNET_PQ_ResultSpec
280 GNUNET_PQ_ResultConverter conv; 666 GNUNET_PQ_ResultConverter conv;
281 667
282 /** 668 /**
283 * Function to clean up result data, NULL if cleanup is 669 * Function to clean up result data, NULL if cleanup is not necessary.
284 * not necessary.
285 */ 670 */
286 GNUNET_PQ_ResultCleanup cleaner; 671 GNUNET_PQ_ResultCleanup cleaner;
287 672
@@ -296,9 +681,9 @@ struct GNUNET_PQ_ResultSpec
296 void *dst; 681 void *dst;
297 682
298 /** 683 /**
299 * 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
300 * (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
301 * and we need to allocate a buffer of the right size). 686 * size).
302 */ 687 */
303 size_t dst_size; 688 size_t dst_size;
304 689
@@ -318,10 +703,8 @@ struct GNUNET_PQ_ResultSpec
318 bool is_nullable; 703 bool is_nullable;
319 704
320 /** 705 /**
321 * Points to a location where we should store 706 * Points to a location where we should store "true" if the result found is
322 * "true" if the result found is NULL, and 707 * NULL, and otherwise "false". Only used if @e is_nullable is true.
323 * otherwise "false". Only used if @e is_nullable
324 * is true.
325 */ 708 */
326 bool *is_null; 709 bool *is_null;
327 710
@@ -335,13 +718,20 @@ struct GNUNET_PQ_ResultSpec
335 */ 718 */
336#define GNUNET_PQ_result_spec_end \ 719#define GNUNET_PQ_result_spec_end \
337 { \ 720 { \
338 NULL, NULL, NULL, NULL, 0, NULL, NULL \ 721 .conv = NULL, \
722 .cleaner = NULL, \
723 .cls = NULL, \
724 .dst = NULL, \
725 .dst_size = 0, \
726 .fname = NULL, \
727 .result_size = NULL, \
728 .is_nullable = false, \
729 .is_null = NULL \
339 } 730 }
340 731
341 732
342/** 733/**
343 * Allow NULL value to be found in the database 734 * Allow NULL value to be found in the database for the given value.
344 * for the given value.
345 * 735 *
346 * @param rs result spec entry to modify 736 * @param rs result spec entry to modify
347 * @param[out] is_null location set to 'true' if the 737 * @param[out] is_null location set to 'true' if the
@@ -377,7 +767,9 @@ GNUNET_PQ_result_spec_variable_size (const char *name,
377 * @return array entry for the result specification to use 767 * @return array entry for the result specification to use
378 */ 768 */
379struct GNUNET_PQ_ResultSpec 769struct GNUNET_PQ_ResultSpec
380GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size); 770GNUNET_PQ_result_spec_fixed_size (const char *name,
771 void *dst,
772 size_t dst_size);
381 773
382 774
383/** 775/**
@@ -399,7 +791,20 @@ GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size);
399 * @return array entry for the result specification to use 791 * @return array entry for the result specification to use
400 */ 792 */
401struct GNUNET_PQ_ResultSpec 793struct GNUNET_PQ_ResultSpec
402GNUNET_PQ_result_spec_string (const char *name, char **dst); 794GNUNET_PQ_result_spec_string (const char *name,
795 char **dst);
796
797
798/**
799 * boolean expected.
800 *
801 * @param name name of the field in the table
802 * @param[out] dst where to store the result
803 * @return array entry for the result specification to use
804 */
805struct GNUNET_PQ_ResultSpec
806GNUNET_PQ_result_spec_bool (const char *name,
807 bool *dst);
403 808
404 809
405/** 810/**
@@ -439,6 +844,18 @@ GNUNET_PQ_result_spec_absolute_time (const char *name,
439 844
440 845
441/** 846/**
847 * Timestamp expected.
848 *
849 * @param name name of the field in the table
850 * @param[out] t where to store the result
851 * @return array entry for the result specification to use
852 */
853struct GNUNET_PQ_ResultSpec
854GNUNET_PQ_result_spec_timestamp (const char *name,
855 struct GNUNET_TIME_Timestamp *t);
856
857
858/**
442 * Relative time expected. 859 * Relative time expected.
443 * 860 *
444 * @param name name of the field in the table 861 * @param name name of the field in the table
@@ -463,6 +880,18 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
463 880
464 881
465/** 882/**
883 * Timestamp expected.
884 *
885 * @param name name of the field in the table
886 * @param[out] tn where to store the result
887 * @return array entry for the result specification to use
888 */
889struct GNUNET_PQ_ResultSpec
890GNUNET_PQ_result_spec_timestamp_nbo (const char *name,
891 struct GNUNET_TIME_TimestampNBO *tn);
892
893
894/**
466 * uint16_t expected. 895 * uint16_t expected.
467 * 896 *
468 * @param name name of the field in the table 897 * @param name name of the field in the table
@@ -470,7 +899,8 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
470 * @return array entry for the result specification to use 899 * @return array entry for the result specification to use
471 */ 900 */
472struct GNUNET_PQ_ResultSpec 901struct GNUNET_PQ_ResultSpec
473GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16); 902GNUNET_PQ_result_spec_uint16 (const char *name,
903 uint16_t *u16);
474 904
475 905
476/** 906/**
@@ -481,7 +911,8 @@ GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16);
481 * @return array entry for the result specification to use 911 * @return array entry for the result specification to use
482 */ 912 */
483struct GNUNET_PQ_ResultSpec 913struct GNUNET_PQ_ResultSpec
484GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32); 914GNUNET_PQ_result_spec_uint32 (const char *name,
915 uint32_t *u32);
485 916
486 917
487/** 918/**
@@ -492,16 +923,235 @@ GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32);
492 * @return array entry for the result specification to use 923 * @return array entry for the result specification to use
493 */ 924 */
494struct GNUNET_PQ_ResultSpec 925struct GNUNET_PQ_ResultSpec
495GNUNET_PQ_result_spec_uint64 (const char *name, uint64_t *u64); 926GNUNET_PQ_result_spec_uint64 (const char *name,
927 uint64_t *u64);
496 928
497 929
498/* ************************* 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
499 941
500/** 942/**
501 * 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
502 */ 950 */
503struct 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);
504 1153
1154/* ************************* pq.c functions ************************ */
505 1155
506/** 1156/**
507 * Execute a prepared statement. 1157 * Execute a prepared statement.
@@ -641,11 +1291,11 @@ GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db,
641 * codes to `enum GNUNET_DB_QueryStatus`. 1291 * codes to `enum GNUNET_DB_QueryStatus`.
642 */ 1292 */
643enum GNUNET_DB_QueryStatus 1293enum GNUNET_DB_QueryStatus
644GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db, 1294GNUNET_PQ_eval_prepared_singleton_select (
645 const char *statement_name, 1295 struct GNUNET_PQ_Context *db,
646 const struct 1296 const char *statement_name,
647 GNUNET_PQ_QueryParam *params, 1297 const struct GNUNET_PQ_QueryParam *params,
648 struct GNUNET_PQ_ResultSpec *rs); 1298 struct GNUNET_PQ_ResultSpec *rs);
649 1299
650 1300
651/* ******************** pq_prepare.c functions ************** */ 1301/* ******************** pq_prepare.c functions ************** */
@@ -667,11 +1317,6 @@ struct GNUNET_PQ_PreparedStatement
667 */ 1317 */
668 const char *sql; 1318 const char *sql;
669 1319
670 /**
671 * Number of arguments included in @e sql.
672 */
673 unsigned int num_arguments;
674
675}; 1320};
676 1321
677 1322
@@ -680,7 +1325,7 @@ struct GNUNET_PQ_PreparedStatement
680 */ 1325 */
681#define GNUNET_PQ_PREPARED_STATEMENT_END \ 1326#define GNUNET_PQ_PREPARED_STATEMENT_END \
682 { \ 1327 { \
683 NULL, NULL, 0 \ 1328 NULL, NULL \
684 } 1329 }
685 1330
686 1331
@@ -689,13 +1334,11 @@ struct GNUNET_PQ_PreparedStatement
689 * 1334 *
690 * @param name name of the statement 1335 * @param name name of the statement
691 * @param sql actual SQL statement 1336 * @param sql actual SQL statement
692 * @param num_args number of arguments in the statement
693 * @return initialized struct 1337 * @return initialized struct
694 */ 1338 */
695struct GNUNET_PQ_PreparedStatement 1339struct GNUNET_PQ_PreparedStatement
696GNUNET_PQ_make_prepare (const char *name, 1340GNUNET_PQ_make_prepare (const char *name,
697 const char *sql, 1341 const char *sql);
698 unsigned int num_args);
699 1342
700 1343
701/** 1344/**
@@ -707,11 +1350,27 @@ GNUNET_PQ_make_prepare (const char *name,
707 * @return #GNUNET_OK on success, 1350 * @return #GNUNET_OK on success,
708 * #GNUNET_SYSERR on error 1351 * #GNUNET_SYSERR on error
709 */ 1352 */
710int 1353enum GNUNET_GenericReturnValue
711GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, 1354GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
712 const struct GNUNET_PQ_PreparedStatement *ps); 1355 const struct GNUNET_PQ_PreparedStatement *ps);
713 1356
714 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
715/* ******************** pq_exec.c functions ************** */ 1374/* ******************** pq_exec.c functions ************** */
716 1375
717 1376
@@ -766,13 +1425,13 @@ GNUNET_PQ_make_try_execute (const char *sql);
766/** 1425/**
767 * Request execution of an array of statements @a es from Postgres. 1426 * Request execution of an array of statements @a es from Postgres.
768 * 1427 *
769 * @param pq database to execute the statements in 1428 * @param db database to execute the statements in
770 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared 1429 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
771 * statements. 1430 * statements.
772 * @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)
773 * #GNUNET_SYSERR on error 1432 * #GNUNET_SYSERR on error
774 */ 1433 */
775int 1434enum GNUNET_GenericReturnValue
776GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db, 1435GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db,
777 const struct GNUNET_PQ_ExecuteStatement *es); 1436 const struct GNUNET_PQ_ExecuteStatement *es);
778 1437
@@ -781,18 +1440,41 @@ GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db,
781 1440
782 1441
783/** 1442/**
1443 * Flags to control PQ operation.
1444 */
1445enum GNUNET_PQ_Options
1446{
1447 /**
1448 * Traditional default. Do nothing special.
1449 */
1450 GNUNET_PQ_FLAG_NONE = 0,
1451
1452 /**
1453 * Dropping database. Do not attempt to initialize
1454 * versioning schema if not present.
1455 */
1456 GNUNET_PQ_FLAG_DROP = 1,
1457
1458 /**
1459 * Check database version is current. Fail to connect if it is not.
1460 */
1461 GNUNET_PQ_FLAG_CHECK_CURRENT = 2
1462};
1463
1464
1465/**
784 * Create a connection to the Postgres database using @a config_str for the 1466 * Create a connection to the Postgres database using @a config_str for the
785 * configuration. Initialize logging via GNUnet's log routines and disable 1467 * configuration. Initialize logging via GNUnet's log routines and disable
786 * 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
787 * 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
788 * 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
789 * were created with #GNUNET_PQ_make_execute(), then the entire operation 1471 * were created with #GNUNET_PQ_make_execute(), then the entire operation
790 * fails. 1472 * fails.
791 * 1473 *
792 * 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
793 * must be a sequence of contiguous integer values starting at 0000. 1475 * must be a sequence of contiguous integer values starting at 0000.
794 * 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
795 * running statements from @e es. The directory is inspected again on 1477 * running statements from @a es. The directory is inspected again on
796 * reconnect. 1478 * reconnect.
797 * 1479 *
798 * @param config_str configuration to use 1480 * @param config_str configuration to use
@@ -810,10 +1492,58 @@ GNUNET_PQ_connect (const char *config_str,
810 1492
811 1493
812/** 1494/**
813 * Connect to a postgres database using the configuration 1495 * Execute SQL statements from @a buf against @a db.
814 * option "CONFIG" in @a section. Also ensures that the 1496 * The given filename infix in @a buf is prefixed with
815 * statements in @a es are executed whenever we (re)connect to the 1497 * the "load_path" and ".sql" is appended to construct
816 * database, and that the prepared statements in @a ps are "ready". 1498 * the full filename.
1499 *
1500 * @param db database context to use
1501 * @param buf filename infix (!) with the SQL code to run
1502 * @return #GNUNET_OK on success, #GNUNET_NO if patch @a buf does not exist, #GNUNET_SYSERR on error
1503 */
1504enum GNUNET_GenericReturnValue
1505GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db,
1506 const char *buf);
1507
1508
1509/**
1510 * Create a connection to the Postgres database using @a config_str for the
1511 * configuration. Initialize logging via GNUnet's log routines and disable
1512 * Postgres's logger. Also ensures that the statements in @a load_path and @a
1513 * es are executed whenever we (re)connect to the database, and that the
1514 * prepared statements in @a ps are "ready". If statements in @a es fail that
1515 * were created with #GNUNET_PQ_make_execute(), then the entire operation
1516 * fails.
1517 *
1518 * In @a load_path, a list of "$XXXX.sql" files is expected where $XXXX
1519 * must be a sequence of contiguous integer values starting at 0000.
1520 * These files are then loaded in sequence using "psql $config_str" before
1521 * running statements from @e es. The directory is inspected again on
1522 * reconnect.
1523 *
1524 * @param config_str configuration to use
1525 * @param load_path path to directory with SQL transactions to run, can be NULL
1526 * @param auto_suffix infix of SQL series to run on every reconnect; runs multiple (!) files, of the form auto_suffix-XXXX where XXXX is from 0 to 9999 (consequtive).
1527 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
1528 * array of statements to execute upon EACH connection, can be NULL
1529 * @param ps array of prepared statements to prepare, can be NULL
1530 * @param flags connection flags
1531 * @return NULL on error
1532 */
1533struct GNUNET_PQ_Context *
1534GNUNET_PQ_connect2 (const char *config_str,
1535 const char *load_path,
1536 const char *auto_suffix,
1537 const struct GNUNET_PQ_ExecuteStatement *es,
1538 const struct GNUNET_PQ_PreparedStatement *ps,
1539 enum GNUNET_PQ_Options flags);
1540
1541
1542/**
1543 * Connect to a postgres database using the configuration option "CONFIG" in
1544 * @a section. Also ensures that the statements in @a es are executed
1545 * whenever we (re)connect to the database, and that the prepared statements
1546 * in @a ps are "ready".
817 * 1547 *
818 * 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
819 * 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.
@@ -835,6 +1565,34 @@ GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
835 1565
836 1566
837/** 1567/**
1568 * Connect to a postgres database using the configuration
1569 * option "CONFIG" in @a section. Also ensures that the
1570 * statements in @a es are executed whenever we (re)connect to the
1571 * database, and that the prepared statements in @a ps are "ready".
1572 *
1573 * The caller does not have to ensure that @a es and @a ps remain allocated
1574 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a
1575 * copy will be made.
1576 *
1577 * @param cfg configuration
1578 * @param section configuration section to use to get Postgres configuration options
1579 * @param load_path_suffix suffix to append to the SQL_DIR in the configuration
1580 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
1581 * array of statements to execute upon EACH connection, can be NULL
1582 * @param ps array of prepared statements to prepare, can be NULL
1583 * @param flags connection flags
1584 * @return the postgres handle, NULL on error
1585 */
1586struct GNUNET_PQ_Context *
1587GNUNET_PQ_connect_with_cfg2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1588 const char *section,
1589 const char *load_path_suffix,
1590 const struct GNUNET_PQ_ExecuteStatement *es,
1591 const struct GNUNET_PQ_PreparedStatement *ps,
1592 enum GNUNET_PQ_Options flags);
1593
1594
1595/**
838 * Reinitialize the database @a db if the connection is down. 1596 * Reinitialize the database @a db if the connection is down.
839 * 1597 *
840 * @param db database connection to reinitialize 1598 * @param db database connection to reinitialize
@@ -893,12 +1651,24 @@ GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh);
893 1651
894 1652
895/** 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/**
896 * Notify all that listen on @a es of an event. 1667 * Notify all that listen on @a es of an event.
897 * 1668 *
898 * Unlike many other calls, this function is thread-safe 1669 * Unlike many other calls, this function is thread-safe and may be called
899 * and may be called from threads that are different 1670 * from threads that are different from the one that setup @a db. However, the
900 * from the one that setup @a db. However, the @a cb 1671 * @a cb will always be called from the thread that runs
901 * will always be called from the thread that runs
902 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler. 1672 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler.
903 * 1673 *
904 * @param db database context to use 1674 * @param db database context to use
@@ -914,6 +1684,17 @@ GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db,
914 1684
915 1685
916/** 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/**
917 * Within the @a db context, run all the SQL files 1698 * Within the @a db context, run all the SQL files
918 * from the @a load_path from 0000-9999.sql (as long 1699 * from the @a load_path from 0000-9999.sql (as long
919 * as the files exist contiguously). 1700 * as the files exist contiguously).