GNUnet  0.20.0
gnunet_sq_lib.h File Reference

helper functions for Sqlite3 DB interactions More...

#include <sqlite3.h>
#include "gnunet_util_lib.h"
Include dependency graph for gnunet_sq_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GNUNET_SQ_QueryParam
 Description of a DB query parameter. More...
 
struct  GNUNET_SQ_ResultSpec
 Description of a DB result cell. More...
 
struct  GNUNET_SQ_PrepareStatement
 Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements(). More...
 
struct  GNUNET_SQ_ExecuteStatement
 Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements(). More...
 

Macros

#define GNUNET_SQ_query_param_end   { NULL, NULL, NULL, 0, 0 }
 End of query parameter specification. More...
 
#define GNUNET_SQ_query_param_auto_from_type(x)   GNUNET_SQ_query_param_fixed_size ((x), sizeof(*(x)))
 Generate fixed-size query parameter with size determined by variable type. More...
 
#define GNUNET_SQ_result_spec_end   { NULL, NULL, NULL, NULL, 0, NULL, 0 }
 End of result parameter specification. More...
 
#define GNUNET_SQ_result_spec_auto_from_type(dst)   GNUNET_SQ_result_spec_fixed_size ((dst), sizeof(*(dst)))
 We expect a fixed-size result, with size determined by the type of * dst More...
 
#define GNUNET_SQ_PREPARE_END   { NULL, NULL }
 Terminator for executable statement list. More...
 
#define GNUNET_SQ_EXECUTE_STATEMENT_END   { NULL, GNUNET_SYSERR }
 Terminator for executable statement list. More...
 

Typedefs

typedef 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. More...
 
typedef 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. More...
 
typedef void(* GNUNET_SQ_ResultCleanup) (void *cls)
 Function called to clean up memory allocated by a GNUNET_SQ_ResultConverter. More...
 

Functions

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. More...
 
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string (const char *ptr)
 Generate query parameter for a string. More...
 
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. More...
 
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
 Generate query parameter for an RSA signature. More...
 
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
 Generate query parameter for an absolute time value. More...
 
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. More...
 
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint16 (const uint16_t *x)
 Generate query parameter for an uint16_t in host byte order. More...
 
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint32 (const uint32_t *x)
 Generate query parameter for an uint32_t in host byte order. More...
 
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64 (const uint64_t *x)
 Generate query parameter for an uint16_t in host byte order. More...
 
