GNUnet  0.20.0
gnunet_pq_lib.h File Reference

helper functions for Postgres DB interactions More...

#include <libpq-fe.h>
#include <stdint.h>
#include "gnunet_common.h"
#include "gnunet_time_lib.h"
#include "gnunet_util_lib.h"
#include "gnunet_db_lib.h"
#include "postgres_ext.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_ArrayHeader_P
 The header for a postgresql array in binary format. 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_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...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_get_oid_by_name (struct GNUNET_PQ_Context *db, const char *name, Oid *oid)
 Returns the oid for a given datatype by name. More...
 
struct GNUNET_PQ_ArrayHeader_P __attribute__ ((packed))
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bool (unsigned int num, const bool *elements, 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, 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, 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, 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, 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, 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[static num], size_t same_size, 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, 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[static num], 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, 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[], 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, 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[], 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, 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[], 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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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_do_poll (struct GNUNET_PQ_Context *db)
 Poll for events right now. 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...
 

Variables

uint32_t ndim
 
uint32_t has_null
 
uint32_t oid
 
uint32_t dim
 
uint32_t lbound
 
struct GNUNET_PQ_ResultSpec __attribute__
 

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 134 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, 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 319 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:
(const void **) (elements), \
sizeof(*(elements[0])), \
(db))
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes_same_size(unsigned int num, const void *elements[static num], size_t same_size, 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 352 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 396 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 687 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 750 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(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 1058 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 1255 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 1327 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 59 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 76 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 128 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 621 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 1178 of file gnunet_pq_lib.h.

Enumeration Type Documentation

◆ 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 1374 of file gnunet_pq_lib.h.

1375 {
1379  GNUNET_PQ_FLAG_NONE = 0,
1380 
1385  GNUNET_PQ_FLAG_DROP = 1,
1386 
1391 };
@ 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:83
void * conv_cls
Closure for conv.
Definition: gnunet_pq_lib.h:95
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_get_oid_by_name()

enum GNUNET_GenericReturnValue GNUNET_PQ_get_oid_by_name ( struct GNUNET_PQ_Context db,
const char *  name,
Oid *  oid 
)

Returns the oid for a given datatype by name.

Parameters
dbThe db-connection
nameThe name of the datatype
[out]oidThe OID of the datatype.
Returns
GNUNET_OK when the datatype was found, GNUNET_SYSERR otherwise

Definition at line 314 of file pq_connect.c.

330 {
331  /* Check if the entry is in the cache already */
332  for (unsigned int i = 0; i < db->oids.num; i++)
333  {
334  /* Pointer comparison */
335  if (name == db->oids.table[i].name)
336  {
337  *oid = db->oids.table[i].oid;
338  return GNUNET_OK;
339  }
340  }
341 
342  /* No entry found in cache, ask database */
343  {
344  enum GNUNET_DB_QueryStatus qs;
345  struct GNUNET_PQ_QueryParam params[] = {
348  };
349  struct GNUNET_PQ_ResultSpec spec[] = {
351  oid),
353  };
354 
355  GNUNET_assert (NULL != db);
356 
358  "gnunet_pq_get_oid_by_name",
359  params,
360  spec);
362  return GNUNET_SYSERR;
363  }
364 
365  /* Add the entry to the cache */
366  if (NULL == db->oids.table)
367  {
368  db->oids.table = GNUNET_new_array (8,
369  typeof(*db->oids.table));
370  db->oids.cap = 8;
371  db->oids.num = 0;
372  }
373 
374  if (db->oids.cap <= db->oids.num)
375  GNUNET_array_grow (db->oids.table,
376  db->oids.cap,
377  db->oids.cap + 8);
378 
379  db->oids.table[db->oids.num].name = name;
380  db->oids.table[db->oids.num].oid = *oid;
381  db->oids.num++;
382 
383  return GNUNET_OK;
384 }
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:37
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:60
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
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
#define GNUNET_PQ_query_param_end
End of query parameter specification.
uint32_t oid
Definition: gnunet_pq_lib.h:2
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.
@ GNUNET_OK
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
Description of a DB result cell.

◆ __attribute__()

struct GNUNET_PQ_ArrayHeader_P __attribute__ ( (packed)  )

◆ GNUNET_PQ_query_param_array_bool()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bool ( unsigned int  num,
const bool *  elements,
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.

977 {
978  Oid oid;
979 
982  "bool",
983  &oid));
984  return query_param_array_generic (num,
985  true,
986  elements,
987  NULL,
988  sizeof(bool),
990  oid);
991 }
enum GNUNET_GenericReturnValue GNUNET_PQ_get_oid_by_name(struct GNUNET_PQ_Context *db, const char *name, Oid *oid)
Returns the oid for a given datatype by name.
Definition: pq_connect.c:326
@ array_of_bool
Definition: pq.h:133
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,
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.

999 {
1000  Oid oid;
1001 
1004  "int2",
1005  &oid));
1006  return query_param_array_generic (num,
1007  true,
1008  elements,
1009  NULL,
1010  sizeof(uint16_t),
1012  oid);
1013 }
@ array_of_uint16
Definition: pq.h:134

◆ GNUNET_PQ_query_param_array_uint32()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint32 ( unsigned int  num,
const uint32_t *  elements,
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.

1021 {
1022  Oid oid;
1023 
1026  "int4",
1027  &oid));
1028  return query_param_array_generic (num,
1029  true,
1030  elements,
1031  NULL,
1032  sizeof(uint32_t),
1034  oid);
1035 }
@ array_of_uint32
Definition: pq.h:135

◆ GNUNET_PQ_query_param_array_uint64()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint64 ( unsigned int  num,
const uint64_t *  elements,
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.

1043 {
1044  Oid oid;
1045 
1048  "int8",
1049  &oid));
1050  return query_param_array_generic (num,
1051  true,
1052  elements,
1053  NULL,
1054  sizeof(uint64_t),
1056  oid);
1057 }
@ array_of_uint64
Definition: pq.h:136

◆ 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,
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.

1066 {
1067  Oid oid;
1068 
1071  "bytea",
1072  &oid));
1073  return query_param_array_generic (num,
1074  true,
1075  elements,
1076  sizes,
1077  0,
1078  array_of_byte,
1079  oid);
1080 }
@ array_of_byte
Definition: pq.h:137

◆ 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,
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.

1112 {
1113  Oid oid;
1114 
1117  "bytea",
1118  &oid));
1119  return query_param_array_generic (num,
1120  true,
1121  elements,
1122  NULL,
1123  same_size,
1124  array_of_byte,
1125  oid);
1126 }

◆ 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[static num],
size_t  same_size,
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.

1135 {
1136  Oid oid;
1137 
1140  "bytea",
1141  &oid));
1142  return query_param_array_generic (num,
1143  false,
1144  elements,
1145  NULL,
1146  same_size,
1147  array_of_byte,
1148  oid);
1149 }

◆ GNUNET_PQ_query_param_array_string()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_string ( unsigned int  num,
const char *  elements,
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.

1157 {
1158  Oid oid;
1159 
1162  "text",
1163  &oid));
1164  return query_param_array_generic (num,
1165  true,
1166  elements,
1167  NULL,
1168  0,
1170  oid);
1171 }
@ array_of_string
Definition: pq.h:138

◆ GNUNET_PQ_query_param_array_ptrs_string()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_string ( unsigned int  num,
const char *  elements[static num],
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.

1179 {
1180  Oid oid;
1181 
1184  "text",
1185  &oid));
1186  return query_param_array_generic (num,
1187  false,
1188  elements,
1189  NULL,
1190  0,
1192  oid);
1193 }

◆ 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,
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.

1201 {
1202  Oid oid;
1203 
1206  "int8",
1207  &oid));
1208  return query_param_array_generic (num,
1209  true,
1210  elements,
1211  NULL,
1212  sizeof(struct GNUNET_TIME_Absolute),
1214  oid);
1215 }
@ array_of_abs_time
Definition: pq.h:139
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[],
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.

1223 {
1224  Oid oid;
1225 
1228  "int8",
1229  &oid));
1230  return query_param_array_generic (num,
1231  false,
1232  elements,
1233  NULL,
1234  sizeof(struct GNUNET_TIME_Absolute),
1236  oid);
1237 }

◆ 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,
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.

1245 {
1246  Oid oid;
1247 
1250  "int8",
1251  &oid));
1252  return query_param_array_generic (num,
1253  true,
1254  elements,
1255  NULL,
1256  sizeof(struct GNUNET_TIME_Relative),
1258  oid);
1259 }
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[],
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.

1267 {
1268  Oid oid;
1269 
1272  "int8",
1273  &oid));
1274  return query_param_array_generic (num,
1275  false,
1276  elements,
1277  NULL,
1278  sizeof(struct GNUNET_TIME_Relative),
1280  oid);
1281 }

◆ 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,
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.

1289 {
1290  Oid oid;
1291 
1294  "int8",
1295  &oid));
1296  return query_param_array_generic (num,
1297  true,
1298  elements,
1299  NULL,
1300  sizeof(struct GNUNET_TIME_Timestamp),
1302  oid);
1303 }
@ array_of_timestamp
Definition: pq.h:141
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[],
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.

1311 {
1312  Oid oid;
1313 
1316  "int8",
1317  &oid));
1318  return query_param_array_generic (num,
1319  false,
1320  elements,
1321  NULL,
1322  sizeof(struct GNUNET_TIME_Timestamp),
1324  oid);
1325 }

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

37 {
38  struct GNUNET_PQ_ResultSpec rsr;
39 
40  rsr = rs;
41  rsr.is_nullable = true;
42  rsr.is_null = is_null;
43  return rsr;
44 }
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 55 of file pq_result_helper.c.

133 {
134  struct GNUNET_PQ_ResultSpec res = {
135  .conv = &extract_varsize_blob,
136  .cleaner = &clean_varsize_blob,
137  .dst = (void *) (dst),
138  .fname = name,
139  .result_size = sptr
140  };
141 
142  return res;
143 }
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 55 of file pq_result_helper.c.

216 {
217  struct GNUNET_PQ_ResultSpec res = {
218  .conv = &extract_fixed_blob,
219  .dst = (dst),
220  .dst_size = dst_size,
221  .fname = name
222  };
223 
224  return res;
225 }
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 485 of file pq_result_helper.c.

502 {
503  struct GNUNET_PQ_ResultSpec res = {
504  .conv = &extract_string,
505  .cleaner = &clean_string,
506  .dst = (void *) dst,
507  .fname = (name)
508  };
509 
510  return res;
511 }
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 485 of file pq_result_helper.c.

573 {
574  struct GNUNET_PQ_ResultSpec res = {
575  .conv = &extract_bool,
576  .dst = (void *) dst,
577  .fname = name
578  };
579 
580  return res;
581 }
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 295 of file pq_result_helper.c.

312 {
313  struct GNUNET_PQ_ResultSpec res = {
314  .conv = &extract_rsa_public_key,
315  .cleaner = &clean_rsa_public_key,
316  .dst = (void *) rsa,
317  .fname = name
318  };
319 
320  return res;
321 }
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 390 of file pq_result_helper.c.

407 {
408  struct GNUNET_PQ_ResultSpec res = {
409  .conv = &extract_rsa_signature,
410  .cleaner = &clean_rsa_signature,
411  .dst = (void *) sig,
412  .fname = name
413  };
414 
415  return res;
416 }
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 485 of file pq_result_helper.c.

