-remove debug message
[oweals/gnunet.git] / src / include / gnunet_sq_lib.h
1 /*
2    This file is part of GNUnet
3    Copyright (C) 2017 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file include/gnunet_sq_lib.h
22  * @brief helper functions for Sqlite3 DB interactions
23  * @author Christian Grothoff
24  */
25 #ifndef GNUNET_SQ_LIB_H
26 #define GNUNET_SQ_LIB_H
27
28 #include <sqlite3.h>
29 #include "gnunet_util_lib.h"
30
31
32 /**
33  * Function called to convert input argument into SQL parameters.
34  *
35  * @param cls closure
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
42  */
43 typedef int
44 (*GNUNET_SQ_QueryConverter)(void *cls,
45                             const void *data,
46                             size_t data_len,
47                             sqlite3_stmt *stmt,
48                             unsigned int off);
49
50
51 /**
52  * @brief Description of a DB query parameter.
53  */
54 struct GNUNET_SQ_QueryParam
55 {
56   /**
57    * Function for how to handle this type of entry.
58    */
59   GNUNET_SQ_QueryConverter conv;
60
61   /**
62    * Closure for @e conv.
63    */
64   void *conv_cls;
65
66   /**
67    * Data or NULL.
68    */
69   const void *data;
70
71   /**
72    * Size of @e data
73    */
74   size_t size;
75
76   /**
77    * Number of parameters eaten by this operation.
78    */
79   unsigned int num_params;
80 };
81
82
83 /**
84  * End of query parameter specification.
85  */
86 #define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
87
88
89 /**
90  * Generate query parameter for a buffer @a ptr of
91  * @a ptr_size bytes.
92  *
93  * @param ptr pointer to the query parameter to pass
94  * @oaran ptr_size number of bytes in @a ptr
95  */
96 struct GNUNET_SQ_QueryParam
97 GNUNET_SQ_query_param_fixed_size (const void *ptr,
98                                   size_t ptr_size);
99
100
101 /**
102  * Generate query parameter for a string.
103  *
104  * @param ptr pointer to the string query parameter to pass
105  */
106 struct GNUNET_SQ_QueryParam
107 GNUNET_SQ_query_param_string (const char *ptr);
108
109
110 /**
111  * Generate fixed-size query parameter with size determined
112  * by variable type.
113  *
114  * @param x pointer to the query parameter to pass.
115  */
116 #define GNUNET_SQ_query_param_auto_from_type( \
117     x) GNUNET_SQ_query_param_fixed_size ((x), sizeof(*(x)))
118
119
120 /**
121  * Generate query parameter for an RSA public key.  The
122  * database must contain a BLOB type in the respective position.
123  *
124  * @param x the query parameter to pass.
125  */
126 struct GNUNET_SQ_QueryParam
127 GNUNET_SQ_query_param_rsa_public_key (const struct
128                                       GNUNET_CRYPTO_RsaPublicKey *x);
129
130
131 /**
132  * Generate query parameter for an RSA signature.  The
133  * database must contain a BLOB type in the respective position.
134  *
135  * @param x the query parameter to pass
136  */
137 struct GNUNET_SQ_QueryParam
138 GNUNET_SQ_query_param_rsa_signature (const struct
139                                      GNUNET_CRYPTO_RsaSignature *x);
140
141
142 /**
143  * Generate query parameter for an absolute time value.
144  * The database must store a 64-bit integer.
145  *
146  * @param x pointer to the query parameter to pass
147  */
148 struct GNUNET_SQ_QueryParam
149 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
150
151
152 /**
153  * Generate query parameter for an absolute time value.
154  * The database must store a 64-bit integer.
155  *
156  * @param x pointer to the query parameter to pass
157  */
158 struct GNUNET_SQ_QueryParam
159 GNUNET_SQ_query_param_absolute_time_nbo (const struct
160                                          GNUNET_TIME_AbsoluteNBO *x);
161
162
163 /**
164  * Generate query parameter for an uint16_t in host byte order.
165  *
166  * @param x pointer to the query parameter to pass
167  */
168 struct GNUNET_SQ_QueryParam
169 GNUNET_SQ_query_param_uint16 (const uint16_t *x);
170
171
172 /**
173  * Generate query parameter for an uint32_t in host byte order.
174  *
175  * @param x pointer to the query parameter to pass
176  */
177 struct GNUNET_SQ_QueryParam
178 GNUNET_SQ_query_param_uint32 (const uint32_t *x);
179
180
181 /**
182  * Generate query parameter for an uint16_t in host byte order.
183  *
184  * @param x pointer to the query parameter to pass
185  */
186 struct GNUNET_SQ_QueryParam
187 GNUNET_SQ_query_param_uint64 (const uint64_t *x);
188
189
190 /**
191  * Execute binding operations for a prepared statement.
192  *
193  * @param db_conn database connection
194  * @param params parameters to the statement
195  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
196  */
197 int
198 GNUNET_SQ_bind (sqlite3_stmt *stmt,
199                 const struct GNUNET_SQ_QueryParam *params);
200
201
202 /**
203  * Reset @a stmt and log error.
204  *
205  * @param dbh database handle
206  * @param stmt statement to reset
207  */
208 void
209 GNUNET_SQ_reset (sqlite3 *dbh,
210                  sqlite3_stmt *stmt);
211
212
213 /**
214  * Extract data from a Postgres database @a result at row @a row.
215  *
216  * @param cls closure
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
221  * @return
222  *   #GNUNET_YES if all results could be extracted
223  *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
224  */
225 typedef int
226 (*GNUNET_SQ_ResultConverter)(void *cls,
227                              sqlite3_stmt *result,
228                              unsigned int column,
229                              size_t *dst_size,
230                              void *dst);
231
232
233 /**
234  * @brief Description of a DB result cell.
235  */
236 struct GNUNET_SQ_ResultSpec;
237
238
239 /**
240  * Function called to clean up memory allocated
241  * by a #GNUNET_SQ_ResultConverter.
242  *
243  * @param cls closure
244  */
245 typedef void
246 (*GNUNET_SQ_ResultCleanup)(void *cls);
247
248
249 /**
250  * @brief Description of a DB result cell.
251  */
252 struct GNUNET_SQ_ResultSpec
253 {
254   /**
255    * What is the format of the result?
256    */
257   GNUNET_SQ_ResultConverter conv;
258
259   /**
260    * Function to clean up result data, NULL if cleanup is
261    * not necessary.
262    */
263   GNUNET_SQ_ResultCleanup cleaner;
264
265   /**
266    * Closure for @e conv and @e cleaner.
267    */
268   void *cls;
269
270   /**
271    * Destination for the data.
272    */
273   void *dst;
274
275   /**
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).
279    */
280   size_t dst_size;
281
282   /**
283    * Where to store actual size of the result.  If left at
284    * NULL, will be made to point to @e dst_size before
285    * @a conv is called.
286    */
287   size_t *result_size;
288
289   /**
290    * Number of parameters (columns) eaten by this operation.
291    */
292   unsigned int num_params;
293 };
294
295
296 /**
297  * End of result parameter specification.
298  *
299  * @return array last entry for the result specification to use
300  */
301 #define GNUNET_SQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, 0 }
302
303
304 /**
305  * Variable-size result expected.
306  *
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
310  */
311 struct GNUNET_SQ_ResultSpec
312 GNUNET_SQ_result_spec_variable_size (void **dst,
313                                      size_t *sptr);
314
315
316 /**
317  * Fixed-size result expected.
318  *
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
322  */
323 struct GNUNET_SQ_ResultSpec
324 GNUNET_SQ_result_spec_fixed_size (void *dst,
325                                   size_t dst_size);
326
327
328 /**
329  * We expect a fixed-size result, with size determined by the type of `* dst`
330  *
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
333  */
334 #define GNUNET_SQ_result_spec_auto_from_type( \
335     dst) GNUNET_SQ_result_spec_fixed_size ((dst), sizeof(*(dst)))
336
337
338 /**
339  * Variable-size result expected.
340  *
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
344  */
345 struct GNUNET_SQ_ResultSpec
346 GNUNET_SQ_result_spec_variable_size (void **dst,
347                                      size_t *sptr);
348
349
350 /**
351  * 0-terminated string expected.
352  *
353  * @param[out] dst where to store the result, allocated
354  * @return array entry for the result specification to use
355  */
356 struct GNUNET_SQ_ResultSpec
357 GNUNET_SQ_result_spec_string (char **dst);
358
359
360 /**
361  * RSA public key expected.
362  *
363  * @param[out] rsa where to store the result
364  * @return array entry for the result specification to use
365  */
366 struct GNUNET_SQ_ResultSpec
367 GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
368
369
370 /**
371  * RSA signature expected.
372  *
373  * @param[out] sig where to store the result;
374  * @return array entry for the result specification to use
375  */
376 struct GNUNET_SQ_ResultSpec
377 GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
378
379
380 /**
381  * Absolute time expected.
382  *
383  * @param[out] at where to store the result
384  * @return array entry for the result specification to use
385  */
386 struct GNUNET_SQ_ResultSpec
387 GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
388
389
390 /**
391  * Absolute time expected.
392  *
393  * @param[out] at where to store the result
394  * @return array entry for the result specification to use
395  */
396 struct GNUNET_SQ_ResultSpec
397 GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
398
399
400 /**
401  * uint16_t expected.
402  *
403  * @param[out] u16 where to store the result
404  * @return array entry for the result specification to use
405  */
406 struct GNUNET_SQ_ResultSpec
407 GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
408
409
410 /**
411  * uint32_t expected.
412  *
413  * @param[out] u32 where to store the result
414  * @return array entry for the result specification to use
415  */
416 struct GNUNET_SQ_ResultSpec
417 GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
418
419
420 /**
421  * uint64_t expected.
422  *
423  * @param[out] u64 where to store the result
424  * @return array entry for the result specification to use
425  */
426 struct GNUNET_SQ_ResultSpec
427 GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
428
429
430 /**
431  * Extract results from a query result according to the given specification.
432  *
433  * @param result result to process
434  * @param[in,out] rs result specification to extract for
435  * @return
436  *   #GNUNET_OK if all results could be extracted
437  *   #GNUNET_SYSERR if a result was invalid (non-existing field)
438  */
439 int
440 GNUNET_SQ_extract_result (sqlite3_stmt *result,
441                           struct GNUNET_SQ_ResultSpec *rs);
442
443
444 /**
445  * Free all memory that was allocated in @a rs during
446  * #GNUNET_SQ_extract_result().
447  *
448  * @param rs reult specification to clean up
449  */
450 void
451 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs);
452
453
454 /* ******************** sq_prepare.c functions ************** */
455
456
457 /**
458  * Information needed to run a list of SQL statements using
459  * #GNUNET_SQ_exec_statements().
460  */
461 struct GNUNET_SQ_PrepareStatement
462 {
463   /**
464    * Actual SQL statement.
465    */
466   const char *sql;
467
468   /**
469    * Where to store handle?
470    */
471   sqlite3_stmt **pstmt;
472 };
473
474
475 /**
476  * Terminator for executable statement list.
477  */
478 #define GNUNET_SQ_PREPARE_END { NULL, NULL }
479
480
481 /**
482  * Create a `struct GNUNET_SQ_PrepareStatement`
483  *
484  * @param sql actual SQL statement
485  * @param pstmt where to store the handle
486  * @return initialized struct
487  */
488 struct GNUNET_SQ_PrepareStatement
489 GNUNET_SQ_make_prepare (const char *sql,
490                         sqlite3_stmt **pstmt);
491
492
493 /**
494  * Prepare all statements given in the (NULL,NULL)-terminated
495  * array at @a ps
496  *
497  * @param dbh database handle
498  * @param ps array of statements to prepare
499  * @return #GNUNET_OK on success
500  */
501 int
502 GNUNET_SQ_prepare (sqlite3 *dbh,
503                    const struct GNUNET_SQ_PrepareStatement *ps);
504
505
506 /* ******************** sq_exec.c functions ************** */
507
508
509 /**
510  * Information needed to run a list of SQL statements using
511  * #GNUNET_SQ_exec_statements().
512  */
513 struct GNUNET_SQ_ExecuteStatement
514 {
515   /**
516    * Actual SQL statement.
517    */
518   const char *sql;
519
520   /**
521    * Should we ignore errors?
522    */
523   int ignore_errors;
524 };
525
526
527 /**
528  * Terminator for executable statement list.
529  */
530 #define GNUNET_SQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
531
532
533 /**
534  * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
535  *
536  * @param sql actual SQL statement
537  * @return initialized struct
538  */
539 struct GNUNET_SQ_ExecuteStatement
540 GNUNET_SQ_make_execute (const char *sql);
541
542
543 /**
544  * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
545  * be tolerated.
546  *
547  * @param sql actual SQL statement
548  * @return initialized struct
549  */
550 struct GNUNET_SQ_ExecuteStatement
551 GNUNET_SQ_make_try_execute (const char *sql);
552
553
554 /**
555  * Request execution of an array of statements @a es from Postgres.
556  *
557  * @param dbh database to execute the statements over
558  * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
559  *            statements.
560  * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
561  *         #GNUNET_SYSERR on error
562  */
563 int
564 GNUNET_SQ_exec_statements (sqlite3 *dbh,
565                            const struct GNUNET_SQ_ExecuteStatement *es);
566
567
568 #endif  /* GNUNET_SQ_LIB_H_ */
569
570 /* end of include/gnunet_sq_lib.h */