GNUnet  0.20.0
gnunet_sq_lib.h
Go to the documentation of this file.
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  */
25 #ifndef GNUNET_SQ_LIB_H
26 #define GNUNET_SQ_LIB_H
27 
28 
29 #include <sqlite3.h>
30 #include "gnunet_util_lib.h"
31 
32 
44 typedef enum GNUNET_GenericReturnValue
45 (*GNUNET_SQ_QueryConverter)(void *cls,
46  const void *data,
47  size_t data_len,
48  sqlite3_stmt *stmt,
49  unsigned int off);
50 
51 
56 {
61 
65  void *conv_cls;
66 
70  const void *data;
71 
75  size_t size;
76 
80  unsigned int num_params;
81 };
82 
83 
87 #define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
88 
89 
98 GNUNET_SQ_query_param_fixed_size (const void *ptr,
99  size_t ptr_size);
100 
101 
108 GNUNET_SQ_query_param_string (const char *ptr);
109 
110 
117 #define GNUNET_SQ_query_param_auto_from_type( \
118  x) GNUNET_SQ_query_param_fixed_size ((x), sizeof(*(x)))
119 
120 
130 
131 
141 
142 
151 
152 
161  const struct GNUNET_TIME_AbsoluteNBO *x);
162 
163 
170 GNUNET_SQ_query_param_uint16 (const uint16_t *x);
171 
172 
179 GNUNET_SQ_query_param_uint32 (const uint32_t *x);
180 
181 
188 GNUNET_SQ_query_param_uint64 (const uint64_t *x);
189 
190 
199 GNUNET_SQ_bind (sqlite3_stmt *stmt,
200  const struct GNUNET_SQ_QueryParam *params);
201 
202 
209 void
210 GNUNET_SQ_reset (sqlite3 *dbh,
211  sqlite3_stmt *stmt);
212 
213 
226 typedef enum GNUNET_GenericReturnValue
227 (*GNUNET_SQ_ResultConverter)(void *cls,
228  sqlite3_stmt *result,
229  unsigned int column,
230  size_t *dst_size,
231  void *dst);
232 
233 
237 struct GNUNET_SQ_ResultSpec;
238 
239 
246 typedef void
247 (*GNUNET_SQ_ResultCleanup)(void *cls);
248 
249 
254 {
259 
265 
269  void *cls;
270 
274  void *dst;
275 
281  size_t dst_size;
282 
288  size_t *result_size;
289 
293  unsigned int num_params;
294 };
295 
296 
302 #define GNUNET_SQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, 0 }
303 
304 
314  size_t *sptr);
315 
316 
326  size_t dst_size);
327 
328 
335 #define GNUNET_SQ_result_spec_auto_from_type( \
336  dst) GNUNET_SQ_result_spec_fixed_size ((dst), sizeof(*(dst)))
337 
338 
348  size_t *sptr);
349 
350 
359 
360 
369 
370 
379 
380 
389 
390 
399 
400 
408 GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
409 
410 
418 GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
419 
420 
428 GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
429 
430 
441 GNUNET_SQ_extract_result (sqlite3_stmt *result,
442  struct GNUNET_SQ_ResultSpec *rs);
443 
444 
451 void
453 
454 
455 /* ******************** sq_prepare.c functions ************** */
456 
457 
463 {
467  const char *sql;
468 
472  sqlite3_stmt **pstmt;
473 };
474 
475 
479 #define GNUNET_SQ_PREPARE_END { NULL, NULL }
480 
481 
490 GNUNET_SQ_make_prepare (const char *sql,
491  sqlite3_stmt **pstmt);
492 
493 
503 GNUNET_SQ_prepare (sqlite3 *dbh,
504  const struct GNUNET_SQ_PrepareStatement *ps);
505 
506 
507 /* ******************** sq_exec.c functions ************** */
508 
509 
515 {
519  const char *sql;
520 
525 };
526 
527 
531 #define GNUNET_SQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
532 
533 
541 GNUNET_SQ_make_execute (const char *sql);
542 
543 
552 GNUNET_SQ_make_try_execute (const char *sql);
553 
554 
565 GNUNET_SQ_exec_statements (sqlite3 *dbh,
566  const struct GNUNET_SQ_ExecuteStatement *es);
567 
568 
569 #endif /* GNUNET_SQ_LIB_H_ */
570 
571 /* end of include/gnunet_sq_lib.h */
uint32_t data
The data value.
static int result
Global testing status.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
struct GNUNET_SQ_PrepareStatement GNUNET_SQ_make_prepare(const char *sql, sqlite3_stmt **pstmt)
Create a struct GNUNET_SQ_PrepareStatement
Definition: sq_prepare.c:38
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_public_key(struct GNUNET_CRYPTO_RsaPublicKey **rsa)
RSA public key expected.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_execute(const char *sql)
Create a struct GNUNET_SQ_ExecuteStatement where errors are fatal.
Definition: sq_exec.c:36
struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_try_execute(const char *sql)
Create a struct GNUNET_SQ_ExecuteStatement where errors should be tolerated.
Definition: sq_exec.c:55
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_signature(struct GNUNET_CRYPTO_RsaSignature **sig)
RSA signature expected.
enum GNUNET_GenericReturnValue GNUNET_SQ_extract_result(sqlite3_stmt *result, struct GNUNET_SQ_ResultSpec *rs)
Extract results from a query result according to the given specification.
Definition: sq.c:76
void(* GNUNET_SQ_ResultCleanup)(void *cls)
Function called to clean up memory allocated by a GNUNET_SQ_ResultConverter.
void GNUNET_SQ_cleanup_result(struct GNUNET_SQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_SQ_extract_result().
Definition: sq.c:105
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time expected.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
enum GNUNET_GenericReturnValue(* GNUNET_SQ_QueryConverter)(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
Definition: gnunet_sq_lib.h:45
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint16(uint16_t *u16)
uint16_t expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
enum GNUNET_GenericReturnValue GNUNET_SQ_exec_statements(sqlite3 *dbh, const struct GNUNET_SQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: sq_exec.c:76
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32(uint32_t *u32)
uint32_t expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_fixed_size(void *dst, size_t dst_size)
Fixed-size result expected.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
enum GNUNET_GenericReturnValue GNUNET_SQ_prepare(sqlite3 *dbh, const struct GNUNET_SQ_PrepareStatement *ps)
Prepare all statements given in the (NULL,NULL)-terminated array at ps.
Definition: sq_prepare.c:51
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
enum GNUNET_GenericReturnValue(* GNUNET_SQ_ResultConverter)(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
enum GNUNET_GenericReturnValue GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:31
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:120
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
GNUNET_GenericReturnValue
Named constants for return values.
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
bool ignore_errors
Should we ignore errors?
const char * sql
Actual SQL statement.
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
const char * sql
Actual SQL statement.
sqlite3_stmt ** pstmt
Where to store handle?
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:56
const void * data
Data or NULL.
Definition: gnunet_sq_lib.h:70
void * conv_cls
Closure for conv.
Definition: gnunet_sq_lib.h:65
GNUNET_SQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_sq_lib.h:60
size_t size
Size of data.
Definition: gnunet_sq_lib.h:75
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_sq_lib.h:80
Description of a DB result cell.
size_t * result_size
Where to store actual size of the result.
GNUNET_SQ_ResultConverter conv
What is the format of the result?
GNUNET_SQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.
void * cls
Closure for conv and cleaner.
unsigned int num_params
Number of parameters (columns) eaten by this operation.
size_t dst_size
Allowed size for the data, 0 for variable-size (in this case, the type of dst is a void ** and we nee...
void * dst
Destination for the data.
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.