726 {
727  struct GNUNET_PQ_ResultSpec res = {
728  .conv = &extract_abs_time,
729  .dst = (void *) at,
730  .dst_size = sizeof(*at),
731  .fname = name
732  };
733 
734  return res;
735 }
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 485 of file pq_result_helper.c.

827 {
828  struct GNUNET_PQ_ResultSpec res = {
829  .conv = &extract_timestamp,
830  .dst = (void *) at,
831  .dst_size = sizeof(*at),
832  .fname = name
833  };
834 
835  return res;
836 }
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 485 of file pq_result_helper.c.

649 {
650  struct GNUNET_PQ_ResultSpec res = {
651  .conv = &extract_rel_time,
652  .dst = (void *) rt,
653  .dst_size = sizeof(*rt),
654  .fname = name
655  };
656 
657  return res;
658 }
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 485 of file pq_result_helper.c.

741 {
742  struct GNUNET_PQ_ResultSpec res =
744  &at->abs_value_us__);
745 
746  return res;
747 }
#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 485 of file pq_result_helper.c.

880 {
881  struct GNUNET_PQ_ResultSpec res = {
882  .conv = &extract_timestamp_nbo,
883  .dst = (void *) at,
884  .dst_size = sizeof(*at),
885  .fname = name
886  };
887 
888  return res;
889 }
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 485 of file pq_result_helper.c.

954 {
955  struct GNUNET_PQ_ResultSpec res = {
956  .conv = &extract_uint16,
957  .dst = (void *) u16,
958  .dst_size = sizeof(*u16),
959  .fname = name
960  };
961 
962  return res;
963 }
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 485 of file pq_result_helper.c.

1028 {
1029  struct GNUNET_PQ_ResultSpec res = {
1030  .conv = &extract_uint32,
1031  .dst = (void *) u32,
1032  .dst_size = sizeof(*u32),
1033  .fname = name
1034  };
1035 
1036  return res;
1037 }
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 485 of file pq_result_helper.c.

1112 {
1113  struct GNUNET_PQ_ResultSpec res = {
1114  .conv = &extract_uint64,
1115  .dst = (void *) u64,
1116  .dst_size = sizeof(*u64),
1117  .fname = name
1118  };
1119 
1120  return res;
1121 }
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 ( 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 1412 of file pq_result_helper.c.

1436 {
1437  struct array_result_cls *info =
1438  GNUNET_new (struct array_result_cls);
1439 
1440  info->num = num;
1441  info->typ = array_of_bool;
1444  "bool",
1445  &info->oid));
1446 
1447  struct GNUNET_PQ_ResultSpec res = {
1448  .conv = extract_array_generic,
1449  .cleaner = array_cleanup,
1450  .dst = (void *) dst,
1451  .fname = name,
1452  .cls = info
1453  };
1454  return res;
1455 }
#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 ( 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 1412 of file pq_result_helper.c.

1464 {
1465  struct array_result_cls *info =
1466  GNUNET_new (struct array_result_cls);
1467 
1468  info->num = num;
1469  info->typ = array_of_uint16;
1472  "int2",
1473  &info->oid));
1474 
1475  struct GNUNET_PQ_ResultSpec res = {
1476  .conv = extract_array_generic,
1477  .cleaner = array_cleanup,
1478  .dst = (void *) dst,
1479  .fname = name,
1480  .cls = info
1481  };
1482  return res;
1483 }

◆ GNUNET_PQ_result_spec_array_uint32()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint32 ( 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 1412 of file pq_result_helper.c.

1492 {
1493  struct array_result_cls *info =
1494  GNUNET_new (struct array_result_cls);
1495 
1496  info->num = num;
1497  info->typ = array_of_uint32;
1500  "int4",
1501  &info->oid));
1502 
1503  struct GNUNET_PQ_ResultSpec res = {
1504  .conv = extract_array_generic,
1505  .cleaner = array_cleanup,
1506  .dst = (void *) dst,
1507  .fname = name,
1508  .cls = info
1509  };
1510  return res;
1511 }

◆ GNUNET_PQ_result_spec_array_uint64()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_uint64 ( 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 1412 of file pq_result_helper.c.

1520 {
1521  struct array_result_cls *info =
1522  GNUNET_new (struct array_result_cls);
1523 
1524  info->num = num;
1525  info->typ = array_of_uint64;
1528  "int8",
1529  &info->oid));
1530 
1531  struct GNUNET_PQ_ResultSpec res = {
1532  .conv = extract_array_generic,
1533  .cleaner = array_cleanup,
1534  .dst = (void *) dst,
1535  .fname = name,
1536  .cls = info
1537  };
1538  return res;
1539 }

◆ GNUNET_PQ_result_spec_array_abs_time()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_abs_time ( 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 1412 of file pq_result_helper.c.

1548 {
1549  struct array_result_cls *info =
1550  GNUNET_new (struct array_result_cls);
1551 
1552  info->num = num;
1553  info->typ = array_of_abs_time;
1556  "int8",
1557  &info->oid));
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_rel_time()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_rel_time ( 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 1412 of file pq_result_helper.c.

1576 {
1577  struct array_result_cls *info =
1578  GNUNET_new (struct array_result_cls);
1579 
1580  info->num = num;
1581  info->typ = array_of_rel_time;
1584  "int8",
1585  &info->oid));
1586 
1587  struct GNUNET_PQ_ResultSpec res = {
1588  .conv = extract_array_generic,
1589  .cleaner = array_cleanup,
1590  .dst = (void *) dst,
1591  .fname = name,
1592  .cls = info
1593  };
1594  return res;
1595 }
@ array_of_rel_time
Definition: pq.h:140

◆ GNUNET_PQ_result_spec_array_timestamp()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_timestamp ( 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 1412 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->typ = array_of_timestamp;
1612  "int8",
1613  &info->oid));
1614 
1615  struct GNUNET_PQ_ResultSpec res = {
1616  .conv = extract_array_generic,
1617  .cleaner = array_cleanup,
1618  .dst = (void *) dst,
1619  .fname = name,
1620  .cls = info
1621  };
1622  return res;
1623 }

◆ GNUNET_PQ_result_spec_array_variable_size()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_variable_size ( 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 1412 of file pq_result_helper.c.

1633 {
1634  struct array_result_cls *info =
1635  GNUNET_new (struct array_result_cls);
1636 
1637  info->num = num;
1638  info->sizes = sizes;
1639  info->typ = array_of_byte;
1642  "bytea",
1643  &info->oid));
1644 
1645  struct GNUNET_PQ_ResultSpec res = {
1646  .conv = extract_array_generic,
1647  .cleaner = array_cleanup,
1648  .dst = (void *) dst,
1649  .fname = name,
1650  .cls = info
1651  };
1652  return res;
1653 }

◆ GNUNET_PQ_result_spec_array_fixed_size()

struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_array_fixed_size ( 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 1412 of file pq_result_helper.c.

1663 {
1664  struct array_result_cls *info =
1665  GNUNET_new (struct array_result_cls);
1666 
1667  info->num = num;
1668  info->same_size = size;
1669  info->typ = array_of_byte;
1672  "bytea",
1673  &info->oid));
1674 
1675  struct GNUNET_PQ_ResultSpec res = {
1676  .conv = extract_array_generic,
1677  .cleaner = array_cleanup,
1678  .dst = (void *) dst,
1679  .fname = name,
1680  .cls = info
1681  };
1682  return res;
1683 }
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 ( 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 1412 of file pq_result_helper.c.

1692 {
1693  struct array_result_cls *info =
1694  GNUNET_new (struct array_result_cls);
1695 
1696  info->num = num;
1697  info->typ = array_of_string;
1700  "text",
1701  &info->oid));
1702 
1703  struct GNUNET_PQ_ResultSpec res = {
1704  .conv = extract_array_generic,
1705  .cleaner = array_cleanup,
1706  .dst = (void *) dst,
1707  .fname = name,
1708  .cls = info
1709  };
1710  return res;
1711 }

◆ 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:427
#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).
@ 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:87
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_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 }
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 }
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 }
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:427
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 619 of file pq_connect.c.

624 {
626  section,
627  load_path_suffix,
628  es,
629  ps,
631 }
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:635

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 635 of file pq_connect.c.

641 {
642  struct GNUNET_PQ_Context *db;
643  char *conninfo;
644  char *load_path;
645 
646  if (GNUNET_OK !=
648  section,
649  "CONFIG",
650  &conninfo))
651  conninfo = NULL;
652  load_path = NULL;
653  if (GNUNET_OK !=
655  section,
656  "SQL_DIR",
657  &load_path))
658  {
660  section,
661  "SQL_DIR");
662  }
663  if ( (NULL != load_path_suffix) &&
664  (NULL == load_path) )
665  {
667  section,
668  "SQL_DIR");
669  return NULL;
670  }
671  db = GNUNET_PQ_connect2 (conninfo == NULL ? "" : conninfo,
672  load_path,
673  load_path_suffix,
674  es,
675  ps,
676  flags);
678  GNUNET_free (conninfo);
679  return db;
680 }
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 427 of file pq_connect.c.

428 {
430  -1);
431  if (NULL != db->conn)
432  PQfinish (db->conn);
433  db->conn = PQconnectdb (db->config_str);
434  if ( (NULL == db->conn) ||
435  (CONNECTION_OK != PQstatus (db->conn)) )
436  {
438  "pq",
439  "Database connection to '%s' failed: %s\n",
440  db->config_str,
441  (NULL != db->conn)
442  ? PQerrorMessage (db->conn)
443  : "PQconnectdb returned NULL");
444  if (NULL != db->conn)
445  {
446  PQfinish (db->conn);
447  db->conn = NULL;
448  }
449  return;
450  }
451  PQsetNoticeReceiver (db->conn,
453  db);
454  PQsetNoticeProcessor (db->conn,
456  db);
457  if (NULL != db->load_path)
458  {
459  PGresult *res;
460  ExecStatusType est;
461 
462  res = PQexec (db->conn,
463  "SELECT"
464  " schema_name"
465  " FROM information_schema.schemata"
466  " WHERE schema_name='_v';");
467  est = PQresultStatus (res);
468  if ( (PGRES_COMMAND_OK != est) &&
469  (PGRES_TUPLES_OK != est) )
470  {
472  "Failed to run statement to check versioning schema. Bad!\n");
473  PQclear (res);
474  PQfinish (db->conn);
475  db->conn = NULL;
476  return;
477  }
478  if (0 == PQntuples (res))
479  {
481 
482  PQclear (res);
483  if (0 != (db->flags & GNUNET_PQ_FLAG_DROP))
484  {
486  "Versioning schema does not exist yet. Not attempting drop!\n");
487  PQfinish (db->conn);
488  db->conn = NULL;
489  return;
490  }
492  "versioning");
493  if (GNUNET_NO == ret)
494  {
496  "Failed to find SQL file to load database versioning logic\n");
497  PQfinish (db->conn);
498  db->conn = NULL;
499  return;
500  }
501  if (GNUNET_SYSERR == ret)
502  {
504  "Failed to run SQL logic to setup database versioning logic\n");
505  PQfinish (db->conn);
506  db->conn = NULL;
507  return;
508  }
509  }
510  else
511  {
512  PQclear (res);
513  }
514  }
515 
516  /* Prepare statement for OID lookup by name */
517  {
518  PGresult *res;
519 
520  res = PQprepare (db->conn,
521  "gnunet_pq_get_oid_by_name",
522  "SELECT"
523  " typname, oid"
524  " FROM pg_type"
525  " WHERE typname = $1"
526  " LIMIT 1",
527  1,
528  NULL);
529  if (PGRES_COMMAND_OK != PQresultStatus (res))
530  {
532  "Failed to run SQL statement prepare OID lookups: %s/%s\n",
533  PQresultErrorMessage (res),
534  PQerrorMessage (db->conn));
535  PQclear (res);
536  PQfinish (db->conn);
537  db->conn = NULL;
538  return;
539  }
540  PQclear (res);
541  }
542 
543  /* Reset the OID-cache and retrieve the OIDs for the supported Array types */
544  db->oids.num = 0;
546  {
548  "Failed to retrieve OID information for array types!\n");
549  PQfinish (db->conn);
550  db->conn = NULL;
551  return;
552  }
553 
554 
555  if (NULL != db->auto_suffix)
556  {
557  PGresult *res;
558 
559  GNUNET_assert (NULL != db->load_path);
560  res = PQprepare (db->conn,
561  "gnunet_pq_check_patch",
562  "SELECT"
563  " applied_by"
564  " FROM _v.patches"
565  " WHERE patch_name = $1"
566  " LIMIT 1",
567  1,
568  NULL);
569  if (PGRES_COMMAND_OK != PQresultStatus (res))
570  {
572  "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
573  PQresultErrorMessage (res),
574  PQerrorMessage (db->conn));
575  PQclear (res);
576  PQfinish (db->conn);
577  db->conn = NULL;
578  return;
579  }
580  PQclear (res);
581 
582  if (GNUNET_SYSERR ==
584  db->auto_suffix))
585  {
587  "Failed to load SQL statements from `%s*'\n",
588  db->auto_suffix);
589  PQfinish (db->conn);
590  db->conn = NULL;
591  return;
592  }
593  }
594 
595  if ( (NULL != db->es) &&
596  (GNUNET_OK !=
598  db->es)) )
599  {
600  PQfinish (db->conn);
601  db->conn = NULL;
602  return;
603  }
604  if ( (NULL != db->ps) &&
605  (GNUNET_OK !=
607  db->ps)) )
608  {
609  PQfinish (db->conn);
610  db->conn = NULL;
611  return;
612  }
614  PQsocket (db->conn));
615 }
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 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
static enum GNUNET_GenericReturnValue load_initial_oids(struct GNUNET_PQ_Context *db)
Load the initial set of OIDs for the supported array-datatypes.
Definition: pq_connect.c:396

References db, 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, load_initial_oids(), pq_notice_processor_cb(), pq_notice_receiver_cb(), res, and ret.

Referenced by do_scheduler_notify(), GNUNET_PQ_connect2(), GNUNET_PQ_event_do_poll(), GNUNET_PQ_exec_prepared(), and load_initial_oids().

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_do_poll()

void GNUNET_PQ_event_do_poll ( struct GNUNET_PQ_Context db)

Poll for events right now.

Useful if we may have triggered an event for ourselves. Not needed when using GNUNET_PQ_event_notify(), but useful when stored procedures may have triggered events. Does nothing if there are no events.

Parameters
[in,out]dbdatabase to check for events

Definition at line 189 of file pq_event.c.

190 {
191  PGnotify *n;
192  unsigned int cnt = 0;
193 
195  "PG poll job active\n");
196  if (1 !=
197  PQconsumeInput (db->conn))
198  {
200  "Failed to read from Postgres: %s\n",
201  PQerrorMessage (db->conn));
202  if (CONNECTION_BAD != PQstatus (db->conn))
203  return;
205  return;
206  }
207  while (NULL != (n = PQnotifies (db->conn)))
208  {
209  struct GNUNET_ShortHashCode sh;
210  struct NotifyContext ctx = {
211  .extra = NULL
212  };
213 
214  cnt++;
215  if ('X' != toupper ((int) n->relname[0]))
216  {
218  "Ignoring notification for unsupported channel identifier `%s'\n",
219  n->relname);
220  PQfreemem (n);
221  continue;
222  }
223  if (GNUNET_OK !=
224  channel_to_sh (&n->relname[1],
225  &sh))
226  {
228  "Ignoring notification for unsupported channel identifier `%s'\n",
229  n->relname);
230  PQfreemem (n);
231  continue;
232  }
233  if ( (NULL != n->extra) &&
234  (GNUNET_OK !=
236  strlen (n->extra),
237  &ctx.extra,
238  &ctx.extra_size)))
239  {
241  "Ignoring notification for unsupported extra data `%s' on channel `%s'\n",
242  n->extra,
243  n->relname);
244  PQfreemem (n);
245  continue;
246  }
248  "Received notification %s with extra data `%.*s'\n",
249  n->relname,
250  (int) ctx.extra_size,
251  (const char *) ctx.extra);
253  &sh,
254  &do_notify,
255  &ctx);
256  GNUNET_free (ctx.extra);
257  PQfreemem (n);
258  }
260  "PG poll job finishes after %u events\n",
261  cnt);
262 }
static struct SolverHandle * sh
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
int GNUNET_CONTAINER_multishortmap_get_multiple(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, GNUNET_CONTAINER_ShortmapIterator it, void *it_cls)
Iterate over all entries in the map that match a particular key.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc(const char *enc, size_t enclen, void **out, size_t *out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:854
static enum GNUNET_GenericReturnValue channel_to_sh(const char *identifier, struct GNUNET_ShortHashCode *sh)
Convert sh to a Postgres identifier.
Definition: pq_event.c:116
static enum GNUNET_GenericReturnValue do_notify(void *cls, const struct GNUNET_ShortHashCode *sh, void *value)
Function called on every event handler that needs to be triggered.
Definition: pq_event.c:174
A 256-bit hashcode.
Closure for do_notify().
Definition: pq_event.c:151

References channel_to_sh(), ctx, db, do_notify(), GNUNET_CONTAINER_multishortmap_get_multiple(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_PQ_reconnect(), GNUNET_STRINGS_string_to_data_alloc(), and sh.

Referenced by do_scheduler_notify().

Here is the call graph for this function:
Here is the caller 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);
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
void GNUNET_PQ_event_do_poll(struct GNUNET_PQ_Context *db)
Poll for events right now.
Definition: pq_event.c:189
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

◆ 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 }
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
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
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 684 of file pq_connect.c.

685 {
686  if (NULL == db)
687  return;
688  GNUNET_assert (0 ==
689  GNUNET_CONTAINER_multishortmap_size (db->channel_map));
691  GNUNET_free (db->es);
692  GNUNET_free (db->ps);
693  GNUNET_free (db->load_path);
694  GNUNET_free (db->auto_suffix);
695  GNUNET_free (db->config_str);
696  GNUNET_free (db->oids.table);
697  db->oids.table = NULL;
698  db->oids.num = 0;
699  db->oids.cap = 0;
700  PQfinish (db->conn);
701  GNUNET_free (db);
702 }

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:

Variable Documentation

◆ ndim

uint32_t ndim

Definition at line 0 of file gnunet_pq_lib.h.

◆ has_null

uint32_t has_null

Definition at line 1 of file gnunet_pq_lib.h.

◆ oid

uint32_t oid

Definition at line 2 of file gnunet_pq_lib.h.

◆ dim

uint32_t dim

Definition at line 3 of file gnunet_pq_lib.h.

Referenced by cores_send_disconnect_info(), and handle_disconnect().

◆ lbound

uint32_t lbound

Definition at line 4 of file gnunet_pq_lib.h.

◆ __attribute__

struct PrismHeader __attribute__