GNUnet  0.19.5
gnunet_pq_lib.h File Reference

helper functions for Postgres DB interactions More...

#include <libpq-fe.h>
#include <stdint.h>
#include "gnunet_time_lib.h"
#include "gnunet_util_lib.h"
#include "gnunet_db_lib.h"
Include dependency graph for gnunet_pq_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_PQ_QueryParam
 Description of a DB query parameter. More...
 
struct  GNUNET_PQ_ResultSpec
 Description of a DB result cell. More...
 
struct  GNUNET_PQ_PreparedStatement
 Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements(). More...
 
struct  GNUNET_PQ_ExecuteStatement
 Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements(). More...
 

Macros

#define GNUNET_PQ_query_param_end
 End of query parameter specification. More...
 
#define GNUNET_PQ_query_param_array_auto_from_type(num, elements, db)
 Generate array of equal-sized query parameter with size determined by variable type from a pointer to an continuous array. More...
 
#define GNUNET_PQ_query_param_array_ptrs_auto_from_type(num, elements, db)
 Generate array of equal-sized query parameter with size determined by variable type from a pointer to an array of pointers to objects of the same type. More...
 
#define GNUNET_PQ_query_param_auto_from_type(x)    GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
 Generate fixed-size query parameter with size determined by variable type. More...
 
#define GNUNET_PQ_result_spec_end
 End of result parameter specification. More...
 
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)    GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))
 We expect a fixed-size result, with size determined by the type of * dst More...
 
#define GNUNET_PQ_result_spec_auto_array_from_type(db, name, num, dst)
 We expect a fixed-size result, with size determined by the type of * dst More...
 
#define GNUNET_PQ_PREPARED_STATEMENT_END
 Terminator for prepared statement list. More...
 
#define GNUNET_PQ_EXECUTE_STATEMENT_END
 Terminator for executable statement list. More...
 

Typedefs

typedef int(* GNUNET_PQ_QueryConverter) (void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
 Function called to convert input argument into SQL parameters. More...
 
typedef void(* GNUNET_PQ_QueryConverter_Cleanup) (void *cls)
 Function called to cleanup the closure of SQL parameters converter. More...
 
typedef enum GNUNET_GenericReturnValue(* GNUNET_PQ_ResultConverter) (void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
 Extract data from a Postgres database result at row row. More...
 
typedef void(* GNUNET_PQ_ResultCleanup) (void *cls, void *rd)
 Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter. More...
 
typedef void(* GNUNET_PQ_PostgresResultHandler) (void *cls, PGresult *result, unsigned int num_results)
 Function to be called with the results of a SELECT statement that has returned num_results results. More...
 

Enumerations

enum  GNUNET_PQ_DataTypes {
  GNUNET_PQ_DATATYPE_UNKNOWN , GNUNET_PQ_DATATYPE_BOOL , GNUNET_PQ_DATATYPE_INT2 , GNUNET_PQ_DATATYPE_INT4 ,
  GNUNET_PQ_DATATYPE_INT8 , GNUNET_PQ_DATATYPE_BYTEA , GNUNET_PQ_DATATYPE_VARCHAR , GNUNET_PQ_DATATYPE_MAX
}
 Array types (in Postgres) that are supported in GNUnet. More...
 
enum  GNUNET_PQ_Options { GNUNET_PQ_FLAG_NONE = 0 , GNUNET_PQ_FLAG_DROP = 1 , GNUNET_PQ_FLAG_CHECK_CURRENT = 2 }
 Flags to control PQ operation. More...
 

Functions

void GNUNET_PQ_cleanup_query_params_closures (const struct GNUNET_PQ_QueryParam *params)
 Must be called to cleanup memory from closures after the query parameters have been used as much as needed. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_null (void)
 Generate query parameter to create a NULL value. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_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_PQ_QueryParam GNUNET_PQ_query_param_string (const char *ptr)
 Generate query parameter for a string. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_bool (bool b)
 Pass a boolean into a query. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bool (unsigned int num, const bool *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of bool in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint16 (unsigned int num, const uint16_t *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of uint16_t in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint32 (unsigned int num, const uint32_t *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of uint32_t in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint64 (unsigned int num, const uint64_t *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of uint64 in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes (unsigned int num, const void *elements, const size_t *sizes, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of buffers elements, each of corresponding size given in sizes. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes_same_size (unsigned int num, const void *elements, size_t same_size, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of buffers elements, each of the same size size. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes_same_size (unsigned int num, const void *elements[], size_t same_size, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of pointers to buffers elements, each of the same size size. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_string (unsigned int num, const char *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of strings (continuous) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_string (unsigned int num, const char *elements[], const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of strings (pointers) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_abs_time (unsigned int num, const struct GNUNET_TIME_Absolute *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of absolute time stamps (continuous) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_abs_time (unsigned int num, const struct GNUNET_TIME_Absolute *elements[], const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of absolute time stamps (pointers) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_rel_time (unsigned int num, const struct GNUNET_TIME_Relative *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of relative time stamps (continuous) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_rel_time (unsigned int num, const struct GNUNET_TIME_Relative *elements[], const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of relative time stamps (pointers) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_timestamp (unsigned int num, const struct GNUNET_TIME_Timestamp *elements, const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of time stamps (continuous) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_timestamp (unsigned int num, const struct GNUNET_TIME_Timestamp *elements[], const struct GNUNET_PQ_Context *db)
 Generate query parameter for an array of time stamps (pointers) More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
 Generate query parameter for an RSA public key. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
 Generate query parameter for an RSA signature. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
 Generate query parameter for a relative time value. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
 Generate query parameter for an absolute time value. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp (const struct GNUNET_TIME_Timestamp *x)
 Generate query parameter for a timestamp. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
 Generate query parameter for an absolute time value. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp_nbo (const struct GNUNET_TIME_TimestampNBO *x)
 Generate query parameter for a timestamp in NBO. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16 (const uint16_t *x)
 Generate query parameter for an uint16_t in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32 (const uint32_t *x)
 Generate query parameter for an uint32_t in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64 (const uint64_t *x)
 Generate query parameter for an uint16_t in host byte order. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_allow_null (struct GNUNET_PQ_ResultSpec rs, bool *is_null)
 Allow NULL value to be found in the database for the given value. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size (const char *name, void **dst, size_t *sptr)
 Variable-size result expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size)
 Fixed-size result expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string (const char *name, char **dst)
 0-terminated string expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_bool (const char *name, bool *dst)
 boolean expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_public_key (const char *name, struct GNUNET_CRYPTO_RsaPublicKey **rsa)
 RSA public key expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_signature (const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
 RSA signature expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at)
 Absolute time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_timestamp (const char *name, struct GNUNET_TIME_Timestamp *t)
 Timestamp expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_relative_time (const char *name, struct GNUNET_TIME_Relative *rt)
 Relative time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
 Absolute time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_timestamp_nbo (const char *name, struct GNUNET_TIME_TimestampNBO *tn)
 Timestamp expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16)
 uint16_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32)
 uint32_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64 (const char *name, uint64_t *u64)
 uint64_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_bool (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, bool **bools)
 array of bool expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint16 (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, uint16_t **dst)
 array of uint16_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint32 (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, uint32_t **dst)
 array of uint32_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint64 (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, uint64_t **dst)
 array of uint64_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_abs_time (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, struct GNUNET_TIME_Absolute **dst)
 array of absolute time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_rel_time (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, struct GNUNET_TIME_Relative **dst)
 array of relative time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_timestamp (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, struct GNUNET_TIME_Timestamp **dst)
 array of relative time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_variable_size (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, size_t **sizes, void **dst)
 Array of variable-size result expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_fixed_size (const struct GNUNET_PQ_Context *db, const char *name, size_t size, size_t *num, void **dst)
 Array of fixed-size result expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_string (const struct GNUNET_PQ_Context *db, const char *name, size_t *num, char **dst)
 Array of 0-terminated strings expected. More...
 
PGresult * GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db, const char *name, const struct GNUNET_PQ_QueryParam *params)
 Execute a prepared statement. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result (PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
 Extract results from a query result according to the given specification. More...
 
void GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
 Free all memory that was allocated in rs during GNUNET_PQ_extract_result(). More...
 
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result (struct GNUNET_PQ_Context *db, const char *statement_name, PGresult *result)
 Check the result's error code to see what happened. More...
 
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select (struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
 Execute a named prepared statement that is NOT a SELECT statement in connection using the given params. More...
 
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
 Execute a named prepared statement that is a SELECT statement which may return multiple results in connection using the given params. More...
 
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
 Execute a named prepared statement that is a SELECT statement which must return a single result in connection using the given params. More...
 
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare (const char *name, const char *sql)
 Create a struct GNUNET_PQ_PreparedStatement. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres, but do not automatically re-prepare the statement if we are disconnected from the database. More...
 
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute (const char *sql)
 Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal. More...
 
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute (const char *sql)
 Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_ExecuteStatement *es)
 Request execution of an array of statements es from Postgres. More...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect (const char *config_str, const char *load_path, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
 Create a connection to the Postgres database using config_str for the configuration. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db, const char *buf)
 Execute SQL statements from buf against db. More...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect2 (const char *config_str, const char *load_path, const char *auto_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
 Create a connection to the Postgres database using config_str for the configuration. More...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
 Connect to a postgres database using the configuration option "CONFIG" in section. More...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect_with_cfg2 (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
 Connect to a postgres database using the configuration option "CONFIG" in section. More...
 
void GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db)
 Reinitialize the database db if the connection is down. More...
 
void GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
 Reinitialize the database db. More...
 
struct GNUNET_DB_EventHandlerGNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, const struct GNUNET_DB_EventHeaderP *es, struct GNUNET_TIME_Relative timeout, GNUNET_DB_EventCallback cb, void *cb_cls)
 Register callback to be invoked on events of type es. More...
 
void GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh)
 Stop notifications. More...
 
void GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, const struct GNUNET_DB_EventHeaderP *es, const void *extra, size_t extra_size)
 Notify all that listen on es of an event. More...
 
char * GNUNET_PG_get_event_notify_channel (const struct GNUNET_DB_EventHeaderP *es)
 Compute the channel that one should notify upon for the given event specification. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db, const char *load_path)
 Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the files exist contiguously). More...
 
void GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db)
 Disconnect from the database, destroying the prepared statements and releasing other associated resources. More...
 

Detailed Description

helper functions for Postgres DB interactions

Author
Christian Grothoff

Definition in file gnunet_pq_lib.h.

Macro Definition Documentation

◆ GNUNET_PQ_query_param_end

#define GNUNET_PQ_query_param_end
Value:
{ \
.conv = NULL, \
.conv_cls = NULL, \
.data = NULL, \
.size = 0, \
.num_params = 0 \
}

End of query parameter specification.

Definition at line 133 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_query_param_array_auto_from_type

#define GNUNET_PQ_query_param_array_auto_from_type (   num,
  elements,
  db 
)
Value:
(elements), \
sizeof(*(elements)), \
(db))
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:97
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes_same_size(unsigned int num, const void *elements, size_t same_size, const struct GNUNET_PQ_Context *db)
Generate query parameter for an array of buffers elements, each of the same size size.

Generate array of equal-sized query parameter with size determined by variable type from a pointer to an continuous array.

Parameters
numNumber of elements in elements
elementsContinuous array of the query parameter to pass.
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 302 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_query_param_array_ptrs_auto_from_type

#define GNUNET_PQ_query_param_array_ptrs_auto_from_type (   num,
  elements,
  db 
)
Value:
(elements), \
sizeof(*(elements[0])), \
(db))
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes_same_size(unsigned int num, const void *elements[], size_t same_size, const struct GNUNET_PQ_Context *db)
Generate query parameter for an array of pointers to buffers elements, each of the same size size.

Generate array of equal-sized query parameter with size determined by variable type from a pointer to an array of pointers to objects of the same type.

Parameters
numNumber of elements in elements
elementsContinuous array of the query parameter to pass.
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 335 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_query_param_auto_from_type

#define GNUNET_PQ_query_param_auto_from_type (   x)     GNUNET_PQ_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.
Returns
query parameter to use

Definition at line 378 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_result_spec_end

#define GNUNET_PQ_result_spec_end
Value:
{ \
.conv = NULL, \
.cleaner = NULL, \
.cls = NULL, \
.dst = NULL, \
.dst_size = 0, \
.fname = NULL, \
.result_size = NULL, \
.is_nullable = false, \
.is_null = NULL \
}

End of result parameter specification.

Returns
array last entry for the result specification to use

Definition at line 669 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_result_spec_auto_from_type

#define GNUNET_PQ_result_spec_auto_from_type (   name,
  dst 
)     GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))

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

Parameters
namename of the field in the table
dstpoint to where to store the result, type fits expected result size
Returns
array entry for the result specification to use

Definition at line 732 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_result_spec_auto_array_from_type

#define GNUNET_PQ_result_spec_auto_array_from_type (   db,
  name,
  num,
  dst 
)
Value:
(db), \
(name), \
sizeof(*(dst)), \
(num), \
(void *) &(dst))
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_fixed_size(const struct GNUNET_PQ_Context *db, const char *name, size_t size, size_t *num, void **dst)
Array of fixed-size result expected.
const char * name

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

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
numpointer to where to store the number of elements
dstpointer to where to store the results, type fits expected result size
Returns
array entry for the result specification to use

Definition at line 1040 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_PREPARED_STATEMENT_END

#define GNUNET_PQ_PREPARED_STATEMENT_END
Value:
{ \
NULL, NULL \
}

Terminator for prepared statement list.

Definition at line 1237 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_EXECUTE_STATEMENT_END

#define GNUNET_PQ_EXECUTE_STATEMENT_END
Value:
{ \
NULL, GNUNET_SYSERR \
}
@ GNUNET_SYSERR

Terminator for executable statement list.

Definition at line 1309 of file gnunet_pq_lib.h.

Typedef Documentation

◆ GNUNET_PQ_QueryConverter

typedef int(* GNUNET_PQ_QueryConverter) (void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)

Function called to convert input argument into SQL parameters.

Parameters
clsclosure
datapointer to input argument
data_lennumber of bytes in data (if applicable)
[out]param_valuesSQL data to set
[out]param_lengthsSQL length data to set
[out]param_formatsSQL format data to set
param_lengthnumber of entries available in the param_values, param_lengths and param_formats arrays
[out]scratchbuffer for dynamic allocations (to be done via GNUNET_malloc()
scratch_lengthnumber of entries left in scratch
Returns
-1 on error, number of offsets used in scratch otherwise

Definition at line 58 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_QueryConverter_Cleanup

typedef void(* GNUNET_PQ_QueryConverter_Cleanup) (void *cls)

Function called to cleanup the closure of SQL parameters converter.

Will be called with GNUNET_PQ_query_params_cleanup

Parameters
clsclosure

Definition at line 75 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_ResultConverter

typedef enum GNUNET_GenericReturnValue(* GNUNET_PQ_ResultConverter) (void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)

Extract data from a Postgres database result at row row.

Parameters
clsclosure
resultwhere to extract data from
introw to extract data from
fnamename (or prefix) of the fields to extract 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_NO if the field was NULL GNUNET_SYSERR if a result was invalid (non-existing field)

Definition at line 127 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_ResultCleanup

typedef void(* GNUNET_PQ_ResultCleanup) (void *cls, void *rd)

Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.

Parameters
clsclosure
rdresult data to clean up

Definition at line 603 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_PostgresResultHandler

typedef void(* GNUNET_PQ_PostgresResultHandler) (void *cls, PGresult *result, unsigned int num_results)

Function to be called with the results of a SELECT statement that has returned num_results results.

Parameters
clsclosure
resultthe postgres result
num_resultthe number of results in result

Definition at line 1160 of file gnunet_pq_lib.h.

Enumeration Type Documentation

◆ GNUNET_PQ_DataTypes

Array types (in Postgres) that are supported in GNUnet.

Enumerator
GNUNET_PQ_DATATYPE_UNKNOWN 
GNUNET_PQ_DATATYPE_BOOL 
GNUNET_PQ_DATATYPE_INT2 
GNUNET_PQ_DATATYPE_INT4 
GNUNET_PQ_DATATYPE_INT8 
GNUNET_PQ_DATATYPE_BYTEA 
GNUNET_PQ_DATATYPE_VARCHAR 
GNUNET_PQ_DATATYPE_MAX 

Definition at line 188 of file gnunet_pq_lib.h.

189 {
190  GNUNET_PQ_DATATYPE_UNKNOWN, /* Unsupported type */
197  GNUNET_PQ_DATATYPE_MAX, /* Must be last */
198 };
@ GNUNET_PQ_DATATYPE_INT8
@ GNUNET_PQ_DATATYPE_BOOL
@ GNUNET_PQ_DATATYPE_MAX
@ GNUNET_PQ_DATATYPE_VARCHAR
@ GNUNET_PQ_DATATYPE_BYTEA
@ GNUNET_PQ_DATATYPE_INT4
@ GNUNET_PQ_DATATYPE_UNKNOWN
@ GNUNET_PQ_DATATYPE_INT2

◆ GNUNET_PQ_Options

Flags to control PQ operation.

Enumerator
GNUNET_PQ_FLAG_NONE 

Traditional default.

Do nothing special.

GNUNET_PQ_FLAG_DROP 

Dropping database.

Do not attempt to initialize versioning schema if not present.

GNUNET_PQ_FLAG_CHECK_CURRENT 

Check database version is current.

Fail to connect if it is not.

Definition at line 1356 of file gnunet_pq_lib.h.

1357 {
1361  GNUNET_PQ_FLAG_NONE = 0,
1362 
1367  GNUNET_PQ_FLAG_DROP = 1,
1368 
1373 };
@ GNUNET_PQ_FLAG_DROP
Dropping database.
@ GNUNET_PQ_FLAG_CHECK_CURRENT
Check database version is current.
@ GNUNET_PQ_FLAG_NONE
Traditional default.

Function Documentation

◆ GNUNET_PQ_cleanup_query_params_closures()

void GNUNET_PQ_cleanup_query_params_closures ( const struct GNUNET_PQ_QueryParam params)

Must be called to cleanup memory from closures after the query parameters have been used as much as needed.

Parameters
paramsArray of GNUNET_PQ_QueryParam which must terminate with a GNUNET_PQ_query_param_end

Definition at line 123 of file pq.c.

125 {
126  for (unsigned int i = 0; 0 != params[i].num_params; i++)
127  {
128  const struct GNUNET_PQ_QueryParam *x = &params[i];
129 
130  if ((NULL != x->conv_cls) &&
131  (NULL != x->conv_cls_cleanup))
132  x->conv_cls_cleanup (x->conv_cls);
133  }
134 
135 }
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:82
void * conv_cls
Closure for conv.
Definition: gnunet_pq_lib.h:94
unsigned int num_params
Number of parameters eaten by this operation.
GNUNET_PQ_QueryConverter_Cleanup conv_cls_cleanup
Function to cleanup the closure conv_cls, may be NULL.

References GNUNET_PQ_QueryParam::conv_cls, GNUNET_PQ_QueryParam::conv_cls_cleanup, and GNUNET_PQ_QueryParam::num_params.

◆ GNUNET_PQ_query_param_null()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_null ( void  )

Generate query parameter to create a NULL value.

Returns
query parameter to use to insert NULL into DB

Definition at line 47 of file pq_query_helper.c.

73 {
74  struct GNUNET_PQ_QueryParam res = {
75  .conv = &qconv_null,
76  .num_params = 1
77  };
78 
79  return res;
80 }
static int res
static int qconv_null(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References data, and GNUNET_break.

Referenced by postgres_plugin_put().

Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_fixed_size()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 98 of file pq_query_helper.c.

123 {
124  struct GNUNET_PQ_QueryParam res = {
125  .conv = &qconv_fixed,
126  .conv_cls = NULL,
127  .data = ptr,
128  .size = ptr_size,
129  .num_params = 1
130  };
131 
132  return res;
133 }
static int qconv_fixed(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

Referenced by postgres_plugin_put(), and postgres_plugin_remove_key().

Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_string()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string ( const char *  ptr)

Generate query parameter for a string.

Parameters
ptrpointer to the string query parameter to pass
Returns
query parameter to use

Definition at line 98 of file pq_query_helper.c.

138 {
140  strlen (ptr));
141 }
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.

◆ GNUNET_PQ_query_param_bool()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_bool ( bool  b)

Pass a boolean into a query.

Parameters
bboolean bit to pass
Returns
query parameter to use

Definition at line 98 of file pq_query_helper.c.

146 {
147  static uint8_t bt = 1;
148  static uint8_t bf = 0;
149 
150  return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf,
151  sizeof (uint8_t));
152 }

References data, and GNUNET_break.

◆ GNUNET_PQ_query_param_array_bool()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bool ( unsigned int  num,
const bool *  elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of bool in host byte order.

Parameters
numNumber of elements in elements
elementsContinuous array of num boolean elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

956 {
957  return query_param_array_generic (num,
958  true,
959  elements,
960  NULL,
961  sizeof(bool),
963  db->oids[GNUNET_PQ_DATATYPE_BOOL]);
964 }
@ array_of_bool
Definition: pq.h:115
static struct GNUNET_PQ_QueryParam query_param_array_generic(unsigned int num, bool continuous, const void *elements, const size_t *sizes, size_t same_size, enum array_types typ, Oid oid)
Function to genreate a typ specific query parameter and corresponding closure.

◆ GNUNET_PQ_query_param_array_uint16()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint16 ( unsigned int  num,
const uint16_t *  elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of uint16_t in host byte order.

Note that the (unsigend) elements are not checked to wrap over INT2_MAX

Parameters
numNumber of elements in elements
elementsContinuous array of num uint16 elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

972 {
973  return query_param_array_generic (num,
974  true,
975  elements,
976  NULL,
977  sizeof(uint16_t),
979  db->oids[GNUNET_PQ_DATATYPE_INT2]);
980 }
@ array_of_uint16
Definition: pq.h:116

◆ GNUNET_PQ_query_param_array_uint32()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint32 ( unsigned int  num,
const uint32_t *  elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of uint32_t in host byte order.

Note that the (unsigend) elements are not checked to wrap over INT4_MAX

Parameters
numNumber of elements in elements
elementsContinuous Array of num uint32_t elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

988 {
989  return query_param_array_generic (num,
990  true,
991  elements,
992  NULL,
993  sizeof(uint32_t),
995  db->oids[GNUNET_PQ_DATATYPE_INT4]);
996 }
@ array_of_uint32
Definition: pq.h:117

◆ GNUNET_PQ_query_param_array_uint64()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint64 ( unsigned int  num,
const uint64_t *  elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of uint64 in host byte order.

Note that the (unsigend) elements are not checked to wrap over INT8_MAX

Parameters
numNumber of elements in elements
elementsContinuous array of num uint64_t elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1004 {
1005  return query_param_array_generic (num,
1006  true,
1007  elements,
1008  NULL,
1009  sizeof(uint64_t),
1011  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1012 }
@ array_of_uint64
Definition: pq.h:118

◆ GNUNET_PQ_query_param_array_bytes()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes ( unsigned int  num,
const void *  elements,
const size_t *  sizes,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of buffers elements, each of corresponding size given in sizes.

Parameters
numNumber of elements in elements
elementsContinous array of num buffers, each of corresponding size given in sizes.
sizesPointer to sizes in bytes of each element in elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1021 {
1022  return query_param_array_generic (num,
1023  true,
1024  elements,
1025  sizes,
1026  0,
1027  array_of_byte,
1028  db->oids[GNUNET_PQ_DATATYPE_BYTEA]);
1029 }
@ array_of_byte
Definition: pq.h:119

◆ GNUNET_PQ_query_param_array_bytes_same_size()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes_same_size ( unsigned int  num,
const void *  elements,
size_t  same_size,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of buffers elements, each of the same size size.

Parameters
numNumber of elements in elements
elementsContinous array of num buffers, each with the same size same_size
same_sizeSize in bytes of each element in elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1055 {
1056  return query_param_array_generic (num,
1057  true,
1058  elements,
1059  NULL,
1060  same_size,
1061  array_of_byte,
1062  db->oids[GNUNET_PQ_DATATYPE_BYTEA]);
1063 }

◆ GNUNET_PQ_query_param_array_ptrs_bytes_same_size()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes_same_size ( unsigned int  num,
const void *  elements[],
size_t  same_size,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of pointers to buffers elements, each of the same size size.

Parameters
numNumber of elements in elements
elementsArray of num pointers to buffers, each with the same size same_size
same_sizeSize in bytes of each element in elements
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1072 {
1073  return query_param_array_generic (num,
1074  false,
1075  elements,
1076  NULL,
1077  same_size,
1078  array_of_byte,
1079  db->oids[GNUNET_PQ_DATATYPE_BYTEA]);
1080 }

◆ GNUNET_PQ_query_param_array_string()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_string ( unsigned int  num,
const char *  elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of strings (continuous)

Parameters
numNumber of elements in elements
elementsArray of num continuous strings (zero-terminated each)
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1088 {
1089  return query_param_array_generic (num,
1090  true,
1091  elements,
1092  NULL,
1093  0,
1096 }
@ array_of_string
Definition: pq.h:120

◆ GNUNET_PQ_query_param_array_ptrs_string()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_string ( unsigned int  num,
const char *  elements[],
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of strings (pointers)

Parameters
numNumber of elements in elements
elementsArray of num pointer to strings
dbDatabase context, needed for database-depending encoding of elements
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1104 {
1105  return query_param_array_generic (num,
1106  false,
1107  elements,
1108  NULL,
1109  0,
1112 }

◆ GNUNET_PQ_query_param_array_abs_time()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_abs_time ( unsigned int  num,
const struct GNUNET_TIME_Absolute elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of absolute time stamps (continuous)

Parameters
numNumber of elements in elements
elementsArray of num absolute timestamps
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1120 {
1121  return query_param_array_generic (num,
1122  true,
1123  elements,
1124  NULL,
1125  sizeof(struct GNUNET_TIME_Absolute),
1127  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1128 }
@ array_of_abs_time
Definition: pq.h:121
Time for absolute times used by GNUnet, in microseconds.

References GNUNET_TIME_Timestamp::abs_time, GNUNET_TIME_Absolute::abs_value_us, array_of_abs_time, array_of_bool, array_of_byte, array_of_rel_time, array_of_string, array_of_timestamp, array_of_uint16, array_of_uint32, array_of_uint64, data, GNUNET_assert, GNUNET_free, GNUNET_htonll(), GNUNET_malloc, GNUNET_memcpy, GNUNET_new_array, h, INT_MAX, len, meta, GNUNET_TIME_Relative::rel_value_us, RETURN_UNLESS, and qconv_array_cls::sizes.

Here is the call graph for this function:

◆ GNUNET_PQ_query_param_array_ptrs_abs_time()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_abs_time ( unsigned int  num,
const struct GNUNET_TIME_Absolute elements[],
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of absolute time stamps (pointers)

Parameters
numNumber of elements in elements
elementsArray of num pointers to absolute timestamps
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1136 {
1137  return query_param_array_generic (num,
1138  false,
1139  elements,
1140  NULL,
1141  sizeof(struct GNUNET_TIME_Absolute),
1143  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1144 }

◆ GNUNET_PQ_query_param_array_rel_time()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_rel_time ( unsigned int  num,
const struct GNUNET_TIME_Relative elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of relative time stamps (continuous)

Parameters
numNumber of elements in elements
elementsArray of num relative timestamps
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1152 {
1153  return query_param_array_generic (num,
1154  true,
1155  elements,
1156  NULL,
1157  sizeof(struct GNUNET_TIME_Relative),
1159  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1160 }
Time for relative time used by GNUnet, in microseconds.

◆ GNUNET_PQ_query_param_array_ptrs_rel_time()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_rel_time ( unsigned int  num,
const struct GNUNET_TIME_Relative elements[],
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of relative time stamps (pointers)

Parameters
numNumber of elements in elements
elementsArray of num pointers to relative timestamps
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1168 {
1169  return query_param_array_generic (num,
1170  false,
1171  elements,
1172  NULL,
1173  sizeof(struct GNUNET_TIME_Relative),
1175  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1176 }

◆ GNUNET_PQ_query_param_array_timestamp()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_timestamp ( unsigned int  num,
const struct GNUNET_TIME_Timestamp elements,
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of time stamps (continuous)

Parameters
numNumber of elements in elements
elementsArray of num timestamps
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1184 {
1185  return query_param_array_generic (num,
1186  true,
1187  elements,
1188  NULL,
1189  sizeof(struct GNUNET_TIME_Timestamp),
1191  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1192 }
@ array_of_timestamp
Definition: pq.h:123
Rounded time for timestamps used by GNUnet, in seconds.

◆ GNUNET_PQ_query_param_array_ptrs_timestamp()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_timestamp ( unsigned int  num,
const struct GNUNET_TIME_Timestamp elements[],
const struct GNUNET_PQ_Context db 
)

Generate query parameter for an array of time stamps (pointers)

Parameters
numNumber of elements in elements
elementsArray of num pointers to relative timestamps
Returns
query parameter to use

Definition at line 648 of file pq_query_helper.c.

1200 {
1201  return query_param_array_generic (num,
1202  false,
1203  elements,
1204  NULL,
1205  sizeof(struct GNUNET_TIME_Timestamp),
1207  db->oids[GNUNET_PQ_DATATYPE_INT8]);
1208 }

◆ GNUNET_PQ_query_param_rsa_public_key()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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.
Returns
query parameter to use

Definition at line 341 of file pq_query_helper.c.

371 {
372  struct GNUNET_PQ_QueryParam res = {
373  .conv = &qconv_rsa_public_key,
374  .data = x,
375  .num_params = 1
376  };
377 
378  return res;
379 }
static int qconv_rsa_public_key(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References buf, data, GNUNET_break, and GNUNET_CRYPTO_rsa_public_key_encode().

Here is the call graph for this function:

◆ GNUNET_PQ_query_param_rsa_signature()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 397 of file pq_query_helper.c.

426 {
427  struct GNUNET_PQ_QueryParam res = {
428  .conv = &qconv_rsa_signature,
429  .data = x,
430  .num_params = 1
431  };
432 
433  return res;
434 }
static int qconv_rsa_signature(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References buf, data, GNUNET_break, and GNUNET_CRYPTO_rsa_signature_encode().

Here is the call graph for this function:

◆ GNUNET_PQ_query_param_relative_time()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_relative_time ( const struct GNUNET_TIME_Relative x)

Generate query parameter for a relative time value.

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass
Returns
query parameter to use

Definition at line 452 of file pq_query_helper.c.

484 {
485  struct GNUNET_PQ_QueryParam res = {
486  .conv = &qconv_rel_time,
487  .data = x,
488  .size = sizeof(*x),
489  .num_params = 1
490  };
491 
492  return res;
493 }
static int qconv_rel_time(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References data, GNUNET_break, GNUNET_htonll(), GNUNET_new, and GNUNET_TIME_Relative::rel_value_us.

Here is the call graph for this function:

◆ GNUNET_PQ_query_param_absolute_time()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 511 of file pq_query_helper.c.

543 {
544  struct GNUNET_PQ_QueryParam res = {
545  .conv = &qconv_abs_time,
546  .data = x,
547  .size = sizeof(*x),
548  .num_params = 1
549  };
550 
551  return res;
552 }
static int qconv_abs_time(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References GNUNET_TIME_Absolute::abs_value_us, data, GNUNET_break, GNUNET_htonll(), and GNUNET_new.

Referenced by delete_old_block(), namecache_postgres_expire_blocks(), postgres_plugin_del(), postgres_plugin_get_closest(), postgres_plugin_get_expiration(), and postgres_plugin_put().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_timestamp()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp ( const struct GNUNET_TIME_Timestamp x)

Generate query parameter for a timestamp.

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass
Returns
query parameter to use

Definition at line 511 of file pq_query_helper.c.

565 {
567 }
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
struct GNUNET_TIME_Absolute abs_time
The actual value.

◆ GNUNET_PQ_query_param_absolute_time_nbo()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 511 of file pq_query_helper.c.

558 {
560 }
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
uint64_t abs_value_us__
The actual value (in network byte order).

◆ GNUNET_PQ_query_param_timestamp_nbo()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp_nbo ( const struct GNUNET_TIME_TimestampNBO x)

Generate query parameter for a timestamp in NBO.

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass
Returns
query parameter to use

Definition at line 511 of file pq_query_helper.c.

573 {
575 }
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
struct GNUNET_TIME_AbsoluteNBO abs_time_nbo
The actual value.

◆ GNUNET_PQ_query_param_uint16()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 170 of file pq_query_helper.c.

200 {
201  struct GNUNET_PQ_QueryParam res = {
202  .conv = &qconv_uint16,
203  .data = x,
204  .size = sizeof(*x),
205  .num_params = 1
206  };
207 
208  return res;
209 }
static int qconv_uint16(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References data, GNUNET_break, and GNUNET_new.

Referenced by postgres_plugin_get_key().

Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_uint32()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 227 of file pq_query_helper.c.

257 {
258  struct GNUNET_PQ_QueryParam res = {
259  .conv = &qconv_uint32,
260  .data = x,
261  .size = sizeof(*x),
262  .num_params = 1
263  };
264 
265  return res;
266 }
static int qconv_uint32(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References data, GNUNET_break, and GNUNET_new.

Referenced by postgres_plugin_del(), postgres_plugin_get_closest(), postgres_plugin_get_key(), postgres_plugin_get_zero_anonymity(), and postgres_plugin_put().

Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_uint64()

struct GNUNET_PQ_QueryParam GNUNET_PQ_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
Returns
query parameter to use

Definition at line 284 of file pq_query_helper.c.

314 {
315  struct GNUNET_PQ_QueryParam res = {
316  .conv = &qconv_uint64,
317  .data = x,
318  .size = sizeof(*x),
319  .num_params = 1
320  };
321 
322  return res;
323 }
static int qconv_uint64(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.

References data, GNUNET_break, GNUNET_htonll(), and GNUNET_new.

Referenced by postgres_plugin_get_key(), postgres_plugin_get_zero_anonymity(), postgres_plugin_put(), process_result(), and repl_proc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_allow_null()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_allow_null ( struct GNUNET_PQ_ResultSpec  rs,
bool *  is_null 
)

Allow NULL value to be found in the database for the given value.

Parameters
rsresult spec entry to modify
[out]is_nulllocation set to 'true' if the value was indeed NULL, set to 'false' if the value was non-NULL
Returns
array entry for the result specification to use

Definition at line 1 of file pq_result_helper.c.

36 {
37  struct GNUNET_PQ_ResultSpec rsr;
38 
39  rsr = rs;
40  rsr.is_nullable = true;
41  rsr.is_null = is_null;
42  return rsr;
43 }
Description of a DB result cell.
bool * is_null
Points to a location where we should store "true" if the result found is NULL, and otherwise "false".
bool is_nullable
True if NULL is allowed for a value in the database.

References GNUNET_PQ_ResultSpec::is_null, and GNUNET_PQ_ResultSpec::is_nullable.

Referenced by handle_results().

Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_variable_size()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size ( const char *  name,
void **  dst,
size_t *  sptr 
)

Variable-size result expected.

Parameters
namename of the field in the table
[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 54 of file pq_result_helper.c.

132 {
133  struct GNUNET_PQ_ResultSpec res = {
134  .conv = &extract_varsize_blob,
135  .cleaner = &clean_varsize_blob,
136  .dst = (void *) (dst),
137  .fname = name,
138  .result_size = sptr
139  };
140 
141  return res;
142 }
static enum GNUNET_GenericReturnValue extract_varsize_blob(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_varsize_blob(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
void * dst
Destination for the data.

Referenced by extract_result_cb(), handle_results(), lookup_records(), and process_result().

Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_fixed_size()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_fixed_size ( const char *  name,
void *  dst,
size_t  dst_size 
)

Fixed-size result expected.

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

Definition at line 54 of file pq_result_helper.c.

215 {
216  struct GNUNET_PQ_ResultSpec res = {
217  .conv = &extract_fixed_blob,
218  .dst = (dst),
219  .dst_size = dst_size,
220  .fname = name
221  };
222 
223  return res;
224 }
static enum GNUNET_GenericReturnValue extract_fixed_blob(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
const char * fname
Field name of the desired result.
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_PQ_result_spec_string()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string ( const char *  name,
char **  dst 
)

0-terminated string expected.

Parameters
namename of the field in the table
[out]dstwhere to store the result, allocated
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

501 {
502  struct GNUNET_PQ_ResultSpec res = {
503  .conv = &extract_string,
504  .cleaner = &clean_string,
505  .dst = (void *) dst,
506  .fname = (name)
507  };
508 
509  return res;
510 }
static enum GNUNET_GenericReturnValue extract_string(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_string(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.

Referenced by lookup_records().

Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_bool()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_bool ( const char *  name,
bool *  dst 
)

boolean expected.

Parameters
namename of the field in the table
[out]dstwhere to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

572 {
573  struct GNUNET_PQ_ResultSpec res = {
574  .conv = &extract_bool,
575  .dst = (void *) dst,
576  .fname = name
577  };
578 
579  return res;
580 }
static enum GNUNET_GenericReturnValue extract_bool(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_PQ_result_spec_rsa_public_key()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_public_key ( const char *  name,
struct GNUNET_CRYPTO_RsaPublicKey **  rsa 
)

RSA public key expected.

Parameters
namename of the field in the table
[out]rsawhere to store the result
Returns
array entry for the result specification to use

Definition at line 294 of file pq_result_helper.c.

311 {
312  struct GNUNET_PQ_ResultSpec res = {
313  .conv = &extract_rsa_public_key,
314  .cleaner = &clean_rsa_public_key,
315  .dst = (void *) rsa,
316  .fname = name
317  };
318 
319  return res;
320 }
static void clean_rsa_public_key(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
static enum GNUNET_GenericReturnValue extract_rsa_public_key(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_PQ_result_spec_rsa_signature()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_signature ( const char *  name,
struct GNUNET_CRYPTO_RsaSignature **  sig 
)

RSA signature expected.

Parameters
namename of the field in the table
[out]sigwhere to store the result;
Returns
array entry for the result specification to use

Definition at line 389 of file pq_result_helper.c.

406 {
407  struct GNUNET_PQ_ResultSpec res = {
408  .conv = &extract_rsa_signature,
409  .cleaner = &clean_rsa_signature,
410  .dst = (void *) sig,
411  .fname = name
412  };
413 
414  return res;
415 }
static enum GNUNET_GenericReturnValue extract_rsa_signature(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_rsa_signature(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.

◆ GNUNET_PQ_result_spec_absolute_time()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time ( const char *  name,
struct GNUNET_TIME_Absolute at 
)

Absolute time expected.

Parameters
namename of the field in the table
[out]atwhere to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

725 {
726  struct GNUNET_PQ_ResultSpec res = {
727  .conv = &extract_abs_time,
728  .dst = (void *) at,
729  .dst_size = sizeof(*at),
730  .fname = name
731  };
732 
733  return res;
734 }
static enum GNUNET_GenericReturnValue extract_abs_time(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

Referenced by extract_result_cb(), handle_results(), and process_result().

Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_timestamp()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_timestamp ( const char *  name,
struct GNUNET_TIME_Timestamp t 
)

Timestamp expected.

Parameters
namename of the field in the table
[out]twhere to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

826 {
827  struct GNUNET_PQ_ResultSpec res = {
828  .conv = &extract_timestamp,
829  .dst = (void *) at,
830  .dst_size = sizeof(*at),
831  .fname = name
832  };
833 
834  return res;
835 }
static enum GNUNET_GenericReturnValue extract_timestamp(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_PQ_result_spec_relative_time()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_relative_time ( const char *  name,
struct GNUNET_TIME_Relative rt 
)

Relative time expected.

Parameters
namename of the field in the table
[out]rtwhere to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

648 {
649  struct GNUNET_PQ_ResultSpec res = {
650  .conv = &extract_rel_time,
651  .dst = (void *) rt,
652  .dst_size = sizeof(*rt),
653  .fname = name
654  };
655 
656  return res;
657 }
static enum GNUNET_GenericReturnValue extract_rel_time(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_PQ_result_spec_absolute_time_nbo()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time_nbo ( const char *  name,
struct GNUNET_TIME_AbsoluteNBO at 
)

Absolute time expected.

Parameters
namename of the field in the table
[out]atwhere to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

740 {
741  struct GNUNET_PQ_ResultSpec res =
743  &at->abs_value_us__);
744 
745  return res;
746 }
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst

◆ GNUNET_PQ_result_spec_timestamp_nbo()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_timestamp_nbo ( const char *  name,
struct GNUNET_TIME_TimestampNBO tn 
)

Timestamp expected.

Parameters
namename of the field in the table
[out]tnwhere to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

879 {
880  struct GNUNET_PQ_ResultSpec res = {
881  .conv = &extract_timestamp_nbo,
882  .dst = (void *) at,
883  .dst_size = sizeof(*at),
884  .fname = name
885  };
886 
887  return res;
888 }
static enum GNUNET_GenericReturnValue extract_timestamp_nbo(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_PQ_result_spec_uint16()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint16 ( const char *  name,
uint16_t *  u16 
)

uint16_t expected.

Parameters
namename of the field in the table
[out]u16where to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

953 {
954  struct GNUNET_PQ_ResultSpec res = {
955  .conv = &extract_uint16,
956  .dst = (void *) u16,
957  .dst_size = sizeof(*u16),
958  .fname = name
959  };
960 
961  return res;
962 }
static enum GNUNET_GenericReturnValue extract_uint16(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

◆ GNUNET_PQ_result_spec_uint32()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32 ( const char *  name,
uint32_t *  u32 
)

uint32_t expected.

Parameters
namename of the field in the table
[out]u32where to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

1027 {
1028  struct GNUNET_PQ_ResultSpec res = {
1029  .conv = &extract_uint32,
1030  .dst = (void *) u32,
1031  .dst_size = sizeof(*u32),
1032  .fname = name
1033  };
1034 
1035  return res;
1036 }
static enum GNUNET_GenericReturnValue extract_uint32(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

Referenced by extract_result_cb(), handle_results(), lookup_records(), and process_result().

Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_uint64()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64 ( const char *  name,
uint64_t *  u64 
)

uint64_t expected.

Parameters
namename of the field in the table
[out]u64where to store the result
Returns
array entry for the result specification to use

Definition at line 484 of file pq_result_helper.c.

1111 {
1112  struct GNUNET_PQ_ResultSpec res = {
1113  .conv = &extract_uint64,
1114  .dst = (void *) u64,
1115  .dst_size = sizeof(*u64),
1116  .fname = name
1117  };
1118 
1119  return res;
1120 }
static enum GNUNET_GenericReturnValue extract_uint64(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.

Referenced by lookup_records(), postgres_plugin_estimate_size(), and process_result().

Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_array_bool()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_bool ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
bool **  bools 
)

array of bool expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array bools.
[out]boolspointer to where to store the result, an array of num bool's. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1401 {
1402  struct array_result_cls *info =
1403  GNUNET_new (struct array_result_cls);
1404 
1405  info->num = num;
1406  info->typ = array_of_bool;
1407  info->oid = db->oids[GNUNET_PQ_DATATYPE_BOOL];
1408 
1409  struct GNUNET_PQ_ResultSpec res = {
1410  .conv = extract_array_generic,
1411  .cleaner = array_cleanup,
1412  .dst = (void *) dst,
1413  .fname = name,
1414  .cls = info
1415  };
1416  return res;
1417 }
#define info
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static enum GNUNET_GenericReturnValue extract_array_generic(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result as array of a specific type from row row.
static void array_cleanup(void *cls, void *rd)
Cleanup of the data and closure of an array spec.
void * cls
Closure for conv and cleaner.
Closure for the array result specifications.

◆ GNUNET_PQ_result_spec_array_uint16()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint16 ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
uint16_t **  dst 
)

array of uint16_t expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array u16s.
[out]dstpointer to where to store the an array of num uint16_t's. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1426 {
1427  struct array_result_cls *info =
1428  GNUNET_new (struct array_result_cls);
1429 
1430  info->num = num;
1431  info->typ = array_of_uint16;
1432  info->oid = db->oids[GNUNET_PQ_DATATYPE_INT2];
1433 
1434  struct GNUNET_PQ_ResultSpec res = {
1435  .conv = extract_array_generic,
1436  .cleaner = array_cleanup,
1437  .dst = (void *) dst,
1438  .fname = name,
1439  .cls = info
1440  };
1441  return res;
1442 }

◆ GNUNET_PQ_result_spec_array_uint32()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint32 ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
uint32_t **  dst 
)

array of uint32_t expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array u32s.
[out]dstpointer to where to store the array of num uint32_t's. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1451 {
1452  struct array_result_cls *info =
1453  GNUNET_new (struct array_result_cls);
1454 
1455  info->num = num;
1456  info->typ = array_of_uint32;
1457  info->oid = db->oids[GNUNET_PQ_DATATYPE_INT4];
1458 
1459  struct GNUNET_PQ_ResultSpec res = {
1460  .conv = extract_array_generic,
1461  .cleaner = array_cleanup,
1462  .dst = (void *) dst,
1463  .fname = name,
1464  .cls = info
1465  };
1466  return res;
1467 }

◆ GNUNET_PQ_result_spec_array_uint64()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint64 ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
uint64_t **  dst 
)

array of uint64_t expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array u64s.
[out]dstpointer to where to store the array of num uint64_t's. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1476 {
1477  struct array_result_cls *info =
1478  GNUNET_new (struct array_result_cls);
1479 
1480  info->num = num;
1481  info->typ = array_of_uint64;
1482  info->oid = db->oids[GNUNET_PQ_DATATYPE_INT8];
1483 
1484  struct GNUNET_PQ_ResultSpec res = {
1485  .conv = extract_array_generic,
1486  .cleaner = array_cleanup,
1487  .dst = (void *) dst,
1488  .fname = name,
1489  .cls = info
1490  };
1491  return res;
1492 }

◆ GNUNET_PQ_result_spec_array_abs_time()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_abs_time ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
struct GNUNET_TIME_Absolute **  dst 
)

array of absolute time expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array u64s.
[out]dstpointer to where to store the array of num absolute time. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1501 {
1502  struct array_result_cls *info =
1503  GNUNET_new (struct array_result_cls);
1504 
1505  info->num = num;
1506  info->typ = array_of_abs_time;
1507  info->oid = db->oids[GNUNET_PQ_DATATYPE_INT8];
1508 
1509  struct GNUNET_PQ_ResultSpec res = {
1510  .conv = extract_array_generic,
1511  .cleaner = array_cleanup,
1512  .dst = (void *) dst,
1513  .fname = name,
1514  .cls = info
1515  };
1516  return res;
1517 }

◆ GNUNET_PQ_result_spec_array_rel_time()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_rel_time ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
struct GNUNET_TIME_Relative **  dst 
)

array of relative time expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array u64s.
[out]dstpointer to where to store the array of num relate time. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1526 {
1527  struct array_result_cls *info =
1528  GNUNET_new (struct array_result_cls);
1529 
1530  info->num = num;
1531  info->typ = array_of_rel_time;
1532  info->oid = db->oids[GNUNET_PQ_DATATYPE_INT8];
1533 
1534  struct GNUNET_PQ_ResultSpec res = {
1535  .conv = extract_array_generic,
1536  .cleaner = array_cleanup,
1537  .dst = (void *) dst,
1538  .fname = name,
1539  .cls = info
1540  };
1541  return res;
1542 }
@ array_of_rel_time
Definition: pq.h:122

◆ GNUNET_PQ_result_spec_array_timestamp()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_timestamp ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
struct GNUNET_TIME_Timestamp **  dst 
)

array of relative time expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements in the array u64s.
[out]dstpointer to where to store the array of num timestamps. Allocated by the function, MUST be freed with GNUNET_free.
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1551 {
1552  struct array_result_cls *info =
1553  GNUNET_new (struct array_result_cls);
1554 
1555  info->num = num;
1556  info->typ = array_of_timestamp;
1557  info->oid = db->oids[GNUNET_PQ_DATATYPE_INT8];
1558 
1559  struct GNUNET_PQ_ResultSpec res = {
1560  .conv = extract_array_generic,
1561  .cleaner = array_cleanup,
1562  .dst = (void *) dst,
1563  .fname = name,
1564  .cls = info
1565  };
1566  return res;
1567 }

◆ GNUNET_PQ_result_spec_array_variable_size()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_variable_size ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
size_t **  sizes,
void **  dst 
)

Array of variable-size result expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements
[out]sizeswhere to store the num size's of byte-buffers in dst
[out]dstwhere to store the continuous array of num byte-buffers , allocated
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1577 {
1578  struct array_result_cls *info =
1579  GNUNET_new (struct array_result_cls);
1580 
1581  info->num = num;
1582  info->sizes = sizes;
1583  info->typ = array_of_byte;
1584  info->oid = db->oids[GNUNET_PQ_DATATYPE_BYTEA];
1585 
1586  struct GNUNET_PQ_ResultSpec res = {
1587  .conv = extract_array_generic,
1588  .cleaner = array_cleanup,
1589  .dst = (void *) dst,
1590  .fname = name,
1591  .cls = info
1592  };
1593  return res;
1594 }

◆ GNUNET_PQ_result_spec_array_fixed_size()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_fixed_size ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t  size,
size_t *  num,
void **  dst 
)

Array of fixed-size result expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
sizenumber of bytes expected in each element of dst
[out]numwhere to store the number of elements
[out]dstwhere to store the results, an continuous array of fixed-size elements
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1604 {
1605  struct array_result_cls *info =
1606  GNUNET_new (struct array_result_cls);
1607 
1608  info->num = num;
1609  info->same_size = size;
1610  info->typ = array_of_byte;
1611  info->oid = db->oids[GNUNET_PQ_DATATYPE_BYTEA];
1612 
1613  struct GNUNET_PQ_ResultSpec res = {
1614  .conv = extract_array_generic,
1615  .cleaner = array_cleanup,
1616  .dst = (void *) dst,
1617  .fname = name,
1618  .cls = info
1619  };
1620  return res;
1621 }
static unsigned int size
Size of the "table".
Definition: peer.c:68

◆ GNUNET_PQ_result_spec_array_string()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_string ( const struct GNUNET_PQ_Context db,
const char *  name,
size_t *  num,
char **  dst 
)

Array of 0-terminated strings expected.

Parameters
dbDatabase context, needed for OID lookup for the correct type
namename of the field in the table
[out]numwhere to store the number of elements
[out]dstwhere to store the allocated continous array of num 0-terminated strings
Returns
array entry for the result specification to use

Definition at line 1377 of file pq_result_helper.c.

1630 {
1631  struct array_result_cls *info =
1632  GNUNET_new (struct array_result_cls);
1633 
1634  info->num = num;
1635  info->typ = array_of_string;
1636  info->oid = db->oids[GNUNET_PQ_DATATYPE_VARCHAR];
1637 
1638  struct GNUNET_PQ_ResultSpec res = {
1639  .conv = extract_array_generic,
1640  .cleaner = array_cleanup,
1641  .dst = (void *) dst,
1642  .fname = name,
1643  .cls = info
1644  };
1645  return res;
1646 }

◆ GNUNET_PQ_exec_prepared()

PGresult* GNUNET_PQ_exec_prepared ( struct GNUNET_PQ_Context db,
const char *  name,
const struct GNUNET_PQ_QueryParam params 
)

Execute a prepared statement.

Parameters
dbdatabase context
namename of the prepared statement
paramsparameters to the statement
Returns
postgres result
Deprecated:
(should become an internal API)

Definition at line 33 of file pq.c.

36 {
37  unsigned int len;
38 
40  "Running prepared statement `%s' on %p\n",
41  name,
42  db);
43  /* count the number of parameters */
44  len = 0;
45  for (unsigned int i = 0; 0 != params[i].num_params; i++)
46  len += params[i].num_params;
47 
48  /* new scope to allow stack allocation without alloca */
49  {
50  /* Scratch buffer for temporary storage */
51  void *scratch[GNUNET_NZL (len)];
52  /* Parameter array we are building for the query */
53  void *param_values[GNUNET_NZL (len)];
54  int param_lengths[GNUNET_NZL (len)];
55  int param_formats[GNUNET_NZL (len)];
56  unsigned int off;
57  /* How many entries in the scratch buffer are in use? */
58  unsigned int soff;
59  PGresult *res;
60  int ret;
61  ConnStatusType status;
62 
63  off = 0;
64  soff = 0;
65  for (unsigned int i = 0; 0 != params[i].num_params; i++)
66  {
67  const struct GNUNET_PQ_QueryParam *x = &params[i];
68 
69  ret = x->conv (x->conv_cls,
70  x->data,
71  x->size,
72  &param_values[off],
73  &param_lengths[off],
74  &param_formats[off],
75  x->num_params,
76  &scratch[soff],
77  len - soff);
78  if (ret < 0)
79  {
80  for (off = 0; off < soff; off++)
81  GNUNET_free (scratch[off]);
82  return NULL;
83  }
84  soff += ret;
85  off += x->num_params;
86  }
87  GNUNET_assert (off == len);
89  "pq",
90  "Executing prepared SQL statement `%s'\n",
91  name);
92  res = PQexecPrepared (db->conn,
93  name,
94  len,
95  (const char **) param_values,
96  param_lengths,
97  param_formats,
98  1);
100  "pq",
101  "Execution of prepared SQL statement `%s' finished (%s)\n",
102  name,
103  PQresStatus (PQresultStatus (res)));
104  if ( (PGRES_COMMAND_OK != PQresultStatus (res)) &&
105  (CONNECTION_OK != (status = PQstatus (db->conn))) )
106  {
108  "pq",
109  "Database disconnected on SQL statement `%s' (reconnecting)\n",
110  name);
112  res = NULL;
113  }
114 
115  for (off = 0; off < soff; off++)
116  GNUNET_free (scratch[off]);
117  return res;
118  }
119 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
uint16_t status
See PRISM_STATUS_*-constants.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:424
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_free(ptr)
Wrapper around free.
const void * data
Data or NULL.
GNUNET_PQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_pq_lib.h:86
size_t size
Size of data.

References GNUNET_PQ_QueryParam::conv, GNUNET_PQ_QueryParam::conv_cls, GNUNET_PQ_QueryParam::data, db, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_log_from, GNUNET_NZL, GNUNET_PQ_reconnect(), len, name, GNUNET_PQ_QueryParam::num_params, res, ret, GNUNET_PQ_QueryParam::size, and status.

Here is the call graph for this function:

◆ GNUNET_PQ_extract_result()

enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result ( PGresult *  result,
struct GNUNET_PQ_ResultSpec rs,
int  row 
)

Extract results from a query result according to the given specification.

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

Definition at line 139 of file pq.c.

152 {
153  unsigned int i;
154 
155  if (NULL == result)
156  return GNUNET_SYSERR;
157  for (i = 0; NULL != rs[i].conv; i++)
158  {
159  struct GNUNET_PQ_ResultSpec *spec;
161 
162  spec = &rs[i];
163  ret = spec->conv (spec->cls,
164  result,
165  row,
166  spec->fname,
167  &spec->dst_size,
168  spec->dst);
169  switch (ret)
170  {
171  case GNUNET_OK:
172  /* canonical case, continue below */
173  if (NULL != spec->is_null)
174  *spec->is_null = false;
175  break;
176  case GNUNET_NO:
177  if (spec->is_nullable)
178  {
179  if (NULL != spec->is_null)
180  *spec->is_null = true;
181  continue;
182  }
184  "NULL field encountered for `%s' where non-NULL was required\n",
185  spec->fname);
186  goto cleanup;
187  case GNUNET_SYSERR:
189  "Failed to extract field `%s'\n",
190  spec->fname);
191  GNUNET_break (0);
192  goto cleanup;
193  }
194  if (NULL != spec->result_size)
195  *spec->result_size = spec->dst_size;
196  }
197  return GNUNET_OK;
198  cleanup:
199  for (unsigned int j = 0; j < i; j++)
200  if (NULL != rs[j].cleaner)
201  rs[j].cleaner (rs[j].cls,
202  rs[j].dst);
203  return GNUNET_SYSERR;
204 }
static void cleanup(void *cls)
Function scheduled as very last function, cleans up after us.
static int result
Global testing status.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_NO
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
GNUNET_PQ_ResultConverter conv
What is the format of the result?
size_t * result_size
Where to store actual size of the result.
GNUNET_PQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.

Referenced by extract_result_cb(), handle_results(), lookup_records(), process_keys(), and process_result().

Here is the caller graph for this function:

◆ GNUNET_PQ_cleanup_result()

void GNUNET_PQ_cleanup_result ( struct GNUNET_PQ_ResultSpec rs)

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

Parameters
rsreult specification to clean up

Definition at line 139 of file pq.c.

140 {
141  for (unsigned int i = 0; NULL != rs[i].conv; i++)
142  if (NULL != rs[i].cleaner)
143  rs[i].cleaner (rs[i].cls,
144  rs[i].dst);
145 }

References GNUNET_PQ_ResultSpec::cleaner, and GNUNET_PQ_ResultSpec::conv.

Referenced by extract_result_cb(), handle_results(), lookup_records(), process_keys(), and process_result().

Here is the caller graph for this function:

◆ GNUNET_PQ_eval_result()

enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result ( struct GNUNET_PQ_Context db,
const char *  statement_name,
PGresult *  result 
)

Check the result's error code to see what happened.

Also logs errors.

Parameters
dbdatabase to execute the statement in
statement_namename of the statement that created result
resultresult to check
Returns
status code from the result, mapping PQ status codes to enum GNUNET_DB_QueryStatus. Never returns positive values as this function does not look at the result set.
Deprecated:
(low level, let's see if we can do with just the high-level functions)

Definition at line 1 of file pq_eval.c.

49 {
50  ExecStatusType est;
51 
52  if (NULL == result)
54  est = PQresultStatus (result);
55  if ((PGRES_COMMAND_OK != est) &&
56  (PGRES_TUPLES_OK != est))
57  {
58  const char *sqlstate;
59  ConnStatusType status;
60 
61  if (CONNECTION_OK != (status = PQstatus (db->conn)))
62  {
64  "pq",
65  "Database connection failed during query `%s': %d (reconnecting)\n",
66  statement_name,
67  status);
70  }
71 
72  sqlstate = PQresultErrorField (result,
73  PG_DIAG_SQLSTATE);
74  if (NULL == sqlstate)
75  {
76  /* very unexpected... */
77  GNUNET_break (0);
79  }
80  if ((0 == strcmp (sqlstate,
82  (0 == strcmp (sqlstate,
84  {
85  /* These two can be retried and have a fair chance of working
86  the next time */
88  "pq",
89  "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
90  statement_name,
91  PQresultErrorField (result,
92  PG_DIAG_MESSAGE_PRIMARY),
93  PQresultErrorField (result,
94  PG_DIAG_MESSAGE_DETAIL),
95  PQresultErrorMessage (result),
96  PQresStatus (PQresultStatus (result)),
97  PQerrorMessage (db->conn));
99  }
100  if (0 == strcmp (sqlstate,
102  {
103  /* Likely no need to retry, INSERT of "same" data. */
105  "pq",
106  "Query `%s' failed with unique violation: %s/%s/%s/%s/%s\n",
107  statement_name,
108  PQresultErrorField (result,
109  PG_DIAG_MESSAGE_PRIMARY),
110  PQresultErrorField (result,
111  PG_DIAG_MESSAGE_DETAIL),
112  PQresultErrorMessage (result),
113  PQresStatus (PQresultStatus (result)),
114  PQerrorMessage (db->conn));
116  }
118  "pq",
119  "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
120  statement_name,
121  PQresultErrorField (result,
122  PG_DIAG_MESSAGE_PRIMARY),
123  PQresultErrorField (result,
124  PG_DIAG_MESSAGE_DETAIL),
125  PQresultErrorMessage (result),
126  PQresStatus (PQresultStatus (result)),
127  PQerrorMessage (db->conn));
129  }
131 }
@ GNUNET_DB_STATUS_HARD_ERROR
A hard error occurred, retrying will not help.
Definition: gnunet_db_lib.h:41
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:55
@ GNUNET_DB_STATUS_SOFT_ERROR
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:47
@ GNUNET_ERROR_TYPE_INFO
#define PQ_DIAG_SQLSTATE_DEADLOCK
Error code returned by Postgres for deadlock.
Definition: pq_eval.c:32
#define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE
Error code returned by Postgres on serialization failure.
Definition: pq_eval.c:42
#define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION
Error code returned by Postgres for uniqueness violation.
Definition: pq_eval.c:37

◆ GNUNET_PQ_eval_prepared_non_select()

enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select ( struct GNUNET_PQ_Context db,
const char *  statement_name,
const struct GNUNET_PQ_QueryParam params 
)

Execute a named prepared statement that is NOT a SELECT statement in connection using the given params.

Returns the resulting session state.

Parameters
dbdatabase to execute the statement with
statement_namename of the statement
paramsparameters to give to the statement (GNUNET_PQ_query_param_end-terminated)
Returns
status code from the result, mapping PQ status codes to enum GNUNET_DB_QueryStatus. If the statement was a DELETE or UPDATE statement, the number of affected rows is returned; if the statement was an INSERT statement, and no row was added due to a UNIQUE violation, we return zero; if INSERT was successful, we return one.

Definition at line 1 of file pq_eval.c.

138 {
139  PGresult *result;
140  enum GNUNET_DB_QueryStatus qs;
141 
143  statement_name,
144  params);
145  if (NULL == result)
148  statement_name,
149  result);
151  {
152  const char *tuples;
153 
154  /* What an awful API, this function really does return a string */
155  tuples = PQcmdTuples (result);
156  if (NULL != tuples)
157  qs = strtol (tuples, NULL, 10);
158  }
159  PQclear (result);
160  return qs;
161 }
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:37
PGresult * GNUNET_PQ_exec_prepared(struct GNUNET_PQ_Context *db, const char *name, const struct GNUNET_PQ_QueryParam *params)
Execute a prepared statement.
Definition: pq.c:33
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(struct GNUNET_PQ_Context *db, const char *statement_name, PGresult *result)
Check the result's error code to see what happened.
Definition: pq_eval.c:46

Referenced by delete_old_block(), namecache_postgres_expire_blocks(), postgres_plugin_put(), postgres_plugin_remove_key(), process_result(), and repl_proc().

Here is the caller graph for this function:

◆ GNUNET_PQ_eval_prepared_multi_select()

enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select ( struct GNUNET_PQ_Context db,
const char *  statement_name,
const struct GNUNET_PQ_QueryParam params,
GNUNET_PQ_PostgresResultHandler  rh,
void *  rh_cls 
)

Execute a named prepared statement that is a SELECT statement which may return multiple results in connection using the given params.

Call rh with the results. Returns the query status including the number of results given to rh (possibly zero). rh will not have been called if the return value is negative.

Parameters
dbdatabase to execute the statement with
statement_namename of the statement
paramsparameters to give to the statement (GNUNET_PQ_query_param_end-terminated)
rhfunction to call with the result set, NULL to ignore
rh_clsclosure to pass to rh
Returns
status code from the result, mapping PQ status codes to enum GNUNET_DB_QueryStatus.

Definition at line 1 of file pq_eval.c.

170 {
171  PGresult *result;
172  enum GNUNET_DB_QueryStatus qs;
173  unsigned int ret;
174 
176  statement_name,
177  params);
178  if (NULL == result)
181  statement_name,
182  result);
183  if (qs < 0)
184  {
185  PQclear (result);
186  return qs;
187  }
188  ret = PQntuples (result);
189  if (NULL != rh)
190  rh (rh_cls,
191  result,
192  ret);
193  PQclear (result);
194  return ret;
195 }

Referenced by postgres_plugin_del(), postgres_plugin_get_closest(), postgres_plugin_get_expiration(), postgres_plugin_get_key(), postgres_plugin_get_keys(), postgres_plugin_get_replication(), and postgres_plugin_get_zero_anonymity().

Here is the caller graph for this function:

◆ GNUNET_PQ_eval_prepared_singleton_select()

enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select ( struct GNUNET_PQ_Context db,
const char *  statement_name,
const struct GNUNET_PQ_QueryParam params,
struct GNUNET_PQ_ResultSpec rs 
)

Execute a named prepared statement that is a SELECT statement which must return a single result in connection using the given params.

Stores the result (if any) in rs, which the caller must then clean up using GNUNET_PQ_cleanup_result() if the return value was GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the resulting session status.

Parameters
dbdatabase to execute the statement with
statement_namename of the statement
paramsparameters to give to the statement (GNUNET_PQ_query_param_end-terminated)
[in,out]rsresult specification to use for storing the result of the query
Returns
status code from the result, mapping PQ status codes to enum GNUNET_DB_QueryStatus.

Definition at line 1 of file pq_eval.c.

204 {
205  PGresult *result;
206  enum GNUNET_DB_QueryStatus qs;
207  int ntuples;
208 
210  statement_name,
211  params);
212  if (NULL == result)
215  statement_name,
216  result);
217  if (qs < 0)
218  {
219  PQclear (result);
220  return qs;
221  }
222  ntuples = PQntuples (result);
223  if (0 == ntuples)
224  {
225  PQclear (result);
227  }
228  if (1 != ntuples)
229  {
230  /* more than one result, but there must be at most one */
231  GNUNET_break (0);
232  PQclear (result);
234  }
235  if (GNUNET_OK !=
237  rs,
238  0))
239  {
240  PQclear (result);
242  }
243  PQclear (result);
245 }
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:60
enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result(PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
Extract results from a query result according to the given specification.
Definition: pq.c:149

Referenced by postgres_plugin_estimate_size().

Here is the caller graph for this function:

◆ GNUNET_PQ_make_prepare()

struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare ( const char *  name,
const char *  sql 
)

Create a struct GNUNET_PQ_PreparedStatement.

Parameters
namename of the statement
sqlactual SQL statement
Returns
initialized struct

Definition at line 1 of file pq_prepare.c.

32 {
33  struct GNUNET_PQ_PreparedStatement ps = {
34  .name = name,
35  .sql = sql
36  };
37 
38  return ps;
39 }
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
const char * name
Name of the statement.
const char * sql
Actual SQL statement.

References name, GNUNET_PQ_PreparedStatement::name, and GNUNET_PQ_PreparedStatement::sql.

◆ GNUNET_PQ_prepare_statements()

enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements ( struct GNUNET_PQ_Context db,
const struct GNUNET_PQ_PreparedStatement ps 
)

Request creation of prepared statements ps from Postgres.

Parameters
dbdatabase to prepare the statements for
psGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared statements.
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1 of file pq_prepare.c.

90 {
91  if (db->ps != ps)
92  {
93  /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
94  unsigned int nlen = 0; /* length of 'ps' array */
95  unsigned int xlen;
96  struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
97 
98  while (NULL != ps[nlen].name)
99  nlen++;
100  xlen = nlen + db->ps_off;
101  if (xlen > db->ps_len)
102  {
103  xlen = 2 * xlen + 1;
104  rps = GNUNET_new_array (xlen,
106  if (NULL != db->ps)
107  memcpy (rps,
108  db->ps,
109  db->ps_off * sizeof (struct GNUNET_PQ_PreparedStatement));
110  GNUNET_free (db->ps);
111  db->ps_len = xlen;
112  db->ps = rps;
113  }
114  memcpy (&db->ps[db->ps_off],
115  ps,
116  nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
117  db->ps_off += nlen;
118  }
119 
120  return GNUNET_PQ_prepare_once (db,
121  ps);
122 }
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_once(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres, but do not automatically re-prepare the sta...
Definition: pq_prepare.c:43

Referenced by GNUNET_PQ_reconnect().

Here is the caller graph for this function:

◆ GNUNET_PQ_prepare_once()

enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_once ( struct GNUNET_PQ_Context db,
const struct GNUNET_PQ_PreparedStatement ps 
)

Request creation of prepared statements ps from Postgres, but do not automatically re-prepare the statement if we are disconnected from the database.

Parameters
dbdatabase to prepare the statements for
psGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared statements.
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1 of file pq_prepare.c.

45 {
46  for (unsigned int i = 0; NULL != ps[i].name; i++)
47  {
48  PGresult *ret;
49 
51  "pq",
52  "Preparing SQL statement `%s' as `%s'\n",
53  ps[i].sql,
54  ps[i].name);
55  ret = PQprepare (db->conn,
56  ps[i].name,
57  ps[i].sql,
58  0,
59  NULL);
60  if (PGRES_COMMAND_OK != PQresultStatus (ret))
61  {
63  "pq",
64  "PQprepare (`%s' as `%s') failed with error: %s\n",
65  ps[i].sql,
66  ps[i].name,
67  PQerrorMessage (db->conn));
68  PQclear (ret);
69  ret = PQdescribePrepared (db->conn,
70  ps[i].name);
71  if (PGRES_COMMAND_OK != PQresultStatus (ret))
72  {
73  PQclear (ret);
74  return GNUNET_SYSERR;
75  }
77  "pq",
78  "Statement `%s' already known. Ignoring the issue in the hope that you are using connection pooling...\n",
79  ps[i].name);
80  }
81  PQclear (ret);
82  }
83  return GNUNET_OK;
84 }
@ GNUNET_ERROR_TYPE_BULK

◆ GNUNET_PQ_make_execute()

struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute ( const char *  sql)

Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal.

Parameters
sqlactual SQL statement
Returns
initialized struct

Definition at line 1 of file pq_exec.c.

31 {
32  struct GNUNET_PQ_ExecuteStatement es = {
33  .sql = sql,
34  .ignore_errors = GNUNET_NO
35  };
36 
37  return es;
38 }
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
const char * sql
Actual SQL statement.

Referenced by postgres_plugin_drop().

Here is the caller graph for this function:

◆ GNUNET_PQ_make_try_execute()

struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute ( const char *  sql)

Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated.

Parameters
sqlactual SQL statement
Returns
initialized struct

Definition at line 1 of file pq_exec.c.

43 {
44  struct GNUNET_PQ_ExecuteStatement es = {
45  .sql = sql,
46  .ignore_errors = GNUNET_YES
47  };
48 
49  return es;
50 }
@ GNUNET_YES

◆ GNUNET_PQ_exec_statements()

enum GNUNET_GenericReturnValue GNUNET_PQ_exec_statements ( struct GNUNET_PQ_Context db,
const struct GNUNET_PQ_ExecuteStatement es 
)

Request execution of an array of statements es from Postgres.

Parameters
dbdatabase to execute the statements in
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 pq_exec.c.

56 {
57  for (unsigned int i = 0; NULL != es[i].sql; i++)
58  {
59  PGresult *result;
60 
62  "Running statement `%s' on %p\n",
63  es[i].sql,
64  db);
65  result = PQexec (db->conn,
66  es[i].sql);
68  "Running statement `%s' on %p finished (%s)\n",
69  es[i].sql,
70  db,
71  PQresStatus (PQresultStatus (result)));
72  if ((GNUNET_NO == es[i].ignore_errors) &&
73  (PGRES_COMMAND_OK != PQresultStatus (result)))
74  {
76  "pq",
77  "Failed to execute `%s': %s/%s/%s/%s/%s",
78  es[i].sql,
79  PQresultErrorField (result,
80  PG_DIAG_MESSAGE_PRIMARY),
81  PQresultErrorField (result,
82  PG_DIAG_MESSAGE_DETAIL),
83  PQresultErrorMessage (result),
84  PQresStatus (PQresultStatus (result)),
85  PQerrorMessage (db->conn));
86  PQclear (result);
87  return GNUNET_SYSERR;
88  }
89  PQclear (result);
90  }
91  return GNUNET_OK;
92 }

References GNUNET_NO, and GNUNET_PQ_ExecuteStatement::sql.

Referenced by GNUNET_PQ_reconnect(), and postgres_plugin_drop().

Here is the caller graph for this function:

◆ GNUNET_PQ_connect()

struct GNUNET_PQ_Context* GNUNET_PQ_connect ( const char *  config_str,
const char *  load_path,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps 
)

Create a connection to the Postgres database using config_str for the configuration.

Initialize logging via GNUnet's log routines and disable Postgres's logger. Also ensures that the statements in load_path and es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in es fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.

In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.

Parameters
config_strconfiguration to use
load_pathpath to directory with SQL transactions to run, can be NULL
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
Returns
NULL on error

Definition at line 69 of file pq_connect.c.

73 {
74  return GNUNET_PQ_connect2 (config_str,
75  load_path,
76  NULL == load_path
77  ? NULL
78  : "",
79  es,
80  ps,
82 }
struct GNUNET_PQ_Context * GNUNET_PQ_connect2(const char *config_str, const char *load_path, const char *auto_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
Create a connection to the Postgres database using config_str for the configuration.
Definition: pq_connect.c:86

References GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::es, GNUNET_PQ_connect2(), GNUNET_PQ_FLAG_NONE, GNUNET_PQ_Context::load_path, and GNUNET_PQ_Context::ps.

Here is the call graph for this function:

◆ GNUNET_PQ_exec_sql()

enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql ( struct GNUNET_PQ_Context db,
const char *  buf 
)

Execute SQL statements from buf against db.

The given filename infix in buf is prefixed with the "load_path" and ".sql" is appended to construct the full filename.

Parameters
dbdatabase context to use
buffilename infix (!) with the SQL code to run
Returns
GNUNET_OK on success, GNUNET_NO if patch buf does not exist, GNUNET_SYSERR on error

Definition at line 86 of file pq_connect.c.

146 {
147  struct GNUNET_OS_Process *psql;
149  unsigned long code;
151  char *fn;
152 
153  GNUNET_asprintf (&fn,
154  "%s%s.sql",
155  db->load_path,
156  buf);
157  if (GNUNET_YES !=
159  {
161  "SQL resource `%s' does not exist\n",
162  fn);
163  GNUNET_free (fn);
164  return GNUNET_NO;
165  }
167  "Applying SQL file `%s' on database %s\n",
168  fn,
169  db->config_str);
171  NULL,
172  NULL,
173  NULL,
174  "psql",
175  "psql",
176  db->config_str,
177  "-f",
178  fn,
179  "-q",
180  "--set",
181  "ON_ERROR_STOP=1",
182  NULL);
183  if (NULL == psql)
184  {
186  "exec",
187  "psql");
188  GNUNET_free (fn);
189  return GNUNET_SYSERR;
190  }
192  &type,
193  &code);
194  if (GNUNET_OK != ret)
195  {
197  "psql on file %s did not finish, killed it!\n",
198  fn);
199  /* can happen if we got a signal, like CTRL-C, before
200  psql was complete */
201  (void) GNUNET_OS_process_kill (psql,
202  SIGKILL);
204  GNUNET_free (fn);
205  return GNUNET_SYSERR;
206  }
208  if ( (GNUNET_OS_PROCESS_EXITED != type) ||
209  (0 != code) )
210  {
212  "Could not run PSQL on file %s: psql exit code was %d\n",
213  fn,
214  (int) code);
215  GNUNET_free (fn);
216  return GNUNET_SYSERR;
217  }
218  GNUNET_free (fn);
219  return GNUNET_OK;
220 }
static char buf[2048]
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:482
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process, waiting on it if dead.
Definition: os_priority.c:862
struct GNUNET_OS_Process * GNUNET_OS_start_process(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename,...)
Start a process.
Definition: os_priority.c:620
GNUNET_OS_ProcessStatusType
Process status types.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:260
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
Definition: gnunet_os_lib.h:77
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

Referenced by GNUNET_PQ_reconnect(), and libgnunet_plugin_datacache_postgres_done().

Here is the caller graph for this function:

◆ GNUNET_PQ_connect2()

struct GNUNET_PQ_Context* GNUNET_PQ_connect2 ( const char *  config_str,
const char *  load_path,
const char *  auto_suffix,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps,
enum GNUNET_PQ_Options  flags 
)

Create a connection to the Postgres database using config_str for the configuration.

Initialize logging via GNUnet's log routines and disable Postgres's logger. Also ensures that the statements in load_path and es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in es fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.

In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.

Parameters
config_strconfiguration to use
load_pathpath to directory with SQL transactions to run, can be NULL
auto_suffixinfix of SQL series to run on every reconnect; runs multiple (!) files, of the form auto_suffix-XXXX where XXXX is from 0 to 9999 (consequtive).
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
flagsconnection flags
Returns
NULL on error

Definition at line 86 of file pq_connect.c.

92 {
93  struct GNUNET_PQ_Context *db;
94  unsigned int elen = 0;
95  unsigned int plen = 0;
96 
97  if (NULL != es)
98  while (NULL != es[elen].sql)
99  elen++;
100  if (NULL != ps)
101  while (NULL != ps[plen].name)
102  plen++;
103 
104  db = GNUNET_new (struct GNUNET_PQ_Context);
105  db->flags = flags;
106  db->config_str = GNUNET_strdup (config_str);
107  if (NULL != load_path)
108  db->load_path = GNUNET_strdup (load_path);
109  if (NULL != auto_suffix)
110  db->auto_suffix = GNUNET_strdup (auto_suffix);
111  if (0 != elen)
112  {
113  db->es = GNUNET_new_array (elen + 1,
115  memcpy (db->es,
116  es,
117  elen * sizeof (struct GNUNET_PQ_ExecuteStatement));
118  }
119  if (0 != plen)
120  {
121  db->ps = GNUNET_new_array (plen + 1,
123  memcpy (db->ps,
124  ps,
125  plen * sizeof (struct GNUNET_PQ_PreparedStatement));
126  }
127  db->channel_map = GNUNET_CONTAINER_multishortmap_create (16,
128  GNUNET_YES);
130  if (NULL == db->conn)
131  {
133  GNUNET_free (db->load_path);
134  GNUNET_free (db->auto_suffix);
135  GNUNET_free (db->config_str);
136  GNUNET_free (db);
137  return NULL;
138  }
139  return db;
140 }
struct GNUNET_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:424
Handle to Postgres database.
Definition: pq.h:36
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:45
enum GNUNET_PQ_Options flags
Flags controlling the connection.
Definition: pq.h:100
char * load_path
Path to load SQL files from.
Definition: pq.h:70
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:50
char * config_str
Configuration to use to connect to the DB.
Definition: pq.h:65
char * auto_suffix
Suffix to append to path to load on startup.
Definition: pq.h:75

References GNUNET_PQ_Context::auto_suffix, GNUNET_PQ_Context::config_str, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_CONTAINER_multishortmap_create(), GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_free, GNUNET_new, GNUNET_new_array, GNUNET_PQ_reconnect(), GNUNET_strdup, GNUNET_YES, GNUNET_PQ_Context::load_path, name, and GNUNET_PQ_Context::ps.

Referenced by GNUNET_PQ_connect(), and GNUNET_PQ_connect_with_cfg2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_connect_with_cfg()

struct GNUNET_PQ_Context* GNUNET_PQ_connect_with_cfg ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section,
const char *  load_path_suffix,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps 
)

Connect to a postgres database using the configuration option "CONFIG" in section.

Also ensures that the statements in es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready".

The caller does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
load_path_suffixsuffix to append to the SQL_DIR in the configuration
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
Returns
the postgres handle, NULL on error

Definition at line 587 of file pq_connect.c.

592 {
594  section,
595  load_path_suffix,
596  es,
597  ps,
599 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg2(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:603

References cfg, GNUNET_PQ_Context::es, GNUNET_PQ_connect_with_cfg2(), GNUNET_PQ_FLAG_NONE, and GNUNET_PQ_Context::ps.

Here is the call graph for this function:

◆ GNUNET_PQ_connect_with_cfg2()

struct GNUNET_PQ_Context* GNUNET_PQ_connect_with_cfg2 ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section,
const char *  load_path_suffix,
const struct GNUNET_PQ_ExecuteStatement es,
const struct GNUNET_PQ_PreparedStatement ps,
enum GNUNET_PQ_Options  flags 
)

Connect to a postgres database using the configuration option "CONFIG" in section.

Also ensures that the statements in es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready".

The caller does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
load_path_suffixsuffix to append to the SQL_DIR in the configuration
esGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of statements to execute upon EACH connection, can be NULL
psarray of prepared statements to prepare, can be NULL
flagsconnection flags
Returns
the postgres handle, NULL on error

Definition at line 603 of file pq_connect.c.

609 {
610  struct GNUNET_PQ_Context *db;
611  char *conninfo;
612  char *load_path;
613 
614  if (GNUNET_OK !=
616  section,
617  "CONFIG",
618  &conninfo))
619  conninfo = NULL;
620  load_path = NULL;
621  if (GNUNET_OK !=
623  section,
624  "SQL_DIR",
625  &load_path))
626  {
628  section,
629  "SQL_DIR");
630  }
631  if ( (NULL != load_path_suffix) &&
632  (NULL == load_path) )
633  {
635  section,
636  "SQL_DIR");
637  return NULL;
638  }
639  db = GNUNET_PQ_connect2 (conninfo == NULL ? "" : conninfo,
640  load_path,
641  load_path_suffix,
642  es,
643  ps,
644  flags);
646  GNUNET_free (conninfo);
647  return db;
648 }
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.

References cfg, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log_config_missing(), GNUNET_OK, GNUNET_PQ_connect2(), GNUNET_PQ_Context::load_path, and GNUNET_PQ_Context::ps.

Referenced by GNUNET_PQ_connect_with_cfg().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_reconnect_if_down()

void GNUNET_PQ_reconnect_if_down ( struct GNUNET_PQ_Context db)

Reinitialize the database db if the connection is down.

Parameters
dbdatabase connection to reinitialize

Definition at line 314 of file pq_connect.c.

315 {
316  if (1 ==
317  PQconsumeInput (db->conn))
318  return;
319  if (CONNECTION_BAD != PQstatus (db->conn))
320  return;
322 }

◆ GNUNET_PQ_reconnect()

void GNUNET_PQ_reconnect ( struct GNUNET_PQ_Context db)

Reinitialize the database db.

Parameters
dbdatabase connection to reinitialize

Definition at line 424 of file pq_connect.c.

425 {
427  -1);
428  if (NULL != db->conn)
429  PQfinish (db->conn);
430  db->conn = PQconnectdb (db->config_str);
431  if ( (NULL == db->conn) ||
432  (CONNECTION_OK != PQstatus (db->conn)) )
433  {
435  "pq",
436  "Database connection to '%s' failed: %s\n",
437  db->config_str,
438  (NULL != db->conn)
439  ? PQerrorMessage (db->conn)
440  : "PQconnectdb returned NULL");
441  if (NULL != db->conn)
442  {
443  PQfinish (db->conn);
444  db->conn = NULL;
445  }
446  return;
447  }
448  PQsetNoticeReceiver (db->conn,
450  db);
451  PQsetNoticeProcessor (db->conn,
453  db);
454  if (NULL != db->load_path)
455  {
456  PGresult *res;
457  ExecStatusType est;
458 
459  res = PQexec (db->conn,
460  "SELECT"
461  " schema_name"
462  " FROM information_schema.schemata"
463  " WHERE schema_name='_v';");
464  est = PQresultStatus (res);
465  if ( (PGRES_COMMAND_OK != est) &&
466  (PGRES_TUPLES_OK != est) )
467  {
469  "Failed to run statement to check versioning schema. Bad!\n");
470  PQclear (res);
471  PQfinish (db->conn);
472  db->conn = NULL;
473  return;
474  }
475  if (0 == PQntuples (res))
476  {
478 
479  PQclear (res);
480  if (0 != (db->flags & GNUNET_PQ_FLAG_DROP))
481  {
483  "Versioning schema does not exist yet. Not attempting drop!\n");
484  PQfinish (db->conn);
485  db->conn = NULL;
486  return;
487  }
489  "versioning");
490  if (GNUNET_NO == ret)
491  {
493  "Failed to find SQL file to load database versioning logic\n");
494  PQfinish (db->conn);
495  db->conn = NULL;
496  return;
497  }
498  if (GNUNET_SYSERR == ret)
499  {
501  "Failed to run SQL logic to setup database versioning logic\n");
502  PQfinish (db->conn);
503  db->conn = NULL;
504  return;
505  }
506  }
507  else
508  {
509  PQclear (res);
510  }
511  }
512 
513  /* Retrieve the OIDs for the supported Array types */
515  {
517  "Failed to retrieve OID information for array types!\n");
518  PQfinish (db->conn);
519  db->conn = NULL;
520  return;
521  }
522 
523  if (NULL != db->auto_suffix)
524  {
525  PGresult *res;
526 
527  GNUNET_assert (NULL != db->load_path);
528  res = PQprepare (db->conn,
529  "gnunet_pq_check_patch",
530  "SELECT"
531  " applied_by"
532  " FROM _v.patches"
533  " WHERE patch_name = $1"
534  " LIMIT 1",
535  1,
536  NULL);
537  if (PGRES_COMMAND_OK != PQresultStatus (res))
538  {
540  "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
541  PQresultErrorMessage (res),
542  PQerrorMessage (db->conn));
543  PQclear (res);
544  PQfinish (db->conn);
545  db->conn = NULL;
546  return;
547  }
548  PQclear (res);
549 
550  if (GNUNET_SYSERR ==
552  db->auto_suffix))
553  {
555  "Failed to load SQL statements from `%s*'\n",
556  db->auto_suffix);
557  PQfinish (db->conn);
558  db->conn = NULL;
559  return;
560  }
561  }
562 
563  if ( (NULL != db->es) &&
564  (GNUNET_OK !=
566  db->es)) )
567  {
568  PQfinish (db->conn);
569  db->conn = NULL;
570  return;
571  }
572  if ( (NULL != db->ps) &&
573  (GNUNET_OK !=
575  db->ps)) )
576  {
577  PQfinish (db->conn);
578  db->conn = NULL;
579  return;
580  }
582  PQsocket (db->conn));
583 }
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: pq_exec.c:54
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition: pq_prepare.c:88
void GNUNET_PQ_event_reconnect_(struct GNUNET_PQ_Context *db, int fd)
Internal API.
Definition: pq_event.c:412
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_path)
Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the fi...
Definition: pq_connect.c:224
static void pq_notice_receiver_cb(void *arg, const PGresult *res)
Function called by libpq whenever it wants to log something.
Definition: pq_connect.c:40
static enum GNUNET_GenericReturnValue get_array_type_oids(struct GNUNET_PQ_Context *db)
Retrieves the Oid's for the supported array types and sets db->arraytype2oid on succes.
Definition: pq_connect.c:333
static void pq_notice_processor_cb(void *arg, const char *message)
Function called by libpq whenever it wants to log something.
Definition: pq_connect.c:57
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql(struct GNUNET_PQ_Context *db, const char *buf)
Execute SQL statements from buf against db.
Definition: pq_connect.c:144

References db, get_array_type_oids(), GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_PQ_event_reconnect_(), GNUNET_PQ_exec_sql(), GNUNET_PQ_exec_statements(), GNUNET_PQ_FLAG_DROP, GNUNET_PQ_prepare_statements(), GNUNET_PQ_run_sql(), GNUNET_SYSERR, pq_notice_processor_cb(), pq_notice_receiver_cb(), res, and ret.

Referenced by do_scheduler_notify(), event_do_poll(), get_array_type_oids(), GNUNET_PQ_connect2(), and GNUNET_PQ_exec_prepared().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_event_listen()

struct GNUNET_DB_EventHandler* GNUNET_PQ_event_listen ( struct GNUNET_PQ_Context db,
const struct GNUNET_DB_EventHeaderP es,
struct GNUNET_TIME_Relative  timeout,
GNUNET_DB_EventCallback  cb,
void *  cb_cls 
)

Register callback to be invoked on events of type es.

Unlike many other calls, this function is thread-safe and may be called from threads that are different from the one that setup db. However, the cb will always be called from the thread that runs #GNUNET_PQ_event_do_poll() or the GNUnet scheduler.

Parameters
dbdatabase context to use
esspecification of the event to listen for
timeoutwhen to trigger cb based on timeout
cbfunction to call when the event happens, possibly multiple times (until GNUNET_PQ_event_listen_cancel() is invoked), including on timeout
cb_clsclosure for cb
Returns
handle useful to cancel the listener

Definition at line 445 of file pq_event.c.

450 {
451  struct GNUNET_DB_EventHandler *eh;
452  bool sub;
453 
454  eh = GNUNET_new (struct GNUNET_DB_EventHandler);
455  eh->db = db;
456  es_to_sh (es,
457  &eh->sh);
458  eh->cb = cb;
459  eh->cb_cls = cb_cls;
460  sub = (NULL ==
462  &eh->sh));
465  &eh->sh,
466  eh,
468  if (NULL == db->event_task)
469  {
471  "Starting event scheduler\n");
473  PQsocket (db->conn));
474  }
475  if (sub)
477  "LISTEN X",
478  eh);
480  &event_timeout,
481  eh);
482  return eh;
483 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multishortmap_put(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void * GNUNET_CONTAINER_multishortmap_get(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Given a key find a value in the map matching the key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1272
static void es_to_sh(const struct GNUNET_DB_EventHeaderP *es, struct GNUNET_ShortHashCode *sh)
Convert es to a short hash.
Definition: pq_event.c:69
static void event_timeout(void *cls)
Function run on timeout for an event.
Definition: pq_event.c:433
static void manage_subscribe(struct GNUNET_PQ_Context *db, const char *cmd, struct GNUNET_DB_EventHandler *eh)
Helper function to trigger an SQL cmd on db.
Definition: pq_event.c:351
static void scheduler_fd_cb(void *cls, int fd)
Function called when the Postgres FD changes and we need to update the scheduler event loop task.
Definition: pq_event.c:313
Handle for an active LISTENer to the database.
Definition: pq_event.c:34
void * cb_cls
Closure for cb.
Definition: pq_event.c:48
struct GNUNET_SCHEDULER_Task * timeout_task
Task to run on timeout.
Definition: pq_event.c:58
struct GNUNET_ShortHashCode sh
Channel name.
Definition: pq_event.c:38
struct GNUNET_PQ_Context * db
Database context this event handler is with.
Definition: pq_event.c:53
GNUNET_DB_EventCallback cb
Function to call on events.
Definition: pq_event.c:43

References GNUNET_DB_EventHandler::cb, GNUNET_DB_EventHandler::cb_cls, db, GNUNET_DB_EventHandler::db, es_to_sh(), event_timeout(), GNUNET_assert, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE, GNUNET_CONTAINER_multishortmap_get(), GNUNET_CONTAINER_multishortmap_put(), GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), manage_subscribe(), scheduler_fd_cb(), GNUNET_DB_EventHandler::sh, timeout, and GNUNET_DB_EventHandler::timeout_task.

Here is the call graph for this function:

◆ GNUNET_PQ_event_listen_cancel()

void GNUNET_PQ_event_listen_cancel ( struct GNUNET_DB_EventHandler eh)

Stop notifications.

Unlike many other calls, this function is thread-safe and may be called from threads that are different from the one that setup db. However, the cb will always be called from the thread that runs #GNUNET_PQ_event_do_poll() or the GNUnet scheduler.

Parameters
ehhandle to unregister.

Definition at line 487 of file pq_event.c.

488 {
489  struct GNUNET_PQ_Context *db = eh->db;
490 
493  &eh->sh,
494  eh));
495  if (NULL ==
497  &eh->sh))
499  "UNLISTEN X",
500  eh);
501  if (0 == GNUNET_CONTAINER_multishortmap_size (db->channel_map))
502  {
504  "Stopping PQ event scheduler job\n");
505  GNUNET_free (db->rfd);
506  if (NULL != db->event_task)
507  {
508  GNUNET_SCHEDULER_cancel (db->event_task);
509  db->event_task = NULL;
510  }
511  }
512  if (NULL != eh->timeout_task)
513  {
515  eh->timeout_task = NULL;
516  }
517  GNUNET_free (eh);
518 }
unsigned int GNUNET_CONTAINER_multishortmap_size(const struct GNUNET_CONTAINER_MultiShortmap *map)
Get the number of key-value pairs in the map.
int GNUNET_CONTAINER_multishortmap_remove(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, const void *value)
Remove the given key-value pair from the map.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975

References db, GNUNET_DB_EventHandler::db, GNUNET_assert, GNUNET_CONTAINER_multishortmap_get(), GNUNET_CONTAINER_multishortmap_remove(), GNUNET_CONTAINER_multishortmap_size(), GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_SCHEDULER_cancel(), manage_subscribe(), GNUNET_DB_EventHandler::sh, and GNUNET_DB_EventHandler::timeout_task.

Here is the call graph for this function:

◆ GNUNET_PQ_event_notify()

void GNUNET_PQ_event_notify ( struct GNUNET_PQ_Context db,
const struct GNUNET_DB_EventHeaderP es,
const void *  extra,
size_t  extra_size 
)

Notify all that listen on es of an event.

Unlike many other calls, this function is thread-safe and may be called from threads that are different from the one that setup db. However, the cb will always be called from the thread that runs #GNUNET_PQ_event_do_poll() or the GNUnet scheduler.

Parameters
dbdatabase context to use
esspecification of the event to generate
extraadditional event data provided
extra_sizenumber of bytes in extra

Definition at line 537 of file pq_event.c.

541 {
542  char sql[16 + 64 + extra_size * 8 / 5 + 8];
543  char *end;
544  PGresult *result;
545 
546  end = stpcpy (sql,
547  "NOTIFY X");
548  end = es_to_channel (es,
549  end);
550  end = stpcpy (end,
551  ", '");
553  extra_size,
554  end,
555  sizeof (sql) - (end - sql) - 1);
556  GNUNET_assert (NULL != end);
557  *end = '\0';
558  end = stpcpy (end,
559  "'");
561  "Executing command `%s'\n",
562  sql);
563  result = PQexec (db->conn,
564  sql);
565  if (PGRES_COMMAND_OK != PQresultStatus (result))
566  {
568  "pq",
569  "Failed to execute `%s': %s/%s/%s/%s/%s",
570  sql,
571  PQresultErrorField (result,
572  PG_DIAG_MESSAGE_PRIMARY),
573  PQresultErrorField (result,
574  PG_DIAG_MESSAGE_DETAIL),
575  PQresultErrorMessage (result),
576  PQresStatus (PQresultStatus (result)),
577  PQerrorMessage (db->conn));
578  }
579  PQclear (result);
580  event_do_poll (db);
581 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:708
static char * es_to_channel(const struct GNUNET_DB_EventHeaderP *es, char identifier[64])
Convert es to a Postgres identifier.
Definition: pq_event.c:135
static void event_do_poll(struct GNUNET_PQ_Context *db)
Definition: pq_event.c:189

◆ GNUNET_PG_get_event_notify_channel()

char* GNUNET_PG_get_event_notify_channel ( const struct GNUNET_DB_EventHeaderP es)

Compute the channel that one should notify upon for the given event specification.

Parameters
esevent specification
Returns
channel to notify upon

Definition at line 522 of file pq_event.c.

523 {
524  char sql[16 + 64 + 8];
525  char *end;
526 
527  end = stpcpy (sql,
528  "X");
529  end = es_to_channel (es,
530  end);
531  GNUNET_assert (NULL != end);
532  return GNUNET_strdup (sql);
533 }

References end, GNUNET_PQ_Context::es, es_to_channel(), GNUNET_assert, and GNUNET_strdup.

Here is the call graph for this function:

◆ GNUNET_PQ_run_sql()

enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql ( struct GNUNET_PQ_Context db,
const char *  load_path 
)

Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the files exist contiguously).

Parameters
dbdatabase context to use
load_pathwhere to find the XXXX.sql files
Returns
GNUNET_OK on success

Definition at line 86 of file pq_connect.c.

226 {
227  const char *load_path_suffix;
228  size_t slen = strlen (load_path) + 10;
229 
230  load_path_suffix = strrchr (load_path, '/');
231  if (NULL == load_path_suffix)
232  load_path_suffix = load_path;
233  else
234  load_path_suffix++; /* skip '/' */
236  "Loading SQL resources from `%s'\n",
237  load_path);
238  for (unsigned int i = 1; i<10000; i++)
239  {
240  char patch_name[slen];
241  enum GNUNET_DB_QueryStatus qs;
242 
243  /* Check with DB versioning schema if this patch was already applied,
244  if so, skip it. */
245  GNUNET_snprintf (patch_name,
246  sizeof (patch_name),
247  "%s%04u",
248  load_path_suffix,
249  i);
250  {
251  char *applied_by;
252  struct GNUNET_PQ_QueryParam params[] = {
253  GNUNET_PQ_query_param_string (patch_name),
255  };
256  struct GNUNET_PQ_ResultSpec rs[] = {
257  GNUNET_PQ_result_spec_string ("applied_by",
258  &applied_by),
260  };
261 
263  "gnunet_pq_check_patch",
264  params,
265  rs);
267  {
269  "Database version %s already applied by %s, skipping\n",
270  patch_name,
271  applied_by);
273  }
274  if (GNUNET_DB_STATUS_HARD_ERROR == qs)
275  {
276  GNUNET_break (0);
277  return GNUNET_SYSERR;
278  }
279  }
281  continue; /* patch already applied, skip it */
282 
283  if (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
284  {
285  /* We are only checking, found unapplied patch, bad! */
287  "Database outdated, patch %s missing. Aborting!\n",
288  patch_name);
289  return GNUNET_SYSERR;
290  }
291  else
292  {
293  /* patch not yet applied, run it! */
295 
296  GNUNET_snprintf (patch_name,
297  sizeof (patch_name),
298  "%s%04u",
299  load_path,
300  i);
302  patch_name);
303  if (GNUNET_NO == ret)
304  break;
305  if (GNUNET_SYSERR == ret)
306  return GNUNET_SYSERR;
307  }
308  }
309  return GNUNET_OK;
310 }
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition: pq_eval.c:199
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
Definition: pq.c:139
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.

Referenced by GNUNET_PQ_reconnect().

Here is the caller graph for this function:

◆ GNUNET_PQ_disconnect()

void GNUNET_PQ_disconnect ( struct GNUNET_PQ_Context db)

Disconnect from the database, destroying the prepared statements and releasing other associated resources.

Parameters
dbdatabase handle to disconnect (will be free'd)

Definition at line 652 of file pq_connect.c.

653 {
654  if (NULL == db)
655  return;
656  GNUNET_assert (0 ==
657  GNUNET_CONTAINER_multishortmap_size (db->channel_map));
659  GNUNET_free (db->es);
660  GNUNET_free (db->ps);
661  GNUNET_free (db->load_path);
662  GNUNET_free (db->auto_suffix);
663  GNUNET_free (db->config_str);
664  PQfinish (db->conn);
665  GNUNET_free (db);
666 }

References db, GNUNET_assert, GNUNET_CONTAINER_multishortmap_destroy(), GNUNET_CONTAINER_multishortmap_size(), and GNUNET_free.

Referenced by database_shutdown(), libgnunet_plugin_datacache_postgres_done(), and libgnunet_plugin_datastore_postgres_done().

Here is the call graph for this function:
Here is the caller graph for this function: