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 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_sq_lib.h
18 * @brief helper functions for Sqlite3 DB interactions
19 * @author Christian Grothoff
21 #ifndef GNUNET_SQ_LIB_H
22 #define GNUNET_SQ_LIB_H
25 #include "gnunet_util_lib.h"
29 * Function called to convert input argument into SQL parameters.
32 * @param data pointer to input argument
33 * @param data_len number of bytes in @a data (if applicable)
34 * @param stmt sqlite statement to bind parameters for
35 * @param off offset of the argument to bind in @a stmt, numbered from 1,
36 * so immediately suitable for passing to `sqlite3_bind`-functions.
37 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
40 (*GNUNET_SQ_QueryConverter)(void *cls,
48 * @brief Description of a DB query parameter.
50 struct GNUNET_SQ_QueryParam
54 * Function for how to handle this type of entry.
56 GNUNET_SQ_QueryConverter conv;
59 * Closure for @e conv.
74 * Number of parameters eaten by this operation.
76 unsigned int num_params;
81 * End of query parameter specification.
83 #define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
87 * Generate query parameter for a buffer @a ptr of
90 * @param ptr pointer to the query parameter to pass
91 * @oaran ptr_size number of bytes in @a ptr
93 struct GNUNET_SQ_QueryParam
94 GNUNET_SQ_query_param_fixed_size (const void *ptr,
100 * Generate query parameter for a string.
102 * @param ptr pointer to the string query parameter to pass
104 struct GNUNET_SQ_QueryParam
105 GNUNET_SQ_query_param_string (const char *ptr);
109 * Generate fixed-size query parameter with size determined
112 * @param x pointer to the query parameter to pass.
114 #define GNUNET_SQ_query_param_auto_from_type(x) GNUNET_SQ_query_param_fixed_size ((x), sizeof (*(x)))
118 * Generate query parameter for an RSA public key. The
119 * database must contain a BLOB type in the respective position.
121 * @param x the query parameter to pass.
123 struct GNUNET_SQ_QueryParam
124 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
128 * Generate query parameter for an RSA signature. The
129 * database must contain a BLOB type in the respective position.
131 * @param x the query parameter to pass
133 struct GNUNET_SQ_QueryParam
134 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
138 * Generate query parameter for an absolute time value.
139 * The database must store a 64-bit integer.
141 * @param x pointer to the query parameter to pass
143 struct GNUNET_SQ_QueryParam
144 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *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_SQ_QueryParam
154 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
158 * Generate query parameter for an uint16_t in host byte order.
160 * @param x pointer to the query parameter to pass
162 struct GNUNET_SQ_QueryParam
163 GNUNET_SQ_query_param_uint16 (const uint16_t *x);
167 * Generate query parameter for an uint32_t in host byte order.
169 * @param x pointer to the query parameter to pass
171 struct GNUNET_SQ_QueryParam
172 GNUNET_SQ_query_param_uint32 (const uint32_t *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_SQ_QueryParam
181 GNUNET_SQ_query_param_uint64 (const uint64_t *x);
185 * Extract data from a Postgres database @a result at row @a row.
188 * @param result where to extract data from
189 * @param row row to extract data from
190 * @param column column to extract data from
191 * @param[in,out] dst_size where to store size of result, may be NULL
192 * @param[out] dst where to store the result
194 * #GNUNET_YES if all results could be extracted
195 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
198 (*GNUNET_SQ_ResultConverter)(void *cls,
199 sqlite3_stmt *result,
207 * @brief Description of a DB result cell.
209 struct GNUNET_SQ_ResultSpec;
213 * Function called to clean up memory allocated
214 * by a #GNUNET_SQ_ResultConverter.
219 (*GNUNET_SQ_ResultCleanup)(void *cls);
223 * @brief Description of a DB result cell.
225 struct GNUNET_SQ_ResultSpec
229 * What is the format of the result?
231 GNUNET_SQ_ResultConverter conv;
234 * Function to clean up result data, NULL if cleanup is
237 GNUNET_SQ_ResultCleanup cleaner;
240 * Closure for @e conv and @e cleaner.
245 * Destination for the data.
250 * Allowed size for the data, 0 for variable-size
251 * (in this case, the type of @e dst is a `void **`
252 * and we need to allocate a buffer of the right size).
257 * Where to store actual size of the result.
262 * Number of parameters (columns) eaten by this operation.
264 unsigned int num_params;
270 * End of result parameter specification.
272 * @return array last entry for the result specification to use
274 #define GNUNET_SQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL }
278 * Variable-size result expected.
280 * @param[out] dst where to store the result, allocated
281 * @param[out] sptr where to store the size of @a dst
282 * @return array entry for the result specification to use
284 struct GNUNET_SQ_ResultSpec
285 GNUNET_SQ_result_spec_variable_size (void **dst,
290 * Fixed-size result expected.
292 * @param[out] dst where to store the result
293 * @param dst_size number of bytes in @a dst
294 * @return array entry for the result specification to use
296 struct GNUNET_SQ_ResultSpec
297 GNUNET_SQ_result_spec_fixed_size (void *dst,
302 * We expect a fixed-size result, with size determined by the type of `* dst`
304 * @param dst point to where to store the result, type fits expected result size
305 * @return array entry for the result specification to use
307 #define GNUNET_SQ_result_spec_auto_from_type(dst) GNUNET_SQ_result_spec_fixed_size (name, (dst), sizeof (*(dst)))
311 * Variable-size result expected.
313 * @param[out] dst where to store the result, allocated
314 * @param[out] sptr where to store the size of @a dst
315 * @return array entry for the result specification to use
317 struct GNUNET_SQ_ResultSpec
318 GNUNET_SQ_result_spec_variable_size (void **dst,
323 * 0-terminated string expected.
325 * @param[out] dst where to store the result, allocated
326 * @return array entry for the result specification to use
328 struct GNUNET_SQ_ResultSpec
329 GNUNET_SQ_result_spec_string (char **dst);
333 * RSA public key expected.
335 * @param[out] rsa where to store the result
336 * @return array entry for the result specification to use
338 struct GNUNET_SQ_ResultSpec
339 GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
343 * RSA signature expected.
345 * @param[out] sig where to store the result;
346 * @return array entry for the result specification to use
348 struct GNUNET_SQ_ResultSpec
349 GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
353 * Absolute time expected.
355 * @param[out] at where to store the result
356 * @return array entry for the result specification to use
358 struct GNUNET_SQ_ResultSpec
359 GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
363 * Absolute time expected.
365 * @param[out] at where to store the result
366 * @return array entry for the result specification to use
368 struct GNUNET_SQ_ResultSpec
369 GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
375 * @param[out] u16 where to store the result
376 * @return array entry for the result specification to use
378 struct GNUNET_SQ_ResultSpec
379 GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
385 * @param[out] u32 where to store the result
386 * @return array entry for the result specification to use
388 struct GNUNET_SQ_ResultSpec
389 GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
395 * @param[out] u64 where to store the result
396 * @return array entry for the result specification to use
398 struct GNUNET_SQ_ResultSpec
399 GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
403 * Execute a prepared statement.
405 * @param db_conn database connection
406 * @param params parameters to the statement
407 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
410 GNUNET_SQ_bind (sqlite3_stmt *stmt,
411 const struct GNUNET_SQ_QueryParam *params);
415 * Extract results from a query result according to the given specification.
417 * @param result result to process
418 * @param[in,out] rs result specification to extract for
419 * @param row row from the result to extract
421 * #GNUNET_OK if all results could be extracted
422 * #GNUNET_SYSERR if a result was invalid (non-existing field)
425 GNUNET_SQ_extract_result (sqlite3_stmt *result,
426 struct GNUNET_SQ_ResultSpec *rs,
431 * Free all memory that was allocated in @a rs during
432 * #GNUNET_SQ_extract_result().
434 * @param rs reult specification to clean up
437 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs);
440 #endif /* GNUNET_SQ_LIB_H_ */
442 /* end of include/gnunet_sq_lib.h */