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
57 * Function for how to handle this type of entry.
59 GNUNET_SQ_QueryConverter conv;
62 * Closure for @e conv.
77 * Number of parameters eaten by this operation.
79 unsigned int num_params;
84 * End of query parameter specification.
86 #define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
90 * Generate query parameter for a buffer @a ptr of
93 * @param ptr pointer to the query parameter to pass
94 * @oaran ptr_size number of bytes in @a ptr
96 struct GNUNET_SQ_QueryParam
97 GNUNET_SQ_query_param_fixed_size (const void *ptr,
102 * Generate query parameter for a string.
104 * @param ptr pointer to the string query parameter to pass
106 struct GNUNET_SQ_QueryParam
107 GNUNET_SQ_query_param_string (const char *ptr);
111 * Generate fixed-size query parameter with size determined
114 * @param x pointer to the query parameter to pass.
116 #define GNUNET_SQ_query_param_auto_from_type( \
117 x) GNUNET_SQ_query_param_fixed_size ((x), sizeof(*(x)))
121 * Generate query parameter for an RSA public key. The
122 * database must contain a BLOB type in the respective position.
124 * @param x the query parameter to pass.
126 struct GNUNET_SQ_QueryParam
127 GNUNET_SQ_query_param_rsa_public_key (const struct
128 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
139 GNUNET_CRYPTO_RsaSignature *x);
143 * Generate query parameter for an absolute time value.
144 * The database must store a 64-bit integer.
146 * @param x pointer to the query parameter to pass
148 struct GNUNET_SQ_QueryParam
149 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
153 * Generate query parameter for an absolute time value.
154 * The database must store a 64-bit integer.
156 * @param x pointer to the query parameter to pass
158 struct GNUNET_SQ_QueryParam
159 GNUNET_SQ_query_param_absolute_time_nbo (const struct
160 GNUNET_TIME_AbsoluteNBO *x);
164 * Generate query parameter for an uint16_t in host byte order.
166 * @param x pointer to the query parameter to pass
168 struct GNUNET_SQ_QueryParam
169 GNUNET_SQ_query_param_uint16 (const uint16_t *x);
173 * Generate query parameter for an uint32_t in host byte order.
175 * @param x pointer to the query parameter to pass
177 struct GNUNET_SQ_QueryParam
178 GNUNET_SQ_query_param_uint32 (const uint32_t *x);
182 * Generate query parameter for an uint16_t in host byte order.
184 * @param x pointer to the query parameter to pass
186 struct GNUNET_SQ_QueryParam
187 GNUNET_SQ_query_param_uint64 (const uint64_t *x);
191 * Execute binding operations for a prepared statement.
193 * @param db_conn database connection
194 * @param params parameters to the statement
195 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
198 GNUNET_SQ_bind (sqlite3_stmt *stmt,
199 const struct GNUNET_SQ_QueryParam *params);
203 * Reset @a stmt and log error.
205 * @param dbh database handle
206 * @param stmt statement to reset
209 GNUNET_SQ_reset (sqlite3 *dbh,
214 * Extract data from a Postgres database @a result at row @a row.
217 * @param result where to extract data from
218 * @param column column to extract data from
219 * @param[in,out] dst_size where to store size of result, may be NULL
220 * @param[out] dst where to store the result
222 * #GNUNET_YES if all results could be extracted
223 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
226 (*GNUNET_SQ_ResultConverter)(void *cls,
227 sqlite3_stmt *result,
234 * @brief Description of a DB result cell.
236 struct GNUNET_SQ_ResultSpec;
240 * Function called to clean up memory allocated
241 * by a #GNUNET_SQ_ResultConverter.
246 (*GNUNET_SQ_ResultCleanup)(void *cls);
250 * @brief Description of a DB result cell.
252 struct GNUNET_SQ_ResultSpec
255 * What is the format of the result?
257 GNUNET_SQ_ResultConverter conv;
260 * Function to clean up result data, NULL if cleanup is
263 GNUNET_SQ_ResultCleanup cleaner;
266 * Closure for @e conv and @e cleaner.
271 * Destination for the data.
276 * Allowed size for the data, 0 for variable-size
277 * (in this case, the type of @e dst is a `void **`
278 * and we need to allocate a buffer of the right size).
283 * Where to store actual size of the result. If left at
284 * NULL, will be made to point to @e dst_size before
290 * Number of parameters (columns) eaten by this operation.
292 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( \
335 dst) GNUNET_SQ_result_spec_fixed_size ((dst), sizeof(*(dst)))
339 * Variable-size result expected.
341 * @param[out] dst where to store the result, allocated
342 * @param[out] sptr where to store the size of @a dst
343 * @return array entry for the result specification to use
345 struct GNUNET_SQ_ResultSpec
346 GNUNET_SQ_result_spec_variable_size (void **dst,
351 * 0-terminated string expected.
353 * @param[out] dst where to store the result, allocated
354 * @return array entry for the result specification to use
356 struct GNUNET_SQ_ResultSpec
357 GNUNET_SQ_result_spec_string (char **dst);
361 * RSA public key expected.
363 * @param[out] rsa where to store the result
364 * @return array entry for the result specification to use
366 struct GNUNET_SQ_ResultSpec
367 GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
371 * RSA signature expected.
373 * @param[out] sig where to store the result;
374 * @return array entry for the result specification to use
376 struct GNUNET_SQ_ResultSpec
377 GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
381 * Absolute time expected.
383 * @param[out] at where to store the result
384 * @return array entry for the result specification to use
386 struct GNUNET_SQ_ResultSpec
387 GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
391 * Absolute time expected.
393 * @param[out] at where to store the result
394 * @return array entry for the result specification to use
396 struct GNUNET_SQ_ResultSpec
397 GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
403 * @param[out] u16 where to store the result
404 * @return array entry for the result specification to use
406 struct GNUNET_SQ_ResultSpec
407 GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
413 * @param[out] u32 where to store the result
414 * @return array entry for the result specification to use
416 struct GNUNET_SQ_ResultSpec
417 GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
423 * @param[out] u64 where to store the result
424 * @return array entry for the result specification to use
426 struct GNUNET_SQ_ResultSpec
427 GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
431 * Extract results from a query result according to the given specification.
433 * @param result result to process
434 * @param[in,out] rs result specification to extract for
436 * #GNUNET_OK if all results could be extracted
437 * #GNUNET_SYSERR if a result was invalid (non-existing field)
440 GNUNET_SQ_extract_result (sqlite3_stmt *result,
441 struct GNUNET_SQ_ResultSpec *rs);
445 * Free all memory that was allocated in @a rs during
446 * #GNUNET_SQ_extract_result().
448 * @param rs reult specification to clean up
451 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;
476 * Terminator for executable statement list.
478 #define GNUNET_SQ_PREPARE_END { NULL, NULL }
482 * Create a `struct GNUNET_SQ_PrepareStatement`
484 * @param sql actual SQL statement
485 * @param pstmt where to store the handle
486 * @return initialized struct
488 struct GNUNET_SQ_PrepareStatement
489 GNUNET_SQ_make_prepare (const char *sql,
490 sqlite3_stmt **pstmt);
494 * Prepare all statements given in the (NULL,NULL)-terminated
497 * @param dbh database handle
498 * @param ps array of statements to prepare
499 * @return #GNUNET_OK on success
502 GNUNET_SQ_prepare (sqlite3 *dbh,
503 const struct GNUNET_SQ_PrepareStatement *ps);
506 /* ******************** sq_exec.c functions ************** */
510 * Information needed to run a list of SQL statements using
511 * #GNUNET_SQ_exec_statements().
513 struct GNUNET_SQ_ExecuteStatement
516 * Actual SQL statement.
521 * Should we ignore errors?
528 * Terminator for executable statement list.
530 #define GNUNET_SQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
534 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
536 * @param sql actual SQL statement
537 * @return initialized struct
539 struct GNUNET_SQ_ExecuteStatement
540 GNUNET_SQ_make_execute (const char *sql);
544 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
547 * @param sql actual SQL statement
548 * @return initialized struct
550 struct GNUNET_SQ_ExecuteStatement
551 GNUNET_SQ_make_try_execute (const char *sql);
555 * Request execution of an array of statements @a es from Postgres.
557 * @param dbh database to execute the statements over
558 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
560 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
561 * #GNUNET_SYSERR on error
564 GNUNET_SQ_exec_statements (sqlite3 *dbh,
565 const struct GNUNET_SQ_ExecuteStatement *es);
568 #endif /* GNUNET_SQ_LIB_H_ */
570 /* end of include/gnunet_sq_lib.h */