gnunet_pq_lib.h (57784B)
1 /* 2 This file is part of GNUnet 3 Copyright (C) 2016, 2017, 2020-2022 GNUnet e.V. 4 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 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /** 21 * @file include/gnunet_pq_lib.h 22 * @brief helper functions for Postgres DB interactions 23 * @author Christian Grothoff 24 */ 25 #ifndef GNUNET_PQ_LIB_H 26 #define GNUNET_PQ_LIB_H 27 28 29 #include <libpq-fe.h> 30 #include <stdint.h> 31 #include "gnunet_common.h" 32 #include "gnunet_time_lib.h" 33 #include "gnunet_util_lib.h" 34 #include "gnunet_db_lib.h" 35 #include "postgres_ext.h" 36 37 /** 38 * Postgres context. 39 */ 40 struct GNUNET_PQ_Context; 41 42 /* ************************* pq_query_helper.c functions ************************ */ 43 44 45 /** 46 * Function called to convert input argument into SQL parameters. 47 * 48 * @param cls closure 49 * @param data pointer to input argument 50 * @param data_len number of bytes in @a data (if applicable) 51 * @param[out] param_values SQL data to set 52 * @param[out] param_lengths SQL length data to set 53 * @param[out] param_formats SQL format data to set 54 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays 55 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc() 56 * @param scratch_length number of entries left in @a scratch 57 * @return -1 on error, number of offsets used in @a scratch otherwise 58 */ 59 typedef int 60 (*GNUNET_PQ_QueryConverter) (void *cls, 61 const void *data, 62 size_t data_len, 63 void *param_values[], 64 int param_lengths[], 65 int param_formats[], 66 unsigned int param_length, 67 void *scratch[], 68 unsigned int scratch_length); 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 */ 76 typedef void 77 (*GNUNET_PQ_QueryConverter_Cleanup)(void *cls); 78 79 /** 80 * @brief Description of a DB query parameter. 81 */ 82 struct GNUNET_PQ_QueryParam 83 { 84 /** 85 * Function for how to handle this type of entry. 86 */ 87 GNUNET_PQ_QueryConverter conv; 88 89 /** 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. 94 */ 95 void *conv_cls; 96 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 /** 105 * Data or NULL. 106 */ 107 const void *data; 108 109 /** 110 * Size of @e data 111 */ 112 size_t size; 113 114 /** 115 * Number of parameters eaten by this operation. 116 */ 117 unsigned int num_params; 118 119 }; 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 */ 127 void 128 GNUNET_PQ_cleanup_query_params_closures ( 129 const struct GNUNET_PQ_QueryParam *params); 130 131 /** 132 * End of query parameter specification. 133 */ 134 #define GNUNET_PQ_query_param_end \ 135 { \ 136 .conv = NULL, \ 137 .conv_cls = NULL, \ 138 .data = NULL, \ 139 .size = 0, \ 140 .num_params = 0 \ 141 } 142 143 144 /** 145 * Generate query parameter to create a NULL value. 146 * 147 * @return query parameter to use to insert NULL into DB 148 */ 149 struct GNUNET_PQ_QueryParam 150 GNUNET_PQ_query_param_null (void); 151 152 153 /** 154 * Generate query parameter for a buffer @a ptr of 155 * @a ptr_size bytes. 156 * 157 * @param ptr pointer to the query parameter to pass 158 * @param ptr_size number of bytes in @a ptr 159 * @return query parameter to use 160 */ 161 struct GNUNET_PQ_QueryParam 162 GNUNET_PQ_query_param_fixed_size (const void *ptr, 163 size_t ptr_size); 164 165 166 /** 167 * Generate query parameter for a string. 168 * 169 * @param ptr pointer to the string query parameter to pass 170 * @return query parameter to use 171 */ 172 struct GNUNET_PQ_QueryParam 173 GNUNET_PQ_query_param_string (const char *ptr); 174 175 176 /** 177 * Pass a boolean into a query. 178 * 179 * @param b boolean bit to pass 180 * @return query parameter to use 181 */ 182 struct GNUNET_PQ_QueryParam 183 GNUNET_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 */ 193 enum GNUNET_GenericReturnValue 194 GNUNET_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 */ 207 struct 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 */ 225 struct GNUNET_PQ_QueryParam 226 GNUNET_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 (unsigned) 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 */ 240 struct GNUNET_PQ_QueryParam 241 GNUNET_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 (unsigned) 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 */ 255 struct GNUNET_PQ_QueryParam 256 GNUNET_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 (unsigned) 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 */ 270 struct GNUNET_PQ_QueryParam 271 GNUNET_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 Continuous 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 */ 286 struct GNUNET_PQ_QueryParam 287 GNUNET_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 * with sizes @a sizes. 296 * 297 * @param num Number of elements in @a elements 298 * @param elements Continuous array of @a num buffers, each with the same size 299 * @a same_size 300 * @param sizes Sizes in bytes of each element in @a elements 301 * @param db Database context, needed for database-depending encoding of @a 302 * elements 303 * @return query parameter to use 304 */ 305 struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes ( 306 unsigned int num, 307 const void *elements[static num], 308 const size_t *sizes, 309 struct GNUNET_PQ_Context *db); 310 311 /** 312 * Generate query parameter for an array of buffers @a elements, 313 * each of the same size @a size. 314 * 315 * @param num Number of elements in @a elements 316 * @param elements Continuous array of @a num buffers, each with the same size @a same_size 317 * @param same_size Size in bytes of each element in @a elements 318 * @param db Database context, needed for database-depending encoding of @a elements 319 * @return query parameter to use 320 */ 321 struct GNUNET_PQ_QueryParam 322 GNUNET_PQ_query_param_array_bytes_same_size ( 323 unsigned int num, 324 const void *elements, 325 size_t same_size, 326 struct GNUNET_PQ_Context *db); 327 328 /** 329 * Generate array of equal-sized query parameter with size determined 330 * by variable type from a pointer to an continuous array. 331 * 332 * @param num Number of elements in @a elements 333 * @param elements Continuous array of the query parameter to pass. 334 * @param db Database context, needed for database-depending encoding of @a elements 335 * @return query parameter to use 336 */ 337 #define GNUNET_PQ_query_param_array_auto_from_type(num, elements, db) \ 338 GNUNET_PQ_query_param_array_bytes_same_size ((num), \ 339 (elements), \ 340 sizeof(*(elements)), \ 341 (db)) 342 343 /** 344 * Generate query parameter for an array of pointers to buffers @a elements, 345 * each of the same size @a size. 346 * 347 * @param num Number of elements in @a elements 348 * @param elements Array of @a num pointers to buffers, each with the same size @a same_size 349 * @param same_size Size in bytes of each element in @a elements 350 * @param db Database context, needed for database-depending encoding of @a elements 351 * @return query parameter to use 352 */ 353 struct GNUNET_PQ_QueryParam 354 GNUNET_PQ_query_param_array_ptrs_bytes_same_size ( 355 unsigned int num, 356 const void *elements[static num], 357 size_t same_size, 358 struct GNUNET_PQ_Context *db); 359 360 361 /** 362 * Generate array of equal-sized query parameter with size determined by 363 * variable type from a pointer to an array of pointers to objects of the same 364 * type. 365 * 366 * @param num Number of elements in @a elements 367 * @param elements Continuous array of the query parameter to pass. 368 * @param db Database context, needed for database-depending encoding of @a elements 369 * @return query parameter to use 370 */ 371 #define GNUNET_PQ_query_param_array_ptrs_auto_from_type(num, elements, db) \ 372 GNUNET_PQ_query_param_array_ptrs_bytes_same_size ((num), \ 373 (const void **) ( \ 374 elements), \ 375 sizeof(*(elements[0])) \ 376 , \ 377 (db)) 378 379 380 /** 381 * Generate query parameter for an array of strings (continuous) 382 * 383 * @param num Number of elements in @a elements 384 * @param elements Array of @a num continuous strings (zero-terminated each) 385 * @param db Database context, needed for database-depending encoding of @a elements 386 * @return query parameter to use 387 */ 388 struct GNUNET_PQ_QueryParam 389 GNUNET_PQ_query_param_array_string ( 390 unsigned int num, 391 const char *elements, 392 struct GNUNET_PQ_Context *db); 393 394 395 /** 396 * Generate query parameter for an array of strings (pointers) 397 * 398 * @param num Number of elements in @a elements 399 * @param elements Array of @a num pointer to strings 400 * @param db Database context, needed for database-depending encoding of @a elements 401 * @return query parameter to use 402 */ 403 struct GNUNET_PQ_QueryParam 404 GNUNET_PQ_query_param_array_ptrs_string ( 405 unsigned int num, 406 const char *elements[static num], 407 struct GNUNET_PQ_Context *db); 408 409 410 /** 411 * Generate fixed-size query parameter with size determined 412 * by variable type. 413 * 414 * @param x pointer to the query parameter to pass. 415 * @return query parameter to use 416 */ 417 #define GNUNET_PQ_query_param_auto_from_type(x) \ 418 GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x))) 419 420 /** 421 * Generate query parameter for an array of absolute time stamps (continuous) 422 * 423 * @param num Number of elements in @a elements 424 * @param elements Array of @a num absolute timestamps 425 * @return query parameter to use 426 */ 427 struct GNUNET_PQ_QueryParam 428 GNUNET_PQ_query_param_array_abs_time ( 429 unsigned int num, 430 const struct GNUNET_TIME_Absolute *elements, 431 struct GNUNET_PQ_Context *db); 432 433 /** 434 * Generate query parameter for an array of absolute time stamps (pointers) 435 * 436 * @param num Number of elements in @a elements 437 * @param elements Array of @a num pointers to absolute timestamps 438 * @return query parameter to use 439 */ 440 struct GNUNET_PQ_QueryParam 441 GNUNET_PQ_query_param_array_ptrs_abs_time ( 442 unsigned int num, 443 const struct GNUNET_TIME_Absolute *elements[], 444 struct GNUNET_PQ_Context *db); 445 446 /** 447 * Generate query parameter for an array of relative time stamps (continuous) 448 * 449 * @param num Number of elements in @a elements 450 * @param elements Array of @a num relative timestamps 451 * @return query parameter to use 452 */ 453 struct GNUNET_PQ_QueryParam 454 GNUNET_PQ_query_param_array_rel_time ( 455 unsigned int num, 456 const struct GNUNET_TIME_Relative *elements, 457 struct GNUNET_PQ_Context *db); 458 459 /** 460 * Generate query parameter for an array of relative time stamps (pointers) 461 * 462 * @param num Number of elements in @a elements 463 * @param elements Array of @a num pointers to relative timestamps 464 * @return query parameter to use 465 */ 466 struct GNUNET_PQ_QueryParam 467 GNUNET_PQ_query_param_array_ptrs_rel_time ( 468 unsigned int num, 469 const struct GNUNET_TIME_Relative *elements[], 470 struct GNUNET_PQ_Context *db); 471 472 /** 473 * Generate query parameter for an array of time stamps (continuous) 474 * 475 * @param num Number of elements in @a elements 476 * @param elements Array of @a num timestamps 477 * @return query parameter to use 478 */ 479 struct GNUNET_PQ_QueryParam 480 GNUNET_PQ_query_param_array_timestamp ( 481 unsigned int num, 482 const struct GNUNET_TIME_Timestamp *elements, 483 struct GNUNET_PQ_Context *db); 484 485 /** 486 * Generate query parameter for an array of time stamps (pointers) 487 * 488 * @param num Number of elements in @a elements 489 * @param elements Array of @a num pointers to relative timestamps 490 * @return query parameter to use 491 */ 492 struct GNUNET_PQ_QueryParam 493 GNUNET_PQ_query_param_array_ptrs_timestamp ( 494 unsigned int num, 495 const struct GNUNET_TIME_Timestamp *elements[], 496 struct GNUNET_PQ_Context *db); 497 498 /** 499 * Generate query parameter for an RSA public key. The 500 * database must contain a BLOB type in the respective position. 501 * 502 * @param x the query parameter to pass. 503 * @return query parameter to use 504 */ 505 struct GNUNET_PQ_QueryParam 506 GNUNET_PQ_query_param_rsa_public_key ( 507 const struct GNUNET_CRYPTO_RsaPublicKey *x); 508 509 510 /** 511 * Generate query parameter for an RSA signature. The 512 * database must contain a BLOB type in the respective position. 513 * 514 * @param x the query parameter to pass 515 * @return query parameter to use 516 */ 517 struct GNUNET_PQ_QueryParam 518 GNUNET_PQ_query_param_rsa_signature ( 519 const struct GNUNET_CRYPTO_RsaSignature *x); 520 521 522 /** 523 * Generate query parameter for a relative time value. 524 * The database must store a 64-bit integer. 525 * 526 * @param x pointer to the query parameter to pass 527 * @return query parameter to use 528 */ 529 struct GNUNET_PQ_QueryParam 530 GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x); 531 532 533 /** 534 * Generate query parameter for an absolute time value. 535 * The database must store a 64-bit integer. 536 * 537 * @param x pointer to the query parameter to pass 538 * @return query parameter to use 539 */ 540 struct GNUNET_PQ_QueryParam 541 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x); 542 543 544 /** 545 * Generate query parameter for a timestamp. 546 * The database must store a 64-bit integer. 547 * 548 * @param x pointer to the query parameter to pass 549 * @return query parameter to use 550 */ 551 struct GNUNET_PQ_QueryParam 552 GNUNET_PQ_query_param_timestamp (const struct GNUNET_TIME_Timestamp *x); 553 554 555 /** 556 * Generate query parameter for an absolute time value. 557 * The database must store a 64-bit integer. 558 * 559 * @param x pointer to the query parameter to pass 560 * @return query parameter to use 561 */ 562 struct GNUNET_PQ_QueryParam 563 GNUNET_PQ_query_param_absolute_time_nbo ( 564 const struct GNUNET_TIME_AbsoluteNBO *x); 565 566 567 /** 568 * Generate query parameter for a timestamp in NBO. 569 * The database must store a 64-bit integer. 570 * 571 * @param x pointer to the query parameter to pass 572 * @return query parameter to use 573 */ 574 struct GNUNET_PQ_QueryParam 575 GNUNET_PQ_query_param_timestamp_nbo ( 576 const struct GNUNET_TIME_TimestampNBO *x); 577 578 579 /** 580 * Generate query parameter for an uint16_t in host byte order. 581 * 582 * @param x pointer to the query parameter to pass 583 * @return query parameter to use 584 */ 585 struct GNUNET_PQ_QueryParam 586 GNUNET_PQ_query_param_uint16 (const uint16_t *x); 587 588 589 /** 590 * Generate query parameter for an uint32_t in host byte order. 591 * 592 * @param x pointer to the query parameter to pass 593 * @return query parameter to use 594 */ 595 struct GNUNET_PQ_QueryParam 596 GNUNET_PQ_query_param_uint32 (const uint32_t *x); 597 598 599 /** 600 * Generate query parameter for an uint64_t in host byte order. 601 * 602 * @param x pointer to the query parameter to pass 603 * @return query parameter to use 604 */ 605 struct GNUNET_PQ_QueryParam 606 GNUNET_PQ_query_param_uint64 (const uint64_t *x); 607 608 609 /** 610 * Generate query parameter for an int16_t in host byte order. 611 * 612 * @param x pointer to the query parameter to pass 613 * @return query parameter to use 614 */ 615 struct GNUNET_PQ_QueryParam 616 GNUNET_PQ_query_param_int16 (const int16_t *x); 617 618 /** 619 * Generate query parameter for an int64_t in host byte order. 620 * 621 * @param x pointer to the query parameter to pass 622 * @return query parameter to use 623 */ 624 struct GNUNET_PQ_QueryParam 625 GNUNET_PQ_query_param_int64 (const int64_t *x); 626 627 /** 628 * Generate query parameter for a blind sign public key. 629 * Internally, the various attributes of the public key 630 * will be serialized into on variable-size BLOB. 631 * 632 * @param pub pointer to the query parameter to pass 633 * 634 */ 635 struct GNUNET_PQ_QueryParam 636 GNUNET_PQ_query_param_blind_sign_pub ( 637 const struct GNUNET_CRYPTO_BlindSignPublicKey *pub); 638 639 /** 640 * Generate query parameter for a blind sign private key of variable size. 641 * 642 * @param priv pointer to the query parameter to pass 643 * 644 */ 645 struct GNUNET_PQ_QueryParam 646 GNUNET_PQ_query_param_blind_sign_priv ( 647 const struct GNUNET_CRYPTO_BlindSignPrivateKey *priv); 648 649 /** 650 * Generate query parameter for an unblinded signature of variable size. 651 * 652 * @param sig pointer to the query parameter to pass 653 * 654 */ 655 struct GNUNET_PQ_QueryParam 656 GNUNET_PQ_query_param_unblinded_sig ( 657 const struct GNUNET_CRYPTO_UnblindedSignature *sig); 658 659 /** 660 * Generate query parameter for a blinded signature of variable size. 661 * 662 * @param b_sig pointer to the query parameter to pass 663 * 664 */ 665 struct GNUNET_PQ_QueryParam 666 GNUNET_PQ_query_param_blinded_sig ( 667 const struct GNUNET_CRYPTO_BlindedSignature *b_sig); 668 669 /* ************************* pq_result_helper.c functions ************************ */ 670 671 672 /** 673 * Extract data from a Postgres database @a result at row @a row. 674 * 675 * @param cls closure 676 * @param result where to extract data from 677 * @param int row to extract data from 678 * @param fname name (or prefix) of the fields to extract from 679 * @param[in,out] dst_size where to store size of result, may be NULL 680 * @param[out] dst where to store the result 681 * @return 682 * #GNUNET_YES if all results could be extracted 683 * #GNUNET_NO if the field was NULL 684 * #GNUNET_SYSERR if a result was invalid (non-existing field) 685 */ 686 typedef enum GNUNET_GenericReturnValue 687 (*GNUNET_PQ_ResultConverter)(void *cls, 688 PGresult *result, 689 int row, 690 const char *fname, 691 size_t *dst_size, 692 void *dst); 693 694 695 /** 696 * Function called to clean up memory allocated 697 * by a #GNUNET_PQ_ResultConverter. 698 * 699 * @param cls closure 700 * @param rd result data to clean up 701 */ 702 typedef void 703 (*GNUNET_PQ_ResultCleanup) (void *cls, 704 void *rd); 705 706 707 /** 708 * @brief Description of a DB result cell. 709 */ 710 struct GNUNET_PQ_ResultSpec 711 { 712 /** 713 * What is the format of the result? 714 */ 715 GNUNET_PQ_ResultConverter conv; 716 717 /** 718 * Function to clean up result data, NULL if cleanup is not necessary. 719 */ 720 GNUNET_PQ_ResultCleanup cleaner; 721 722 /** 723 * Closure for @e conv and @e cleaner. 724 */ 725 void *cls; 726 727 /** 728 * Destination for the data. 729 */ 730 void *dst; 731 732 /** 733 * Allowed size for the data, 0 for variable-size (in this case, the type of 734 * @e dst is a `void **` and we need to allocate a buffer of the right 735 * size). 736 */ 737 size_t dst_size; 738 739 /** 740 * Field name of the desired result. 741 */ 742 const char *fname; 743 744 /** 745 * Where to store actual size of the result. 746 */ 747 size_t *result_size; 748 749 /** 750 * True if NULL is allowed for a value in the database. 751 */ 752 bool is_nullable; 753 754 /** 755 * Points to a location where we should store "true" if the result found is 756 * NULL, and otherwise "false". Only used if @e is_nullable is true. 757 */ 758 bool *is_null; 759 760 }; 761 762 763 /** 764 * End of result parameter specification. 765 * 766 * @return array last entry for the result specification to use 767 */ 768 #define GNUNET_PQ_result_spec_end \ 769 { \ 770 .conv = NULL, \ 771 .cleaner = NULL, \ 772 .cls = NULL, \ 773 .dst = NULL, \ 774 .dst_size = 0, \ 775 .fname = NULL, \ 776 .result_size = NULL, \ 777 .is_nullable = false, \ 778 .is_null = NULL \ 779 } 780 781 782 /** 783 * Allow NULL value to be found in the database for the given value. 784 * 785 * @param rs result spec entry to modify 786 * @param[out] is_null location set to 'true' if the 787 * value was indeed NULL, set to 'false' if the 788 * value was non-NULL 789 * @return array entry for the result specification to use 790 */ 791 struct GNUNET_PQ_ResultSpec 792 GNUNET_PQ_result_spec_allow_null (struct GNUNET_PQ_ResultSpec rs, 793 bool *is_null); 794 795 796 /** 797 * Variable-size result expected. 798 * 799 * @param name name of the field in the table 800 * @param[out] dst where to store the result, allocated 801 * @param[out] sptr where to store the size of @a dst 802 * @return array entry for the result specification to use 803 */ 804 struct GNUNET_PQ_ResultSpec 805 GNUNET_PQ_result_spec_variable_size (const char *name, 806 void **dst, 807 size_t *sptr); 808 809 810 /** 811 * Fixed-size result expected. 812 * 813 * @param name name of the field in the table 814 * @param[out] dst where to store the result 815 * @param dst_size number of bytes in @a dst 816 * @return array entry for the result specification to use 817 */ 818 struct GNUNET_PQ_ResultSpec 819 GNUNET_PQ_result_spec_fixed_size (const char *name, 820 void *dst, 821 size_t dst_size); 822 823 824 /** 825 * We expect a fixed-size result, with size determined by the type of `* dst` 826 * 827 * @param name name of the field in the table 828 * @param dst point to where to store the result, type fits expected result size 829 * @return array entry for the result specification to use 830 */ 831 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) \ 832 GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst))) 833 834 835 /** 836 * 0-terminated string expected. 837 * 838 * @param name name of the field in the table 839 * @param[out] dst where to store the result, allocated 840 * @return array entry for the result specification to use 841 */ 842 struct GNUNET_PQ_ResultSpec 843 GNUNET_PQ_result_spec_string (const char *name, 844 char **dst); 845 846 847 /** 848 * boolean expected. 849 * 850 * @param name name of the field in the table 851 * @param[out] dst where to store the result 852 * @return array entry for the result specification to use 853 */ 854 struct GNUNET_PQ_ResultSpec 855 GNUNET_PQ_result_spec_bool (const char *name, 856 bool *dst); 857 858 859 /** 860 * RSA public key expected. 861 * 862 * @param name name of the field in the table 863 * @param[out] rsa where to store the result 864 * @return array entry for the result specification to use 865 */ 866 struct GNUNET_PQ_ResultSpec 867 GNUNET_PQ_result_spec_rsa_public_key (const char *name, 868 struct GNUNET_CRYPTO_RsaPublicKey **rsa); 869 870 871 /** 872 * RSA signature expected. 873 * 874 * @param name name of the field in the table 875 * @param[out] sig where to store the result; 876 * @return array entry for the result specification to use 877 */ 878 struct GNUNET_PQ_ResultSpec 879 GNUNET_PQ_result_spec_rsa_signature (const char *name, 880 struct GNUNET_CRYPTO_RsaSignature **sig); 881 882 883 /** 884 * Absolute time expected. 885 * 886 * @param name name of the field in the table 887 * @param[out] at where to store the result 888 * @return array entry for the result specification to use 889 */ 890 struct GNUNET_PQ_ResultSpec 891 GNUNET_PQ_result_spec_absolute_time (const char *name, 892 struct GNUNET_TIME_Absolute *at); 893 894 895 /** 896 * Timestamp expected. 897 * 898 * @param name name of the field in the table 899 * @param[out] t where to store the result 900 * @return array entry for the result specification to use 901 */ 902 struct GNUNET_PQ_ResultSpec 903 GNUNET_PQ_result_spec_timestamp (const char *name, 904 struct GNUNET_TIME_Timestamp *t); 905 906 907 /** 908 * Relative time expected. 909 * 910 * @param name name of the field in the table 911 * @param[out] rt where to store the result 912 * @return array entry for the result specification to use 913 */ 914 struct GNUNET_PQ_ResultSpec 915 GNUNET_PQ_result_spec_relative_time (const char *name, 916 struct GNUNET_TIME_Relative *rt); 917 918 919 /** 920 * Absolute time expected. 921 * 922 * @param name name of the field in the table 923 * @param[out] at where to store the result 924 * @return array entry for the result specification to use 925 */ 926 struct GNUNET_PQ_ResultSpec 927 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, 928 struct GNUNET_TIME_AbsoluteNBO *at); 929 930 931 /** 932 * Timestamp expected. 933 * 934 * @param name name of the field in the table 935 * @param[out] tn where to store the result 936 * @return array entry for the result specification to use 937 */ 938 struct GNUNET_PQ_ResultSpec 939 GNUNET_PQ_result_spec_timestamp_nbo (const char *name, 940 struct GNUNET_TIME_TimestampNBO *tn); 941 942 943 /** 944 * uint16_t expected. 945 * 946 * @param name name of the field in the table 947 * @param[out] u16 where to store the result 948 * @return array entry for the result specification to use 949 */ 950 struct GNUNET_PQ_ResultSpec 951 GNUNET_PQ_result_spec_uint16 (const char *name, 952 uint16_t *u16); 953 954 955 /** 956 * uint32_t expected. 957 * 958 * @param name name of the field in the table 959 * @param[out] u32 where to store the result 960 * @return array entry for the result specification to use 961 */ 962 struct GNUNET_PQ_ResultSpec 963 GNUNET_PQ_result_spec_uint32 (const char *name, 964 uint32_t *u32); 965 966 967 /** 968 * uint64_t expected. 969 * 970 * @param name name of the field in the table 971 * @param[out] u64 where to store the result 972 * @return array entry for the result specification to use 973 */ 974 struct GNUNET_PQ_ResultSpec 975 GNUNET_PQ_result_spec_uint64 (const char *name, 976 uint64_t *u64); 977 978 979 /** 980 * int16_t expected. 981 * 982 * @param name name of the field in the table 983 * @param[out] i16 where to store the result 984 * @return array entry for the result specification to use 985 */ 986 struct GNUNET_PQ_ResultSpec 987 GNUNET_PQ_result_spec_int16 (const char *name, 988 int16_t *i16); 989 990 991 /** 992 * int64_t expected. 993 * 994 * @param name name of the field in the table 995 * @param[out] i64 where to store the result 996 * @return array entry for the result specification to use 997 */ 998 struct GNUNET_PQ_ResultSpec 999 GNUNET_PQ_result_spec_int64 (const char *name, 1000 int64_t *i64); 1001 1002 /** 1003 * Allow NULL values in an array to be found in the database for the given value. 1004 * 1005 * @param rs result spec entry to modify, MUST be one for the _array_ types 1006 * @param[out] is_nulls location where to put the array of bools, 1007 * whose elements will be set to 'true' if the value was indeed NULL, 1008 * or to 'false' if the value was non-NULL. 1009 * This parameter MUST NOT be NULL itself. 1010 * The length of the allocated array will be equal to the length of 1011 * the result for the values in @a rs. 1012 * @return array entry for the result specification to use 1013 */ 1014 struct GNUNET_PQ_ResultSpec 1015 GNUNET_PQ_result_spec_array_allow_nulls ( 1016 struct GNUNET_PQ_ResultSpec rs, 1017 bool **is_nulls); 1018 1019 /** 1020 * array of bool expected. 1021 * 1022 * @param db Database context, needed for OID lookup for the correct type 1023 * @param name name of the field in the table 1024 * @param[out] num where to store the number of elements in the array @a bools. 1025 * @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. 1026 * @return array entry for the result specification to use 1027 */ 1028 struct GNUNET_PQ_ResultSpec 1029 GNUNET_PQ_result_spec_array_bool ( 1030 struct GNUNET_PQ_Context *db, 1031 const char *name, 1032 size_t *num, 1033 bool **bools); 1034 1035 /** 1036 * array of uint16_t expected. 1037 * 1038 * @param db Database context, needed for OID lookup for the correct type 1039 * @param name name of the field in the table 1040 * @param[out] num where to store the number of elements in the array @a u16s. 1041 * @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. 1042 * @return array entry for the result specification to use 1043 */ 1044 struct GNUNET_PQ_ResultSpec 1045 GNUNET_PQ_result_spec_array_uint16 ( 1046 struct GNUNET_PQ_Context *db, 1047 const char *name, 1048 size_t *num, 1049 uint16_t **dst); 1050 1051 /** 1052 * array of uint32_t expected. 1053 * 1054 * @param db Database context, needed for OID lookup for the correct type 1055 * @param name name of the field in the table 1056 * @param[out] num where to store the number of elements in the array @a u32s. 1057 * @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. 1058 * @return array entry for the result specification to use 1059 */ 1060 struct GNUNET_PQ_ResultSpec 1061 GNUNET_PQ_result_spec_array_uint32 ( 1062 struct GNUNET_PQ_Context *db, 1063 const char *name, 1064 size_t *num, 1065 uint32_t **dst); 1066 1067 /** 1068 * array of uint64_t expected. 1069 * 1070 * @param db Database context, needed for OID lookup for the correct type 1071 * @param name name of the field in the table 1072 * @param[out] num where to store the number of elements in the array @a u64s. 1073 * @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. 1074 * @return array entry for the result specification to use 1075 */ 1076 struct GNUNET_PQ_ResultSpec 1077 GNUNET_PQ_result_spec_array_uint64 ( 1078 struct GNUNET_PQ_Context *db, 1079 const char *name, 1080 size_t *num, 1081 uint64_t **dst); 1082 1083 1084 /** 1085 * array of absolute time expected. 1086 * 1087 * @param db Database context, needed for OID lookup for the correct type 1088 * @param name name of the field in the table 1089 * @param[out] num where to store the number of elements in the array @a u64s. 1090 * @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. 1091 * @return array entry for the result specification to use 1092 */ 1093 struct GNUNET_PQ_ResultSpec 1094 GNUNET_PQ_result_spec_array_abs_time ( 1095 struct GNUNET_PQ_Context *db, 1096 const char *name, 1097 size_t *num, 1098 struct GNUNET_TIME_Absolute **dst); 1099 1100 /** 1101 * array of relative time expected. 1102 * 1103 * @param db Database context, needed for OID lookup for the correct type 1104 * @param name name of the field in the table 1105 * @param[out] num where to store the number of elements in the array @a u64s. 1106 * @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. 1107 * @return array entry for the result specification to use 1108 */ 1109 struct GNUNET_PQ_ResultSpec 1110 GNUNET_PQ_result_spec_array_rel_time ( 1111 struct GNUNET_PQ_Context *db, 1112 const char *name, 1113 size_t *num, 1114 struct GNUNET_TIME_Relative **dst); 1115 1116 /** 1117 * array of relative time expected. 1118 * 1119 * @param db Database context, needed for OID lookup for the correct type 1120 * @param name name of the field in the table 1121 * @param[out] num where to store the number of elements in the array @a u64s. 1122 * @param[out] dst pointer to where to store the array of @a num timestamps. Allocated by the function, MUST be freed with GNUNET_free. 1123 * @return array entry for the result specification to use 1124 */ 1125 1126 struct GNUNET_PQ_ResultSpec 1127 GNUNET_PQ_result_spec_array_timestamp ( 1128 struct GNUNET_PQ_Context *db, 1129 const char *name, 1130 size_t *num, 1131 struct GNUNET_TIME_Timestamp **dst); 1132 1133 /** 1134 * Array of variable-size result expected. 1135 * 1136 * @param db Database context, needed for OID lookup for the correct type 1137 * @param name name of the field in the table 1138 * @param[out] num where to store the number of elements 1139 * @param[out] sizes where to store the @a num size's of byte-buffers in @a dst 1140 * @param[out] dst where to store the continuous array of @a num byte-buffers of sizes given in @a size, allocated 1141 * @return array entry for the result specification to use 1142 */ 1143 struct GNUNET_PQ_ResultSpec 1144 GNUNET_PQ_result_spec_array_variable_size ( 1145 struct GNUNET_PQ_Context *db, 1146 const char *name, 1147 size_t *num, 1148 size_t **sizes, 1149 void **dst); 1150 1151 1152 /** 1153 * Array of fixed-size result expected. 1154 * 1155 * @param db Database context, needed for OID lookup for the correct type 1156 * @param name name of the field in the table 1157 * @param size number of bytes expected in each element of @a dst 1158 * @param[out] num where to store the number of elements 1159 * @param[out] dst where to store the results, an continuous array of @num fixed-size elements 1160 * @return array entry for the result specification to use 1161 */ 1162 struct GNUNET_PQ_ResultSpec 1163 GNUNET_PQ_result_spec_array_fixed_size ( 1164 struct GNUNET_PQ_Context *db, 1165 const char *name, 1166 size_t size, 1167 size_t *num, 1168 void **dst); 1169 1170 1171 /** 1172 * We expect a fixed-size result, with size determined by the type of `* dst` 1173 * 1174 * @param db Database context, needed for OID lookup for the correct type 1175 * @param name name of the field in the table 1176 * @param num pointer to where to store the number of elements 1177 * @param dst pointer to where to store the results, type fits expected result size 1178 * @return array entry for the result specification to use 1179 */ 1180 #define GNUNET_PQ_result_spec_auto_array_from_type(db, name, num, dst) \ 1181 GNUNET_PQ_result_spec_array_fixed_size ( \ 1182 (db), \ 1183 (name), \ 1184 sizeof(*(dst)), \ 1185 (num), \ 1186 (void *) &(dst)) 1187 1188 1189 /** 1190 * Array of 0-terminated strings expected. 1191 * 1192 * @param db Database context, needed for OID lookup for the correct type 1193 * @param name name of the field in the table 1194 * @param[out] num where to store the number of elements 1195 * @param[out] dst where to store the allocated continuous array of @a num 0-terminated strings 1196 * @return array entry for the result specification to use 1197 */ 1198 struct GNUNET_PQ_ResultSpec 1199 GNUNET_PQ_result_spec_array_string ( 1200 struct GNUNET_PQ_Context *db, 1201 const char *name, 1202 size_t *num, 1203 char **dst); 1204 1205 1206 /** 1207 * Blind sign public key expected. 1208 * 1209 * @param name name of the field in the table 1210 * @param[out] public_key where to store the denomination signature 1211 * @return array entry for the result specification to use 1212 */ 1213 struct GNUNET_PQ_ResultSpec 1214 GNUNET_PQ_result_spec_blind_sign_pub ( 1215 const char *name, 1216 struct GNUNET_CRYPTO_BlindSignPublicKey **public_key); 1217 1218 1219 /** 1220 * Blinded signature expected. 1221 * 1222 * @param name name of the field in the table 1223 * @param[out] b_sig where to store the blinded signature 1224 * @return array entry for the result specification to use 1225 */ 1226 struct GNUNET_PQ_ResultSpec 1227 GNUNET_PQ_result_spec_blinded_sig ( 1228 const char *name, 1229 struct GNUNET_CRYPTO_BlindedSignature **b_sig); 1230 1231 1232 /** 1233 * Unblinded signature expected. 1234 * 1235 * @param name name of the field in the table 1236 * @param[out] ub_sig where to store the unblinded signature 1237 * @return array entry for the result specification to use 1238 */ 1239 struct GNUNET_PQ_ResultSpec 1240 GNUNET_PQ_result_spec_unblinded_sig ( 1241 const char *name, 1242 struct GNUNET_CRYPTO_UnblindedSignature **ub_sig); 1243 1244 1245 /** 1246 * Blind sign private key expected. 1247 * 1248 * @param name name of the field in the table 1249 * @param[out] private_key where to store the denomination signature 1250 * @return array entry for the result specification to use 1251 */ 1252 struct GNUNET_PQ_ResultSpec 1253 GNUNET_PQ_result_spec_blind_sign_priv ( 1254 const char *name, 1255 struct GNUNET_CRYPTO_BlindSignPrivateKey **private_key); 1256 1257 /* ************************* pq.c functions ************************ */ 1258 1259 /** 1260 * Execute a prepared statement. 1261 * 1262 * @param db database context 1263 * @param name name of the prepared statement 1264 * @param params parameters to the statement 1265 * @return postgres result 1266 * @deprecated (should become an internal API) 1267 */ 1268 PGresult * 1269 GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db, 1270 const char *name, 1271 const struct GNUNET_PQ_QueryParam *params); 1272 1273 1274 /** 1275 * Extract results from a query result according to the given specification. 1276 * 1277 * @param result result to process 1278 * @param[in,out] rs result specification to extract for 1279 * @param row row from the result to extract 1280 * @return 1281 * #GNUNET_YES if all results could be extracted 1282 * #GNUNET_SYSERR if a result was invalid (non-existing field) 1283 */ 1284 enum GNUNET_GenericReturnValue 1285 GNUNET_PQ_extract_result (PGresult *result, 1286 struct GNUNET_PQ_ResultSpec *rs, 1287 int row); 1288 1289 1290 /** 1291 * Free all memory that was allocated in @a rs during 1292 * #GNUNET_PQ_extract_result(). 1293 * 1294 * @param rs result specification to clean up 1295 */ 1296 void 1297 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs); 1298 1299 1300 /* ******************** pq_eval.c functions ************** */ 1301 1302 1303 /** 1304 * Check the @a result's error code to see what happened. 1305 * Also logs errors. 1306 * 1307 * @param db database to execute the statement in 1308 * @param statement_name name of the statement that created @a result 1309 * @param result result to check 1310 * @return status code from the result, mapping PQ status 1311 * codes to `enum GNUNET_DB_QueryStatus`. Never 1312 * returns positive values as this function does 1313 * not look at the result set. 1314 * @deprecated (low level, let's see if we can do with just the high-level functions) 1315 */ 1316 enum GNUNET_DB_QueryStatus 1317 GNUNET_PQ_eval_result (struct GNUNET_PQ_Context *db, 1318 const char *statement_name, 1319 PGresult *result); 1320 1321 1322 /** 1323 * Execute a named prepared @a statement that is NOT a SELECT 1324 * statement in @a connection using the given @a params. Returns the 1325 * resulting session state. 1326 * 1327 * @param db database to execute the statement with 1328 * @param statement_name name of the statement 1329 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) 1330 * @return status code from the result, mapping PQ status 1331 * codes to `enum GNUNET_DB_QueryStatus`. If the 1332 * statement was a DELETE or UPDATE statement, the 1333 * number of affected rows is returned; if the 1334 * statement was an INSERT statement, and no row 1335 * was added due to a UNIQUE violation, we return 1336 * zero; if INSERT was successful, we return one. 1337 */ 1338 enum GNUNET_DB_QueryStatus 1339 GNUNET_PQ_eval_prepared_non_select (struct GNUNET_PQ_Context *db, 1340 const char *statement_name, 1341 const struct GNUNET_PQ_QueryParam *params); 1342 1343 1344 /** 1345 * Function to be called with the results of a SELECT statement 1346 * that has returned @a num_results results. 1347 * 1348 * @param cls closure 1349 * @param result the postgres result 1350 * @param num_result the number of results in @a result 1351 */ 1352 typedef void 1353 (*GNUNET_PQ_PostgresResultHandler) (void *cls, 1354 PGresult *result, 1355 unsigned int num_results); 1356 1357 1358 /** 1359 * Execute a named prepared @a statement that is a SELECT statement 1360 * which may return multiple results in @a connection using the given 1361 * @a params. Call @a rh with the results. Returns the query 1362 * status including the number of results given to @a rh (possibly zero). 1363 * @a rh will not have been called if the return value is negative. 1364 * 1365 * @param db database to execute the statement with 1366 * @param statement_name name of the statement 1367 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) 1368 * @param rh function to call with the result set, NULL to ignore 1369 * @param rh_cls closure to pass to @a rh 1370 * @return status code from the result, mapping PQ status 1371 * codes to `enum GNUNET_DB_QueryStatus`. 1372 */ 1373 enum GNUNET_DB_QueryStatus 1374 GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db, 1375 const char *statement_name, 1376 const struct GNUNET_PQ_QueryParam *params, 1377 GNUNET_PQ_PostgresResultHandler rh, 1378 void *rh_cls); 1379 1380 1381 /** 1382 * Execute a named prepared @a statement that is a SELECT statement 1383 * which must return a single result in @a connection using the given 1384 * @a params. Stores the result (if any) in @a rs, which the caller 1385 * must then clean up using #GNUNET_PQ_cleanup_result() if the return 1386 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the 1387 * resulting session status. 1388 * 1389 * @param db database to execute the statement with 1390 * @param statement_name name of the statement 1391 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) 1392 * @param[in,out] rs result specification to use for storing the result of the query 1393 * @return status code from the result, mapping PQ status 1394 * codes to `enum GNUNET_DB_QueryStatus`. 1395 */ 1396 enum GNUNET_DB_QueryStatus 1397 GNUNET_PQ_eval_prepared_singleton_select ( 1398 struct GNUNET_PQ_Context *db, 1399 const char *statement_name, 1400 const struct GNUNET_PQ_QueryParam *params, 1401 struct GNUNET_PQ_ResultSpec *rs); 1402 1403 1404 /* ******************** pq_prepare.c functions ************** */ 1405 1406 1407 /** 1408 * Information needed to prepare a list of SQL statements using 1409 * #GNUNET_PQ_prepare_statements(). 1410 */ 1411 struct GNUNET_PQ_PreparedStatement 1412 { 1413 /** 1414 * Name of the statement. 1415 */ 1416 const char *name; 1417 1418 /** 1419 * Actual SQL statement. 1420 */ 1421 const char *sql; 1422 1423 }; 1424 1425 1426 /** 1427 * Terminator for prepared statement list. 1428 */ 1429 #define GNUNET_PQ_PREPARED_STATEMENT_END \ 1430 { \ 1431 NULL, NULL \ 1432 } 1433 1434 1435 /** 1436 * Create a `struct GNUNET_PQ_PreparedStatement`. 1437 * 1438 * @param name name of the statement 1439 * @param sql actual SQL statement 1440 * @return initialized struct 1441 */ 1442 struct GNUNET_PQ_PreparedStatement 1443 GNUNET_PQ_make_prepare (const char *name, 1444 const char *sql); 1445 1446 1447 /** 1448 * Request creation of prepared statements @a ps from Postgres. 1449 * 1450 * @param db database to prepare the statements for 1451 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared 1452 * statements. 1453 * @return #GNUNET_OK on success, 1454 * #GNUNET_SYSERR on error 1455 */ 1456 enum GNUNET_GenericReturnValue 1457 GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, 1458 const struct GNUNET_PQ_PreparedStatement *ps); 1459 1460 1461 /** 1462 * Request creation of prepared statements @a ps from Postgres, but do not 1463 * automatically re-prepare the statement if we are disconnected from the 1464 * database. 1465 * 1466 * @param db database to prepare the statements for 1467 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared 1468 * statements. 1469 * @return #GNUNET_OK on success, 1470 * #GNUNET_SYSERR on error 1471 */ 1472 enum GNUNET_GenericReturnValue 1473 GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db, 1474 const struct GNUNET_PQ_PreparedStatement *ps); 1475 1476 1477 /* ******************** pq_exec.c functions ************** */ 1478 1479 1480 /** 1481 * Information needed to run a list of SQL statements using 1482 * #GNUNET_PQ_exec_statements(). 1483 */ 1484 struct GNUNET_PQ_ExecuteStatement 1485 { 1486 /** 1487 * Actual SQL statement. 1488 */ 1489 const char *sql; 1490 1491 /** 1492 * Should we ignore errors? 1493 */ 1494 int ignore_errors; 1495 }; 1496 1497 1498 /** 1499 * Terminator for executable statement list. 1500 */ 1501 #define GNUNET_PQ_EXECUTE_STATEMENT_END \ 1502 { \ 1503 NULL, GNUNET_SYSERR \ 1504 } 1505 1506 1507 /** 1508 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal. 1509 * 1510 * @param sql actual SQL statement 1511 * @return initialized struct 1512 */ 1513 struct GNUNET_PQ_ExecuteStatement 1514 GNUNET_PQ_make_execute (const char *sql); 1515 1516 1517 /** 1518 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should 1519 * be tolerated. 1520 * 1521 * @param sql actual SQL statement 1522 * @return initialized struct 1523 */ 1524 struct GNUNET_PQ_ExecuteStatement 1525 GNUNET_PQ_make_try_execute (const char *sql); 1526 1527 1528 /** 1529 * Request execution of an array of statements @a es from Postgres. 1530 * 1531 * @param db database to execute the statements in 1532 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared 1533 * statements. 1534 * @return #GNUNET_OK on success (modulo statements where errors can be ignored) 1535 * #GNUNET_SYSERR on error 1536 */ 1537 enum GNUNET_GenericReturnValue 1538 GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db, 1539 const struct GNUNET_PQ_ExecuteStatement *es); 1540 1541 1542 /* ******************** pq_connect.c functions ************** */ 1543 1544 1545 /** 1546 * Flags to control PQ operation. 1547 */ 1548 enum GNUNET_PQ_Options 1549 { 1550 /** 1551 * Traditional default. Do nothing special. 1552 */ 1553 GNUNET_PQ_FLAG_NONE = 0, 1554 1555 /** 1556 * Dropping database. Do not attempt to initialize 1557 * versioning schema if not present. 1558 */ 1559 GNUNET_PQ_FLAG_DROP = 1, 1560 1561 /** 1562 * Check database version is current. Fail to connect if it is not. 1563 */ 1564 GNUNET_PQ_FLAG_CHECK_CURRENT = 2 1565 }; 1566 1567 1568 /** 1569 * Create a connection to the Postgres database using @a config_str for the 1570 * configuration. Initialize logging via GNUnet's log routines and disable 1571 * Postgres's logger. Also ensures that the statements in @a load_path and @a 1572 * es are executed whenever we (re)connect to the database, and that the 1573 * prepared statements in @a ps are "ready". If statements in @a es fail that 1574 * were created with #GNUNET_PQ_make_execute(), then the entire operation 1575 * fails. 1576 * 1577 * In @a load_path, a list of "$XXXX.sql" files is expected where $XXXX 1578 * must be a sequence of contiguous integer values starting at 0000. 1579 * These files are then loaded in sequence using "psql $config_str" before 1580 * running statements from @a es. The directory is inspected again on 1581 * reconnect. 1582 * 1583 * @param config_str configuration to use 1584 * @param load_path path to directory with SQL transactions to run, can be NULL 1585 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated 1586 * array of statements to execute upon EACH connection, can be NULL 1587 * @param ps array of prepared statements to prepare, can be NULL 1588 * @return NULL on error 1589 */ 1590 struct GNUNET_PQ_Context * 1591 GNUNET_PQ_connect (const char *config_str, 1592 const char *load_path, 1593 const struct GNUNET_PQ_ExecuteStatement *es, 1594 const struct GNUNET_PQ_PreparedStatement *ps); 1595 1596 1597 /** 1598 * Execute SQL statements from @a buf against @a db. 1599 * The given filename infix in @a buf is prefixed with 1600 * the "load_path" and ".sql" is appended to construct 1601 * the full filename. 1602 * 1603 * @param db database context to use 1604 * @param buf filename infix (!) with the SQL code to run 1605 * @return #GNUNET_OK on success, #GNUNET_NO if patch @a buf does not exist, #GNUNET_SYSERR on error 1606 */ 1607 enum GNUNET_GenericReturnValue 1608 GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db, 1609 const char *buf); 1610 1611 1612 /** 1613 * Create a connection to the Postgres database using @a config_str for the 1614 * configuration. Initialize logging via GNUnet's log routines and disable 1615 * Postgres's logger. Also ensures that the statements in @a load_path and @a 1616 * es are executed whenever we (re)connect to the database, and that the 1617 * prepared statements in @a ps are "ready". If statements in @a es fail that 1618 * were created with #GNUNET_PQ_make_execute(), then the entire operation 1619 * fails. 1620 * 1621 * In @a load_path, a list of "$XXXX.sql" files is expected where $XXXX 1622 * must be a sequence of contiguous integer values starting at 0000. 1623 * These files are then loaded in sequence using "psql $config_str" before 1624 * running statements from @e es. The directory is inspected again on 1625 * reconnect. 1626 * 1627 * @param config_str configuration to use 1628 * @param load_path path to directory with SQL transactions to run, can be NULL 1629 * @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 (consecutive). 1630 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated 1631 * array of statements to execute upon EACH connection, can be NULL 1632 * @param ps array of prepared statements to prepare, can be NULL 1633 * @param flags connection flags 1634 * @return NULL on error 1635 */ 1636 struct GNUNET_PQ_Context * 1637 GNUNET_PQ_connect2 (const char *config_str, 1638 const char *load_path, 1639 const char *auto_suffix, 1640 const struct GNUNET_PQ_ExecuteStatement *es, 1641 const struct GNUNET_PQ_PreparedStatement *ps, 1642 enum GNUNET_PQ_Options flags); 1643 1644 1645 /** 1646 * Connect to a postgres database using the configuration option "CONFIG" in 1647 * @a section. Also ensures that the statements in @a es are executed 1648 * whenever we (re)connect to the database, and that the prepared statements 1649 * in @a ps are "ready". 1650 * 1651 * The caller does not have to ensure that @a es and @a ps remain allocated 1652 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a copy will be made. 1653 * 1654 * @param cfg configuration 1655 * @param section configuration section to use to get Postgres configuration options 1656 * @param load_path_suffix suffix to append to the SQL_DIR in the configuration 1657 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated 1658 * array of statements to execute upon EACH connection, can be NULL 1659 * @param ps array of prepared statements to prepare, can be NULL 1660 * @return the postgres handle, NULL on error 1661 */ 1662 struct GNUNET_PQ_Context * 1663 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, 1664 const char *section, 1665 const char *load_path_suffix, 1666 const struct GNUNET_PQ_ExecuteStatement *es, 1667 const struct GNUNET_PQ_PreparedStatement *ps); 1668 1669 1670 /** 1671 * Connect to a postgres database using the configuration 1672 * option "CONFIG" in @a section. Also ensures that the 1673 * statements in @a es are executed whenever we (re)connect to the 1674 * database, and that the prepared statements in @a ps are "ready". 1675 * 1676 * The caller does not have to ensure that @a es and @a ps remain allocated 1677 * and initialized in memory until #GNUNET_PQ_disconnect() is called, as a 1678 * copy will be made. 1679 * 1680 * @param cfg configuration 1681 * @param section configuration section to use to get Postgres configuration options 1682 * @param load_path_suffix suffix to append to the SQL_DIR in the configuration 1683 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated 1684 * array of statements to execute upon EACH connection, can be NULL 1685 * @param ps array of prepared statements to prepare, can be NULL 1686 * @param flags connection flags 1687 * @return the postgres handle, NULL on error 1688 */ 1689 struct GNUNET_PQ_Context * 1690 GNUNET_PQ_connect_with_cfg2 (const struct GNUNET_CONFIGURATION_Handle *cfg, 1691 const char *section, 1692 const char *load_path_suffix, 1693 const struct GNUNET_PQ_ExecuteStatement *es, 1694 const struct GNUNET_PQ_PreparedStatement *ps, 1695 enum GNUNET_PQ_Options flags); 1696 1697 1698 /** 1699 * Reinitialize the database @a db if the connection is down. 1700 * 1701 * @param db database connection to reinitialize 1702 */ 1703 void 1704 GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db); 1705 1706 1707 /** 1708 * Reinitialize the database @a db. 1709 * 1710 * @param db database connection to reinitialize 1711 */ 1712 void 1713 GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db); 1714 1715 1716 /** 1717 * Register callback to be invoked on events of type @a es. 1718 * 1719 * Unlike many other calls, this function is thread-safe 1720 * and may be called from threads that are different 1721 * from the one that setup @a db. However, the @a cb 1722 * will always be called from the thread that runs 1723 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler. 1724 * 1725 * @param db database context to use 1726 * @param es specification of the event to listen for 1727 * @param timeout when to trigger @a cb based on timeout 1728 * @param cb function to call when the event happens, possibly 1729 * multiple times (until #GNUNET_PQ_event_listen_cancel() is invoked), including on timeout 1730 * @param cb_cls closure for @a cb 1731 * @return handle useful to cancel the listener 1732 */ 1733 struct GNUNET_DB_EventHandler * 1734 GNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, 1735 const struct GNUNET_DB_EventHeaderP *es, 1736 struct GNUNET_TIME_Relative timeout, 1737 GNUNET_DB_EventCallback cb, 1738 void *cb_cls); 1739 1740 1741 /** 1742 * Stop notifications. 1743 * 1744 * Unlike many other calls, this function is thread-safe 1745 * and may be called from threads that are different 1746 * from the one that setup @a db. However, the @a cb 1747 * will always be called from the thread that runs 1748 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler. 1749 * 1750 * @param eh handle to unregister. 1751 */ 1752 void 1753 GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh); 1754 1755 1756 /** 1757 * Poll for events right now. Useful if we may have 1758 * triggered an event for ourselves. Not needed when 1759 * using GNUNET_PQ_event_notify(), but useful when 1760 * stored procedures may have triggered events. 1761 * Does nothing if there are no events. 1762 * 1763 * @param[in,out] db database to check for events 1764 */ 1765 void 1766 GNUNET_PQ_event_do_poll (struct GNUNET_PQ_Context *db); 1767 1768 1769 /** 1770 * Notify all that listen on @a es of an event. 1771 * 1772 * Unlike many other calls, this function is thread-safe and may be called 1773 * from threads that are different from the one that setup @a db. However, the 1774 * @a cb will always be called from the thread that runs 1775 * #GNUNET_PQ_event_do_poll() or the GNUnet scheduler. 1776 * 1777 * @param db database context to use 1778 * @param es specification of the event to generate 1779 * @param extra additional event data provided 1780 * @param extra_size number of bytes in @a extra 1781 */ 1782 void 1783 GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, 1784 const struct GNUNET_DB_EventHeaderP *es, 1785 const void *extra, 1786 size_t extra_size); 1787 1788 1789 /** 1790 * Compute the channel that one should notify upon for the given event 1791 * specification. 1792 * 1793 * @param es event specification 1794 * @return channel to notify upon 1795 */ 1796 char * 1797 GNUNET_PQ_get_event_notify_channel (const struct GNUNET_DB_EventHeaderP *es); 1798 1799 1800 /** 1801 * Within the @a db context, run all the SQL files in the load path 1802 * where the name starts with the @a load_suffix and ends with 1803 * consecutive numbers from "[0000-9999].sql". 1804 * 1805 * @param db database context to use 1806 * @param load_suffix suffix to append to the load path 1807 * to find the XXXX.sql files, usually "subsystem-". 1808 * 1809 * @return #GNUNET_OK on success 1810 */ 1811 enum GNUNET_GenericReturnValue 1812 GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db, 1813 const char *load_suffix); 1814 1815 1816 /** 1817 * Disconnect from the database, destroying the prepared statements 1818 * and releasing other associated resources. 1819 * 1820 * @param db database handle to disconnect (will be free'd) 1821 */ 1822 void 1823 GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db); 1824 1825 1826 #endif /* GNUNET_PQ_LIB_H_ */ 1827 1828 /* end of include/gnunet_pq_lib.h */