GNUnet  0.11.x
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 #include <sqlite3.h>
29 #include "gnunet_util_lib.h"
30 
31 
43 typedef int
45  const void *data,
46  size_t data_len,
47  sqlite3_stmt *stmt,
48  unsigned int off);
49 
50 
55 {
60 
64  void *conv_cls;
65 
69  const void *data;
70 
74  size_t size;
75 
79  unsigned int num_params;
80 };
81 
82 
86 #define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
87 
88 
97 GNUNET_SQ_query_param_fixed_size (const void *ptr,
98  size_t ptr_size);
99 
100 
107 GNUNET_SQ_query_param_string (const char *ptr);
108 
109 
116 #define GNUNET_SQ_query_param_auto_from_type( \
117  x) GNUNET_SQ_query_param_fixed_size ((x), sizeof(*(x)))
118 
119 
129 
130 
140 
141 
150 
151 
161 
162 
169 GNUNET_SQ_query_param_uint16 (const uint16_t *x);
170 
171 
178 GNUNET_SQ_query_param_uint32 (const uint32_t *x);
179 
180 
187 GNUNET_SQ_query_param_uint64 (const uint64_t *x);
188 
189 
197 int
198 GNUNET_SQ_bind (sqlite3_stmt *stmt,
199  const struct GNUNET_SQ_QueryParam *params);
200 
201 
208 void
209 GNUNET_SQ_reset (sqlite3 *dbh,
210  sqlite3_stmt *stmt);
211 
212 
225 typedef int
227  sqlite3_stmt *result,
228  unsigned int column,
229  size_t *dst_size,
230  void *dst);
231 
232 
236 struct GNUNET_SQ_ResultSpec;
237 
238 
245 typedef void
247 
248 
253 {
258 
264 
268  void *cls;
269 
273  void *dst;
274 
280  size_t dst_size;
281 
287  size_t *result_size;
288 
292  unsigned int num_params;
293 };
294 
295 
301 #define GNUNET_SQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL, 0 }
302 
303 
313  size_t *sptr);
314 
315 
325  size_t dst_size);
326 
327 
334 #define GNUNET_SQ_result_spec_auto_from_type( \
335  dst) GNUNET_SQ_result_spec_fixed_size ((dst), sizeof(*(dst)))
336 
337 
347  size_t *sptr);
348 
349 
358 
359 
368 
369 
378 
379 
388 
389 
398 
399 
407 GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
408 
409 
417 GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
418 
419 
427 GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
428 
429 
439 int
440 GNUNET_SQ_extract_result (sqlite3_stmt *result,
441  struct GNUNET_SQ_ResultSpec *rs);
442 
443 
450 void
452 
453 
454 /* ******************** sq_prepare.c functions ************** */
455 
456 
462 {
466  const char *sql;
467 
471  sqlite3_stmt **pstmt;
472 };
473 
474 
478 #define GNUNET_SQ_PREPARE_END { NULL, NULL }
479 
480 
489 GNUNET_SQ_make_prepare (const char *sql,
490  sqlite3_stmt **pstmt);
491 
492 
501 int
502 GNUNET_SQ_prepare (sqlite3 *dbh,
503  const struct GNUNET_SQ_PrepareStatement *ps);
504 
505 
506 /* ******************** sq_exec.c functions ************** */
507 
508 
514 {
518  const char *sql;
519 
524 };
525 
526 
530 #define GNUNET_SQ_EXECUTE_STATEMENT_END { NULL, GNUNET_SYSERR }
531 
532 
540 GNUNET_SQ_make_execute (const char *sql);
541 
542 
551 GNUNET_SQ_make_try_execute (const char *sql);
552 
553 
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 */
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint16(uint16_t *u16)
uint16_t expected.
Description of a DB result cell.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32(uint32_t *u32)
uint32_t expected.
an RSA signature
Definition: crypto_rsa.c:65
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.
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_sq_lib.h:79
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
GNUNET_SQ_ResultConverter conv
What is the format of the result?
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
int ignore_errors
Should we ignore errors?
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
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.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_SQ_PrepareStatement GNUNET_SQ_make_prepare(const char *sql, sqlite3_stmt **pstmt)
Create a struct GNUNET_SQ_PrepareStatement
Definition: sq_prepare.c:37
int 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:82
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_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time expected.
const char * sql
Actual SQL statement.
void * cls
Closure for conv and cleaner.
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:117
int 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
GNUNET_SQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.
unsigned int num_params
Number of parameters (columns) eaten by this operation.
void * conv_cls
Closure for conv.
Definition: gnunet_sq_lib.h:64
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_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
static int result
Global testing status.
const char * sql
Actual SQL statement.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
void * dst
Destination for the data.
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...
int GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
void * cls
Closure for the callbacks.
size_t * result_size
Where to store actual size of the result.
int(* 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.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:54
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:132
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
size_t size
Size of data.
Definition: gnunet_sq_lib.h:74
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_public_key(struct GNUNET_CRYPTO_RsaPublicKey **rsa)
RSA public key expected.
const void * data
Data or NULL.
Definition: gnunet_sq_lib.h:69
int 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:58
int(* 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:44
GNUNET_SQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_sq_lib.h:59
sqlite3_stmt ** pstmt
Where to store handle?
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.
Time for absolute times used by GNUnet, in microseconds.
void(* GNUNET_SQ_ResultCleanup)(void *cls)
Function called to clean up memory allocated by a GNUNET_SQ_ResultConverter.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
uint32_t data
The data value.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_fixed_size(void *dst, size_t dst_size)
Fixed-size result expected.
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_ResultSpec GNUNET_SQ_result_spec_rsa_signature(struct GNUNET_CRYPTO_RsaSignature **sig)
RSA signature expected.