GNUnet  0.11.x
Functions
pq_connect.c File Reference

functions to connect to libpq (PostGres) More...

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

Go to the source code of this file.

Functions

static void pq_notice_receiver_cb (void *arg, const PGresult *res)
 Function called by libpq whenever it wants to log something. More...
 
static void pq_notice_processor_cb (void *arg, const char *message)
 Function called by libpq whenever it wants to log something. 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...
 
void GNUNET_PQ_reconnect_if_down (struct GNUNET_PQ_Context *db)
 Reinitialize the database db if the connection is down. More...
 
void GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
 Reinitialize the database db. More...
 
struct GNUNET_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_disconnect (struct GNUNET_PQ_Context *db)
 Disconnect from the database, destroying the prepared statements and releasing other associated resources. More...
 

Detailed Description

functions to connect to libpq (PostGres)

Author
Christian Grothoff

Definition in file pq_connect.c.

Function Documentation

◆ pq_notice_receiver_cb()

static void pq_notice_receiver_cb ( void *  arg,
const PGresult *  res 
)
static

Function called by libpq whenever it wants to log something.

We already log whenever we care, so this function does nothing and merely exists to silence the libpq logging.

Parameters
argthe SQL connection that was used
resinformation about some libpq event

Definition at line 38 of file pq_connect.c.

Referenced by GNUNET_PQ_reconnect().

40 {
41  /* do nothing, intentionally */
42  (void) arg;
43  (void) res;
44 }
static int res
Here is the caller graph for this function:

◆ pq_notice_processor_cb()

static void pq_notice_processor_cb ( void *  arg,
const char *  message 
)
static

Function called by libpq whenever it wants to log something.

We log those using the GNUnet logger.

Parameters
argthe SQL connection that was used
messageinformation about some libpq event

Definition at line 55 of file pq_connect.c.

References GNUNET_ERROR_TYPE_INFO, and GNUNET_log_from.

Referenced by GNUNET_PQ_reconnect().

57 {
58  (void) arg;
60  "pq",
61  "%s",
62  message);
63 }
#define GNUNET_log_from(kind, comp,...)
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.

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_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_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".

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_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: