GNUnet  0.10.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 (PGconn *db_conn, 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 (PGconn *connection, 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 (PGconn *connection, 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 (PGconn *connection, 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 (PGconn *connection, 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 (PGconn *connection, 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 (PGconn *connection, const struct GNUNET_PQ_ExecuteStatement *es)
 Request execution of an array of statements es from Postgres. More...
 
PGconn * GNUNET_PQ_connect (const char *config_str)
 Create a connection to the Postgres database using config_str for the configuration. More...
 
PGconn * GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Connect to a postgres database using the configuration option "CONFIG" in section. 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 287 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 326 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 599 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:79

Terminator for executable statement list.

Definition at line 658 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 49 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 218 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 233 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 518 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  return res;
81 }
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:63
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 90 of file pq_query_helper.c.

References GNUNET_PQ_query_param_fixed_size().

Referenced by namestore_postgres_lookup_records(), and namestore_postgres_store_records().

91 {
92  return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
93 }
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 317 of file pq_query_helper.c.

References qconv_rsa_public_key(), and res.

318 {
319  struct GNUNET_PQ_QueryParam res =
320  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
321  return res;
322 }
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:63
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 375 of file pq_query_helper.c.

References qconv_rsa_signature(), and res.

376 {
377  struct GNUNET_PQ_QueryParam res =
378  { &qconv_rsa_signature, NULL, (x), 0, 1 };
379  return res;
380 }
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
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 436 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().

437 {
438  struct GNUNET_PQ_QueryParam res =
439  { &qconv_abs_time, NULL, x, sizeof (*x), 1 };
440 
441  return res;
442 }
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:63
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 452 of file pq_query_helper.c.

References GNUNET_PQ_query_param_auto_from_type.

Referenced by delete_old_block(), and namecache_postgres_cache_block().

453 {
455 }
#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 145 of file pq_query_helper.c.

References qconv_uint16(), and res.

Referenced by postgres_plugin_get_key().

146 {
147  struct GNUNET_PQ_QueryParam res =
148  { &qconv_uint16, NULL, x, sizeof (*x), 1 };
149  return res;
150 }
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:63
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 202 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().

203 {
204  struct GNUNET_PQ_QueryParam res =
205  { &qconv_uint32, NULL, x, sizeof (*x), 1 };
206  return res;
207 }
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:63
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 259 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().

260 {
261  struct GNUNET_PQ_QueryParam res =
262  { &qconv_uint64, NULL, x, sizeof (*x), 1 };
263  return res;
264 }
static int res
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
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  return res;
133 }
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
const char * name
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.
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 207 of file pq_result_helper.c.

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

210 {
211  struct GNUNET_PQ_ResultSpec res =
213  NULL, NULL,
214  (dst), dst_size, name, NULL };
215  return res;
216 }
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 516 of file pq_result_helper.c.

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

Referenced by parse_result_call_iterator().

518 {
519  struct GNUNET_PQ_ResultSpec res =
520  { &extract_string,
521  &clean_string,
522  NULL,
523  (void *) dst, 0, (name), NULL };
524  return res;
525 }
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 310 of file pq_result_helper.c.

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

312 {
313  struct GNUNET_PQ_ResultSpec res =
316  NULL,
317  (void *) rsa, 0, name, NULL };
318  return res;
319 }
Description of a DB result cell.
static int res
const char * name
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.
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 413 of file pq_result_helper.c.

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

415 {
416  struct GNUNET_PQ_ResultSpec res =
419  NULL,
420  (void *) sig, 0, (name), NULL };
421  return res;
422 }
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
const char * name
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.
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 601 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().

603 {
604  struct GNUNET_PQ_ResultSpec res =
605  { &extract_abs_time,
606  NULL,
607  NULL,
608  (void *) at, sizeof (*at), (name), NULL };
609  return res;
610 }
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 621 of file pq_result_helper.c.

References GNUNET_PQ_result_spec_auto_from_type, and res.

623 {
624  struct GNUNET_PQ_ResultSpec res =
626  return res;
627 }
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 700 of file pq_result_helper.c.

References extract_uint16(), name, and res.

702 {
703  struct GNUNET_PQ_ResultSpec res =
704  { &extract_uint16,
705  NULL,
706  NULL,
707  (void *) u16, sizeof (*u16), (name), NULL };
708  return res;
709 }
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 782 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().

784 {
785  struct GNUNET_PQ_ResultSpec res =
786  { &extract_uint32,
787  NULL,
788  NULL,
789  (void *) u32, sizeof (*u32), (name), NULL };
790  return res;
791 }
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 864 of file pq_result_helper.c.

References extract_uint64(), name, and res.

Referenced by parse_result_call_iterator(), and postgres_plugin_estimate_size().

866 {
867  struct GNUNET_PQ_ResultSpec res =
868  { &extract_uint64,
869  NULL,
870  NULL,
871  (void *) u64, sizeof (*u64), (name), NULL };
872  return res;
873 }
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 ( PGconn *  db_conn,
const char *  name,
const struct GNUNET_PQ_QueryParam params 
)

Execute a prepared statement.

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

Definition at line 41 of file pq.c.

References 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, len, GNUNET_PQ_QueryParam::num_params, res, ret, and GNUNET_PQ_QueryParam::size.

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

44 {
45  unsigned int len;
46  unsigned int i;
47 
49  "Running prepared statement `%s' on %p\n",
50  name,
51  db_conn);
52  /* count the number of parameters */
53  len = 0;
54  for (i=0;0 != params[i].num_params;i++)
55  len += params[i].num_params;
56 
57  /* new scope to allow stack allocation without alloca */
58  {
59  /* Scratch buffer for temporary storage */
60  void *scratch[len];
61  /* Parameter array we are building for the query */
62  void *param_values[len];
63  int param_lengths[len];
64  int param_formats[len];
65  unsigned int off;
66  /* How many entries in the scratch buffer are in use? */
67  unsigned int soff;
68  PGresult *res;
69  int ret;
70 
71  off = 0;
72  soff = 0;
73  for (i=0;0 != params[i].num_params;i++)
74  {
75  const struct GNUNET_PQ_QueryParam *x = &params[i];
76 
77  ret = x->conv (x->conv_cls,
78  x->data,
79  x->size,
80  &param_values[off],
81  &param_lengths[off],
82  &param_formats[off],
83  x->num_params,
84  &scratch[soff],
85  len - soff);
86  if (ret < 0)
87  {
88  for (off = 0; off < soff; off++)
89  GNUNET_free (scratch[off]);
90  return NULL;
91  }
92  soff += ret;
93  off += x->num_params;
94  }
95  GNUNET_assert (off == len);
97  "pq",
98  "Executing prepared SQL statement `%s'\n",
99  name);
100  res = PQexecPrepared (db_conn,
101  name,
102  len,
103  (const char **) param_values,
104  param_lengths,
105  param_formats,
106  1);
107  for (off = 0; off < soff; off++)
108  GNUNET_free (scratch[off]);
109  return res;
110  }
111 }
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.
const void * data
Data or NULL.
Definition: gnunet_pq_lib.h:79
static int ret
Final status code.
Definition: gnunet-arm.c:89
void * conv_cls
Closure for conv.
Definition: gnunet_pq_lib.h:74
static int res
const char * name
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_pq_lib.h:89
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
#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 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 144 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().

147 {
148  unsigned int i;
149  int ret;
150 
151  for (i=0; NULL != rs[i].conv; i++)
152  {
153  struct GNUNET_PQ_ResultSpec *spec;
154 
155  spec = &rs[i];
156  ret = spec->conv (spec->cls,
157  result,
158  row,
159  spec->fname,
160  &spec->dst_size,
161  spec->dst);
162  if (GNUNET_OK != ret)
163  {
165  return GNUNET_SYSERR;
166  }
167  if (NULL != spec->result_size)
168  *spec->result_size = spec->dst_size;
169  }
170  return GNUNET_OK;
171 }
const char * fname
Field name of the desired result.
Description of a DB result cell.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
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:121
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:79
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 121 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().

122 {
123  unsigned int i;
124 
125  for (i=0; NULL != rs[i].conv; i++)
126  if (NULL != rs[i].cleaner)
127  rs[i].cleaner (rs[i].cls,
128  rs[i].dst);
129 }
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 ( PGconn *  connection,
const char *  statement_name,
PGresult *  result 
)

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

Also logs errors.

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

Definition at line 60 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_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log_from, PQ_DIAG_SQLSTATE_DEADLOCK, PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE, and PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION.

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

63 {
64  ExecStatusType est;
65 
66  est = PQresultStatus (result);
67  if ( (PGRES_COMMAND_OK != est) &&
68  (PGRES_TUPLES_OK != est) )
69  {
70  const char *sqlstate;
71 
72  sqlstate = PQresultErrorField (result,
73  PG_DIAG_SQLSTATE);
74  if (NULL == sqlstate)
75  {
76  /* very unexpected... */
77  GNUNET_break (0);
79  }
80  if ( (0 == strcmp (sqlstate,
82  (0 == strcmp (sqlstate,
84  {
85  /* These two can be retried and have a fair chance of working
86  the next time */
88  "pq",
89  "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
90  statement_name,
91  PQresultErrorField (result,
92  PG_DIAG_MESSAGE_PRIMARY),
93  PQresultErrorField (result,
94  PG_DIAG_MESSAGE_DETAIL),
95  PQresultErrorMessage (result),
96  PQresStatus (PQresultStatus (result)),
97  PQerrorMessage (connection));
99  }
100  if (0 == strcmp (sqlstate,
102  {
103  /* Likely no need to retry, INSERT of "same" data. */
105  "pq",
106  "Query `%s' failed with unique violation: %s/%s/%s/%s/%s\n",
107  statement_name,
108  PQresultErrorField (result,
109  PG_DIAG_MESSAGE_PRIMARY),
110  PQresultErrorField (result,
111  PG_DIAG_MESSAGE_DETAIL),
112  PQresultErrorMessage (result),
113  PQresStatus (PQresultStatus (result)),
114  PQerrorMessage (connection));
116  }
118  "pq",
119  "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
120  statement_name,
121  PQresultErrorField (result,
122  PG_DIAG_MESSAGE_PRIMARY),
123  PQresultErrorField (result,
124  PG_DIAG_MESSAGE_DETAIL),
125  PQresultErrorMessage (result),
126  PQresStatus (PQresultStatus (result)),
127  PQerrorMessage (connection));
129  }
131 }
#define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE
Error code returned by Postgres on serialization failure.
Definition: pq_eval.c:43
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...
#define PQ_DIAG_SQLSTATE_DEADLOCK
Error code returned by Postgres for deadlock.
Definition: pq_eval.c:33
#define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION
Error code returned by Postgres for uniqueness violation.
Definition: pq_eval.c:38
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 caller graph for this function:

◆ GNUNET_PQ_eval_prepared_non_select()

enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select ( PGconn *  connection,
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
connectionconnection to execute the statement in
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
connectionconnection to execute the statement in
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 151 of file pq_eval.c.

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

154 {
155  PGresult *result;
156  enum GNUNET_DB_QueryStatus qs;
157 
158  result = GNUNET_PQ_exec_prepared (connection,
159  statement_name,
160  params);
161  qs = GNUNET_PQ_eval_result (connection,
162  statement_name,
163  result);
165  {
166  const char *tuples;
167 
168  /* What an awful API, this function really does return a string */
169  tuples = PQcmdTuples (result);
170  if (NULL != tuples)
171  qs = strtol (tuples, NULL, 10);
172  }
173  PQclear (result);
174  return qs;
175 }
static int result
Global testing status.
PGresult * GNUNET_PQ_exec_prepared(PGconn *db_conn, const char *name, const struct GNUNET_PQ_QueryParam *params)
Execute a prepared statement.
Definition: pq.c:41
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(PGconn *connection, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:60
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 ( PGconn *  connection,
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
connectionconnection to execute the statement in
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 194 of file pq_eval.c.

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

199 {
200  PGresult *result;
201  enum GNUNET_DB_QueryStatus qs;
202  unsigned int ret;
203 
204  result = GNUNET_PQ_exec_prepared (connection,
205  statement_name,
206  params);
207  qs = GNUNET_PQ_eval_result (connection,
208  statement_name,
209  result);
210  if (qs < 0)
211  {
212  PQclear (result);
213  return qs;
214  }
215  ret = PQntuples (result);
216  if (NULL != rh)
217  rh (rh_cls,
218  result,
219  ret);
220  PQclear (result);
221  return ret;
222 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
static int result
Global testing status.
PGresult * GNUNET_PQ_exec_prepared(PGconn *db_conn, const char *name, const struct GNUNET_PQ_QueryParam *params)
Execute a prepared statement.
Definition: pq.c:41
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(PGconn *connection, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:60
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 ( PGconn *  connection,
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
connectionconnection to execute the statement in
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 241 of file pq_eval.c.

References GNUNET_break, GNUNET_DB_STATUS_HARD_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().

245 {
246  PGresult *result;
247  enum GNUNET_DB_QueryStatus qs;
248 
249  result = GNUNET_PQ_exec_prepared (connection,
250  statement_name,
251  params);
252  qs = GNUNET_PQ_eval_result (connection,
253  statement_name,
254  result);
255  if (qs < 0)
256  {
257  PQclear (result);
258  return qs;
259  }
260  if (0 == PQntuples (result))
261  {
262  PQclear (result);
264  }
265  if (1 != PQntuples (result))
266  {
267  /* more than one result, but there must be at most one */
268  GNUNET_break (0);
269  PQclear (result);
271  }
272  if (GNUNET_OK !=
273  GNUNET_PQ_extract_result (result,
274  rs,
275  0))
276  {
277  PQclear (result);
279  }
280  PQclear (result);
282 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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.
PGresult * GNUNET_PQ_exec_prepared(PGconn *db_conn, const char *name, const struct GNUNET_PQ_QueryParam *params)
Execute a prepared statement.
Definition: pq.c:41
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_result(PGconn *connection, const char *statement_name, PGresult *result)
Check the result&#39;s error code to see what happened.
Definition: pq_eval.c:60
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:144
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 39 of file pq_prepare.c.

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

Referenced by database_setup(), and init_connection().

42 {
43  struct GNUNET_PQ_PreparedStatement ps = {
44  .name = name,
45  .sql = sql,
46  .num_arguments = num_args
47  };
48 
49  return ps;
50 }
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 ( PGconn *  connection,
const struct GNUNET_PQ_PreparedStatement ps 
)

Request creation of prepared statements ps from Postgres.

Parameters
connectionconnection 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 63 of file pq_prepare.c.

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

Referenced by database_setup(), and init_connection().

65 {
66  for (unsigned int i=0;NULL != ps[i].name;i++)
67  {
68  PGresult *ret;
69 
71  "pq",
72  "Preparing SQL statement `%s' as `%s'\n",
73  ps[i].sql,
74  ps[i].name);
75  ret = PQprepare (connection,
76  ps[i].name,
77  ps[i].sql,
78  ps[i].num_arguments,
79  NULL);
80  if (PGRES_COMMAND_OK != PQresultStatus (ret))
81  {
83  "pq",
84  _("PQprepare (`%s' as `%s') failed with error: %s\n"),
85  ps[i].sql,
86  ps[i].name,
87  PQerrorMessage (connection));
88  PQclear (ret);
89  return GNUNET_SYSERR;
90  }
91  PQclear (ret);
92  }
93  return GNUNET_OK;
94 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
const char * name
Name of the statement.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
const char * sql
Actual SQL statement.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 37 of file pq_exec.c.

References GNUNET_NO, and GNUNET_PQ_ExecuteStatement::sql.

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

38 {
39  struct GNUNET_PQ_ExecuteStatement es = {
40  .sql = sql,
41  .ignore_errors = GNUNET_NO
42  };
43 
44  return es;
45 }
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
#define GNUNET_NO
Definition: gnunet_common.h:81
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 56 of file pq_exec.c.

References GNUNET_YES, and GNUNET_PQ_ExecuteStatement::sql.

Referenced by database_setup(), and init_connection().

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

◆ GNUNET_PQ_exec_statements()

int GNUNET_PQ_exec_statements ( PGconn *  connection,
const struct GNUNET_PQ_ExecuteStatement es 
)

Request execution of an array of statements es from Postgres.

Parameters
connectionconnection to execute the statements over
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 77 of file pq_exec.c.

References 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 database_setup(), init_connection(), and postgres_plugin_drop().

79 {
80  for (unsigned int i=0; NULL != es[i].sql; i++)
81  {
82  PGresult *result;
83 
85  "Running statement `%s' on %p\n",
86  es[i].sql,
87  connection);
88  result = PQexec (connection,
89  es[i].sql);
90  if ( (GNUNET_NO == es[i].ignore_errors) &&
91  (PGRES_COMMAND_OK != PQresultStatus (result)) )
92  {
94  "pq",
95  "Failed to execute `%s': %s/%s/%s/%s/%s",
96  es[i].sql,
97  PQresultErrorField (result,
98  PG_DIAG_MESSAGE_PRIMARY),
99  PQresultErrorField (result,
100  PG_DIAG_MESSAGE_DETAIL),
101  PQresultErrorMessage (result),
102  PQresStatus (PQresultStatus (result)),
103  PQerrorMessage (connection));
104  PQclear (result);
105  return GNUNET_SYSERR;
106  }
107  PQclear (result);
108  }
109  return GNUNET_OK;
110 }
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
int ignore_errors
Should we ignore errors?
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#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()

PGconn* GNUNET_PQ_connect ( const char *  config_str)

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.

Parameters
config_strconfiguration to use
Returns
NULL on error

Definition at line 73 of file pq_connect.c.

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, pq_notice_processor_cb(), and pq_notice_receiver_cb().

Referenced by GNUNET_PQ_connect_with_cfg().

74 {
75  PGconn *conn;
76 
77  conn = PQconnectdb (config_str);
78  if ( (NULL == conn) ||
79  (CONNECTION_OK !=
80  PQstatus (conn)) )
81  {
83  "pq",
84  "Database connection to '%s' failed: %s\n",
85  config_str,
86  (NULL != conn) ?
87  PQerrorMessage (conn)
88  : "PQconnectdb returned NULL");
89  if (NULL != conn)
90  PQfinish (conn);
91  return NULL;
92  }
93  PQsetNoticeReceiver (conn,
95  conn);
96  PQsetNoticeProcessor (conn,
98  conn);
99  return conn;
100 }
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 void pq_notice_processor_cb(void *arg, const char *message)
Function called by libpq whenever it wants to log something.
Definition: pq_connect.c:54
#define GNUNET_log_from(kind, comp,...)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_connect_with_cfg()

PGconn* GNUNET_PQ_connect_with_cfg ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  section 
)

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

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
Returns
the postgres handle, NULL on error

Definition at line 112 of file pq_connect.c.

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

Referenced by database_setup(), and init_connection().

114 {
115  PGconn *dbh;
116  char *conninfo;
117 
118  /* Open database and precompile statements */
119  if (GNUNET_OK !=
121  section,
122  "CONFIG",
123  &conninfo))
124  conninfo = NULL;
125  dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo);
126  GNUNET_free_non_null (conninfo);
127  return dbh;
128 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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
PGconn * GNUNET_PQ_connect(const char *config_str)
Create a connection to the Postgres database using config_str for the configuration.
Definition: pq_connect.c:73
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.
Here is the call graph for this function:
Here is the caller graph for this function: