2 This file is part of GNUnet
3 Copyright (C) 2016, 2017 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file include/gnunet_pq_lib.h
22 * @brief helper functions for Postgres DB interactions
23 * @author Christian Grothoff
25 #ifndef GNUNET_PQ_LIB_H
26 #define GNUNET_PQ_LIB_H
29 #include "gnunet_util_lib.h"
30 #include "gnunet_db_lib.h"
32 /* ************************* pq_query_helper.c functions ************************ */
36 * Function called to convert input argument into SQL parameters.
39 * @param data pointer to input argument
40 * @param data_len number of bytes in @a data (if applicable)
41 * @param[out] param_values SQL data to set
42 * @param[out] param_lengths SQL length data to set
43 * @param[out] param_formats SQL format data to set
44 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
45 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
46 * @param scratch_length number of entries left in @a scratch
47 * @return -1 on error, number of offsets used in @a scratch otherwise
50 (*GNUNET_PQ_QueryConverter) (void *cls,
56 unsigned int param_length,
58 unsigned int scratch_length);
62 * @brief Description of a DB query parameter.
64 struct GNUNET_PQ_QueryParam
67 * Function for how to handle this type of entry.
69 GNUNET_PQ_QueryConverter conv;
72 * Closure for @e conv.
87 * Number of parameters eaten by this operation.
89 unsigned int num_params;
94 * End of query parameter specification.
96 #define GNUNET_PQ_query_param_end \
98 NULL, NULL, NULL, 0, 0 \
103 * Generate query parameter for a buffer @a ptr of
106 * @param ptr pointer to the query parameter to pass
107 * @oaran ptr_size number of bytes in @a ptr
109 struct GNUNET_PQ_QueryParam
110 GNUNET_PQ_query_param_fixed_size (const void *ptr, size_t ptr_size);
114 * Generate query parameter for a string.
116 * @param ptr pointer to the string query parameter to pass
118 struct GNUNET_PQ_QueryParam
119 GNUNET_PQ_query_param_string (const char *ptr);
123 * Generate fixed-size query parameter with size determined
126 * @param x pointer to the query parameter to pass.
128 #define GNUNET_PQ_query_param_auto_from_type(x) \
129 GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
133 * Generate query parameter for an RSA public key. The
134 * database must contain a BLOB type in the respective position.
136 * @param x the query parameter to pass.
138 struct GNUNET_PQ_QueryParam
139 GNUNET_PQ_query_param_rsa_public_key (
140 const struct GNUNET_CRYPTO_RsaPublicKey *x);
144 * Generate query parameter for an RSA signature. The
145 * database must contain a BLOB type in the respective position.
147 * @param x the query parameter to pass
149 struct GNUNET_PQ_QueryParam
150 GNUNET_PQ_query_param_rsa_signature (
151 const struct GNUNET_CRYPTO_RsaSignature *x);
155 * Generate query parameter for an absolute time value.
156 * The database must store a 64-bit integer.
158 * @param x pointer to the query parameter to pass
160 struct GNUNET_PQ_QueryParam
161 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
165 * Generate query parameter for an absolute time value.
166 * The database must store a 64-bit integer.
168 * @param x pointer to the query parameter to pass
170 struct GNUNET_PQ_QueryParam
171 GNUNET_PQ_query_param_absolute_time_nbo (
172 const struct GNUNET_TIME_AbsoluteNBO *x);
176 * Generate query parameter for an uint16_t in host byte order.
178 * @param x pointer to the query parameter to pass
180 struct GNUNET_PQ_QueryParam
181 GNUNET_PQ_query_param_uint16 (const uint16_t *x);
185 * Generate query parameter for an uint32_t in host byte order.
187 * @param x pointer to the query parameter to pass
189 struct GNUNET_PQ_QueryParam
190 GNUNET_PQ_query_param_uint32 (const uint32_t *x);
194 * Generate query parameter for an uint16_t in host byte order.
196 * @param x pointer to the query parameter to pass
198 struct GNUNET_PQ_QueryParam
199 GNUNET_PQ_query_param_uint64 (const uint64_t *x);
202 /* ************************* pq_result_helper.c functions ************************ */
206 * Extract data from a Postgres database @a result at row @a row.
209 * @param result where to extract data from
210 * @param int row to extract data from
211 * @param fname name (or prefix) of the fields to extract from
212 * @param[in,out] dst_size where to store size of result, may be NULL
213 * @param[out] dst where to store the result
215 * #GNUNET_YES if all results could be extracted
216 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
219 (*GNUNET_PQ_ResultConverter) (void *cls,
228 * Function called to clean up memory allocated
229 * by a #GNUNET_PQ_ResultConverter.
232 * @param rd result data to clean up
235 (*GNUNET_PQ_ResultCleanup) (void *cls,
240 * @brief Description of a DB result cell.
242 struct GNUNET_PQ_ResultSpec
245 * What is the format of the result?
247 GNUNET_PQ_ResultConverter conv;
250 * Function to clean up result data, NULL if cleanup is
253 GNUNET_PQ_ResultCleanup cleaner;
256 * Closure for @e conv and @e cleaner.
261 * Destination for the data.
266 * Allowed size for the data, 0 for variable-size
267 * (in this case, the type of @e dst is a `void **`
268 * and we need to allocate a buffer of the right size).
273 * Field name of the desired result.
278 * Where to store actual size of the result.
285 * End of result parameter specification.
287 * @return array last entry for the result specification to use
289 #define GNUNET_PQ_result_spec_end \
291 NULL, NULL, NULL, NULL, 0, NULL, NULL \
296 * Variable-size result expected.
298 * @param name name of the field in the table
299 * @param[out] dst where to store the result, allocated
300 * @param[out] sptr where to store the size of @a dst
301 * @return array entry for the result specification to use
303 struct GNUNET_PQ_ResultSpec
304 GNUNET_PQ_result_spec_variable_size (const char *name,
310 * Fixed-size result expected.
312 * @param name name of the field in the table
313 * @param[out] dst where to store the result
314 * @param dst_size number of bytes in @a dst
315 * @return array entry for the result specification to use
317 struct GNUNET_PQ_ResultSpec
318 GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size);
322 * We expect a fixed-size result, with size determined by the type of `* dst`
324 * @param name name of the field in the table
325 * @param dst point to where to store the result, type fits expected result size
326 * @return array entry for the result specification to use
328 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) \
329 GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))
333 * 0-terminated string expected.
335 * @param name name of the field in the table
336 * @param[out] dst where to store the result, allocated
337 * @return array entry for the result specification to use
339 struct GNUNET_PQ_ResultSpec
340 GNUNET_PQ_result_spec_string (const char *name, char **dst);
344 * RSA public key expected.
346 * @param name name of the field in the table
347 * @param[out] rsa where to store the result
348 * @return array entry for the result specification to use
350 struct GNUNET_PQ_ResultSpec
351 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
352 struct GNUNET_CRYPTO_RsaPublicKey **rsa);
356 * RSA signature expected.
358 * @param name name of the field in the table
359 * @param[out] sig where to store the result;
360 * @return array entry for the result specification to use
362 struct GNUNET_PQ_ResultSpec
363 GNUNET_PQ_result_spec_rsa_signature (const char *name,
364 struct GNUNET_CRYPTO_RsaSignature **sig);
368 * Absolute time expected.
370 * @param name name of the field in the table
371 * @param[out] at where to store the result
372 * @return array entry for the result specification to use
374 struct GNUNET_PQ_ResultSpec
375 GNUNET_PQ_result_spec_absolute_time (const char *name,
376 struct GNUNET_TIME_Absolute *at);
380 * Absolute time expected.
382 * @param name name of the field in the table
383 * @param[out] at where to store the result
384 * @return array entry for the result specification to use
386 struct GNUNET_PQ_ResultSpec
387 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
388 struct GNUNET_TIME_AbsoluteNBO *at);
394 * @param name name of the field in the table
395 * @param[out] u16 where to store the result
396 * @return array entry for the result specification to use
398 struct GNUNET_PQ_ResultSpec
399 GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16);
405 * @param name name of the field in the table
406 * @param[out] u32 where to store the result
407 * @return array entry for the result specification to use
409 struct GNUNET_PQ_ResultSpec
410 GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32);
416 * @param name name of the field in the table
417 * @param[out] u64 where to store the result
418 * @return array entry for the result specification to use
420 struct GNUNET_PQ_ResultSpec
421 GNUNET_PQ_result_spec_uint64 (const char *name, uint64_t *u64);
424 /* ************************* pq.c functions ************************ */
429 struct GNUNET_PQ_Context;
433 * Execute a prepared statement.
435 * @param db database context
436 * @param name name of the prepared statement
437 * @param params parameters to the statement
438 * @return postgres result
439 * @deprecated (should become an internal API)
442 GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db,
444 const struct GNUNET_PQ_QueryParam *params);
448 * Extract results from a query result according to the given specification.
450 * @param result result to process
451 * @param[in,out] rs result specification to extract for
452 * @param row row from the result to extract
454 * #GNUNET_YES if all results could be extracted
455 * #GNUNET_SYSERR if a result was invalid (non-existing field)
456 * @deprecated (should become an internal API)
459 GNUNET_PQ_extract_result (PGresult *result,
460 struct GNUNET_PQ_ResultSpec *rs,
465 * Free all memory that was allocated in @a rs during
466 * #GNUNET_PQ_extract_result().
468 * @param rs reult specification to clean up
471 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs);
474 /* ******************** pq_eval.c functions ************** */
478 * Check the @a result's error code to see what happened.
481 * @param db database to execute the statement in
482 * @param statement_name name of the statement that created @a result
483 * @param result result to check
484 * @return status code from the result, mapping PQ status
485 * codes to `enum GNUNET_DB_QueryStatus`. Never
486 * returns positive values as this function does
487 * not look at the result set.
488 * @deprecated (low level, let's see if we can do with just the high-level functions)
490 enum GNUNET_DB_QueryStatus
491 GNUNET_PQ_eval_result (struct GNUNET_PQ_Context *db,
492 const char *statement_name,
497 * Execute a named prepared @a statement that is NOT a SELECT
498 * statement in @a connnection using the given @a params. Returns the
499 * resulting session state.
501 * @param db database to execute the statement with
502 * @param statement_name name of the statement
503 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
504 * @return status code from the result, mapping PQ status
505 * codes to `enum GNUNET_DB_QueryStatus`. If the
506 * statement was a DELETE or UPDATE statement, the
507 * number of affected rows is returned; if the
508 * statment was an INSERT statement, and no row
509 * was added due to a UNIQUE violation, we return
510 * zero; if INSERT was successful, we return one.
512 enum GNUNET_DB_QueryStatus
513 GNUNET_PQ_eval_prepared_non_select (struct GNUNET_PQ_Context *db,
514 const char *statement_name,
515 const struct GNUNET_PQ_QueryParam *params);
519 * Function to be called with the results of a SELECT statement
520 * that has returned @a num_results results.
523 * @param result the postgres result
524 * @param num_result the number of results in @a result
527 (*GNUNET_PQ_PostgresResultHandler) (void *cls,
529 unsigned int num_results);
533 * Execute a named prepared @a statement that is a SELECT statement
534 * which may return multiple results in @a connection using the given
535 * @a params. Call @a rh with the results. Returns the query
536 * status including the number of results given to @a rh (possibly zero).
537 * @a rh will not have been called if the return value is negative.
539 * @param db database to execute the statement with
540 * @param statement_name name of the statement
541 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
542 * @param rh function to call with the result set, NULL to ignore
543 * @param rh_cls closure to pass to @a rh
544 * @return status code from the result, mapping PQ status
545 * codes to `enum GNUNET_DB_QueryStatus`.
547 enum GNUNET_DB_QueryStatus
548 GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db,
549 const char *statement_name,
550 const struct GNUNET_PQ_QueryParam *params,
551 GNUNET_PQ_PostgresResultHandler rh,
556 * Execute a named prepared @a statement that is a SELECT statement
557 * which must return a single result in @a connection using the given
558 * @a params. Stores the result (if any) in @a rs, which the caller
559 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
560 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
561 * resulting session status.
563 * @param db database to execute the statement with
564 * @param statement_name name of the statement
565 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
566 * @param[in,out] rs result specification to use for storing the result of the query
567 * @return status code from the result, mapping PQ status
568 * codes to `enum GNUNET_DB_QueryStatus`.
570 enum GNUNET_DB_QueryStatus
571 GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db,
572 const char *statement_name,
574 GNUNET_PQ_QueryParam *params,
575 struct GNUNET_PQ_ResultSpec *rs);
578 /* ******************** pq_prepare.c functions ************** */
582 * Information needed to prepare a list of SQL statements using
583 * #GNUNET_PQ_prepare_statements().
585 struct GNUNET_PQ_PreparedStatement
588 * Name of the statement.
593 * Actual SQL statement.
598 * Number of arguments included in @e sql.
600 unsigned int num_arguments;
606 * Terminator for prepared statement list.
608 #define GNUNET_PQ_PREPARED_STATEMENT_END \
615 * Create a `struct GNUNET_PQ_PreparedStatement`.
617 * @param name name of the statement
618 * @param sql actual SQL statement
619 * @param num_args number of arguments in the statement
620 * @return initialized struct
622 struct GNUNET_PQ_PreparedStatement
623 GNUNET_PQ_make_prepare (const char *name,
625 unsigned int num_args);
629 * Request creation of prepared statements @a ps from Postgres.
631 * @param db database to prepare the statements for
632 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
634 * @return #GNUNET_OK on success,
635 * #GNUNET_SYSERR on error
638 GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
639 const struct GNUNET_PQ_PreparedStatement *ps);
642 /* ******************** pq_exec.c functions ************** */
646 * Information needed to run a list of SQL statements using
647 * #GNUNET_PQ_exec_statements().
649 struct GNUNET_PQ_ExecuteStatement
652 * Actual SQL statement.
657 * Should we ignore errors?
664 * Terminator for executable statement list.
666 #define GNUNET_PQ_EXECUTE_STATEMENT_END \
668 NULL, GNUNET_SYSERR \
673 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
675 * @param sql actual SQL statement
676 * @return initialized struct
678 struct GNUNET_PQ_ExecuteStatement
679 GNUNET_PQ_make_execute (const char *sql);
683 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
686 * @param sql actual SQL statement
687 * @return initialized struct
689 struct GNUNET_PQ_ExecuteStatement
690 GNUNET_PQ_make_try_execute (const char *sql);
694 * Request execution of an array of statements @a es from Postgres.
696 * @param pq database to execute the statements in
697 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
699 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
700 * #GNUNET_SYSERR on error
703 GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db,
704 const struct GNUNET_PQ_ExecuteStatement *es);
707 /* ******************** pq_connect.c functions ************** */
711 * Create a connection to the Postgres database using @a config_str
712 * for the configuration. Initialize logging via GNUnet's log
713 * routines and disable Postgres's logger. Also ensures that the
714 * statements in @a es are executed whenever we (re)connect to the
715 * database, and that the prepared statements in @a ps are "ready".
716 * If statements in @es fail that were created with
717 * #GNUNET_PQ_make_execute(), then the entire operation fails.
719 * @param config_str configuration to use
720 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
721 * array of statements to execute upon EACH connection, can be NULL
722 * @param ps array of prepared statements to prepare, can be NULL
723 * @return NULL on error
725 struct GNUNET_PQ_Context *
726 GNUNET_PQ_connect (const char *config_str,
727 const struct GNUNET_PQ_ExecuteStatement *es,
728 const struct GNUNET_PQ_PreparedStatement *ps);
732 * Connect to a postgres database using the configuration
733 * option "CONFIG" in @a section. Also ensures that the
734 * statements in @a es are executed whenever we (re)connect to the
735 * database, and that the prepared statements in @a ps are "ready".
737 * @param cfg configuration
738 * @param section configuration section to use to get Postgres configuration options
739 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated
740 * array of statements to execute upon EACH connection, can be NULL
741 * @param ps array of prepared statements to prepare, can be NULL
742 * @return the postgres handle, NULL on error
744 struct GNUNET_PQ_Context *
745 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
747 const struct GNUNET_PQ_ExecuteStatement *es,
748 const struct GNUNET_PQ_PreparedStatement *ps);
752 * Reinitialize the database @a db if the connection is down.
754 * @param db database connection to reinitialize
757 GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db);
761 * Reinitialize the database @a db.
763 * @param db database connection to reinitialize
766 GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db);
770 * Disconnect from the database, destroying the prepared statements
771 * and releasing other associated resources.
773 * @param db database handle to disconnect (will be free'd)
776 GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db);
779 #endif /* GNUNET_PQ_LIB_H_ */
781 /* end of include/gnunet_pq_lib.h */