GNUnet  0.11.x
Functions
pq.c File Reference

helper functions for libpq (PostGres) interactions More...

#include "platform.h"
#include "pq.h"
Include dependency graph for pq.c:

Go to the source code of this file.

Functions

PGresult * GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db, const char *name, const struct GNUNET_PQ_QueryParam *params)
 Execute a prepared statement. 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...
 
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...
 

Detailed Description

helper functions for libpq (PostGres) interactions

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in
Florian Dold
Christian Grothoff

Definition in file pq.c.

Function Documentation

◆ 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 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_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 }
GNUNET_PQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.
GNUNET_PQ_ResultConverter conv
What is the format of the result?
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)

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: