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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
17 * @file include/gnunet_pq_lib.h
18 * @brief helper functions for Postgres DB interactions
19 * @author Christian Grothoff
21 #ifndef GNUNET_PQ_LIB_H
22 #define GNUNET_PQ_LIB_H
25 #include "gnunet_util_lib.h"
26 #include "gnunet_db_lib.h"
28 /* ************************* pq_query_helper.c functions ************************ */
32 * Function called to convert input argument into SQL parameters.
35 * @param data pointer to input argument
36 * @param data_len number of bytes in @a data (if applicable)
37 * @param[out] param_values SQL data to set
38 * @param[out] param_lengths SQL length data to set
39 * @param[out] param_formats SQL format data to set
40 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
41 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
42 * @param scratch_length number of entries left in @a scratch
43 * @return -1 on error, number of offsets used in @a scratch otherwise
46 (*GNUNET_PQ_QueryConverter)(void *cls,
52 unsigned int param_length,
54 unsigned int scratch_length);
58 * @brief Description of a DB query parameter.
60 struct GNUNET_PQ_QueryParam
64 * Function for how to handle this type of entry.
66 GNUNET_PQ_QueryConverter conv;
69 * Closure for @e conv.
84 * Number of parameters eaten by this operation.
86 unsigned int num_params;
91 * End of query parameter specification.
93 #define GNUNET_PQ_query_param_end { NULL, NULL, NULL, 0, 0 }
97 * Generate query parameter for a buffer @a ptr of
100 * @param ptr pointer to the query parameter to pass
101 * @oaran ptr_size number of bytes in @a ptr
103 struct GNUNET_PQ_QueryParam
104 GNUNET_PQ_query_param_fixed_size (const void *ptr,
110 * Generate query parameter for a string.
112 * @param ptr pointer to the string query parameter to pass
114 struct GNUNET_PQ_QueryParam
115 GNUNET_PQ_query_param_string (const char *ptr);
119 * Generate fixed-size query parameter with size determined
122 * @param x pointer to the query parameter to pass.
124 #define GNUNET_PQ_query_param_auto_from_type(x) GNUNET_PQ_query_param_fixed_size ((x), sizeof (*(x)))
128 * Generate query parameter for an RSA public key. The
129 * database must contain a BLOB type in the respective position.
131 * @param x the query parameter to pass.
133 struct GNUNET_PQ_QueryParam
134 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
138 * Generate query parameter for an RSA signature. The
139 * database must contain a BLOB type in the respective position.
141 * @param x the query parameter to pass
143 struct GNUNET_PQ_QueryParam
144 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
148 * Generate query parameter for an absolute time value.
149 * The database must store a 64-bit integer.
151 * @param x pointer to the query parameter to pass
153 struct GNUNET_PQ_QueryParam
154 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
158 * Generate query parameter for an absolute time value.
159 * The database must store a 64-bit integer.
161 * @param x pointer to the query parameter to pass
163 struct GNUNET_PQ_QueryParam
164 GNUNET_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
168 * Generate query parameter for an uint16_t in host byte order.
170 * @param x pointer to the query parameter to pass
172 struct GNUNET_PQ_QueryParam
173 GNUNET_PQ_query_param_uint16 (const uint16_t *x);
177 * Generate query parameter for an uint32_t in host byte order.
179 * @param x pointer to the query parameter to pass
181 struct GNUNET_PQ_QueryParam
182 GNUNET_PQ_query_param_uint32 (const uint32_t *x);
186 * Generate query parameter for an uint16_t in host byte order.
188 * @param x pointer to the query parameter to pass
190 struct GNUNET_PQ_QueryParam
191 GNUNET_PQ_query_param_uint64 (const uint64_t *x);
194 /* ************************* pq_result_helper.c functions ************************ */
198 * Extract data from a Postgres database @a result at row @a row.
201 * @param result where to extract data from
202 * @param int row to extract data from
203 * @param fname name (or prefix) of the fields to extract from
204 * @param[in,out] dst_size where to store size of result, may be NULL
205 * @param[out] dst where to store the result
207 * #GNUNET_YES if all results could be extracted
208 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
211 (*GNUNET_PQ_ResultConverter)(void *cls,
220 * Function called to clean up memory allocated
221 * by a #GNUNET_PQ_ResultConverter.
224 * @param rd result data to clean up
227 (*GNUNET_PQ_ResultCleanup)(void *cls,
232 * @brief Description of a DB result cell.
234 struct GNUNET_PQ_ResultSpec
238 * What is the format of the result?
240 GNUNET_PQ_ResultConverter conv;
243 * Function to clean up result data, NULL if cleanup is
246 GNUNET_PQ_ResultCleanup cleaner;
249 * Closure for @e conv and @e cleaner.
254 * Destination for the data.
259 * Allowed size for the data, 0 for variable-size
260 * (in this case, the type of @e dst is a `void **`
261 * and we need to allocate a buffer of the right size).
266 * Field name of the desired result.
271 * Where to store actual size of the result.
279 * End of result parameter specification.
281 * @return array last entry for the result specification to use
283 #define GNUNET_PQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, NULL }
287 * Variable-size result expected.
289 * @param name name of the field in the table
290 * @param[out] dst where to store the result, allocated
291 * @param[out] sptr where to store the size of @a dst
292 * @return array entry for the result specification to use
294 struct GNUNET_PQ_ResultSpec
295 GNUNET_PQ_result_spec_variable_size (const char *name,
301 * Fixed-size result expected.
303 * @param name name of the field in the table
304 * @param[out] dst where to store the result
305 * @param dst_size number of bytes in @a dst
306 * @return array entry for the result specification to use
308 struct GNUNET_PQ_ResultSpec
309 GNUNET_PQ_result_spec_fixed_size (const char *name,
316 * We expect a fixed-size result, with size determined by the type of `* dst`
318 * @param name name of the field in the table
319 * @param dst point to where to store the result, type fits expected result size
320 * @return array entry for the result specification to use
322 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof (*(dst)))
326 * 0-terminated string expected.
328 * @param name name of the field in the table
329 * @param[out] dst where to store the result, allocated
330 * @return array entry for the result specification to use
332 struct GNUNET_PQ_ResultSpec
333 GNUNET_PQ_result_spec_string (const char *name,
338 * RSA public key expected.
340 * @param name name of the field in the table
341 * @param[out] rsa where to store the result
342 * @return array entry for the result specification to use
344 struct GNUNET_PQ_ResultSpec
345 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
346 struct GNUNET_CRYPTO_RsaPublicKey **rsa);
350 * RSA signature expected.
352 * @param name name of the field in the table
353 * @param[out] sig where to store the result;
354 * @return array entry for the result specification to use
356 struct GNUNET_PQ_ResultSpec
357 GNUNET_PQ_result_spec_rsa_signature (const char *name,
358 struct GNUNET_CRYPTO_RsaSignature **sig);
362 * Absolute time expected.
364 * @param name name of the field in the table
365 * @param[out] at where to store the result
366 * @return array entry for the result specification to use
368 struct GNUNET_PQ_ResultSpec
369 GNUNET_PQ_result_spec_absolute_time (const char *name,
370 struct GNUNET_TIME_Absolute *at);
374 * Absolute time expected.
376 * @param name name of the field in the table
377 * @param[out] at where to store the result
378 * @return array entry for the result specification to use
380 struct GNUNET_PQ_ResultSpec
381 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
382 struct GNUNET_TIME_AbsoluteNBO *at);
388 * @param name name of the field in the table
389 * @param[out] u16 where to store the result
390 * @return array entry for the result specification to use
392 struct GNUNET_PQ_ResultSpec
393 GNUNET_PQ_result_spec_uint16 (const char *name,
400 * @param name name of the field in the table
401 * @param[out] u32 where to store the result
402 * @return array entry for the result specification to use
404 struct GNUNET_PQ_ResultSpec
405 GNUNET_PQ_result_spec_uint32 (const char *name,
412 * @param name name of the field in the table
413 * @param[out] u64 where to store the result
414 * @return array entry for the result specification to use
416 struct GNUNET_PQ_ResultSpec
417 GNUNET_PQ_result_spec_uint64 (const char *name,
421 /* ************************* pq.c functions ************************ */
424 * Execute a prepared statement.
426 * @param db_conn database connection
427 * @param name name of the prepared statement
428 * @param params parameters to the statement
429 * @return postgres result
430 * @deprecated (should become an internal API)
433 GNUNET_PQ_exec_prepared (PGconn *db_conn,
435 const struct GNUNET_PQ_QueryParam *params);
439 * Extract results from a query result according to the given specification.
441 * @param result result to process
442 * @param[in,out] rs result specification to extract for
443 * @param row row from the result to extract
445 * #GNUNET_YES if all results could be extracted
446 * #GNUNET_SYSERR if a result was invalid (non-existing field)
447 * @deprecated (should become an internal API)
450 GNUNET_PQ_extract_result (PGresult *result,
451 struct GNUNET_PQ_ResultSpec *rs,
456 * Free all memory that was allocated in @a rs during
457 * #GNUNET_PQ_extract_result().
459 * @param rs reult specification to clean up
462 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs);
465 /* ******************** pq_eval.c functions ************** */
469 * Check the @a result's error code to see what happened.
472 * @param connection connection to execute the statement in
473 * @param statement_name name of the statement that created @a result
474 * @param result result to check
475 * @return status code from the result, mapping PQ status
476 * codes to `enum GNUNET_DB_QueryStatus`. Never
477 * returns positive values as this function does
478 * not look at the result set.
479 * @deprecated (low level, let's see if we can do with just the high-level functions)
481 enum GNUNET_DB_QueryStatus
482 GNUNET_PQ_eval_result (PGconn *connection,
483 const char *statement_name,
488 * Execute a named prepared @a statement that is NOT a SELECT
489 * statement in @a connnection using the given @a params. Returns the
490 * resulting session state.
492 * @param connection connection to execute the statement in
493 * @param statement_name name of the statement
494 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
495 * @return status code from the result, mapping PQ status
496 * codes to `enum GNUNET_DB_QueryStatus`. If the
497 * statement was a DELETE or UPDATE statement, the
498 * number of affected rows is returned; if the
499 * statment was an INSERT statement, and no row
500 * was added due to a UNIQUE violation, we return
501 * zero; if INSERT was successful, we return one.
503 enum GNUNET_DB_QueryStatus
504 GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
505 const char *statement_name,
506 const struct GNUNET_PQ_QueryParam *params);
510 * Function to be called with the results of a SELECT statement
511 * that has returned @a num_results results.
514 * @param result the postgres result
515 * @param num_result the number of results in @a result
518 (*GNUNET_PQ_PostgresResultHandler)(void *cls,
520 unsigned int num_results);
524 * Execute a named prepared @a statement that is a SELECT statement
525 * which may return multiple results in @a connection using the given
526 * @a params. Call @a rh with the results. Returns the query
527 * status including the number of results given to @a rh (possibly zero).
528 * @a rh will not have been called if the return value is negative.
530 * @param connection connection to execute the statement in
531 * @param statement_name name of the statement
532 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
533 * @param rh function to call with the result set, NULL to ignore
534 * @param rh_cls closure to pass to @a rh
535 * @return status code from the result, mapping PQ status
536 * codes to `enum GNUNET_DB_QueryStatus`.
538 enum GNUNET_DB_QueryStatus
539 GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
540 const char *statement_name,
541 const struct GNUNET_PQ_QueryParam *params,
542 GNUNET_PQ_PostgresResultHandler rh,
547 * Execute a named prepared @a statement that is a SELECT statement
548 * which must return a single result in @a connection using the given
549 * @a params. Stores the result (if any) in @a rs, which the caller
550 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
551 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
552 * resulting session status.
554 * @param connection connection to execute the statement in
555 * @param statement_name name of the statement
556 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
557 * @param[in,out] rs result specification to use for storing the result of the query
558 * @return status code from the result, mapping PQ status
559 * codes to `enum GNUNET_DB_QueryStatus`.
561 enum GNUNET_DB_QueryStatus
562 GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
563 const char *statement_name,
564 const struct GNUNET_PQ_QueryParam *params,
565 struct GNUNET_PQ_ResultSpec *rs);
568 /* ******************** pq_prepare.c functions ************** */
572 * Information needed to prepare a list of SQL statements using
573 * #GNUNET_PQ_prepare_statements().
575 struct GNUNET_PQ_PreparedStatement {
578 * Name of the statement.
583 * Actual SQL statement.
588 * Number of arguments included in @e sql.
590 unsigned int num_arguments;
596 * Terminator for prepared statement list.
598 #define GNUNET_PQ_PREPARED_STATEMENT_END { NULL, NULL, 0 }
602 * Create a `struct GNUNET_PQ_PreparedStatement`.
604 * @param name name of the statement
605 * @param sql actual SQL statement
606 * @param num_args number of arguments in the statement
607 * @return initialized struct
609 struct GNUNET_PQ_PreparedStatement
610 GNUNET_PQ_make_prepare (const char *name,
612 unsigned int num_args);
616 * Request creation of prepared statements @a ps from Postgres.
618 * @param connection connection to prepare the statements for
619 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
621 * @return #GNUNET_OK on success,
622 * #GNUNET_SYSERR on error
625 GNUNET_PQ_prepare_statements (PGconn *connection,
626 const struct GNUNET_PQ_PreparedStatement *ps);
629 /* ******************** pq_exec.c functions ************** */
633 * Information needed to run a list of SQL statements using
634 * #GNUNET_PQ_exec_statements().
636 struct GNUNET_PQ_ExecuteStatement {
639 * Actual SQL statement.
644 * Should we ignore errors?
652 * Terminator for executable statement list.
654 #define GNUNET_PQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
658 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
660 * @param sql actual SQL statement
661 * @return initialized struct
663 struct GNUNET_PQ_ExecuteStatement
664 GNUNET_PQ_make_execute (const char *sql);
668 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
671 * @param sql actual SQL statement
672 * @return initialized struct
674 struct GNUNET_PQ_ExecuteStatement
675 GNUNET_PQ_make_try_execute (const char *sql);
679 * Request execution of an array of statements @a es from Postgres.
681 * @param connection connection to execute the statements over
682 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
684 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
685 * #GNUNET_SYSERR on error
688 GNUNET_PQ_exec_statements (PGconn *connection,
689 const struct GNUNET_PQ_ExecuteStatement *es);
692 /* ******************** pq_connect.c functions ************** */
696 * Create a connection to the Postgres database using @a config_str
697 * for the configuration. Initialize logging via GNUnet's log
698 * routines and disable Postgres's logger.
700 * @param config_str configuration to use
701 * @return NULL on error
704 GNUNET_PQ_connect (const char *config_str);
708 * Connect to a postgres database using the configuration
709 * option "CONFIG" in @a section.
711 * @param cfg configuration
712 * @param section configuration section to use to get Postgres configuration options
713 * @return the postgres handle, NULL on error
716 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
717 const char *section);
721 #endif /* GNUNET_PQ_LIB_H_ */
723 /* end of include/gnunet_pq_lib.h */