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 char *load_path, 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...
 
static int apply_patch (struct GNUNET_PQ_Context *db, const char *load_path, unsigned int i)
 Apply patch number from path load_path. More...
 
int GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db, const char *load_path)
 Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the files exist contiguously). 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 char *load_path_suffix, 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 char *  load_path,
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 load_path and 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.

In load_path, a list of "$XXXX.sql" files is expected where $XXXX must be a sequence of contiguous integer values starting at 0000. These files are then loaded in sequence using "psql $config_str" before running statements from es. The directory is inspected again on reconnect.

Parameters
config_strconfiguration to use
load_pathpath to directory with SQL transactions to run, can be NULL
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 89 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, GNUNET_PQ_Context::load_path, name, and GNUNET_PQ_Context::ps.

Referenced by GNUNET_PQ_connect_with_cfg().

93 {
94  struct GNUNET_PQ_Context *db;
95  unsigned int elen = 0;
96  unsigned int plen = 0;
97 
98  if (NULL != es)
99  while (NULL != es[elen].sql)
100  elen++;
101  if (NULL != ps)
102  while (NULL != ps[plen].name)
103  plen++;
104 
105  db = GNUNET_new (struct GNUNET_PQ_Context);
107  if (NULL != load_path)
109  if (0 != elen)
110  {
111  db->es = GNUNET_new_array (elen + 1,
113  memcpy (db->es,
114  es,
115  elen * sizeof (struct GNUNET_PQ_ExecuteStatement));
116  }
117  if (0 != plen)
118  {
119  db->ps = GNUNET_new_array (plen + 1,
121  memcpy (db->ps,
122  ps,
123  plen * sizeof (struct GNUNET_PQ_PreparedStatement));
124  }
125  GNUNET_PQ_reconnect (db);
126  if (NULL == db->conn)
127  {
128  GNUNET_free (db->load_path);
129  GNUNET_free (db->config_str);
130  GNUNET_free (db);
131  return NULL;
132  }
133  return db;
134 }
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:324
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
char * load_path
Path to load SQL files from.
Definition: pq.h:59
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:

◆ apply_patch()

static int apply_patch ( struct GNUNET_PQ_Context db,
const char *  load_path,
unsigned int  i 
)
static

Apply patch number from path load_path.

Parameters
dbdatabase context to use
load_pathwhere to find the SQL code to run
ipatch number to append to the load_path
Returns
GNUNET_OK on success, GNUNET_NO if patch i does not exist, GNUNET_SYSERR on error

Definition at line 146 of file pq_connect.c.

References buf, GNUNET_PQ_Context::config_str, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_log_strerror_file, GNUNET_OK, GNUNET_OS_INHERIT_STD_NONE, GNUNET_OS_process_destroy(), GNUNET_OS_PROCESS_EXITED, GNUNET_OS_process_wait_status(), GNUNET_OS_start_process(), GNUNET_snprintf(), GNUNET_SYSERR, and type.

Referenced by GNUNET_PQ_reconnect(), and GNUNET_PQ_run_sql().

149 {
150  struct GNUNET_OS_Process *psql;
152  unsigned long code;
153  size_t slen = strlen (load_path) + 10;
154  char buf[slen];
155 
156  GNUNET_snprintf (buf,
157  sizeof (buf),
158  "%s%04u.sql",
159  load_path,
160  i);
162  "Applying SQL file `%s' on database %s\n",
163  buf,
164  db->config_str);
166  NULL,
167  NULL,
168  NULL,
169  "psql",
170  "psql",
171  db->config_str,
172  "-f",
173  buf,
174  "-q",
175  "--set",
176  "ON_ERROR_STOP=1",
177  NULL);
178  if (NULL == psql)
179  {
181  "exec",
182  "psql");
183  return GNUNET_SYSERR;
184  }
187  &type,
188  &code));
190  if ( (GNUNET_OS_PROCESS_EXITED != type) ||
191  (0 != code) )
192  {
194  "Could not run PSQL on file %s: %d\n",
195  buf,
196  (int) code);
197  return GNUNET_SYSERR;
198  }
199  return GNUNET_OK;
200 }
struct GNUNET_OS_Process * GNUNET_OS_start_process(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename,...)
Start a process.
Definition: os_priority.c:678
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:287
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * config_str
Configuration to use to connect to the DB.
Definition: pq.h:54
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
No standard streams should be inherited.
Definition: gnunet_os_lib.h:73
GNUNET_OS_ProcessStatusType
Process status types.
int GNUNET_OS_process_wait_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process, waiting on it if dead.
Definition: os_priority.c:964
static char buf[2048]
The process exited with a return code.
#define GNUNET_log(kind,...)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PQ_run_sql()

int GNUNET_PQ_run_sql ( struct GNUNET_PQ_Context db,
const char *  load_path 
)

Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the files exist contiguously).

Parameters
dbdatabase context to use
load_pathwhere to find the XXXX.sql files
Returns
GNUNET_OK on success

Definition at line 213 of file pq_connect.c.

References apply_patch(), buf, GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_DISK_file_test(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_string(), GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_string(), GNUNET_snprintf(), GNUNET_SYSERR, GNUNET_YES, and ret.

Referenced by GNUNET_PQ_reconnect().

215 {
216  const char *load_path_suffix;
217  size_t slen = strlen (load_path) + 10;
218 
219  load_path_suffix = strrchr (load_path, '/');
220  if (NULL == load_path_suffix)
221  {
222  GNUNET_break (0);
223  return GNUNET_SYSERR;
224  }
225  load_path_suffix++; /* skip '/' */
226  for (unsigned int i = 1; i<10000; i++)
227  {
228  enum GNUNET_DB_QueryStatus qs;
229  {
230  char buf[slen];
231 
232  /* First, check patch actually exists */
233  GNUNET_snprintf (buf,
234  sizeof (buf),
235  "%s%04u.sql",
236  load_path,
237  i);
238  if (GNUNET_YES !=
239  GNUNET_DISK_file_test (buf))
240  return GNUNET_OK; /* We are done */
241  }
242 
243  /* Second, check with DB versioning schema if this patch was already applied,
244  if so, skip it. */
245  {
246  char patch_name[slen];
247 
248  GNUNET_snprintf (patch_name,
249  sizeof (patch_name),
250  "%s%04u",
251  load_path_suffix,
252  i);
253  {
254  char *applied_by;
255  struct GNUNET_PQ_QueryParam params[] = {
256  GNUNET_PQ_query_param_string (patch_name),
258  };
259  struct GNUNET_PQ_ResultSpec rs[] = {
260  GNUNET_PQ_result_spec_string ("applied_by",
261  &applied_by),
263  };
264 
266  "gnunet_pq_check_patch",
267  params,
268  rs);
270  {
272  "Database version %s already applied by %s, skipping\n",
273  patch_name,
274  applied_by);
276  }
277  if (GNUNET_DB_STATUS_HARD_ERROR == qs)
278  {
279  GNUNET_break (0);
280  return GNUNET_SYSERR;
281  }
282  }
283  }
285  continue; /* patch already applied, skip it */
286 
287  /* patch not yet applied, run it! */
288  {
289  int ret;
290 
291  ret = apply_patch (db,
292  load_path,
293  i);
294  if (GNUNET_NO == ret)
295  break;
296  if (GNUNET_SYSERR == ret)
297  return GNUNET_SYSERR;
298  }
299  }
300  return GNUNET_OK;
301 }
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:544
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
static int apply_patch(struct GNUNET_PQ_Context *db, const char *load_path, unsigned int i)
Apply patch number from path load_path.
Definition: pq_connect.c:146
Description of a DB result cell.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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...
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
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 co...
Definition: pq_eval.c:258
static char buf[2048]
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:58
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
#define GNUNET_log(kind,...)
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
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
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 310 of file pq_connect.c.

References GNUNET_PQ_Context::conn, and GNUNET_PQ_reconnect().

311 {
312  if (CONNECTION_BAD != PQstatus (db->conn))
313  return;
314  GNUNET_PQ_reconnect (db);
315 }
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:324
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 324 of file pq_connect.c.

References apply_patch(), GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::conn, GNUNET_PQ_Context::es, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_PQ_exec_statements(), GNUNET_PQ_prepare_statements(), GNUNET_PQ_run_sql(), GNUNET_SYSERR, GNUNET_PQ_Context::load_path, pq_notice_processor_cb(), pq_notice_receiver_cb(), GNUNET_PQ_Context::ps, res, and ret.

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

325 {
326  if (NULL != db->conn)
327  PQfinish (db->conn);
328  db->conn = PQconnectdb (db->config_str);
329  if ( (NULL == db->conn) ||
330  (CONNECTION_OK != PQstatus (db->conn)) )
331  {
333  "pq",
334  "Database connection to '%s' failed: %s\n",
335  db->config_str,
336  (NULL != db->conn) ?
337  PQerrorMessage (db->conn)
338  : "PQconnectdb returned NULL");
339  if (NULL != db->conn)
340  {
341  PQfinish (db->conn);
342  db->conn = NULL;
343  }
344  return;
345  }
346  PQsetNoticeReceiver (db->conn,
348  db);
349  PQsetNoticeProcessor (db->conn,
351  db);
352  if (NULL != db->load_path)
353  {
354  PGresult *res;
355 
356  res = PQprepare (db->conn,
357  "gnunet_pq_check_patch",
358  "SELECT"
359  " applied_by"
360  " FROM _v.patches"
361  " WHERE patch_name = $1"
362  " LIMIT 1",
363  1,
364  NULL);
365  if (PGRES_COMMAND_OK != PQresultStatus (res))
366  {
367  int ret;
368 
369  PQclear (res);
371  "Failed to prepare statement to check patch level. Likely versioning schema does not exist yet, loading patch level 0000!\n");
372  ret = apply_patch (db,
373  db->load_path,
374  0);
375  if (GNUNET_NO == ret)
376  {
378  "Failed to find SQL file to load database versioning logic\n");
379  PQfinish (db->conn);
380  db->conn = NULL;
381  return;
382  }
383  if (GNUNET_SYSERR == ret)
384  {
386  "Failed to run SQL logic to setup database versioning logic\n");
387  PQfinish (db->conn);
388  db->conn = NULL;
389  return;
390  }
391  /* try again to prepare our statement! */
392  res = PQprepare (db->conn,
393  "gnunet_pq_check_patch",
394  "SELECT"
395  " applied_by"
396  " FROM _v.patches"
397  " WHERE patch_name = $1"
398  " LIMIT 1",
399  1,
400  NULL);
401  if (PGRES_COMMAND_OK != PQresultStatus (res))
402  {
404  "Failed to run SQL logic to setup database versioning logic: %s/%s\n",
405  PQresultErrorMessage (res),
406  PQerrorMessage (db->conn));
407  PQclear (res);
408  PQfinish (db->conn);
409  db->conn = NULL;
410  return;
411  }
412  }
413  PQclear (res);
414 
415  if (GNUNET_SYSERR ==
416  GNUNET_PQ_run_sql (db,
417  db->load_path))
418  {
420  "Failed to load SQL statements from `%s*'\n",
421  db->load_path);
422  PQfinish (db->conn);
423  db->conn = NULL;
424  return;
425  }
426  }
427  if ( (NULL != db->es) &&
428  (GNUNET_OK !=
430  db->es)) )
431  {
432  PQfinish (db->conn);
433  db->conn = NULL;
434  return;
435  }
436  if ( (NULL != db->ps) &&
437  (GNUNET_OK !=
439  db->ps)) )
440  {
441  PQfinish (db->conn);
442  db->conn = NULL;
443  return;
444  }
445 }
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
char * load_path
Path to load SQL files from.
Definition: pq.h:59
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:44
static int apply_patch(struct GNUNET_PQ_Context *db, const char *load_path, unsigned int i)
Apply patch number from path load_path.
Definition: pq_connect.c:146
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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
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 int res
#define GNUNET_log(kind,...)
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,...)
int GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_path)
Within the db context, run all the SQL files from the load_path from 0000-9999.sql (as long as the fi...
Definition: pq_connect.c:213
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 char *  load_path_suffix,
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 does not have to ensure that es and ps remain allocated and initialized in memory until GNUNET_PQ_disconnect() is called, as a copy will be made.

Parameters
cfgconfiguration
sectionconfiguration section to use to get Postgres configuration options
load_path_suffixsuffix to append to the SQL_DIR in the configuration
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 466 of file pq_connect.c.

References db, GNUNET_asprintf(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_free, GNUNET_OK, GNUNET_PQ_connect(), and GNUNET_PQ_Context::load_path.

Referenced by database_setup(), and init_connection().

471 {
472  struct GNUNET_PQ_Context *db;
473  char *conninfo;
474  char *load_path;
475  char *sp;
476 
477  if (GNUNET_OK !=
479  section,
480  "CONFIG",
481  &conninfo))
482  conninfo = NULL;
483  load_path = NULL;
484  sp = NULL;
485  if ( (NULL != load_path_suffix) &&
486  (GNUNET_OK ==
488  section,
489  "SQL_DIR",
490  &sp)) )
491  GNUNET_asprintf (&load_path,
492  "%s%s",
493  sp,
494  load_path_suffix);
495  db = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo,
496  load_path,
497  es,
498  ps);
499  GNUNET_free (load_path);
500  GNUNET_free (sp);
501  GNUNET_free (conninfo);
502  return db;
503 }
char * load_path
Path to load SQL files from.
Definition: pq.h:59
static char * section
Name of the section.
Definition: gnunet-config.c:33
struct GNUNET_PQ_Context * GNUNET_PQ_connect(const char *config_str, const char *load_path, 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:89
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
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
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
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_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 513 of file pq_connect.c.

References GNUNET_PQ_Context::config_str, GNUNET_PQ_Context::conn, GNUNET_PQ_Context::es, GNUNET_free, GNUNET_PQ_Context::load_path, and GNUNET_PQ_Context::ps.

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

514 {
515  GNUNET_free (db->es);
516  GNUNET_free (db->ps);
517  GNUNET_free (db->load_path);
518  GNUNET_free (db->config_str);
519  PQfinish (db->conn);
520  GNUNET_free (db);
521 }
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
char * load_path
Path to load SQL files from.
Definition: pq.h:59
struct GNUNET_PQ_ExecuteStatement * es
Statements to execute upon connection.
Definition: pq.h:44
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_free(ptr)
Wrapper around free.
Here is the caller graph for this function: