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