2 This file is part of GNUnet
3 Copyright (C) 2016 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 DB interactions
19 * @author Christian Grothoff
21 #ifndef GNUNET_PQ_LIB_H_
22 #define GNUNET_PQ_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[out] param_values SQL data to set
35 * @param[out] param_lengths SQL length data to set
36 * @param[out] param_formats SQL format data to set
37 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
38 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
39 * @param scratch_length number of entries left in @a scratch
40 * @return -1 on error, number of offsets used in @a scratch otherwise
43 (*GNUNET_PQ_QueryConverter)(void *cls,
49 unsigned int param_length,
51 unsigned int scratch_length);
55 * @brief Description of a DB query parameter.
57 struct GNUNET_PQ_QueryParam
61 * Function for how to handle this type of entry.
63 GNUNET_PQ_QueryConverter conv;
66 * Closure for @e conv.
81 * Number of parameters eaten by this operation.
83 unsigned int num_params;
88 * End of query parameter specification.
90 #define GNUNET_PQ_query_param_end { NULL, NULL, NULL, 0, 0 }
94 * Generate query parameter for a buffer @a ptr of
97 * @param ptr pointer to the query parameter to pass
98 * @oaran ptr_size number of bytes in @a ptr
100 struct GNUNET_PQ_QueryParam
101 GNUNET_PQ_query_param_fixed_size (const void *ptr,
107 * Generate query parameter for a string.
109 * @param ptr pointer to the string query parameter to pass
111 struct GNUNET_PQ_QueryParam
112 GNUNET_PQ_query_param_string (const char *ptr);
116 * Generate fixed-size query parameter with size determined
119 * @param x pointer to the query parameter to pass.
121 #define GNUNET_PQ_query_param_auto_from_type(x) GNUNET_PQ_query_param_fixed_size ((x), sizeof (*(x)))
125 * Generate query parameter for an RSA public key. The
126 * database must contain a BLOB type in the respective position.
128 * @param x the query parameter to pass.
130 struct GNUNET_PQ_QueryParam
131 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
135 * Generate query parameter for an RSA signature. The
136 * database must contain a BLOB type in the respective position.
138 * @param x the query parameter to pass
140 struct GNUNET_PQ_QueryParam
141 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
145 * Generate query parameter for an absolute time value.
146 * The database must store a 64-bit integer.
148 * @param x pointer to the query parameter to pass
150 struct GNUNET_PQ_QueryParam
151 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
155 * Generate query parameter for an absolute time value.
156 * The database must store a 64-bit integer.
158 * @param x pointer to the query parameter to pass
160 struct GNUNET_PQ_QueryParam
161 GNUNET_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
165 * Generate query parameter for an uint16_t in host byte order.
167 * @param x pointer to the query parameter to pass
169 struct GNUNET_PQ_QueryParam
170 GNUNET_PQ_query_param_uint16 (const uint16_t *x);
174 * Generate query parameter for an uint32_t in host byte order.
176 * @param x pointer to the query parameter to pass
178 struct GNUNET_PQ_QueryParam
179 GNUNET_PQ_query_param_uint32 (const uint32_t *x);
183 * Generate query parameter for an uint16_t in host byte order.
185 * @param x pointer to the query parameter to pass
187 struct GNUNET_PQ_QueryParam
188 GNUNET_PQ_query_param_uint64 (const uint64_t *x);
192 * Extract data from a Postgres database @a result at row @a row.
195 * @param result where to extract data from
196 * @param int row to extract data from
197 * @param fname name (or prefix) of the fields to extract from
198 * @param[in,out] dst_size where to store size of result, may be NULL
199 * @param[out] dst where to store the result
201 * #GNUNET_YES if all results could be extracted
202 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
205 (*GNUNET_PQ_ResultConverter)(void *cls,
214 * Function called to clean up memory allocated
215 * by a #GNUNET_PQ_ResultConverter.
218 * @param rd result data to clean up
221 (*GNUNET_PQ_ResultCleanup)(void *cls,
226 * @brief Description of a DB result cell.
228 struct GNUNET_PQ_ResultSpec
232 * What is the format of the result?
234 GNUNET_PQ_ResultConverter conv;
237 * Function to clean up result data, NULL if cleanup is
240 GNUNET_PQ_ResultCleanup cleaner;
243 * Closure for @e conv and @e cleaner.
248 * Destination for the data.
253 * Allowed size for the data, 0 for variable-size
254 * (in this case, the type of @e dst is a `void **`
255 * and we need to allocate a buffer of the right size).
260 * Field name of the desired result.
265 * Where to store actual size of the result.
273 * End of result parameter specification.
275 * @return array last entry for the result specification to use
277 #define GNUNET_PQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, NULL }
281 * Variable-size result expected.
283 * @param name name of the field in the table
284 * @param[out] dst where to store the result, allocated
285 * @param[out] sptr where to store the size of @a dst
286 * @return array entry for the result specification to use
288 struct GNUNET_PQ_ResultSpec
289 GNUNET_PQ_result_spec_variable_size (const char *name,
295 * Fixed-size result expected.
297 * @param name name of the field in the table
298 * @param[out] dst where to store the result
299 * @param dst_size number of bytes in @a dst
300 * @return array entry for the result specification to use
302 struct GNUNET_PQ_ResultSpec
303 GNUNET_PQ_result_spec_fixed_size (const char *name,
310 * We expect a fixed-size result, with size determined by the type of `* dst`
312 * @param name name of the field in the table
313 * @param dst point to where to store the result, type fits expected result size
314 * @return array entry for the result specification to use
316 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof (*(dst)))
320 * Variable-size result expected.
322 * @param name name of the field in the table
323 * @param[out] dst where to store the result, allocated
324 * @param[out] sptr where to store the size of @a dst
325 * @return array entry for the result specification to use
327 struct GNUNET_PQ_ResultSpec
328 GNUNET_PQ_result_spec_variable_size (const char *name,
334 * 0-terminated string expected.
336 * @param name name of the field in the table
337 * @param[out] dst where to store the result, allocated
338 * @return array entry for the result specification to use
340 struct GNUNET_PQ_ResultSpec
341 GNUNET_PQ_result_spec_string (const char *name,
346 * RSA public key expected.
348 * @param name name of the field in the table
349 * @param[out] rsa where to store the result
350 * @return array entry for the result specification to use
352 struct GNUNET_PQ_ResultSpec
353 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
354 struct GNUNET_CRYPTO_RsaPublicKey **rsa);
358 * RSA signature expected.
360 * @param name name of the field in the table
361 * @param[out] sig where to store the result;
362 * @return array entry for the result specification to use
364 struct GNUNET_PQ_ResultSpec
365 GNUNET_PQ_result_spec_rsa_signature (const char *name,
366 struct GNUNET_CRYPTO_RsaSignature **sig);
370 * Absolute time expected.
372 * @param name name of the field in the table
373 * @param[out] at where to store the result
374 * @return array entry for the result specification to use
376 struct GNUNET_PQ_ResultSpec
377 GNUNET_PQ_result_spec_absolute_time (const char *name,
378 struct GNUNET_TIME_Absolute *at);
382 * Absolute time expected.
384 * @param name name of the field in the table
385 * @param[out] at where to store the result
386 * @return array entry for the result specification to use
388 struct GNUNET_PQ_ResultSpec
389 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
390 struct GNUNET_TIME_AbsoluteNBO *at);
396 * @param name name of the field in the table
397 * @param[out] u16 where to store the result
398 * @return array entry for the result specification to use
400 struct GNUNET_PQ_ResultSpec
401 GNUNET_PQ_result_spec_uint16 (const char *name,
408 * @param name name of the field in the table
409 * @param[out] u32 where to store the result
410 * @return array entry for the result specification to use
412 struct GNUNET_PQ_ResultSpec
413 GNUNET_PQ_result_spec_uint32 (const char *name,
420 * @param name name of the field in the table
421 * @param[out] u64 where to store the result
422 * @return array entry for the result specification to use
424 struct GNUNET_PQ_ResultSpec
425 GNUNET_PQ_result_spec_uint64 (const char *name,
430 * Execute a prepared statement.
432 * @param db_conn database connection
433 * @param name name of the prepared statement
434 * @param params parameters to the statement
435 * @return postgres result
438 GNUNET_PQ_exec_prepared (PGconn *db_conn,
440 const struct GNUNET_PQ_QueryParam *params);
444 * Extract results from a query result according to the given specification.
446 * @param result result to process
447 * @param[in,out] rs result specification to extract for
448 * @param row row from the result to extract
450 * #GNUNET_YES if all results could be extracted
451 * #GNUNET_SYSERR if a result was invalid (non-existing field)
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 #endif /* GNUNET_PQ_LIB_H_ */
471 /* end of include/gnunet_pq_lib.h */