enum GNUNET_GenericReturnValue GNUNET_SQ_bind (sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
 Execute binding operations for a prepared statement. More...
 
void GNUNET_SQ_reset (sqlite3 *dbh, sqlite3_stmt *stmt)
 Reset stmt and log error. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size (void **dst, size_t *sptr)
 Variable-size result expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_fixed_size (void *dst, size_t dst_size)
 Fixed-size result expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string (char **dst)
 0-terminated string expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
 RSA public key expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
 RSA signature expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
 Absolute time expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
 Absolute time expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint16 (uint16_t *u16)
 uint16_t expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32 (uint32_t *u32)
 uint32_t expected. More...
 
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64 (uint64_t *u64)
 uint64_t expected. More...
 
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. More...
 
void GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
 Free all memory that was allocated in rs during GNUNET_SQ_extract_result(). More...
 
struct GNUNET_SQ_PrepareStatement GNUNET_SQ_make_prepare (const char *sql, sqlite3_stmt **pstmt)
 Create a struct GNUNET_SQ_PrepareStatement More...
 
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. More...
 
struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_execute (const char *sql)
 Create a struct GNUNET_SQ_ExecuteStatement where errors are fatal. More...
 
struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_try_execute (const char *sql)
 Create a struct GNUNET_SQ_ExecuteStatement where errors should be tolerated. More...
 
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. More...
 

Detailed Description

helper functions for Sqlite3 DB interactions

Author
Christian Grothoff

Definition in file gnunet_sq_lib.h.

Macro Definition Documentation

◆ GNUNET_SQ_query_param_end

#define GNUNET_SQ_query_param_end   { NULL, NULL, NULL, 0, 0 }

End of query parameter specification.

Definition at line 87 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_query_param_auto_from_type

#define GNUNET_SQ_query_param_auto_from_type (   x)    GNUNET_SQ_query_param_fixed_size ((x), sizeof(*(x)))

Generate fixed-size query parameter with size determined by variable type.

Parameters
xpointer to the query parameter to pass.

Definition at line 117 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_result_spec_end

#define GNUNET_SQ_result_spec_end   { NULL, NULL, NULL, NULL, 0, NULL, 0 }

End of result parameter specification.

Returns
array last entry for the result specification to use

Definition at line 302 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_result_spec_auto_from_type

#define GNUNET_SQ_result_spec_auto_from_type (   dst)    GNUNET_SQ_result_spec_fixed_size ((dst), sizeof(*(dst)))

We expect a fixed-size result, with size determined by the type of * dst

Parameters
dstpoint to where to store the result, type fits expected result size
Returns
array entry for the result specification to use

Definition at line 335 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_PREPARE_END

#define GNUNET_SQ_PREPARE_END   { NULL, NULL }

Terminator for executable statement list.

Definition at line 479 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_EXECUTE_STATEMENT_END

#define GNUNET_SQ_EXECUTE_STATEMENT_END   { NULL, GNUNET_SYSERR }

Terminator for executable statement list.

Definition at line 531 of file gnunet_sq_lib.h.

Typedef Documentation

◆ GNUNET_SQ_QueryConverter

typedef 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.

Parameters
clsclosure
datapointer to input argument
data_lennumber of bytes in data (if applicable)
stmtsqlite statement to bind parameters for
offoffset of the argument to bind in stmt, numbered from 1, so immediately suitable for passing to sqlite3_bind-functions.
Returns
GNUNET_SYSERR on error, GNUNET_OK on success

Definition at line 1 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_ResultConverter

typedef 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.

Parameters
clsclosure
resultwhere to extract data from
columncolumn to extract data from
[in,out]dst_sizewhere to store size of result, may be NULL
[out]dstwhere to store the result
Returns
GNUNET_YES if all results could be extracted GNUNET_SYSERR if a result was invalid (non-existing field or NULL)

Definition at line 210 of file gnunet_sq_lib.h.

◆ GNUNET_SQ_ResultCleanup

typedef void(* GNUNET_SQ_ResultCleanup) (void *cls)

Function called to clean up memory allocated by a GNUNET_SQ_ResultConverter.

Parameters
clsclosure

Definition at line 246 of file gnunet_sq_lib.h.

Function Documentation

◆ GNUNET_SQ_query_param_fixed_size()

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.

Parameters
ptrpointer to the query parameter to pass
ptr_sizenumber of bytes in ptr

Definition at line 41 of file sq_query_helper.c.

68 {
69  struct GNUNET_SQ_QueryParam qp = {
71  .data = ptr,
72  .size = ptr_size,
73  .num_params = 1
74  };
75 
76  return qp;
77 }
static int bind_fixed_blob(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:56
GNUNET_SQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_sq_lib.h:60

Referenced by namecache_sqlite_cache_block(), peerstore_sqlite_store_record(), sqlite_plugin_put(), and sqlite_plugin_remove_key().

Here is the caller graph for this function:

◆ GNUNET_SQ_query_param_string()

struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string ( const char *  ptr)

Generate query parameter for a string.

Parameters
ptrpointer to the string query parameter to pass

Definition at line 92 of file sq_query_helper.c.

124 {
125  struct GNUNET_SQ_QueryParam qp = {
126  .conv = &bind_string,
127  .data = ptr,
128  .num_params = 1
129  };
130 
131  return qp;
132 }
static int bind_string(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.

Referenced by peerstore_sqlite_delete_records(), peerstore_sqlite_iterate_records(), and peerstore_sqlite_store_record().

Here is the caller graph for this function:

◆ GNUNET_SQ_query_param_rsa_public_key()

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.

The database must contain a BLOB type in the respective position.

Parameters
xthe query parameter to pass.

Definition at line 147 of file sq_query_helper.c.

184 {
185  struct GNUNET_SQ_QueryParam qp = {
186  .conv = &bind_rsa_pub,
187  .data = x,
188  .num_params = 1
189  };
190 
191  return qp;
192 }
static int bind_rsa_pub(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.

◆ GNUNET_SQ_query_param_rsa_signature()

struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_signature ( const struct GNUNET_CRYPTO_RsaSignature x)

Generate query parameter for an RSA signature.

The database must contain a BLOB type in the respective position.

Parameters
xthe query parameter to pass

Definition at line 207 of file sq_query_helper.c.

243 {
244  struct GNUNET_SQ_QueryParam qp = {
245  .conv = &bind_rsa_sig,
246  .data = x,
247  .num_params = 1
248  };
249 
250  return qp;
251 }
static int bind_rsa_sig(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.

◆ GNUNET_SQ_query_param_absolute_time()

struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time ( const struct GNUNET_TIME_Absolute x)

Generate query parameter for an absolute time value.

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass

Definition at line 266 of file sq_query_helper.c.

296 {
297  struct GNUNET_SQ_QueryParam qp = {
298  .conv = &bind_abstime,
299  .data = x,
300  .size = sizeof(struct GNUNET_TIME_Absolute),
301  .num_params = 1
302  };
303 
304  return qp;
305 }
static int bind_abstime(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
Time for absolute times used by GNUnet, in microseconds.

Referenced by get_any(), get_typed(), namecache_sqlite_cache_block(), namecache_sqlite_expire_blocks(), peerstore_sqlite_expire_records(), peerstore_sqlite_store_record(), sqlite_plugin_get_closest(), sqlite_plugin_get_expiration(), and sqlite_plugin_put().

Here is the caller graph for this function:

◆ GNUNET_SQ_query_param_absolute_time_nbo()

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.

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass

Definition at line 320 of file sq_query_helper.c.

351 {
352  struct GNUNET_SQ_QueryParam qp = {
353  .conv = &bind_nbotime,
354  .data = x,
355  .size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
356  .num_params = 1
357  };
358 
359  return qp;
360 }
static int bind_nbotime(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
Time for absolute time used by GNUnet, in microseconds and in network byte order.

◆ GNUNET_SQ_query_param_uint16()

struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint16 ( const uint16_t *  x)

Generate query parameter for an uint16_t in host byte order.

Parameters
xpointer to the query parameter to pass

Definition at line 375 of file sq_query_helper.c.

400 {
401  struct GNUNET_SQ_QueryParam qp = {
402  .conv = &bind_u16,
403  .data = x,
404  .size = sizeof(uint16_t),
405  .num_params = 1
406  };
407 
408  return qp;
409 }
static int bind_u16(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_sq_lib.h:80

◆ GNUNET_SQ_query_param_uint32()

struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint32 ( const uint32_t *  x)

Generate query parameter for an uint32_t in host byte order.

Parameters
xpointer to the query parameter to pass

Definition at line 424 of file sq_query_helper.c.

449 {
450  struct GNUNET_SQ_QueryParam qp = {
451  .conv = &bind_u32,
452  .data = x,
453  .size = sizeof(uint32_t),
454  .num_params = 1
455  };
456 
457  return qp;
458 }
static int bind_u32(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.

Referenced by get_any(), get_typed(), sqlite_plugin_get_closest(), sqlite_plugin_get_key(), sqlite_plugin_get_replication(), sqlite_plugin_get_zero_anonymity(), and sqlite_plugin_put().

Here is the caller graph for this function:

◆ GNUNET_SQ_query_param_uint64()

struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64 ( const uint64_t *  x)

Generate query parameter for an uint16_t in host byte order.

Parameters
xpointer to the query parameter to pass

Definition at line 473 of file sq_query_helper.c.

498 {
499  struct GNUNET_SQ_QueryParam qp = {
500  .conv = &bind_u64,
501  .data = x,
502  .size = sizeof(uint64_t),
503  .num_params = 1
504  };
505 
506  return qp;
507 }
static int bind_u64(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.

Referenced by delete_by_rowid(), sqlite_plugin_get_key(), sqlite_plugin_get_replication(), sqlite_plugin_get_zero_anonymity(), and sqlite_plugin_put().

Here is the caller graph for this function:

◆ GNUNET_SQ_bind()

enum GNUNET_GenericReturnValue GNUNET_SQ_bind ( sqlite3_stmt *  stmt,
const struct GNUNET_SQ_QueryParam params 
)

Execute binding operations for a prepared statement.

Parameters
db_conndatabase connection
paramsparameters to the statement
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1 of file sq.c.

33 {
34  unsigned int j;
35 
36  j = 1;
37  for (unsigned int i = 0; NULL != params[i].conv; i++)
38  {
39  if (GNUNET_OK !=
40  params[i].conv (params[i].conv_cls,
41  params[i].data,
42  params[i].size,
43  stmt,
44  j))
45  {
47  "sq",
48  _ ("Failure to bind %u-th SQL parameter\n"),
49  i);
50  if (SQLITE_OK !=
51  sqlite3_reset (stmt))
52  {
54  "sq",
55  _ ("Failure in sqlite3_reset (!)\n"));
56  return GNUNET_SYSERR;
57  }
58  }
59  GNUNET_assert (0 != params[i].num_params);
60  j += params[i].num_params;
61  }
62  return GNUNET_OK;
63 }
static GstElement * conv
uint32_t data
The data value.
#define GNUNET_log_from(kind, comp,...)
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _, conv, GNUNET_SQ_QueryParam::conv, data, GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_from, GNUNET_OK, GNUNET_SYSERR, GNUNET_SQ_QueryParam::num_params, and size.

Referenced by delete_by_rowid(), get_any(), get_typed(), namecache_sqlite_cache_block(), namecache_sqlite_expire_blocks(), namecache_sqlite_lookup_block(), peerstore_sqlite_delete_records(), peerstore_sqlite_expire_records(), peerstore_sqlite_iterate_records(), peerstore_sqlite_store_record(), sqlite_plugin_get_closest(), sqlite_plugin_get_expiration(), sqlite_plugin_get_key(), sqlite_plugin_get_replication(), sqlite_plugin_get_zero_anonymity(), sqlite_plugin_put(), and sqlite_plugin_remove_key().

Here is the caller graph for this function:

◆ GNUNET_SQ_reset()

void GNUNET_SQ_reset ( sqlite3 *  dbh,
sqlite3_stmt *  stmt 
)

Reset stmt and log error.

Parameters
dbhdatabase handle
stmtstatement to reset

Definition at line 120 of file sq.c.

122 {
123  if (SQLITE_OK !=
124  sqlite3_reset (stmt))
126  "sqlite",
127  _ ("Failed to reset sqlite statement with error: %s\n"),
128  sqlite3_errmsg (dbh));
129 }
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_BULK

References _, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, and GNUNET_log_from.

Referenced by delete_by_rowid(), execute_get(), get_any(), get_typed(), namecache_sqlite_cache_block(), namecache_sqlite_expire_blocks(), namecache_sqlite_lookup_block(), peerstore_sqlite_delete_records(), peerstore_sqlite_expire_records(), peerstore_sqlite_iterate_records(), peerstore_sqlite_store_record(), sqlite_plugin_get_closest(), sqlite_plugin_get_replication(), sqlite_plugin_put(), and sqlite_plugin_remove_key().

Here is the caller graph for this function:

◆ GNUNET_SQ_result_spec_variable_size()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size ( void **  dst,
size_t *  sptr 
)

Variable-size result expected.

Parameters
[out]dstwhere to store the result, allocated
[out]sptrwhere to store the size of dst
Returns
array entry for the result specification to use

Definition at line 99 of file sq_result_helper.c.

121 {
122  struct GNUNET_SQ_ResultSpec rs = {
124  .cleaner = &clean_var_blob,
125  .dst = dst,
126  .cls = dst,
127  .result_size = sptr,
128  .num_params = 1
129  };
130 
131  return rs;
132 }
static void clean_var_blob(void *cls)
Cleanup memory allocated by extract_var_blob().
static int extract_var_blob(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract variable-sized binary data from a Postgres database result at row row.
Description of a DB result cell.
GNUNET_SQ_ResultConverter conv
What is the format of the result?
void * dst
Destination for the data.

Referenced by execute_get(), get_any(), get_typed(), namecache_sqlite_lookup_block(), peerstore_sqlite_iterate_records(), and sqlite_plugin_get_closest().

Here is the caller graph for this function:

◆ GNUNET_SQ_result_spec_fixed_size()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_fixed_size ( void *  dst,
size_t  dst_size 
)

Fixed-size result expected.

Parameters
[out]dstwhere to store the result
dst_sizenumber of bytes in dst
Returns
array entry for the result specification to use

Definition at line 148 of file sq_result_helper.c.

200 {
201  struct GNUNET_SQ_ResultSpec rs = {
203  .dst = dst,
204  .dst_size = dst_size,
205  .num_params = 1
206  };
207 
208  return rs;
209 }
static int extract_fixed_blob(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract fixed-sized binary data from a Postgres database result at row row.
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...

◆ GNUNET_SQ_result_spec_string()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string ( char **  dst)

0-terminated string expected.

Parameters
[out]dstwhere to store the result, allocated
Returns
array entry for the result specification to use

Definition at line 269 of file sq_result_helper.c.

289 {
290  struct GNUNET_SQ_ResultSpec rs = {
292  .cleaner = &clean_utf8_string,
293  .cls = dst,
294  .dst = dst,
295  .num_params = 1
296  };
297 
298  return rs;
299 }
static int extract_utf8_string(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract fixed-sized binary data from a Postgres database result at row row.
static void clean_utf8_string(void *cls)
Cleanup memory allocated by extract_var_blob().

Referenced by peerstore_sqlite_iterate_records().

Here is the caller graph for this function:

◆ GNUNET_SQ_result_spec_rsa_public_key()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_public_key ( struct GNUNET_CRYPTO_RsaPublicKey **  rsa)

RSA public key expected.

Parameters
[out]rsawhere to store the result
Returns
array entry for the result specification to use

Definition at line 362 of file sq_result_helper.c.

382 {
383  struct GNUNET_SQ_ResultSpec rs = {
384  .conv = &extract_rsa_pub,
385  .cleaner = &clean_rsa_pub,
386  .dst = rsa,
387  .cls = rsa,
388  .num_params = 1
389  };
390 
391  return rs;
392 }
static void clean_rsa_pub(void *cls)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
static int extract_rsa_pub(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_SQ_result_spec_rsa_signature()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_signature ( struct GNUNET_CRYPTO_RsaSignature **  sig)

RSA signature expected.

Parameters
[out]sigwhere to store the result;
Returns
array entry for the result specification to use

Definition at line 455 of file sq_result_helper.c.

475 {
476  struct GNUNET_SQ_ResultSpec rs = {
477  .conv = &extract_rsa_sig,
478  .cleaner = &clean_rsa_sig,
479  .dst = sig,
480  .cls = sig,
481  .num_params = 1
482  };
483 
484  return rs;
485 }
static int extract_rsa_sig(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_rsa_sig(void *cls)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.

◆ GNUNET_SQ_result_spec_absolute_time()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time ( struct GNUNET_TIME_Absolute at)

Absolute time expected.

Parameters
[out]atwhere to store the result
Returns
array entry for the result specification to use

Definition at line 501 of file sq_result_helper.c.

535 {
536  struct GNUNET_SQ_ResultSpec rs = {
538  .dst = at,
539  .dst_size = sizeof(struct GNUNET_TIME_Absolute),
540  .num_params = 1
541  };
542 
543  return rs;
544 }
static int extract_abs_time(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract absolute time value from a Postgres database result at row row.

Referenced by execute_get(), get_any(), get_typed(), peerstore_sqlite_iterate_records(), and sqlite_plugin_get_closest().

Here is the caller graph for this function:

◆ GNUNET_SQ_result_spec_absolute_time_nbo()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time_nbo ( struct GNUNET_TIME_AbsoluteNBO at)

Absolute time expected.

Parameters
[out]atwhere to store the result
Returns
array entry for the result specification to use

Definition at line 560 of file sq_result_helper.c.

594 {
595  struct GNUNET_SQ_ResultSpec rs = {
597  .dst = at,
598  .dst_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
599  .num_params = 1
600  };
601 
602  return rs;
603 }
static int extract_abs_time_nbo(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract absolute time value in NBO from a Postgres database result at row row.

◆ GNUNET_SQ_result_spec_uint16()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint16 ( uint16_t *  u16)

uint16_t expected.

Parameters
[out]u16where to store the result
Returns
array entry for the result specification to use

Definition at line 619 of file sq_result_helper.c.

656 {
657  struct GNUNET_SQ_ResultSpec rs = {
658  .conv = &extract_uint16,
659  .dst = u16,
660  .dst_size = sizeof(uint16_t),
661  .num_params = 1
662  };
663 
664  return rs;
665 }
static int extract_uint16(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract 16-bit integer from a Postgres database result at row row.
unsigned int num_params
Number of parameters (columns) eaten by this operation.

◆ GNUNET_SQ_result_spec_uint32()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32 ( uint32_t *  u32)

uint32_t expected.

Parameters
[out]u32where to store the result
Returns
array entry for the result specification to use

Definition at line 681 of file sq_result_helper.c.

718 {
719  struct GNUNET_SQ_ResultSpec rs = {
720  .conv = &extract_uint32,
721  .dst = u32,
722  .dst_size = sizeof(uint32_t),
723  .num_params = 1
724  };
725 
726  return rs;
727 }
static int extract_uint32(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract 32-bit integer from a Postgres database result at row row.

Referenced by execute_get(), get_any(), get_typed(), and sqlite_plugin_get_closest().

Here is the caller graph for this function:

◆ GNUNET_SQ_result_spec_uint64()

struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64 ( uint64_t *  u64)

uint64_t expected.

Parameters
[out]u64where to store the result
Returns
array entry for the result specification to use

Definition at line 743 of file sq_result_helper.c.

773 {
774  struct GNUNET_SQ_ResultSpec rs = {
775  .conv = &extract_uint64,
776  .dst = u64,
777  .dst_size = sizeof(uint64_t),
778  .num_params = 1
779  };
780 
781  return rs;
782 }
static int extract_uint64(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract 64-bit integer from a Postgres database result at row row.

Referenced by execute_get().

Here is the caller graph for this function:

◆ GNUNET_SQ_extract_result()

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.

Parameters
resultresult to process
[in,out]rsresult specification to extract for
Returns
GNUNET_OK if all results could be extracted GNUNET_SYSERR if a result was invalid (non-existing field)

Definition at line 1 of file sq.c.

78 {
79  unsigned int j = 0;
80 
81  for (unsigned int i = 0; NULL != rs[i].conv; i++)
82  {
83  if (NULL == rs[i].result_size)
84  rs[i].result_size = &rs[i].dst_size;
85  if (GNUNET_OK !=
86  rs[i].conv (rs[i].cls,
87  result,
88  j,
89  rs[i].result_size,
90  rs[i].dst))
91  {
92  for (unsigned int k = 0; k < i; k++)
93  if (NULL != rs[k].cleaner)
94  rs[k].cleaner (rs[k].cls);
95  return GNUNET_SYSERR;
96  }
97  GNUNET_assert (0 != rs[i].num_params);
98  j += rs[i].num_params;
99  }
100  return GNUNET_OK;
101 }
static int result
Global testing status.
size_t * result_size
Where to store actual size of the result.
GNUNET_SQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.

Referenced by execute_get(), get_any(), get_typed(), namecache_sqlite_lookup_block(), peerstore_sqlite_iterate_records(), sqlite_plugin_get_closest(), and sqlite_plugin_get_keys().

Here is the caller graph for this function:

◆ GNUNET_SQ_cleanup_result()

void GNUNET_SQ_cleanup_result ( struct GNUNET_SQ_ResultSpec rs)

Free all memory that was allocated in rs during GNUNET_SQ_extract_result().

Parameters
rsreult specification to clean up

Definition at line 105 of file sq.c.

106 {
107  for (unsigned int i = 0; NULL != rs[i].conv; i++)
108  if (NULL != rs[i].cleaner)
109  rs[i].cleaner (rs[i].cls);
110 }

References GNUNET_SQ_ResultSpec::cleaner, and GNUNET_SQ_ResultSpec::conv.

Referenced by execute_get(), get_any(), get_typed(), namecache_sqlite_lookup_block(), peerstore_sqlite_iterate_records(), and sqlite_plugin_get_closest().

Here is the caller graph for this function:

◆ GNUNET_SQ_make_prepare()

struct GNUNET_SQ_PrepareStatement GNUNET_SQ_make_prepare ( const char *  sql,
sqlite3_stmt **  pstmt 
)

Create a struct GNUNET_SQ_PrepareStatement

Parameters
sqlactual SQL statement
pstmtwhere to store the handle
Returns
initialized struct

Definition at line 1 of file sq_prepare.c.

40 {
41  struct GNUNET_SQ_PrepareStatement ps = {
42  .sql = sql,
43  .pstmt = pstmt
44  };
45 
46  return ps;
47 }
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?

References GNUNET_SQ_PrepareStatement::pstmt, and GNUNET_SQ_PrepareStatement::sql.

Referenced by database_setup().

Here is the caller graph for this function:

◆ GNUNET_SQ_prepare()

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.

Parameters
dbhdatabase handle
psarray of statements to prepare
Returns
GNUNET_OK on success

Definition at line 1 of file sq_prepare.c.

53 {
54  for (unsigned int i = 0; NULL != ps[i].sql; i++)
55  {
56  const char *epos = NULL;
57  int ret;
58 
59  if (SQLITE_OK !=
60  (ret = sqlite3_prepare_v2 (dbh,
61  ps[i].sql,
62  strlen (ps[i].sql),
63  ps[i].pstmt,
64  &epos)))
65  {
67  "Failed to prepare SQL `%s': error %d at %s\n",
68  ps[i].sql,
69  ret,
70  epos);
71  return GNUNET_SYSERR;
72  }
73  }
74  return GNUNET_OK;
75 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_log(kind,...)

Referenced by database_setup().

Here is the caller graph for this function:

◆ GNUNET_SQ_make_execute()

struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_execute ( const char *  sql)

Create a struct GNUNET_SQ_ExecuteStatement where errors are fatal.

Parameters
sqlactual SQL statement
Returns
initialized struct

Definition at line 1 of file sq_exec.c.

37 {
38  struct GNUNET_SQ_ExecuteStatement es = {
39  .sql = sql,
40  .ignore_errors = GNUNET_NO
41  };
42 
43  return es;
44 }
@ GNUNET_NO
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
const char * sql
Actual SQL statement.

Referenced by database_setup().

Here is the caller graph for this function:

◆ GNUNET_SQ_make_try_execute()

struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_try_execute ( const char *  sql)

Create a struct GNUNET_SQ_ExecuteStatement where errors should be tolerated.

Parameters
sqlactual SQL statement
Returns
initialized struct

Definition at line 1 of file sq_exec.c.

56 {
57  struct GNUNET_SQ_ExecuteStatement es = {
58  .sql = sql,
59  .ignore_errors = GNUNET_YES
60  };
61 
62  return es;
63 }
@ GNUNET_YES

Referenced by database_setup().

Here is the caller graph for this function:

◆ GNUNET_SQ_exec_statements()

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.

Parameters
dbhdatabase to execute the statements over
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared statements.
Returns
GNUNET_OK on success (modulo statements where errors can be ignored) GNUNET_SYSERR on error

Definition at line 1 of file sq_exec.c.

78 {
79  for (unsigned int i = 0; NULL != es[i].sql; i++)
80  {
81  char *emsg = NULL;
82 
83  if (SQLITE_OK !=
84  sqlite3_exec (dbh,
85  es[i].sql,
86  NULL,
87  NULL,
88  &emsg))
89  {
90  if (es[i].ignore_errors)
91  {
93  "Failed to run SQL `%s': %s\n",
94  es[i].sql,
95  emsg);
96  }
97  else
98  {
100  "Failed to run SQL `%s': %s\n",
101  es[i].sql,
102  emsg);
103  sqlite3_free (emsg);
104  return GNUNET_SYSERR;
105  }
106  sqlite3_free (emsg);
107  }
108  }
109  return GNUNET_OK;
110 }
@ GNUNET_ERROR_TYPE_DEBUG

References GNUNET_NO, and GNUNET_SQ_ExecuteStatement::sql.

Referenced by database_setup().

Here is the caller graph for this function: