GNUnet  0.11.x
Data Structures | Macros | Typedefs | 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 int(* 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...
 

Functions

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size (const void *ptr, size_t ptr_size)
 Generate query parameter for a buffer ptr of ptr_size bytes. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string (const char *ptr)
 Generate query parameter for a string. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
 Generate query parameter for an RSA public key. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
 Generate query parameter for an RSA signature. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
 Generate query parameter for an absolute time value. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
 Generate query parameter for an absolute time value. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16 (const uint16_t *x)
 Generate query parameter for an uint16_t in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32 (const uint32_t *x)
 Generate query parameter for an uint32_t in host byte order. More...
 
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64 (const uint64_t *x)
 Generate query parameter for an uint16_t in host byte order. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size (const char *name, void **dst, size_t *sptr)
 Variable-size result expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_fixed_size (const char *name, void *dst, size_t dst_size)
 Fixed-size result expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string (const char *name, char **dst)
 0-terminated string expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_public_key (const char *name, struct GNUNET_CRYPTO_RsaPublicKey **rsa)
 RSA public key expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_signature (const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
 RSA signature expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at)
 Absolute time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
 Absolute time expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint16 (const char *name, uint16_t *u16)
 uint16_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32 (const char *name, uint32_t *u32)
 uint32_t expected. More...
 
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64 (const char *name, uint64_t *u64)
 uint64_t expected. More...
 
PGresult * GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db, const char *name, const struct GNUNET_PQ_QueryParam *params)
 Execute a prepared statement. More...
 
int 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 connnection using the given params. More...
 
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
 Execute a named prepared statement that is a SELECT statement which may return multiple results in connection using the given params. More...
 
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
 Execute a named prepared statement that is a SELECT statement which must return a single result in connection using the given params. More...
 
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare (const char *name, const char *sql, unsigned int num_args)
 Create a struct GNUNET_PQ_PreparedStatement. More...
 
int GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres. More...
 
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute (const char *sql)
 Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal. More...
 
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute (const char *sql)
 Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated. More...
 
int 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 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_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, 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...
 
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...
 
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

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

◆ GNUNET_PQ_result_spec_end

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

End of result parameter specification.

Returns
array last entry for the result specification to use

Definition at line 289 of file gnunet_pq_lib.h.

Referenced by extract_result_cb(), handle_results(), namecache_postgres_lookup_block(), parse_result_call_iterator(), postgres_plugin_del(), postgres_plugin_estimate_size(), postgres_plugin_get_random(), process_keys(), and process_result().

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

Referenced by extract_result_cb(), GNUNET_PQ_result_spec_absolute_time_nbo(), parse_result_call_iterator(), postgres_plugin_del(), postgres_plugin_get_random(), process_keys(), and process_result().

◆ GNUNET_PQ_PREPARED_STATEMENT_END

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

Terminator for prepared statement list.

Definition at line 608 of file gnunet_pq_lib.h.

Referenced by database_setup(), and init_connection().

◆ GNUNET_PQ_EXECUTE_STATEMENT_END

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

Terminator for executable statement list.

Definition at line 666 of file gnunet_pq_lib.h.

Referenced by database_setup(), init_connection(), and postgres_plugin_drop().

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

◆ GNUNET_PQ_ResultConverter

typedef int(* 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_SYSERR if a result was invalid (non-existing field or NULL)

Definition at line 219 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 235 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 527 of file gnunet_pq_lib.h.

Function Documentation

◆ GNUNET_PQ_query_param_fixed_size()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size ( const void *  ptr,
size_t  ptr_size 
)

Generate query parameter for a buffer ptr of ptr_size bytes.

Parameters
ptrpointer to the query parameter to pass ptr_size number of bytes in ptr

Definition at line 75 of file pq_query_helper.c.

References qconv_fixed(), and res.

Referenced by GNUNET_PQ_query_param_string(), namecache_postgres_cache_block(), namestore_postgres_store_records(), postgres_plugin_put(), and postgres_plugin_remove_key().

77 {
78  struct GNUNET_PQ_QueryParam res =
79  { &qconv_fixed, NULL, ptr, ptr_size, 1 };
80 
81  return res;
82 }
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.
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
Here is the call graph for this function:
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

Definition at line 91 of file pq_query_helper.c.

References GNUNET_PQ_query_param_fixed_size().

Referenced by namestore_postgres_lookup_records(), and namestore_postgres_store_records().

92 {
93  return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
94 }
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_rsa_public_key()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_public_key ( const struct GNUNET_CRYPTO_RsaPublicKey x)

Generate query parameter for an RSA public key.

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

Parameters
xthe query parameter to pass.

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

Parameters
xthe query parameter to pass
Returns
array entry for the query parameters to use

Definition at line 321 of file pq_query_helper.c.

References qconv_rsa_public_key(), and res.

323 {
324  struct GNUNET_PQ_QueryParam res =
325  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
326 
327  return res;
328 }
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.
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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

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

Parameters
xthe query parameter to pass
Returns
array entry for the query parameters to use

Definition at line 381 of file pq_query_helper.c.

References qconv_rsa_signature(), and res.

382 {
383  struct GNUNET_PQ_QueryParam res =
384  { &qconv_rsa_signature, NULL, (x), 0, 1 };
385 
386  return res;
387 }
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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.
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

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass
Returns
array entry for the query parameters to use

Definition at line 443 of file pq_query_helper.c.

References qconv_abs_time(), and res.

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

444 {
445  struct GNUNET_PQ_QueryParam res =
446  { &qconv_abs_time, NULL, x, sizeof(*x), 1 };
447 
448  return res;
449 }
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.
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_query_param_absolute_time_nbo()

struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo ( const struct GNUNET_TIME_AbsoluteNBO x)

Generate query parameter for an absolute time value.

The database must store a 64-bit integer.

Parameters
xpointer to the query parameter to pass

Definition at line 459 of file pq_query_helper.c.

References GNUNET_PQ_query_param_auto_from_type.

Referenced by delete_old_block(), and namecache_postgres_cache_block().

461 {
463 }
#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).
Here is the caller graph for this function:

◆ 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

Definition at line 146 of file pq_query_helper.c.

References qconv_uint16(), and res.

Referenced by postgres_plugin_get_key().

147 {
148  struct GNUNET_PQ_QueryParam res =
149  { &qconv_uint16, NULL, x, sizeof(*x), 1 };
150 
151  return res;
152 }
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.
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
Here is the call graph for this function:
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

Definition at line 204 of file pq_query_helper.c.

References qconv_uint32(), and res.

Referenced by namestore_postgres_store_records(), postgres_plugin_del(), postgres_plugin_get(), postgres_plugin_get_closest(), postgres_plugin_get_key(), postgres_plugin_get_random(), postgres_plugin_get_zero_anonymity(), postgres_plugin_put(), process_result(), and repl_proc().

205 {
206  struct GNUNET_PQ_QueryParam res =
207  { &qconv_uint32, NULL, x, sizeof(*x), 1 };
208 
209  return res;
210 }
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.
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
Here is the call graph for this function:
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

Generate query parameter for an uint16_t in host byte order.

Parameters
xpointer to the query parameter to pass

Definition at line 262 of file pq_query_helper.c.

References qconv_uint64(), and res.

Referenced by namestore_postgres_iterate_records(), namestore_postgres_store_records(), postgres_plugin_get_key(), postgres_plugin_get_zero_anonymity(), and postgres_plugin_put().

263 {
264  struct GNUNET_PQ_QueryParam res =
265  { &qconv_uint64, NULL, x, sizeof(*x), 1 };
266 
267  return res;
268 }
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_variable_size()

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

Variable-size result expected.

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

Definition at line 124 of file pq_result_helper.c.

References clean_varsize_blob(), GNUNET_PQ_ResultSpec::dst, extract_varsize_blob(), name, and res.

Referenced by extract_result_cb(), handle_results(), namecache_postgres_lookup_block(), parse_result_call_iterator(), postgres_plugin_get_random(), and process_result().

127 {
128  struct GNUNET_PQ_ResultSpec res =
130  &clean_varsize_blob, NULL,
131  (void *) (dst), 0, name, sptr };
132 
133  return res;
134 }
Description of a DB result cell.
static int 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 int res
static void clean_varsize_blob(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
const char * name
void * dst
Destination for the data.
Here is the call graph for this function:
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 208 of file pq_result_helper.c.

References GNUNET_PQ_ResultSpec::dst, GNUNET_PQ_ResultSpec::dst_size, extract_fixed_blob(), and res.

211 {
212  struct GNUNET_PQ_ResultSpec res =
214  NULL, NULL,
215  (dst), dst_size, name, NULL };
216 
217  return res;
218 }
Description of a DB result cell.
static int 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.
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...
static int res
const char * name
void * dst
Destination for the data.
Here is the call graph for this function:

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

References clean_string(), GNUNET_PQ_ResultSpec::dst, extract_string(), and res.

Referenced by parse_result_call_iterator().

522 {
523  struct GNUNET_PQ_ResultSpec res =
524  { &extract_string,
525  &clean_string,
526  NULL,
527  (void *) dst, 0, (name), NULL };
528 
529  return res;
530 }
static void clean_string(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
Description of a DB result cell.
static int 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 int res
const char * name
void * dst
Destination for the data.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_result_spec_rsa_public_key()

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

RSA public key expected.

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

Definition at line 312 of file pq_result_helper.c.

References clean_rsa_public_key(), extract_rsa_public_key(), and res.

314 {
315  struct GNUNET_PQ_ResultSpec res =
318  NULL,
319  (void *) rsa, 0, name, NULL };
320 
321  return res;
322 }
Description of a DB result cell.
static int res
static void clean_rsa_public_key(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
static int 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.
const char * name
Here is the call graph for this function:

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

References clean_rsa_signature(), extract_rsa_signature(), and res.

418 {
419  struct GNUNET_PQ_ResultSpec res =
422  NULL,
423  (void *) sig, 0, (name), NULL };
424 
425  return res;
426 }
Description of a DB result cell.
static void clean_rsa_signature(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
static int res
static int 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.
const char * name
Here is the call graph for this function:

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

References extract_abs_time(), name, and res.

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

608 {
609  struct GNUNET_PQ_ResultSpec res =
610  { &extract_abs_time,
611  NULL,
612  NULL,
613  (void *) at, sizeof(*at), (name), NULL };
614 
615  return res;
616 }
Description of a DB result cell.
static int res
static int 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.
const char * name
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

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

References GNUNET_PQ_result_spec_auto_from_type, and res.

629 {
630  struct GNUNET_PQ_ResultSpec res =
632 
633  return res;
634 }
Description of a DB result cell.
static int res
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst
const char * name
uint64_t abs_value_us__
The actual value (in network byte order).

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

References extract_uint16(), name, and res.

709 {
710  struct GNUNET_PQ_ResultSpec res =
711  { &extract_uint16,
712  NULL,
713  NULL,
714  (void *) u16, sizeof(*u16), (name), NULL };
715 
716  return res;
717 }
Description of a DB result cell.
static int res
const char * name
static int 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.
Here is the call graph for this function:

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

References extract_uint32(), name, and res.

Referenced by extract_result_cb(), handle_results(), parse_result_call_iterator(), postgres_plugin_del(), postgres_plugin_get_random(), and process_result().

792 {
793  struct GNUNET_PQ_ResultSpec res =
794  { &extract_uint32,
795  NULL,
796  NULL,
797  (void *) u32, sizeof(*u32), (name), NULL };
798 
799  return res;
800 }
Description of a DB result cell.
static int 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.
static int res
const char * name
Here is the call graph for this function:
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 873 of file pq_result_helper.c.

References extract_uint64(), name, and res.

Referenced by parse_result_call_iterator(), and postgres_plugin_estimate_size().

875 {
876  struct GNUNET_PQ_ResultSpec res =
877  { &extract_uint64,
878  NULL,
879  NULL,
880  (void *) u64, sizeof(*u64), (name), NULL };
881 
882  return res;
883 }
Description of a DB result cell.
static int 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.
static int res
const char * name
Here is the call graph for this function:
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)
Parameters
dbdatabase handle
namename of the prepared statement
paramsparameters to the statement
Returns
postgres result

Definition at line 39 of file pq.c.

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

Referenced by GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_eval_prepared_non_select(), and GNUNET_PQ_eval_prepared_singleton_select().

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

◆ GNUNET_PQ_extract_result()

int 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)
Deprecated:
(should become an internal API)
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 151 of file pq.c.

References GNUNET_PQ_ResultSpec::cls, GNUNET_PQ_ResultSpec::conv, GNUNET_PQ_ResultSpec::dst, GNUNET_PQ_ResultSpec::dst_size, GNUNET_PQ_ResultSpec::fname, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_SYSERR, GNUNET_PQ_ResultSpec::result_size, and ret.

Referenced by extract_result_cb(), GNUNET_PQ_eval_prepared_singleton_select(), handle_results(), parse_result_call_iterator(), process_keys(), and process_result().

154 {
155  if (NULL == result)
156  return GNUNET_SYSERR;
157  for (unsigned int i = 0; NULL != rs[i].conv; i++)
158  {
159  struct GNUNET_PQ_ResultSpec *spec;
160  int ret;
161 
162  spec = &rs[i];
163  ret = spec->conv (spec->cls,
164  result,
165  row,
166  spec->fname,
167  &spec->dst_size,
168  spec->dst);
169  if (GNUNET_OK != ret)
170  {
172  return GNUNET_SYSERR;
173  }
174  if (NULL != spec->result_size)
175  *spec->result_size = spec->dst_size;
176  }
177  return GNUNET_OK;
178 }
const char * fname
Field name of the desired result.
Description of a DB result cell.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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:130
void * cls
Closure for conv and cleaner.
static int result
Global testing status.
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...
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
void * dst
Destination for the data.
size_t * result_size
Where to store actual size of the result.
GNUNET_PQ_ResultConverter conv
What is the format of the result?
Here is the call graph for this function:
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 130 of file pq.c.

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

Referenced by extract_result_cb(), GNUNET_PQ_extract_result(), handle_results(), namecache_postgres_lookup_block(), parse_result_call_iterator(), postgres_plugin_del(), postgres_plugin_get_random(), process_keys(), and process_result().

131 {
132  for (unsigned int i = 0; NULL != rs[i].conv; i++)
133  if (NULL != rs[i].cleaner)
134  rs[i].cleaner (rs[i].cls,
135  rs[i].dst);
136 }
void * cls
Closure for conv and cleaner.
GNUNET_PQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.
void * dst
Destination for the data.
GNUNET_PQ_ResultConverter conv
What is the format of the 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 59 of file pq_eval.c.

References GNUNET_PQ_Context::conn, GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_DB_STATUS_SOFT_ERROR, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log_from, GNUNET_PQ_reconnect(), PQ_DIAG_SQLSTATE_DEADLOCK, PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE, PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION, and status.

Referenced by GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_eval_prepared_non_select(), and GNUNET_PQ_eval_prepared_singleton_select().

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 }
#define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE
Error code returned by Postgres on serialization failure.
Definition: pq_eval.c:42
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:150
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:45
A hard error occurred, retrying will not help.
Definition: gnunet_db_lib.h:39
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
PGconn * conn
Actual connection.
Definition: pq.h:39
#define PQ_DIAG_SQLSTATE_DEADLOCK
Error code returned by Postgres for deadlock.
Definition: pq_eval.c:32
#define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION
Error code returned by Postgres for uniqueness violation.
Definition: pq_eval.c:37
uint16_t status
See PRISM_STATUS_*-constants.
static int result
Global testing status.
#define GNUNET_log_from(kind, comp,...)
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 connnection 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 statment 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 statment 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 164 of file pq_eval.c.

References GNUNET_DB_STATUS_SOFT_ERROR, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_PQ_eval_result(), GNUNET_PQ_exec_prepared(), and result.

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

167 {
168  PGresult *result;
169  enum GNUNET_DB_QueryStatus qs;
170 
171  result = GNUNET_PQ_exec_prepared (db,
172  statement_name,
173  params);
174  if (NULL == result)
176  qs = GNUNET_PQ_eval_result (db,
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 }
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(struct GNUNET_PQ_Context *db, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:59
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:39
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:45
static int result
Global testing status.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
Here is the call graph for this function:
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 209 of file pq_eval.c.

References GNUNET_DB_STATUS_SOFT_ERROR, GNUNET_PQ_eval_result(), GNUNET_PQ_exec_prepared(), result, and ret.

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

214 {
215  PGresult *result;
216  enum GNUNET_DB_QueryStatus qs;
217  unsigned int ret;
218 
219  result = GNUNET_PQ_exec_prepared (db,
220  statement_name,
221  params);
222  if (NULL == result)
224  qs = GNUNET_PQ_eval_result (db,
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 }
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(struct GNUNET_PQ_Context *db, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:59
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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:39
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:45
static int result
Global testing status.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
Here is the call graph for this function:
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 258 of file pq_eval.c.

References GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_DB_STATUS_SOFT_ERROR, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_OK, GNUNET_PQ_eval_result(), GNUNET_PQ_exec_prepared(), GNUNET_PQ_extract_result(), and result.

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

263 {
264  PGresult *result;
265  enum GNUNET_DB_QueryStatus qs;
266 
267  result = GNUNET_PQ_exec_prepared (db,
268  statement_name,
269  params);
270  if (NULL == result)
272  qs = GNUNET_PQ_eval_result (db,
273  statement_name,
274  result);
275  if (qs < 0)
276  {
277  PQclear (result);
278  return qs;
279  }
280  if (0 == PQntuples (result))
281  {
282  PQclear (result);
284  }
285  if (1 != PQntuples (result))
286  {
287  /* more than one result, but there must be at most one */
288  GNUNET_break (0);
289  PQclear (result);
291  }
292  if (GNUNET_OK !=
293  GNUNET_PQ_extract_result (result,
294  rs,
295  0))
296  {
297  PQclear (result);
299  }
300  PQclear (result);
302 }
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(struct GNUNET_PQ_Context *db, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:59
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:39
A soft error occurred, retrying the transaction may succeed.
Definition: gnunet_db_lib.h:45
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
A hard error occurred, retrying will not help.
Definition: gnunet_db_lib.h:39
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static int result
Global testing status.
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:58
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
int 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:151
Here is the call graph for this function:
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 38 of file pq_prepare.c.

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

Referenced by database_setup(), and init_connection().

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.
const char * name
Here is the caller graph for this function:

◆ GNUNET_PQ_prepare_statements()

int GNUNET_PQ_prepare_statements ( struct GNUNET_PQ_Context db,
const struct GNUNET_PQ_PreparedStatement ps 
)

Request creation of prepared statements ps from Postgres.

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

Definition at line 62 of file pq_prepare.c.

References _, GNUNET_PQ_Context::conn, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free_non_null, GNUNET_log_from, GNUNET_new_array, GNUNET_OK, GNUNET_SYSERR, GNUNET_PQ_PreparedStatement::name, GNUNET_PQ_PreparedStatement::num_arguments, GNUNET_PQ_Context::ps, ret, and GNUNET_PQ_PreparedStatement::sql.

Referenced by GNUNET_PQ_reconnect().

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));
87  db->ps = rps;
88  }
89 
90  /* actually prepare statements */
91  for (unsigned int i = 0; NULL != ps[i].name; i++)
92  {
93  PGresult *ret;
94 
96  "pq",
97  "Preparing SQL statement `%s' as `%s'\n",
98  ps[i].sql,
99  ps[i].name);
100  ret = PQprepare (db->conn,
101  ps[i].name,
102  ps[i].sql,
103  ps[i].num_arguments,
104  NULL);
105  if (PGRES_COMMAND_OK != PQresultStatus (ret))
106  {
108  "pq",
109  _ ("PQprepare (`%s' as `%s') failed with error: %s\n"),
110  ps[i].sql,
111  ps[i].name,
112  PQerrorMessage (db->conn));
113  PQclear (ret);
114  return GNUNET_SYSERR;
115  }
116  PQclear (ret);
117  }
118  return GNUNET_OK;
119 }
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
const char * name
Name of the statement.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
PGconn * conn
Actual connection.
Definition: pq.h:39
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
const char * sql
Actual SQL statement.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
const char * name
unsigned int num_arguments
Number of arguments included in sql.
#define GNUNET_log_from(kind, comp,...)
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 36 of file pq_exec.c.

References GNUNET_NO, and GNUNET_PQ_ExecuteStatement::sql.

Referenced by database_setup(), init_connection(), and postgres_plugin_drop().

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().
#define GNUNET_NO
Definition: gnunet_common.h:78
const char * sql
Actual SQL statement.
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 55 of file pq_exec.c.

References GNUNET_YES, and GNUNET_PQ_ExecuteStatement::sql.

Referenced by database_setup(), and init_connection().

56 {
57  struct GNUNET_PQ_ExecuteStatement es = {
58  .sql = sql,
59  .ignore_errors = GNUNET_YES
60  };
61 
62  return es;
63 }
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
#define GNUNET_YES
Definition: gnunet_common.h:77
const char * sql
Actual SQL statement.
Here is the caller graph for this function:

◆ GNUNET_PQ_exec_statements()

int 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 76 of file pq_exec.c.

References GNUNET_PQ_Context::conn, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_PQ_ExecuteStatement::ignore_errors, result, and GNUNET_PQ_ExecuteStatement::sql.

Referenced by GNUNET_PQ_reconnect(), and postgres_plugin_drop().

78 {
79  for (unsigned int i = 0; NULL != es[i].sql; i++)
80  {
81  PGresult *result;
82 
84  "Running statement `%s' on %p\n",
85  es[i].sql,
86  db);
87  result = PQexec (db->conn,
88  es[i].sql);
89  if ((GNUNET_NO == es[i].ignore_errors) &&
90  (PGRES_COMMAND_OK != PQresultStatus (result)))
91  {
93  "pq",
94  "Failed to execute `%s': %s/%s/%s/%s/%s",
95  es[i].sql,
96  PQresultErrorField (result,
97  PG_DIAG_MESSAGE_PRIMARY),
98  PQresultErrorField (result,
99  PG_DIAG_MESSAGE_DETAIL),
100  PQresultErrorMessage (result),
101  PQresStatus (PQresultStatus (result)),
102  PQerrorMessage (db->conn));
103  PQclear (result);
104  return GNUNET_SYSERR;
105  }
106  PQclear (result);
107  }
108  return GNUNET_OK;
109 }
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
int ignore_errors
Should we ignore errors?
PGconn * conn
Actual connection.
Definition: pq.h:39
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
const char * sql
Actual SQL statement.
Here is the caller graph for this function:

◆ GNUNET_PQ_connect()

struct GNUNET_PQ_Context* GNUNET_PQ_connect ( const char *  config_str,
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 es are executed whenever we (re)connect to the database, and that the prepared statements in ps are "ready". If statements in fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.

Parameters
config_strconfiguration to use
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

Initialize logging via GNUnet's log routines and disable Postgres's logger. 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". If statements in fail that were created with GNUNET_PQ_make_execute(), then the entire operation fails.

The caller MUST ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as they may be needed repeatedly and no copy will be made.

Parameters
config_strconfiguration to use
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 86 of file pq_connect.c.

References GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::conn, db, GNUNET_PQ_Context::es, GNUNET_free, GNUNET_new, GNUNET_new_array, GNUNET_PQ_reconnect(), GNUNET_strdup, name, and GNUNET_PQ_Context::ps.

Referenced by GNUNET_PQ_connect_with_cfg().

89 {
90  struct GNUNET_PQ_Context *db;
91  unsigned int elen = 0;
92  unsigned int plen = 0;
93 
94  if (NULL != es)
95  while (NULL != es[elen].sql)
96  elen++;
97  if (NULL != ps)
98  while (NULL != ps[plen].name)
99  plen++;
100 
101  db = GNUNET_new (struct GNUNET_PQ_Context);
103  if (0 != elen)
104  {
105  db->es = GNUNET_new_array (elen + 1,
107  memcpy (db->es,
108  es,
109  elen * sizeof (struct GNUNET_PQ_ExecuteStatement));
110  }
111  if (0 != plen)
112  {
113  db->ps = GNUNET_new_array (plen + 1,
115  memcpy (db->ps,
116  ps,
117  plen * sizeof (struct GNUNET_PQ_PreparedStatement));
118  }
119  GNUNET_PQ_reconnect (db);
120  if (NULL == db->conn)
121  {
122  GNUNET_free (db->config_str);
123  GNUNET_free (db);
124  return NULL;
125  }
126  return db;
127 }
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:150
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:44
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
PGconn * conn
Actual connection.
Definition: pq.h:39
char * config_str
Configuration to use to connect to the DB.
Definition: pq.h:54
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
const char * name
Handle to Postgres database.
Definition: pq.h:34
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_connect_with_cfg()

struct GNUNET_PQ_Context* GNUNET_PQ_connect_with_cfg ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section,
const 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".

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
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

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 MUST ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as they may be needed repeatedly and no copy will be made.

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
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 217 of file pq_connect.c.

References db, GNUNET_CONFIGURATION_get_value_string(), GNUNET_free_non_null, GNUNET_OK, and GNUNET_PQ_connect().

Referenced by database_setup(), and init_connection().

221 {
222  struct GNUNET_PQ_Context *db;
223  char *conninfo;
224 
225  if (GNUNET_OK !=
227  section,
228  "CONFIG",
229  &conninfo))
230  conninfo = NULL;
231  db = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo,
232  es,
233  ps);
234  GNUNET_free_non_null (conninfo);
235  return db;
236 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static char * section
Name of the section.
Definition: gnunet-config.c:33
int 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.
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
struct GNUNET_PQ_Context * GNUNET_PQ_connect(const char *config_str, 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.
Definition: pq_connect.c:86
Handle to Postgres database.
Definition: pq.h:34
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 136 of file pq_connect.c.

References GNUNET_PQ_Context::conn, and GNUNET_PQ_reconnect().

137 {
138  if (CONNECTION_BAD != PQstatus (db->conn))
139  return;
140  GNUNET_PQ_reconnect (db);
141 }
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:150
PGconn * conn
Actual connection.
Definition: pq.h:39
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 150 of file pq_connect.c.

References GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::conn, GNUNET_PQ_Context::es, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, GNUNET_OK, GNUNET_PQ_exec_statements(), GNUNET_PQ_prepare_statements(), pq_notice_processor_cb(), pq_notice_receiver_cb(), and GNUNET_PQ_Context::ps.

Referenced by GNUNET_PQ_connect(), GNUNET_PQ_eval_result(), GNUNET_PQ_exec_prepared(), and GNUNET_PQ_reconnect_if_down().

151 {
152  if (NULL != db->conn)
153  PQfinish (db->conn);
154  db->conn = PQconnectdb (db->config_str);
155  if ((NULL == db->conn) ||
156  (CONNECTION_OK != PQstatus (db->conn)))
157  {
159  "pq",
160  "Database connection to '%s' failed: %s\n",
161  db->config_str,
162  (NULL != db->conn) ?
163  PQerrorMessage (db->conn)
164  : "PQconnectdb returned NULL");
165  if (NULL != db->conn)
166  {
167  PQfinish (db->conn);
168  db->conn = NULL;
169  }
170  return;
171  }
172  PQsetNoticeReceiver (db->conn,
174  db);
175  PQsetNoticeProcessor (db->conn,
177  db);
178  if ( (NULL != db->es) &&
179  (GNUNET_OK !=
181  db->es)) )
182  {
183  PQfinish (db->conn);
184  db->conn = NULL;
185  return;
186  }
187  if ( (NULL != db->ps) &&
188  (GNUNET_OK !=
190  db->ps)) )
191  {
192  PQfinish (db->conn);
193  db->conn = NULL;
194  return;
195  }
196 }
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:44
int GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition: pq_prepare.c:62
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
PGconn * conn
Actual connection.
Definition: pq.h:39
int 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
char * config_str
Configuration to use to connect to the DB.
Definition: pq.h:54
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:38
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:55
#define GNUNET_log_from(kind, comp,...)
Here is the call graph for this function:
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 246 of file pq_connect.c.

References GNUNET_PQ_Context::conn, GNUNET_PQ_Context::es, GNUNET_free, GNUNET_free_non_null, and GNUNET_PQ_Context::ps.

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

247 {
248  GNUNET_free_non_null (db->es);
249  GNUNET_free_non_null (db->ps);
250  PQfinish (db->conn);
251  GNUNET_free (db);
252 }
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:44
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
PGconn * conn
Actual connection.
Definition: pq.h:39
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: