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
68 * Function for how to handle this type of entry.
70 GNUNET_PQ_QueryConverter conv;
73 * Closure for @e conv.
88 * Number of parameters eaten by this operation.
90 unsigned int num_params;
95 * End of query parameter specification.
97 #define GNUNET_PQ_query_param_end { NULL, NULL, NULL, 0, 0 }
101 * Generate query parameter for a buffer @a ptr of
104 * @param ptr pointer to the query parameter to pass
105 * @oaran ptr_size number of bytes in @a ptr
107 struct GNUNET_PQ_QueryParam
108 GNUNET_PQ_query_param_fixed_size (const void *ptr,
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) GNUNET_PQ_query_param_fixed_size ((x), sizeof (*(x)))
132 * Generate query parameter for an RSA public key. The
133 * database must contain a BLOB type in the respective position.
135 * @param x the query parameter to pass.
137 struct GNUNET_PQ_QueryParam
138 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
142 * Generate query parameter for an RSA signature. The
143 * database must contain a BLOB type in the respective position.
145 * @param x the query parameter to pass
147 struct GNUNET_PQ_QueryParam
148 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
152 * Generate query parameter for an absolute time value.
153 * The database must store a 64-bit integer.
155 * @param x pointer to the query parameter to pass
157 struct GNUNET_PQ_QueryParam
158 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
162 * Generate query parameter for an absolute time value.
163 * The database must store a 64-bit integer.
165 * @param x pointer to the query parameter to pass
167 struct GNUNET_PQ_QueryParam
168 GNUNET_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
172 * Generate query parameter for an uint16_t in host byte order.
174 * @param x pointer to the query parameter to pass
176 struct GNUNET_PQ_QueryParam
177 GNUNET_PQ_query_param_uint16 (const uint16_t *x);
181 * Generate query parameter for an uint32_t in host byte order.
183 * @param x pointer to the query parameter to pass
185 struct GNUNET_PQ_QueryParam
186 GNUNET_PQ_query_param_uint32 (const uint32_t *x);
190 * Generate query parameter for an uint16_t in host byte order.
192 * @param x pointer to the query parameter to pass
194 struct GNUNET_PQ_QueryParam
195 GNUNET_PQ_query_param_uint64 (const uint64_t *x);
198 /* ************************* pq_result_helper.c functions ************************ */
202 * Extract data from a Postgres database @a result at row @a row.
205 * @param result where to extract data from
206 * @param int row to extract data from
207 * @param fname name (or prefix) of the fields to extract from
208 * @param[in,out] dst_size where to store size of result, may be NULL
209 * @param[out] dst where to store the result
211 * #GNUNET_YES if all results could be extracted
212 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
215 (*GNUNET_PQ_ResultConverter)(void *cls,
224 * Function called to clean up memory allocated
225 * by a #GNUNET_PQ_ResultConverter.
228 * @param rd result data to clean up
231 (*GNUNET_PQ_ResultCleanup)(void *cls,
236 * @brief Description of a DB result cell.
238 struct GNUNET_PQ_ResultSpec
242 * What is the format of the result?
244 GNUNET_PQ_ResultConverter conv;
247 * Function to clean up result data, NULL if cleanup is
250 GNUNET_PQ_ResultCleanup cleaner;
253 * Closure for @e conv and @e cleaner.
258 * Destination for the data.
263 * Allowed size for the data, 0 for variable-size
264 * (in this case, the type of @e dst is a `void **`
265 * and we need to allocate a buffer of the right size).
270 * Field name of the desired result.
275 * Where to store actual size of the result.
283 * End of result parameter specification.
285 * @return array last entry for the result specification to use
287 #define GNUNET_PQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, NULL }
291 * Variable-size result expected.
293 * @param name name of the field in the table
294 * @param[out] dst where to store the result, allocated
295 * @param[out] sptr where to store the size of @a dst
296 * @return array entry for the result specification to use
298 struct GNUNET_PQ_ResultSpec
299 GNUNET_PQ_result_spec_variable_size (const char *name,
305 * Fixed-size result expected.
307 * @param name name of the field in the table
308 * @param[out] dst where to store the result
309 * @param dst_size number of bytes in @a dst
310 * @return array entry for the result specification to use
312 struct GNUNET_PQ_ResultSpec
313 GNUNET_PQ_result_spec_fixed_size (const char *name,
320 * We expect a fixed-size result, with size determined by the type of `* dst`
322 * @param name name of the field in the table
323 * @param dst point to where to store the result, type fits expected result size
324 * @return array entry for the result specification to use
326 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof (*(dst)))
330 * 0-terminated string expected.
332 * @param name name of the field in the table
333 * @param[out] dst where to store the result, allocated
334 * @return array entry for the result specification to use
336 struct GNUNET_PQ_ResultSpec
337 GNUNET_PQ_result_spec_string (const char *name,
342 * RSA public key expected.
344 * @param name name of the field in the table
345 * @param[out] rsa where to store the result
346 * @return array entry for the result specification to use
348 struct GNUNET_PQ_ResultSpec
349 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
350 struct GNUNET_CRYPTO_RsaPublicKey **rsa);
354 * RSA signature expected.
356 * @param name name of the field in the table
357 * @param[out] sig where to store the result;
358 * @return array entry for the result specification to use
360 struct GNUNET_PQ_ResultSpec
361 GNUNET_PQ_result_spec_rsa_signature (const char *name,
362 struct GNUNET_CRYPTO_RsaSignature **sig);
366 * Absolute time expected.
368 * @param name name of the field in the table
369 * @param[out] at where to store the result
370 * @return array entry for the result specification to use
372 struct GNUNET_PQ_ResultSpec
373 GNUNET_PQ_result_spec_absolute_time (const char *name,
374 struct GNUNET_TIME_Absolute *at);
378 * Absolute time expected.
380 * @param name name of the field in the table
381 * @param[out] at where to store the result
382 * @return array entry for the result specification to use
384 struct GNUNET_PQ_ResultSpec
385 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
386 struct GNUNET_TIME_AbsoluteNBO *at);
392 * @param name name of the field in the table
393 * @param[out] u16 where to store the result
394 * @return array entry for the result specification to use
396 struct GNUNET_PQ_ResultSpec
397 GNUNET_PQ_result_spec_uint16 (const char *name,
404 * @param name name of the field in the table
405 * @param[out] u32 where to store the result
406 * @return array entry for the result specification to use
408 struct GNUNET_PQ_ResultSpec
409 GNUNET_PQ_result_spec_uint32 (const char *name,
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,
425 /* ************************* pq.c functions ************************ */
428 * Execute a prepared statement.
430 * @param db_conn database connection
431 * @param name name of the prepared statement
432 * @param params parameters to the statement
433 * @return postgres result
434 * @deprecated (should become an internal API)
437 GNUNET_PQ_exec_prepared (PGconn *db_conn,
439 const struct GNUNET_PQ_QueryParam *params);
443 * Extract results from a query result according to the given specification.
445 * @param result result to process
446 * @param[in,out] rs result specification to extract for
447 * @param row row from the result to extract
449 * #GNUNET_YES if all results could be extracted
450 * #GNUNET_SYSERR if a result was invalid (non-existing field)
451 * @deprecated (should become an internal API)
454 GNUNET_PQ_extract_result (PGresult *result,
455 struct GNUNET_PQ_ResultSpec *rs,
460 * Free all memory that was allocated in @a rs during
461 * #GNUNET_PQ_extract_result().
463 * @param rs reult specification to clean up
466 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs);
469 /* ******************** pq_eval.c functions ************** */
473 * Check the @a result's error code to see what happened.
476 * @param connection connection to execute the statement in
477 * @param statement_name name of the statement that created @a result
478 * @param result result to check
479 * @return status code from the result, mapping PQ status
480 * codes to `enum GNUNET_DB_QueryStatus`. Never
481 * returns positive values as this function does
482 * not look at the result set.
483 * @deprecated (low level, let's see if we can do with just the high-level functions)
485 enum GNUNET_DB_QueryStatus
486 GNUNET_PQ_eval_result (PGconn *connection,
487 const char *statement_name,
492 * Execute a named prepared @a statement that is NOT a SELECT
493 * statement in @a connnection using the given @a params. Returns the
494 * resulting session state.
496 * @param connection connection to execute the statement in
497 * @param statement_name name of the statement
498 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
499 * @return status code from the result, mapping PQ status
500 * codes to `enum GNUNET_DB_QueryStatus`. If the
501 * statement was a DELETE or UPDATE statement, the
502 * number of affected rows is returned; if the
503 * statment was an INSERT statement, and no row
504 * was added due to a UNIQUE violation, we return
505 * zero; if INSERT was successful, we return one.
507 enum GNUNET_DB_QueryStatus
508 GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
509 const char *statement_name,
510 const struct GNUNET_PQ_QueryParam *params);
514 * Function to be called with the results of a SELECT statement
515 * that has returned @a num_results results.
518 * @param result the postgres result
519 * @param num_result the number of results in @a result
522 (*GNUNET_PQ_PostgresResultHandler)(void *cls,
524 unsigned int num_results);
528 * Execute a named prepared @a statement that is a SELECT statement
529 * which may return multiple results in @a connection using the given
530 * @a params. Call @a rh with the results. Returns the query
531 * status including the number of results given to @a rh (possibly zero).
532 * @a rh will not have been called if the return value is negative.
534 * @param connection connection to execute the statement in
535 * @param statement_name name of the statement
536 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
537 * @param rh function to call with the result set, NULL to ignore
538 * @param rh_cls closure to pass to @a rh
539 * @return status code from the result, mapping PQ status
540 * codes to `enum GNUNET_DB_QueryStatus`.
542 enum GNUNET_DB_QueryStatus
543 GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
544 const char *statement_name,
545 const struct GNUNET_PQ_QueryParam *params,
546 GNUNET_PQ_PostgresResultHandler rh,
551 * Execute a named prepared @a statement that is a SELECT statement
552 * which must return a single result in @a connection using the given
553 * @a params. Stores the result (if any) in @a rs, which the caller
554 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
555 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
556 * resulting session status.
558 * @param connection connection to execute the statement in
559 * @param statement_name name of the statement
560 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
561 * @param[in,out] rs result specification to use for storing the result of the query
562 * @return status code from the result, mapping PQ status
563 * codes to `enum GNUNET_DB_QueryStatus`.
565 enum GNUNET_DB_QueryStatus
566 GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
567 const char *statement_name,
568 const struct GNUNET_PQ_QueryParam *params,
569 struct GNUNET_PQ_ResultSpec *rs);
572 /* ******************** pq_prepare.c functions ************** */
576 * Information needed to prepare a list of SQL statements using
577 * #GNUNET_PQ_prepare_statements().
579 struct GNUNET_PQ_PreparedStatement {
582 * Name of the statement.
587 * Actual SQL statement.
592 * Number of arguments included in @e sql.
594 unsigned int num_arguments;
600 * Terminator for prepared statement list.
602 #define GNUNET_PQ_PREPARED_STATEMENT_END { NULL, NULL, 0 }
606 * Create a `struct GNUNET_PQ_PreparedStatement`.
608 * @param name name of the statement
609 * @param sql actual SQL statement
610 * @param num_args number of arguments in the statement
611 * @return initialized struct
613 struct GNUNET_PQ_PreparedStatement
614 GNUNET_PQ_make_prepare (const char *name,
616 unsigned int num_args);
620 * Request creation of prepared statements @a ps from Postgres.
622 * @param connection connection to prepare the statements for
623 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
625 * @return #GNUNET_OK on success,
626 * #GNUNET_SYSERR on error
629 GNUNET_PQ_prepare_statements (PGconn *connection,
630 const struct GNUNET_PQ_PreparedStatement *ps);
633 /* ******************** pq_exec.c functions ************** */
637 * Information needed to run a list of SQL statements using
638 * #GNUNET_PQ_exec_statements().
640 struct GNUNET_PQ_ExecuteStatement {
643 * Actual SQL statement.
648 * Should we ignore errors?
656 * Terminator for executable statement list.
658 #define GNUNET_PQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
662 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
664 * @param sql actual SQL statement
665 * @return initialized struct
667 struct GNUNET_PQ_ExecuteStatement
668 GNUNET_PQ_make_execute (const char *sql);
672 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
675 * @param sql actual SQL statement
676 * @return initialized struct
678 struct GNUNET_PQ_ExecuteStatement
679 GNUNET_PQ_make_try_execute (const char *sql);
683 * Request execution of an array of statements @a es from Postgres.
685 * @param connection connection to execute the statements over
686 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
688 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
689 * #GNUNET_SYSERR on error
692 GNUNET_PQ_exec_statements (PGconn *connection,
693 const struct GNUNET_PQ_ExecuteStatement *es);
696 /* ******************** pq_connect.c functions ************** */
700 * Create a connection to the Postgres database using @a config_str
701 * for the configuration. Initialize logging via GNUnet's log
702 * routines and disable Postgres's logger.
704 * @param config_str configuration to use
705 * @return NULL on error
708 GNUNET_PQ_connect (const char *config_str);
712 * Connect to a postgres database using the configuration
713 * option "CONFIG" in @a section.
715 * @param cfg configuration
716 * @param section configuration section to use to get Postgres configuration options
717 * @return the postgres handle, NULL on error
720 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
721 const char *section);
725 #endif /* GNUNET_PQ_LIB_H_ */
727 /* end of include/gnunet_pq_lib.h */