GNUnet  0.11.x
Data Structures | Macros | Typedefs | Enumerations | Functions
gnunet_pq_lib.h File Reference

helper functions for Postgres DB interactions More...

#include <libpq-fe.h>
#include "gnunet_util_lib.h"
#include "gnunet_db_lib.h"
Include dependency graph for gnunet_pq_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GNUNET_PQ_QueryParam
 Description of a DB query parameter. More...
 
struct  GNUNET_PQ_ResultSpec
 Description of a DB result cell. More...
 
struct  GNUNET_PQ_PreparedStatement
 Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements(). More...
 
struct  GNUNET_PQ_ExecuteStatement
 Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements(). More...
 

Macros

#define GNUNET_PQ_query_param_end
 End of query parameter specification. More...
 
#define GNUNET_PQ_query_param_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_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 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

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_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_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_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_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_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_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...
 
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, unsigned int num_args)
 Create a struct GNUNET_PQ_PreparedStatement. More...
 
int GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres. 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...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect2 (const char *config_str, const char *load_path, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
 Create a connection to the Postgres database using config_str for the configuration. More...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
 Connect to a postgres database using the configuration option "CONFIG" in section. More...
 
struct GNUNET_PQ_ContextGNUNET_PQ_connect_with_cfg2 (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps, enum GNUNET_PQ_Options flags)
 Connect to a postgres database using the configuration option "CONFIG" in section. More...
 
void GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db)
 Reinitialize the database db if the connection is down. More...
 
void GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
 Reinitialize the database db. More...
 
struct GNUNET_DB_EventHandlerGNUNET_PQ_event_listen (struct GNUNET_PQ_Context *db, const struct GNUNET_DB_EventHeaderP *es, struct GNUNET_TIME_Relative timeout, GNUNET_DB_EventCallback cb, void *cb_cls)
 Register callback to be invoked on events of type es. More...
 
void GNUNET_PQ_event_listen_cancel (struct GNUNET_DB_EventHandler *eh)
 Stop notifications. More...
 
void GNUNET_PQ_event_notify (struct GNUNET_PQ_Context *db, const struct GNUNET_DB_EventHeaderP *es, const void *extra, size_t extra_size)
 Notify all that listen on es of an event. More...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db, const char *load_path)
 Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the files exist contiguously). More...
 
void GNUNET_PQ_disconnect (struct GNUNET_PQ_Context *db)
 Disconnect from the database, destroying the prepared statements and releasing other associated resources. More...
 

Detailed Description

helper functions for Postgres DB interactions

Author
Christian Grothoff

Definition in file gnunet_pq_lib.h.

Macro Definition Documentation

◆ GNUNET_PQ_query_param_end

#define GNUNET_PQ_query_param_end
Value:
{ \
NULL, NULL, NULL, 0, 0 \
}

End of query parameter specification.

Definition at line 97 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 142 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_result_spec_end

#define GNUNET_PQ_result_spec_end
Value:
{ \
NULL, NULL, NULL, NULL, 0, NULL, NULL \
}

End of result parameter specification.

Returns
array last entry for the result specification to use

Definition at line 336 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 390 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_PREPARED_STATEMENT_END

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

Terminator for prepared statement list.

Definition at line 684 of file gnunet_pq_lib.h.

◆ GNUNET_PQ_EXECUTE_STATEMENT_END

#define GNUNET_PQ_EXECUTE_STATEMENT_END
Value:
{ \
NULL, GNUNET_SYSERR \
}
@ GNUNET_SYSERR
Definition: gnunet_common.h:93

Terminator for executable statement list.

Definition at line 742 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 49 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 49 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 267 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 602 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 789 of file gnunet_pq_lib.h.

790 {
795 
801 
806 };
@ 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_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 45 of file pq_query_helper.c.

71 {
72  struct GNUNET_PQ_QueryParam res = {
73  &qconv_null, NULL, NULL, 0, 1
74  };
75 
76  return res;
77 }
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.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:65

References data, and GNUNET_break.

◆ 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 @oaran ptr_size number of bytes in ptr
Returns
query parameter to use

Definition at line 95 of file pq_query_helper.c.

120 {
121  struct GNUNET_PQ_QueryParam res = {
122  &qconv_fixed, NULL, ptr, ptr_size, 1
123  };
124 
125  return res;
126 }
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.

References data, and GNUNET_break.

Referenced by namecache_postgres_cache_block(), namestore_postgres_store_records(), 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 95 of file pq_query_helper.c.

131 {
133  strlen (ptr));
134 }
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.

Referenced by namestore_postgres_lookup_records(), and namestore_postgres_store_records().

Here is the caller graph for this function:

◆ 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 311 of file pq_query_helper.c.

341 {
342  struct GNUNET_PQ_QueryParam res =
343  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
344 
345  return res;
346 }
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 364 of file pq_query_helper.c.

393 {
394  struct GNUNET_PQ_QueryParam res =
395  { &qconv_rsa_signature, NULL, (x), 0, 1 };
396 
397  return res;
398 }
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 416 of file pq_query_helper.c.

448 {
449  struct GNUNET_PQ_QueryParam res =
450  { &qconv_rel_time, NULL, x, sizeof(*x), 1 };
451 
452  return res;
453 }
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 471 of file pq_query_helper.c.

503 {
504  struct GNUNET_PQ_QueryParam res = {
505  &qconv_abs_time, NULL, x, sizeof(*x), 1
506  };
507 
508  return res;
509 }
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_cache_block(), namecache_postgres_expire_blocks(), postgres_plugin_del(), postgres_plugin_get(), postgres_plugin_get_closest(), postgres_plugin_get_expiration(), postgres_plugin_get_random(), and postgres_plugin_put().

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

◆ 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 471 of file pq_query_helper.c.

515 {
517 }
#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_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 152 of file pq_query_helper.c.

182 {
183  struct GNUNET_PQ_QueryParam res =
184  { &qconv_uint16, NULL, x, sizeof(*x), 1 };
185 
186  return res;
187 }
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 205 of file pq_query_helper.c.

235 {
236  struct GNUNET_PQ_QueryParam res =
237  { &qconv_uint32, NULL, x, sizeof(*x), 1 };
238 
239  return res;
240 }
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 namestore_postgres_store_records(), postgres_plugin_del(), postgres_plugin_get(), postgres_plugin_get_closest(), postgres_plugin_get_key(), postgres_plugin_get_random(), postgres_plugin_get_zero_anonymity(), postgres_plugin_put(), process_result(), and repl_proc().

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 258 of file pq_query_helper.c.

288 {
289  struct GNUNET_PQ_QueryParam res =
290  { &qconv_uint64, NULL, x, sizeof(*x), 1 };
291 
292  return res;
293 }
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 namestore_postgres_iterate_records(), namestore_postgres_store_records(), postgres_plugin_get_key(), postgres_plugin_get_zero_anonymity(), and postgres_plugin_put().

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.

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

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

◆ 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 51 of file pq_result_helper.c.

129 {
130  struct GNUNET_PQ_ResultSpec res = {
131  .conv = &extract_varsize_blob,
132  .cleaner = &clean_varsize_blob,
133  .dst = (void *) (dst),
134  .fname = name,
135  .result_size = sptr
136  };
137 
138  return res;
139 }
const char * name
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(), namecache_postgres_lookup_block(), parse_result_call_iterator(), postgres_plugin_get_random(), 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 51 of file pq_result_helper.c.

212 {
213  struct GNUNET_PQ_ResultSpec res = {
214  .conv = &extract_fixed_blob,
215  .dst = (dst),
216  .dst_size = dst_size,
217  .fname = name
218  };
219 
220  return res;
221 }
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 481 of file pq_result_helper.c.

498 {
499  struct GNUNET_PQ_ResultSpec res =
500  { &extract_string,
501  &clean_string,
502  NULL,
503  (void *) dst, 0, (name), NULL };
504 
505  return res;
506 }
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 parse_result_call_iterator().

Here is the caller graph for this function:

◆ 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 291 of file pq_result_helper.c.

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

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

654 {
655  struct GNUNET_PQ_ResultSpec res =
656  { &extract_abs_time,
657  NULL,
658  NULL,
659  (void *) at, sizeof(*at), (name), NULL };
660 
661  return res;
662 }
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(), postgres_plugin_get_random(), and process_result().

Here is the caller graph for this function:

◆ 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 481 of file pq_result_helper.c.

574 {
575  struct GNUNET_PQ_ResultSpec res = {
577  NULL,
578  NULL,
579  (void *) rt,
580  sizeof(*rt),
581  name,
582  NULL
583  };
584 
585  return res;
586 }
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 481 of file pq_result_helper.c.

668 {
669  struct GNUNET_PQ_ResultSpec res =
671 
672  return res;
673 }
#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_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 481 of file pq_result_helper.c.

738 {
739  struct GNUNET_PQ_ResultSpec res =
740  { &extract_uint16,
741  NULL,
742  NULL,
743  (void *) u16, sizeof(*u16), (name), NULL };
744 
745  return res;
746 }
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 481 of file pq_result_helper.c.

811 {
812  struct GNUNET_PQ_ResultSpec res = {
814  NULL,
815  NULL,
816  (void *) u32,
817  sizeof(*u32),
818  (name),
819  NULL
820  };
821 
822  return res;
823 }
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(), parse_result_call_iterator(), postgres_plugin_del(), postgres_plugin_get_random(), 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 481 of file pq_result_helper.c.

892 {
893  struct GNUNET_PQ_ResultSpec res = {
895  NULL,
896  NULL,
897  (void *) u64,
898  sizeof(*u64),
899  (name),
900  NULL
901  };
902 
903  return res;
904 }
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 parse_result_call_iterator(), and postgres_plugin_estimate_size().

Here is the caller graph for this function:

◆ 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 32 of file pq.c.

35 {
36  unsigned int len;
37 
39  "Running prepared statement `%s' on %p\n",
40  name,
41  db);
42  /* count the number of parameters */
43  len = 0;
44  for (unsigned int i = 0; 0 != params[i].num_params; i++)
45  len += params[i].num_params;
46 
47  /* new scope to allow stack allocation without alloca */
48  {
49  /* Scratch buffer for temporary storage */
50  void *scratch[len];
51  /* Parameter array we are building for the query */
52  void *param_values[len];
53  int param_lengths[len];
54  int param_formats[len];
55  unsigned int off;
56  /* How many entries in the scratch buffer are in use? */
57  unsigned int soff;
58  PGresult *res;
59  int ret;
60  ConnStatusType status;
61 
62  off = 0;
63  soff = 0;
64  for (unsigned int i = 0; 0 != params[i].num_params; i++)
65  {
66  const struct GNUNET_PQ_QueryParam *x = &params[i];
67 
68  ret = x->conv (x->conv_cls,
69  x->data,
70  x->size,
71  &param_values[off],
72  &param_lengths[off],
73  &param_formats[off],
74  x->num_params,
75  &scratch[soff],
76  len - soff);
77  if (ret < 0)
78  {
79  for (off = 0; off < soff; off++)
80  GNUNET_free (scratch[off]);
81  return NULL;
82  }
83  soff += ret;
84  off += x->num_params;
85  }
86  GNUNET_assert (off == len);
88  "pq",
89  "Executing prepared SQL statement `%s'\n",
90  name);
91  res = PQexecPrepared (db->conn,
92  name,
93  len,
94  (const char **) param_values,
95  param_lengths,
96  param_formats,
97  1);
98  if ( (PGRES_COMMAND_OK != PQresultStatus (res)) &&
99  (CONNECTION_OK != (status = PQstatus (db->conn))) )
100  {
102  "pq",
103  "Database disconnected on SQL statement `%s' (reconnecting)\n",
104  name);
106  res = NULL;
107  }
108 
109  for (off = 0; off < soff; off++)
110  GNUNET_free (scratch[off]);
111  return res;
112  }
113 }
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...
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:312
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_free(ptr)
Wrapper around free.
void * conv_cls
Closure for conv.
Definition: gnunet_pq_lib.h:74
const void * data
Data or NULL.
Definition: gnunet_pq_lib.h:79
GNUNET_PQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_pq_lib.h:69
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_pq_lib.h:89
size_t size
Size of data.
Definition: gnunet_pq_lib.h:84

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_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 117 of file pq.c.

130 {
131  unsigned int i;
132 
133  if (NULL == result)
134  return GNUNET_SYSERR;
135  for (i = 0; NULL != rs[i].conv; i++)
136  {
137  struct GNUNET_PQ_ResultSpec *spec;
139 
140  spec = &rs[i];
141  ret = spec->conv (spec->cls,
142  result,
143  row,
144  spec->fname,
145  &spec->dst_size,
146  spec->dst);
147  switch (ret)
148  {
149  case GNUNET_OK:
150  /* canonical case, continue below */
151  if (NULL != spec->is_null)
152  *spec->is_null = false;
153  break;
154  case GNUNET_NO:
155  if (spec->is_nullable)
156  {
157  if (NULL != spec->is_null)
158  *spec->is_null = true;
159  continue;
160  }
162  "NULL field encountered for `%s' where non-NULL was required\n",
163  spec->fname);
164  goto cleanup;
165  case GNUNET_SYSERR:
166  GNUNET_break (0);
167  goto cleanup;
168  }
169  if (NULL != spec->result_size)
170  *spec->result_size = spec->dst_size;
171  }
172  return GNUNET_OK;
173 cleanup:
174  for (unsigned int j = 0; j < i; j++)
175  if (NULL != rs[j].cleaner)
176  rs[j].cleaner (rs[j].cls,
177  rs[j].dst);
178  return GNUNET_SYSERR;
179 }
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.
Definition: gnunet_common.h:92
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_NO
Definition: gnunet_common.h:94
#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
void * cls
Closure for conv and cleaner.
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(), parse_result_call_iterator(), 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 117 of file pq.c.

118 {
119  for (unsigned int i = 0; NULL != rs[i].conv; i++)
120  if (NULL != rs[i].cleaner)
121  rs[i].cleaner (rs[i].cls,
122  rs[i].dst);
123 }

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

Referenced by extract_result_cb(), handle_results(), namecache_postgres_lookup_block(), parse_result_call_iterator(), postgres_plugin_del(), postgres_plugin_get_random(), 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)

Also logs errors.

Parameters
dbdatabase to execute the statement with
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.

62 {
63  ExecStatusType est;
64 
65  if (NULL == result)
67  est = PQresultStatus (result);
68  if ((PGRES_COMMAND_OK != est) &&
69  (PGRES_TUPLES_OK != est))
70  {
71  const char *sqlstate;
72  ConnStatusType status;
73 
74  if (CONNECTION_OK != (status = PQstatus (db->conn)))
75  {
77  "pq",
78  "Database connection failed during query `%s': %d (reconnecting)\n",
79  statement_name,
80  status);
83  }
84 
85  sqlstate = PQresultErrorField (result,
86  PG_DIAG_SQLSTATE);
87  if (NULL == sqlstate)
88  {
89  /* very unexpected... */
90  GNUNET_break (0);
92  }
93  if ((0 == strcmp (sqlstate,
95  (0 == strcmp (sqlstate,
97  {
98  /* These two can be retried and have a fair chance of working
99  the next time */
101  "pq",
102  "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
103  statement_name,
104  PQresultErrorField (result,
105  PG_DIAG_MESSAGE_PRIMARY),
106  PQresultErrorField (result,
107  PG_DIAG_MESSAGE_DETAIL),
108  PQresultErrorMessage (result),
109  PQresStatus (PQresultStatus (result)),
110  PQerrorMessage (db->conn));
112  }
113  if (0 == strcmp (sqlstate,
115  {
116  /* Likely no need to retry, INSERT of "same" data. */
118  "pq",
119  "Query `%s' failed with unique violation: %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  "pq",
132  "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
133  statement_name,
134  PQresultErrorField (result,
135  PG_DIAG_MESSAGE_PRIMARY),
136  PQresultErrorField (result,
137  PG_DIAG_MESSAGE_DETAIL),
138  PQresultErrorMessage (result),
139  PQresStatus (PQresultStatus (result)),
140  PQerrorMessage (db->conn));
142  }
144 }
@ GNUNET_DB_STATUS_HARD_ERROR
A hard error occurred, retrying will not help.
Definition: gnunet_db_lib.h:40
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:54
@ GNUNET_DB_STATUS_SOFT_ERROR
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:46
@ 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.

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.

167 {
168  PGresult *result;
169  enum GNUNET_DB_QueryStatus qs;
170 
172  statement_name,
173  params);
174  if (NULL == result)
177  statement_name,
178  result);
180  {
181  const char *tuples;
182 
183  /* What an awful API, this function really does return a string */
184  tuples = PQcmdTuples (result);
185  if (NULL != tuples)
186  qs = strtol (tuples, NULL, 10);
187  }
188  PQclear (result);
189  return qs;
190 }
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:36
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:32
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:59

Referenced by delete_old_block(), namecache_postgres_cache_block(), namecache_postgres_expire_blocks(), namestore_postgres_store_records(), postgres_plugin_del(), 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.

214 {
215  PGresult *result;
216  enum GNUNET_DB_QueryStatus qs;
217  unsigned int ret;
218 
220  statement_name,
221  params);
222  if (NULL == result)
225  statement_name,
226  result);
227  if (qs < 0)
228  {
229  PQclear (result);
230  return qs;
231  }
232  ret = PQntuples (result);
233  if (NULL != rh)
234  rh (rh_cls,
235  result,
236  ret);
237  PQclear (result);
238  return ret;
239 }

Referenced by namestore_postgres_iterate_records(), namestore_postgres_lookup_records(), namestore_postgres_zone_to_name(), postgres_plugin_get(), 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.

263 {
264  PGresult *result;
265  enum GNUNET_DB_QueryStatus qs;
266  int ntuples;
267 
269  statement_name,
270  params);
271  if (NULL == result)
274  statement_name,
275  result);
276  if (qs < 0)
277  {
278  PQclear (result);
279  return qs;
280  }
281  ntuples = PQntuples (result);
282  if (0 == ntuples)
283  {
284  PQclear (result);
286  }
287  if (1 != ntuples)
288  {
289  /* more than one result, but there must be at most one */
290  GNUNET_break (0);
291  PQclear (result);
293  }
294  if (GNUNET_OK !=
296  rs,
297  0))
298  {
299  PQclear (result);
301  }
302  PQclear (result);
304 }
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:59
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:127

Referenced by namecache_postgres_lookup_block(), postgres_plugin_del(), postgres_plugin_estimate_size(), and postgres_plugin_get_random().

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,
unsigned int  num_args 
)

Create a struct GNUNET_PQ_PreparedStatement.

Parameters
namename of the statement
sqlactual SQL statement
num_argsnumber of arguments in the statement
Returns
initialized struct

Definition at line 1 of file pq_prepare.c.

41 {
42  struct GNUNET_PQ_PreparedStatement ps = {
43  .name = name,
44  .sql = sql,
45  .num_arguments = num_args
46  };
47 
48  return ps;
49 }
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.

Referenced by database_setup(), and init_connection().

Here is the caller graph for this function:

◆ GNUNET_PQ_prepare_statements()

int 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 62 of file pq_prepare.c.

64 {
65  if (db->ps != ps)
66  {
67  /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
68  unsigned int olen = 0; /* length of existing 'db->ps' array */
69  unsigned int nlen = 0; /* length of 'ps' array */
70  struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
71 
72  if (NULL != db->ps)
73  while (NULL != db->ps[olen].name)
74  olen++;
75  while (NULL != ps[nlen].name)
76  nlen++;
77  rps = GNUNET_new_array (olen + nlen + 1,
79  if (NULL != db->ps)
80  memcpy (rps,
81  db->ps,
82  olen * sizeof (struct GNUNET_PQ_PreparedStatement));
83  memcpy (&rps[olen],
84  ps,
85  nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
86  GNUNET_free (db->ps);
87  db->ps = rps;
88  }
89 
90  /* actually prepare statements */
91  for (unsigned int i = 0; NULL != ps[i].name; i++)
92  {
93  PGresult *ret;
94 
96  "pq",
97  "Preparing SQL statement `%s' as `%s'\n",
98  ps[i].sql,
99  ps[i].name);
100  ret = PQprepare (db->conn,
101  ps[i].name,
102  ps[i].sql,
103  ps[i].num_arguments,
104  NULL);
105  if (PGRES_COMMAND_OK != PQresultStatus (ret))
106  {
108  "pq",
109  _ ("PQprepare (`%s' as `%s') failed with error: %s\n"),
110  ps[i].sql,
111  ps[i].name,
112  PQerrorMessage (db->conn));
113  PQclear (ret);
114  return GNUNET_SYSERR;
115  }
116  PQclear (ret);
117  }
118  return GNUNET_OK;
119 }
@ GNUNET_ERROR_TYPE_BULK
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

References _, db, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_from, GNUNET_new_array, GNUNET_OK, GNUNET_SYSERR, name, GNUNET_PQ_PreparedStatement::name, GNUNET_PQ_PreparedStatement::num_arguments, ret, and GNUNET_PQ_PreparedStatement::sql.

Referenced by GNUNET_PQ_reconnect().

Here is the caller graph for this function:

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

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

Referenced by database_setup(), init_connection(), and 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.

56 {
57  struct GNUNET_PQ_ExecuteStatement es = {
58  .sql = sql,
59  .ignore_errors = GNUNET_YES
60  };
61 
62  return es;
63 }
@ GNUNET_YES
Definition: gnunet_common.h:97

Referenced by database_setup(), and init_connection().

Here is the caller graph for this function:

◆ 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
pqdatabase 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
Parameters
dbdatabase to execute the statements with
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.

78 {
79  for (unsigned int i = 0; NULL != es[i].sql; i++)
80  {
81  PGresult *result;
82 
84  "Running statement `%s' on %p\n",
85  es[i].sql,
86  db);
87  result = PQexec (db->conn,
88  es[i].sql);
89  if ((GNUNET_NO == es[i].ignore_errors) &&
90  (PGRES_COMMAND_OK != PQresultStatus (result)))
91  {
93  "pq",
94  "Failed to execute `%s': %s/%s/%s/%s/%s",
95  es[i].sql,
96  PQresultErrorField (result,
97  PG_DIAG_MESSAGE_PRIMARY),
98  PQresultErrorField (result,
99  PG_DIAG_MESSAGE_DETAIL),
100  PQresultErrorMessage (result),
101  PQresStatus (PQresultStatus (result)),
102  PQerrorMessage (db->conn));
103  PQclear (result);
104  return GNUNET_SYSERR;
105  }
106  PQclear (result);
107  }
108  return GNUNET_OK;
109 }

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

72 {
73  return GNUNET_PQ_connect2 (config_str,
74  load_path,
75  es,
76  ps,
78 }
struct GNUNET_PQ_Context * GNUNET_PQ_connect2(const char *config_str, const char *load_path, 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:82

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

struct GNUNET_PQ_Context* GNUNET_PQ_connect2 ( const char *  config_str,
const char *  load_path,
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
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 82 of file pq_connect.c.

87 {
88  struct GNUNET_PQ_Context *db;
89  unsigned int elen = 0;
90  unsigned int plen = 0;
91 
92  if (NULL != es)
93  while (NULL != es[elen].sql)
94  elen++;
95  if (NULL != ps)
96  while (NULL != ps[plen].name)
97  plen++;
98 
99  db = GNUNET_new (struct GNUNET_PQ_Context);
100  db->flags = flags;
101  db->config_str = GNUNET_strdup (config_str);
102  if (NULL != load_path)
103  db->load_path = GNUNET_strdup (load_path);
104  if (0 != elen)
105  {
106  db->es = GNUNET_new_array (elen + 1,
108  memcpy (db->es,
109  es,
110  elen * sizeof (struct GNUNET_PQ_ExecuteStatement));
111  }
112  if (0 != plen)
113  {
114  db->ps = GNUNET_new_array (plen + 1,
116  memcpy (db->ps,
117  ps,
118  plen * sizeof (struct GNUNET_PQ_PreparedStatement));
119  }
120  db->channel_map = GNUNET_CONTAINER_multishortmap_create (16,
121  GNUNET_YES);
123  if (NULL == db->conn)
124  {
125  GNUNET_free (db->load_path);
126  GNUNET_free (db->config_str);
127  GNUNET_free (db);
128  return NULL;
129  }
130  return db;
131 }
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).
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:312
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:80
char * load_path
Path to load SQL files from.
Definition: pq.h:60
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:55

Referenced by GNUNET_PQ_connect(), and GNUNET_PQ_connect_with_cfg2().

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

454 {
456  section,
457  load_path_suffix,
458  es,
459  ps,
461 }
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:465

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

Referenced by database_setup(), and init_connection().

Here is the call graph for this function:
Here is the caller 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 465 of file pq_connect.c.

471 {
472  struct GNUNET_PQ_Context *db;
473  char *conninfo;
474  char *load_path;
475  char *sp;
476 
477  if (GNUNET_OK !=
479  section,
480  "CONFIG",
481  &conninfo))
482  conninfo = NULL;
483  load_path = NULL;
484  sp = NULL;
485  if ( (NULL != load_path_suffix) &&
486  (GNUNET_OK ==
488  section,
489  "SQL_DIR",
490  &sp)) )
492  "%s%s",
493  sp,
494  load_path_suffix);
495  db = GNUNET_PQ_connect2 (conninfo == NULL ? "" : conninfo,
496  load_path,
497  es,
498  ps,
499  flags);
501  GNUNET_free (sp);
502  GNUNET_free (conninfo);
503  return db;
504 }
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.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.

References cfg, db, GNUNET_PQ_Context::es, GNUNET_PQ_Context::flags, GNUNET_asprintf(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_free, 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 300 of file pq_connect.c.

301 {
302  if (1 ==
303  PQconsumeInput (db->conn))
304  return;
305  if (CONNECTION_BAD != PQstatus (db->conn))
306  return;
308 }

References db, and GNUNET_PQ_reconnect().

Here is the call graph for this function:

◆ GNUNET_PQ_reconnect()

void GNUNET_PQ_reconnect ( struct GNUNET_PQ_Context db)

Reinitialize the database db.

Parameters
dbdatabase connection to reinitialize

Definition at line 312 of file pq_connect.c.

313 {
315  -1);
316  if (NULL != db->conn)
317  PQfinish (db->conn);
318  db->conn = PQconnectdb (db->config_str);
319  if ( (NULL == db->conn) ||
320  (CONNECTION_OK != PQstatus (db->conn)) )
321  {
323  "pq",
324  "Database connection to '%s' failed: %s\n",
325  db->config_str,
326  (NULL != db->conn) ?
327  PQerrorMessage (db->conn)
328  : "PQconnectdb returned NULL");
329  if (NULL != db->conn)
330  {
331  PQfinish (db->conn);
332  db->conn = NULL;
333  }
334  return;
335  }
336  PQsetNoticeReceiver (db->conn,
338  db);
339  PQsetNoticeProcessor (db->conn,
341  db);
342  if (NULL != db->load_path)
343  {
344  PGresult *res;
345 
346  res = PQprepare (db->conn,
347  "gnunet_pq_check_patch",
348  "SELECT"
349  " applied_by"
350  " FROM _v.patches"
351  " WHERE patch_name = $1"
352  " LIMIT 1",
353  1,
354  NULL);
355  if (PGRES_COMMAND_OK != PQresultStatus (res))
356  {
358 
359  PQclear (res);
360  if (0 != (db->flags & GNUNET_PQ_FLAG_DROP))
361  {
363  "Failed to prepare statement to check patch level. Likely versioning schema does not exist yet. Not attempting drop!\n");
364  PQfinish (db->conn);
365  db->conn = NULL;
366  return;
367  }
369  "Failed to prepare statement to check patch level. Likely versioning schema does not exist yet, loading patch level 0000!\n");
370  ret = apply_patch (db,
371  db->load_path,
372  0);
373  if (GNUNET_NO == ret)
374  {
376  "Failed to find SQL file to load database versioning logic\n");
377  PQfinish (db->conn);
378  db->conn = NULL;
379  return;
380  }
381  if (GNUNET_SYSERR == ret)
382  {
384  "Failed to run SQL logic to setup database versioning logic\n");
385  PQfinish (db->conn);
386  db->conn = NULL;
387  return;
388  }
389  /* try again to prepare our statement! */
390  res = PQprepare (db->conn,
391  "gnunet_pq_check_patch",
392  "SELECT"
393  " applied_by"
394  " FROM _v.patches"
395  " WHERE patch_name = $1"
396  " LIMIT 1",
397  1,
398  NULL);
399  if (PGRES_COMMAND_OK != PQresultStatus (res))
400  {
402  "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
403  PQresultErrorMessage (res),
404  PQerrorMessage (db->conn));
405  PQclear (res);
406  PQfinish (db->conn);
407  db->conn = NULL;
408  return;
409  }
410  }
411  PQclear (res);
412 
413  if (GNUNET_SYSERR ==
415  db->load_path))
416  {
418  "Failed to load SQL statements from `%s*'\n",
419  db->load_path);
420  PQfinish (db->conn);
421  db->conn = NULL;
422  return;
423  }
424  }
425  if ( (NULL != db->es) &&
426  (GNUNET_OK !=
428  db->es)) )
429  {
430  PQfinish (db->conn);
431  db->conn = NULL;
432  return;
433  }
434  if ( (NULL != db->ps) &&
435  (GNUNET_OK !=
437  db->ps)) )
438  {
439  PQfinish (db->conn);
440  db->conn = NULL;
441  return;
442  }
444  PQsocket (db->conn));
445 }
int 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:62
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:76
@ GNUNET_ERROR_TYPE_WARNING
void GNUNET_PQ_event_reconnect_(struct GNUNET_PQ_Context *db, int fd)
Internal API.
Definition: pq_event.c:383
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:201
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:39
static enum GNUNET_GenericReturnValue apply_patch(struct GNUNET_PQ_Context *db, const char *load_path, unsigned int i)
Apply patch number from path load_path.
Definition: pq_connect.c:143
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:56

References apply_patch(), db, 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_statements(), GNUNET_PQ_FLAG_DROP, GNUNET_PQ_prepare_statements(), GNUNET_PQ_run_sql(), GNUNET_SYSERR, pq_notice_processor_cb(), pq_notice_receiver_cb(), res, and ret.

Referenced by apply_patch(), GNUNET_PQ_exec_prepared(), and GNUNET_PQ_reconnect_if_down().

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 416 of file pq_event.c.

421 {
422  struct GNUNET_DB_EventHandler *eh;
423 
424  eh = GNUNET_new (struct GNUNET_DB_EventHandler);
425  eh->db = db;
426  es_to_sh (es,
427  &eh->sh);
428  eh->cb = cb;
429  eh->cb_cls = cb_cls;
432  &eh->sh,
433  eh,
435  if (NULL == db->event_task)
436  {
438  "Starting event scheduler\n");
440  PQsocket (db->conn));
441  }
443  "LISTEN X",
444  eh);
446  &event_timeout,
447  eh);
448  return eh;
449 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
int 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.
@ 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:1269
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:404
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:324
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:286
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_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 453 of file pq_event.c.

454 {
455  struct GNUNET_PQ_Context *db = eh->db;
456 
459  &eh->sh,
460  eh));
462  "UNLISTEN X",
463  eh);
464  if (0 == GNUNET_CONTAINER_multishortmap_size (db->channel_map))
465  {
467  "Stopping PQ event scheduler job\n");
468  GNUNET_free (db->rfd);
469  if (NULL != db->event_task)
470  {
471  GNUNET_SCHEDULER_cancel (db->event_task);
472  db->event_task = NULL;
473  }
474  }
475  if (NULL != eh->timeout_task)
476  {
478  eh->timeout_task = NULL;
479  }
480  GNUNET_free (eh);
481 }
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:972

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

Here is the call graph for this function:

◆ GNUNET_PQ_event_notify()

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

Notify all that listen on es of an event.

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

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

Definition at line 485 of file pq_event.c.

489 {
490  char sql[16 + 64 + extra_size * 8 / 5 + 8];
491  char *end;
492  PGresult *result;
493 
494  end = stpcpy (sql,
495  "NOTIFY X");
496  end = es_to_channel (es,
497  end);
498  end = stpcpy (end,
499  ", '");
501  extra_size,
502  end,
503  sizeof (sql) - (end - sql) - 1);
504  GNUNET_assert (NULL != end);
505  *end = '\0';
506  end = stpcpy (end,
507  "'");
509  "Executing command `%s'\n",
510  sql);
511  result = PQexec (db->conn,
512  sql);
513  if (PGRES_COMMAND_OK != PQresultStatus (result))
514  {
516  "pq",
517  "Failed to execute `%s': %s/%s/%s/%s/%s",
518  sql,
519  PQresultErrorField (result,
520  PG_DIAG_MESSAGE_PRIMARY),
521  PQresultErrorField (result,
522  PG_DIAG_MESSAGE_DETAIL),
523  PQresultErrorMessage (result),
524  PQresStatus (PQresultStatus (result)),
525  PQerrorMessage (db->conn));
526  }
527  PQclear (result);
528  event_do_poll (db);
529 }
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:695
static char * es_to_channel(const struct GNUNET_DB_EventHeaderP *es, char identifier[64])
Convert es to a Postgres identifier.
Definition: pq_event.c:135
static void event_do_poll(struct GNUNET_PQ_Context *db)
Definition: pq_event.c:189

◆ GNUNET_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 82 of file pq_connect.c.

203 {
204  const char *load_path_suffix;
205  size_t slen = strlen (load_path) + 10;
206 
207  load_path_suffix = strrchr (load_path, '/');
208  if (NULL == load_path_suffix)
209  {
210  GNUNET_break (0);
211  return GNUNET_SYSERR;
212  }
213  load_path_suffix++; /* skip '/' */
215  "Loading SQL resources from `%s'\n",
216  load_path);
217  for (unsigned int i = 1; i<10000; i++)
218  {
219  char patch_name[slen];
220  char buf[slen];
221  enum GNUNET_DB_QueryStatus qs;
222 
223  /* First, check patch actually exists */
225  sizeof (buf),
226  "%s%04u.sql",
227  load_path,
228  i);
229  if (GNUNET_YES !=
231  return GNUNET_OK; /* We are done */
232 
233  /* Second, check with DB versioning schema if this patch was already applied,
234  if so, skip it. */
235  GNUNET_snprintf (patch_name,
236  sizeof (patch_name),
237  "%s%04u",
238  load_path_suffix,
239  i);
240  {
241  char *applied_by;
242  struct GNUNET_PQ_QueryParam params[] = {
243  GNUNET_PQ_query_param_string (patch_name),
245  };
246  struct GNUNET_PQ_ResultSpec rs[] = {
247  GNUNET_PQ_result_spec_string ("applied_by",
248  &applied_by),
250  };
251 
253  "gnunet_pq_check_patch",
254  params,
255  rs);
257  {
259  "Database version %s already applied by %s, skipping\n",
260  patch_name,
261  applied_by);
263  }
264  if (GNUNET_DB_STATUS_HARD_ERROR == qs)
265  {
266  GNUNET_break (0);
267  return GNUNET_SYSERR;
268  }
269  }
271  continue; /* patch already applied, skip it */
272 
273  if (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
274  {
275  /* We are only checking, found unapplied patch, bad! */
277  "Database outdated, patch %s missing. Aborting!\n",
278  patch_name);
279  return GNUNET_SYSERR;
280  }
281  else
282  {
283  /* patch not yet applied, run it! */
285 
286  ret = apply_patch (db,
287  load_path,
288  i);
289  if (GNUNET_NO == ret)
290  break;
291  if (GNUNET_SYSERR == ret)
292  return GNUNET_SYSERR;
293  }
294  }
295  return GNUNET_OK;
296 }
static char buf[2048]
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:258
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:97
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:117
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.
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
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 508 of file pq_connect.c.

509 {
510  if (NULL == db)
511  return;
512  GNUNET_assert (0 ==
513  GNUNET_CONTAINER_multishortmap_size (db->channel_map));
515  GNUNET_free (db->es);
516  GNUNET_free (db->ps);
517  GNUNET_free (db->load_path);
518  GNUNET_free (db->config_str);
519  PQfinish (db->conn);
520  GNUNET_free (db);
521 }
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.

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: