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...
 
enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result (PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
 Extract results from a query result according to the given specification. More...
 

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 context
namename of the prepared statement
paramsparameters to the statement
Returns
postgres result
Deprecated:
(should become an internal API)

Definition at line 32 of file pq.c.

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

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

Here is the call graph for this function:

◆ GNUNET_PQ_cleanup_result()

void GNUNET_PQ_cleanup_result ( struct GNUNET_PQ_ResultSpec rs)

Free all memory that was allocated in rs during GNUNET_PQ_extract_result().

Parameters
rsreult specification to clean up

Definition at line 117 of file pq.c.

118 {
119  for (unsigned int i = 0; NULL != rs[i].conv; i++)
120  if (NULL != rs[i].cleaner)
121  rs[i].cleaner (rs[i].cls,
122  rs[i].dst);
123 }
GNUNET_PQ_ResultConverter conv
What is the format of the result?
GNUNET_PQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.

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

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

Here is the caller graph for this function:

◆ GNUNET_PQ_extract_result()

enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result ( PGresult *  result,
struct GNUNET_PQ_ResultSpec rs,
int  row 
)

Extract results from a query result according to the given specification.

Parameters
resultresult to process
[in,out]rsresult specification to extract for
rowrow from the result to extract
Returns
GNUNET_YES if all results could be extracted GNUNET_SYSERR if a result was invalid (non-existing field)

Definition at line 117 of file pq.c.

130 {
131  unsigned int i;
132 
133  if (NULL == result)
134  return GNUNET_SYSERR;
135  for (i = 0; NULL != rs[i].conv; i++)
136  {
137  struct GNUNET_PQ_ResultSpec *spec;
139 
140  spec = &rs[i];
141  ret = spec->conv (spec->cls,
142  result,
143  row,
144  spec->fname,
145  &spec->dst_size,
146  spec->dst);
147  switch (ret)
148  {
149  case GNUNET_OK:
150  /* canonical case, continue below */
151  if (NULL != spec->is_null)
152  *spec->is_null = false;
153  break;
154  case GNUNET_NO:
155  if (spec->is_nullable)
156  {
157  if (NULL != spec->is_null)
158  *spec->is_null = true;
159  continue;
160  }
162  "NULL field encountered for `%s' where non-NULL was required\n",
163  spec->fname);
164  goto cleanup;
165  case GNUNET_SYSERR:
166  GNUNET_break (0);
167  goto cleanup;
168  }
169  if (NULL != spec->result_size)
170  *spec->result_size = spec->dst_size;
171  }
172  return GNUNET_OK;
173 cleanup:
174  for (unsigned int j = 0; j < i; j++)
175  if (NULL != rs[j].cleaner)
176  rs[j].cleaner (rs[j].cls,
177  rs[j].dst);
178  return GNUNET_SYSERR;
179 }
static void cleanup(void *cls)
Function scheduled as very last function, cleans up after us.
static int result
Global testing status.
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:92
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
Description of a DB result cell.
const char * fname
Field name of the desired result.
bool * is_null
Points to a location where we should store "true" if the result found is NULL, and otherwise "false".
void * dst
Destination for the data.
bool is_nullable
True if NULL is allowed for a value in the database.
void * cls
Closure for conv and cleaner.
size_t * result_size
Where to store actual size of the result.
size_t dst_size
Allowed size for the data, 0 for variable-size (in this case, the type of dst is a void ** and we nee...

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

Here is the caller graph for this function: