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_bool (bool b)
 Pass a boolean into a query. 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 *t)
 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...
 
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...
 
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...
 
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:
{ \
.conv = NULL, \
.conv_cls = NULL, \
.data = NULL, \
.size = 0, \
.num_params = 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 156 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 373 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 437 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 768 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 826 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 304 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 686 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 873 of file gnunet_pq_lib.h.

874 {
879 
885 
890 };
@ 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  .conv = &qconv_null,
74  .num_params = 1
75  };
76 
77  return res;
78 }
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 96 of file pq_query_helper.c.

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

132 {
134  strlen (ptr));
135 }
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_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 96 of file pq_query_helper.c.

140 {
141  static uint8_t bt = 1;
142  static uint8_t bf = 0;
143 
144  return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf,
145  sizeof (uint8_t));
146 }

References data, and GNUNET_break.

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

365 {
366  struct GNUNET_PQ_QueryParam res = {
367  .conv = &qconv_rsa_public_key,
368  .data = x,
369  .num_params = 1
370  };
371 
372  return res;
373 }
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 391 of file pq_query_helper.c.

420 {
421  struct GNUNET_PQ_QueryParam res = {
422  .conv = &qconv_rsa_signature,
423  .data = x,
424  .num_params = 1
425  };
426 
427  return res;
428 }
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 446 of file pq_query_helper.c.

478 {
479  struct GNUNET_PQ_QueryParam res = {
480  .conv = &qconv_rel_time,
481  .data = x,
482  .size = sizeof(*x),
483  .num_params = 1
484  };
485 
486  return res;
487 }
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 505 of file pq_query_helper.c.

537 {
538  struct GNUNET_PQ_QueryParam res = {
539  .conv = &qconv_abs_time,
540  .data = x,
541  .size = sizeof(*x),
542  .num_params = 1
543  };
544 
545  return res;
546 }
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(), 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 505 of file pq_query_helper.c.

559 {
561 }
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 505 of file pq_query_helper.c.

552 {
554 }
#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 t)

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

567 {
568  return GNUNET_PQ_query_param_absolute_time_nbo (&x->abs_time_nbo);
569 }
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.

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

194 {
195  struct GNUNET_PQ_QueryParam res = {
196  .conv = &qconv_uint16,
197  .data = x,
198  .size = sizeof(*x),
199  .num_params = 1
200  };
201 
202  return res;
203 }
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 221 of file pq_query_helper.c.

251 {
252  struct GNUNET_PQ_QueryParam res = {
253  .conv = &qconv_uint32,
254  .data = x,
255  .size = sizeof(*x),
256  .num_params = 1
257  };
258 
259  return res;
260 }
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_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 278 of file pq_query_helper.c.

308 {
309  struct GNUNET_PQ_QueryParam res = {
310  .conv = &qconv_uint64,
311  .data = x,
312  .size = sizeof(*x),
313  .num_params = 1
314  };
315 
316  return res;
317 }
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(), 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  .conv = &extract_string,
501  .cleaner = &clean_string,
502  .dst = (void *) dst,
503  .fname = (name)
504  };
505 
506  return res;
507 }
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_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 481 of file pq_result_helper.c.

569 {
570  struct GNUNET_PQ_ResultSpec res = {
571  .conv = &extract_bool,
572  .dst = (void *) dst,
573  .fname = name
574  };
575 
576  return res;
577 }
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 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.

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

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

645 {
646  struct GNUNET_PQ_ResultSpec res = {
647  .conv = &extract_rel_time,
648  .dst = (void *) rt,
649  .dst_size = sizeof(*rt),
650  .fname = name
651  };
652 
653  return res;
654 }
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.

737 {
738  struct GNUNET_PQ_ResultSpec res =
740  &at->abs_value_us__);
741 
742  return res;
743 }
#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 481 of file pq_result_helper.c.

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

950 {
951  struct GNUNET_PQ_ResultSpec res = {
952  .conv = &extract_uint16,
953  .dst = (void *) u16,
954  .dst_size = sizeof(*u16),
955  .fname = name
956  };
957 
958  return res;
959 }
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.

1024 {
1025  struct GNUNET_PQ_ResultSpec res = {
1026  .conv = &extract_uint32,
1027  .dst = (void *) u32,
1028  .dst_size = sizeof(*u32),
1029  .fname = name
1030  };
1031 
1032  return res;
1033 }
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(), 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.

1108 {
1109  struct GNUNET_PQ_ResultSpec res = {
1110  .conv = &extract_uint64,
1111  .dst = (void *) u64,
1112  .dst_size = sizeof(*u64),
1113  .fname = name
1114  };
1115 
1116  return res;
1117 }
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);
99  "pq",
100  "Execution of prepared SQL statement `%s' finished (%d)\n",
101  name,
102  PGRES_COMMAND_OK == PQresultStatus (res));
103  if ( (PGRES_COMMAND_OK != PQresultStatus (res)) &&
104  (CONNECTION_OK != (status = PQstatus (db->conn))) )
105  {
107  "pq",
108  "Database disconnected on SQL statement `%s' (reconnecting)\n",
109  name);
111  res = NULL;
112  }
113 
114  for (off = 0; off < soff; off++)
115  GNUNET_free (scratch[off]);
116  return res;
117  }
118 }
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:94
#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:313
#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 122 of file pq.c.

135 {
136  unsigned int i;
137 
138  if (NULL == result)
139  return GNUNET_SYSERR;
140  for (i = 0; NULL != rs[i].conv; i++)
141  {
142  struct GNUNET_PQ_ResultSpec *spec;
144 
145  spec = &rs[i];
146  ret = spec->conv (spec->cls,
147  result,
148  row,
149  spec->fname,
150  &spec->dst_size,
151  spec->dst);
152  switch (ret)
153  {
154  case GNUNET_OK:
155  /* canonical case, continue below */
156  if (NULL != spec->is_null)
157  *spec->is_null = false;
158  break;
159  case GNUNET_NO:
160  if (spec->is_nullable)
161  {
162  if (NULL != spec->is_null)
163  *spec->is_null = true;
164  continue;
165  }
167  "NULL field encountered for `%s' where non-NULL was required\n",
168  spec->fname);
169  goto cleanup;
170  case GNUNET_SYSERR:
172  "Failed to extract field `%s'\n",
173  spec->fname);
174  GNUNET_break (0);
175  goto cleanup;
176  }
177  if (NULL != spec->result_size)
178  *spec->result_size = spec->dst_size;
179  }
180  return GNUNET_OK;
181 cleanup:
182  for (unsigned int j = 0; j < i; j++)
183  if (NULL != rs[j].cleaner)
184  rs[j].cleaner (rs[j].cls,
185  rs[j].dst);
186  return GNUNET_SYSERR;
187 }
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 122 of file pq.c.

123 {
124  for (unsigned int i = 0; NULL != rs[i].conv; i++)
125  if (NULL != rs[i].cleaner)
126  rs[i].cleaner (rs[i].cls,
127  rs[i].dst);
128 }

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(), 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:132

Referenced by namecache_postgres_lookup_block(), postgres_plugin_del(), and 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,
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()

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.

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  ret = PQdescribePrepared (db->conn,
115  ps[i].name);
116  if (PGRES_COMMAND_OK != PQresultStatus (ret))
117  {
118  PQclear (ret);
119  return GNUNET_SYSERR;
120  }
122  "pq",
123  "Statement `%s' already known. Ignoring the issue in the hope that you are using connection pooling...\n",
124  ps[i].name);
125  }
126  PQclear (ret);
127  }
128  return GNUNET_OK;
129 }
@ GNUNET_ERROR_TYPE_BULK
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.

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);
90  "Running statement `%s' on %p finished (%d)\n",
91  es[i].sql,
92  db,
93  PGRES_COMMAND_OK == PQresultStatus (result));
94  if ((GNUNET_NO == es[i].ignore_errors) &&
95  (PGRES_COMMAND_OK != PQresultStatus (result)))
96  {
98  "pq",
99  "Failed to execute `%s': %s/%s/%s/%s/%s",
100  es[i].sql,
101  PQresultErrorField (result,
102  PG_DIAG_MESSAGE_PRIMARY),
103  PQresultErrorField (result,
104  PG_DIAG_MESSAGE_DETAIL),
105  PQresultErrorMessage (result),
106  PQresStatus (PQresultStatus (result)),
107  PQerrorMessage (db->conn));
108  PQclear (result);
109  return GNUNET_SYSERR;
110  }
111  PQclear (result);
112  }
113  return GNUNET_OK;
114 }

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  {
126  GNUNET_free (db->load_path);
127  GNUNET_free (db->config_str);
128  GNUNET_free (db);
129  return NULL;
130  }
131  return db;
132 }
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_.
#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:313
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 450 of file pq_connect.c.

455 {
457  section,
458  load_path_suffix,
459  es,
460  ps,
462 }
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:466

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

472 {
473  struct GNUNET_PQ_Context *db;
474  char *conninfo;
475  char *load_path;
476  char *sp;
477 
478  if (GNUNET_OK !=
480  section,
481  "CONFIG",
482  &conninfo))
483  conninfo = NULL;
484  load_path = NULL;
485  sp = NULL;
486  if ( (NULL != load_path_suffix) &&
487  (GNUNET_OK ==
489  section,
490  "SQL_DIR",
491  &sp)) )
493  "%s%s",
494  sp,
495  load_path_suffix);
496  db = GNUNET_PQ_connect2 (conninfo == NULL ? "" : conninfo,
497  load_path,
498  es,
499  ps,
500  flags);
502  GNUNET_free (sp);
503  GNUNET_free (conninfo);
504  return db;
505 }
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 301 of file pq_connect.c.

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

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

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

444 {
445  struct GNUNET_DB_EventHandler *eh;
446  bool sub;
447 
448  eh = GNUNET_new (struct GNUNET_DB_EventHandler);
449  eh->db = db;
450  es_to_sh (es,
451  &eh->sh);
452  eh->cb = cb;
453  eh->cb_cls = cb_cls;
454  sub = (NULL ==
456  &eh->sh));
459  &eh->sh,
460  eh,
462  if (NULL == db->event_task)
463  {
465  "Starting event scheduler\n");
467  PQsocket (db->conn));
468  }
469  if (sub)
471  "LISTEN X",
472  eh);
474  &event_timeout,
475  eh);
476  return eh;
477 }
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.
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:1254
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:427
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:345
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:307
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 481 of file pq_event.c.

482 {
483  struct GNUNET_PQ_Context *db = eh->db;
484 
487  &eh->sh,
488  eh));
489  if (NULL ==
491  &eh->sh))
493  "UNLISTEN X",
494  eh);
495  if (0 == GNUNET_CONTAINER_multishortmap_size (db->channel_map))
496  {
498  "Stopping PQ event scheduler job\n");
499  GNUNET_free (db->rfd);
500  if (NULL != db->event_task)
501  {
502  GNUNET_SCHEDULER_cancel (db->event_task);
503  db->event_task = NULL;
504  }
505  }
506  if (NULL != eh->timeout_task)
507  {
509  eh->timeout_task = NULL;
510  }
511  GNUNET_free (eh);
512 }
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:957

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

Here is the call graph for this function:

◆ GNUNET_PQ_event_notify()

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

Notify all that listen on es of an event.

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

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

Definition at line 516 of file pq_event.c.

520 {
521  char sql[16 + 64 + extra_size * 8 / 5 + 8];
522  char *end;
523  PGresult *result;
524 
525  end = stpcpy (sql,
526  "NOTIFY X");
527  end = es_to_channel (es,
528  end);
529  end = stpcpy (end,
530  ", '");
532  extra_size,
533  end,
534  sizeof (sql) - (end - sql) - 1);
535  GNUNET_assert (NULL != end);
536  *end = '\0';
537  end = stpcpy (end,
538  "'");
540  "Executing command `%s'\n",
541  sql);
542  result = PQexec (db->conn,
543  sql);
544  if (PGRES_COMMAND_OK != PQresultStatus (result))
545  {
547  "pq",
548  "Failed to execute `%s': %s/%s/%s/%s/%s",
549  sql,
550  PQresultErrorField (result,
551  PG_DIAG_MESSAGE_PRIMARY),
552  PQresultErrorField (result,
553  PG_DIAG_MESSAGE_DETAIL),
554  PQresultErrorMessage (result),
555  PQresStatus (PQresultStatus (result)),
556  PQerrorMessage (db->conn));
557  }
558  PQclear (result);
559  event_do_poll (db);
560 }
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:709
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.

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

510 {
511  if (NULL == db)
512  return;
513  GNUNET_assert (0 ==
514  GNUNET_CONTAINER_multishortmap_size (db->channel_map));
516  GNUNET_free (db->es);
517  GNUNET_free (db->ps);
518  GNUNET_free (db->load_path);
519  GNUNET_free (db->config_str);
520  PQfinish (db->conn);
521  GNUNET_free (db);
522 }

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: