2 This file is part of GNUnet
3 Copyright (C) 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_sq_lib.h
22 * @brief helper functions for Sqlite3 DB interactions
23 * @author Christian Grothoff
25 #ifndef GNUNET_SQ_LIB_H
26 #define GNUNET_SQ_LIB_H
29 #include "gnunet_util_lib.h"
33 * Function called to convert input argument into SQL parameters.
36 * @param data pointer to input argument
37 * @param data_len number of bytes in @a data (if applicable)
38 * @param stmt sqlite statement to bind parameters for
39 * @param off offset of the argument to bind in @a stmt, numbered from 1,
40 * so immediately suitable for passing to `sqlite3_bind`-functions.
41 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
44 (*GNUNET_SQ_QueryConverter)(void *cls,
52 * @brief Description of a DB query parameter.
54 struct GNUNET_SQ_QueryParam
58 * Function for how to handle this type of entry.
60 GNUNET_SQ_QueryConverter conv;
63 * Closure for @e conv.
78 * Number of parameters eaten by this operation.
80 unsigned int num_params;
85 * End of query parameter specification.
87 #define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
91 * Generate query parameter for a buffer @a ptr of
94 * @param ptr pointer to the query parameter to pass
95 * @oaran ptr_size number of bytes in @a ptr
97 struct GNUNET_SQ_QueryParam
98 GNUNET_SQ_query_param_fixed_size (const void *ptr,
104 * Generate query parameter for a string.
106 * @param ptr pointer to the string query parameter to pass
108 struct GNUNET_SQ_QueryParam
109 GNUNET_SQ_query_param_string (const char *ptr);
113 * Generate fixed-size query parameter with size determined
116 * @param x pointer to the query parameter to pass.
118 #define GNUNET_SQ_query_param_auto_from_type(x) GNUNET_SQ_query_param_fixed_size ((x), sizeof (*(x)))
122 * Generate query parameter for an RSA public key. The
123 * database must contain a BLOB type in the respective position.
125 * @param x the query parameter to pass.
127 struct GNUNET_SQ_QueryParam
128 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
132 * Generate query parameter for an RSA signature. The
133 * database must contain a BLOB type in the respective position.
135 * @param x the query parameter to pass
137 struct GNUNET_SQ_QueryParam
138 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
142 * Generate query parameter for an absolute time value.
143 * The database must store a 64-bit integer.
145 * @param x pointer to the query parameter to pass
147 struct GNUNET_SQ_QueryParam
148 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *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_SQ_QueryParam
158 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
162 * Generate query parameter for an uint16_t in host byte order.
164 * @param x pointer to the query parameter to pass
166 struct GNUNET_SQ_QueryParam
167 GNUNET_SQ_query_param_uint16 (const uint16_t *x);
171 * Generate query parameter for an uint32_t in host byte order.
173 * @param x pointer to the query parameter to pass
175 struct GNUNET_SQ_QueryParam
176 GNUNET_SQ_query_param_uint32 (const uint32_t *x);
180 * Generate query parameter for an uint16_t in host byte order.
182 * @param x pointer to the query parameter to pass
184 struct GNUNET_SQ_QueryParam
185 GNUNET_SQ_query_param_uint64 (const uint64_t *x);
189 * Execute binding operations for a prepared statement.
191 * @param db_conn database connection
192 * @param params parameters to the statement
193 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
196 GNUNET_SQ_bind (sqlite3_stmt *stmt,
197 const struct GNUNET_SQ_QueryParam *params);
201 * Reset @a stmt and log error.
203 * @param dbh database handle
204 * @param stmt statement to reset
207 GNUNET_SQ_reset (sqlite3 *dbh,
212 * Extract data from a Postgres database @a result at row @a row.
215 * @param result where to extract data from
216 * @param column column to extract data from
217 * @param[in,out] dst_size where to store size of result, may be NULL
218 * @param[out] dst where to store the result
220 * #GNUNET_YES if all results could be extracted
221 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
224 (*GNUNET_SQ_ResultConverter)(void *cls,
225 sqlite3_stmt *result,
232 * @brief Description of a DB result cell.
234 struct GNUNET_SQ_ResultSpec;
238 * Function called to clean up memory allocated
239 * by a #GNUNET_SQ_ResultConverter.
244 (*GNUNET_SQ_ResultCleanup)(void *cls);
248 * @brief Description of a DB result cell.
250 struct GNUNET_SQ_ResultSpec
254 * What is the format of the result?
256 GNUNET_SQ_ResultConverter conv;
259 * Function to clean up result data, NULL if cleanup is
262 GNUNET_SQ_ResultCleanup cleaner;
265 * Closure for @e conv and @e cleaner.
270 * Destination for the data.
275 * Allowed size for the data, 0 for variable-size
276 * (in this case, the type of @e dst is a `void **`
277 * and we need to allocate a buffer of the right size).
282 * Where to store actual size of the result. If left at
283 * NULL, will be made to point to @e dst_size before
289 * Number of parameters (columns) eaten by this operation.
291 unsigned int num_params;
297 * End of result parameter specification.
299 * @return array last entry for the result specification to use
301 #define GNUNET_SQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, 0 }
305 * Variable-size result expected.
307 * @param[out] dst where to store the result, allocated
308 * @param[out] sptr where to store the size of @a dst
309 * @return array entry for the result specification to use
311 struct GNUNET_SQ_ResultSpec
312 GNUNET_SQ_result_spec_variable_size (void **dst,
317 * Fixed-size result expected.
319 * @param[out] dst where to store the result
320 * @param dst_size number of bytes in @a dst
321 * @return array entry for the result specification to use
323 struct GNUNET_SQ_ResultSpec
324 GNUNET_SQ_result_spec_fixed_size (void *dst,
329 * We expect a fixed-size result, with size determined by the type of `* dst`
331 * @param dst point to where to store the result, type fits expected result size
332 * @return array entry for the result specification to use
334 #define GNUNET_SQ_result_spec_auto_from_type(dst) GNUNET_SQ_result_spec_fixed_size ((dst), sizeof (*(dst)))
338 * Variable-size result expected.
340 * @param[out] dst where to store the result, allocated
341 * @param[out] sptr where to store the size of @a dst
342 * @return array entry for the result specification to use
344 struct GNUNET_SQ_ResultSpec
345 GNUNET_SQ_result_spec_variable_size (void **dst,
350 * 0-terminated string expected.
352 * @param[out] dst where to store the result, allocated
353 * @return array entry for the result specification to use
355 struct GNUNET_SQ_ResultSpec
356 GNUNET_SQ_result_spec_string (char **dst);
360 * RSA public key expected.
362 * @param[out] rsa where to store the result
363 * @return array entry for the result specification to use
365 struct GNUNET_SQ_ResultSpec
366 GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
370 * RSA signature expected.
372 * @param[out] sig where to store the result;
373 * @return array entry for the result specification to use
375 struct GNUNET_SQ_ResultSpec
376 GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
380 * Absolute time expected.
382 * @param[out] at where to store the result
383 * @return array entry for the result specification to use
385 struct GNUNET_SQ_ResultSpec
386 GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
390 * Absolute time expected.
392 * @param[out] at where to store the result
393 * @return array entry for the result specification to use
395 struct GNUNET_SQ_ResultSpec
396 GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
402 * @param[out] u16 where to store the result
403 * @return array entry for the result specification to use
405 struct GNUNET_SQ_ResultSpec
406 GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
412 * @param[out] u32 where to store the result
413 * @return array entry for the result specification to use
415 struct GNUNET_SQ_ResultSpec
416 GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
422 * @param[out] u64 where to store the result
423 * @return array entry for the result specification to use
425 struct GNUNET_SQ_ResultSpec
426 GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
430 * Extract results from a query result according to the given specification.
432 * @param result result to process
433 * @param[in,out] rs result specification to extract for
435 * #GNUNET_OK if all results could be extracted
436 * #GNUNET_SYSERR if a result was invalid (non-existing field)
439 GNUNET_SQ_extract_result (sqlite3_stmt *result,
440 struct GNUNET_SQ_ResultSpec *rs);
444 * Free all memory that was allocated in @a rs during
445 * #GNUNET_SQ_extract_result().
447 * @param rs reult specification to clean up
450 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs);
454 /* ******************** sq_prepare.c functions ************** */
458 * Information needed to run a list of SQL statements using
459 * #GNUNET_SQ_exec_statements().
461 struct GNUNET_SQ_PrepareStatement {
464 * Actual SQL statement.
469 * Where to store handle?
471 sqlite3_stmt **pstmt;
477 * Terminator for executable statement list.
479 #define GNUNET_SQ_PREPARE_END { NULL, NULL }
483 * Create a `struct GNUNET_SQ_PrepareStatement`
485 * @param sql actual SQL statement
486 * @param pstmt where to store the handle
487 * @return initialized struct
489 struct GNUNET_SQ_PrepareStatement
490 GNUNET_SQ_make_prepare (const char *sql,
491 sqlite3_stmt **pstmt);
496 * Prepare all statements given in the (NULL,NULL)-terminated
499 * @param dbh database handle
500 * @param ps array of statements to prepare
501 * @return #GNUNET_OK on success
504 GNUNET_SQ_prepare (sqlite3 *dbh,
505 const struct GNUNET_SQ_PrepareStatement *ps);
508 /* ******************** sq_exec.c functions ************** */
512 * Information needed to run a list of SQL statements using
513 * #GNUNET_SQ_exec_statements().
515 struct GNUNET_SQ_ExecuteStatement {
518 * Actual SQL statement.
523 * Should we ignore errors?
531 * Terminator for executable statement list.
533 #define GNUNET_SQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
537 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
539 * @param sql actual SQL statement
540 * @return initialized struct
542 struct GNUNET_SQ_ExecuteStatement
543 GNUNET_SQ_make_execute (const char *sql);
547 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
550 * @param sql actual SQL statement
551 * @return initialized struct
553 struct GNUNET_SQ_ExecuteStatement
554 GNUNET_SQ_make_try_execute (const char *sql);
558 * Request execution of an array of statements @a es from Postgres.
560 * @param dbh database to execute the statements over
561 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
563 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
564 * #GNUNET_SYSERR on error
567 GNUNET_SQ_exec_statements (sqlite3 *dbh,
568 const struct GNUNET_SQ_ExecuteStatement *es);
572 #endif /* GNUNET_SQ_LIB_H_ */
574 /* end of include/gnunet_sq_lib.h */