GNUnet  0.11.x
Functions
pq_prepare.c File Reference

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

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

Go to the source code of this file.

Functions

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 (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres. More...
 

Detailed Description

functions to connect to libpq (PostGres)

Author
Christian Grothoff

Definition in file pq_prepare.c.

Function Documentation

◆ 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 38 of file pq_prepare.c.

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

Referenced by database_setup(), and init_connection().

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

Request creation of prepared statements ps from Postgres.

Parameters
dbdatabase 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 62 of file pq_prepare.c.

References _, GNUNET_PQ_Context::conn, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free_non_null, GNUNET_log_from, GNUNET_new_array, GNUNET_OK, GNUNET_SYSERR, GNUNET_PQ_PreparedStatement::name, GNUNET_PQ_PreparedStatement::num_arguments, GNUNET_PQ_Context::ps, ret, and GNUNET_PQ_PreparedStatement::sql.

Referenced by GNUNET_PQ_reconnect().

64 {
65  if (db->ps != ps)
66  {
67  /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
68  unsigned int olen = 0; /* length of existing 'db->ps' array */
69  unsigned int nlen = 0; /* length of 'ps' array */
70  struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
71 
72  if (NULL != db->ps)
73  while (NULL != db->ps[olen].name)
74  olen++;
75  while (NULL != ps[nlen].name)
76  nlen++;
77  rps = GNUNET_new_array (olen + nlen + 1,
79  if (NULL != db->ps)
80  memcpy (rps,
81  db->ps,
82  olen * sizeof (struct GNUNET_PQ_PreparedStatement));
83  memcpy (&rps[olen],
84  ps,
85  nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
87  db->ps = rps;
88  }
89 
90  /* actually prepare statements */
91  for (unsigned int i = 0; NULL != ps[i].name; i++)
92  {
93  PGresult *ret;
94 
96  "pq",
97  "Preparing SQL statement `%s' as `%s'\n",
98  ps[i].sql,
99  ps[i].name);
100  ret = PQprepare (db->conn,
101  ps[i].name,
102  ps[i].sql,
103  ps[i].num_arguments,
104  NULL);
105  if (PGRES_COMMAND_OK != PQresultStatus (ret))
106  {
108  "pq",
109  _ ("PQprepare (`%s' as `%s') failed with error: %s\n"),
110  ps[i].sql,
111  ps[i].name,
112  PQerrorMessage (db->conn));
113  PQclear (ret);
114  return GNUNET_SYSERR;
115  }
116  PQclear (ret);
117  }
118  return GNUNET_OK;
119 }
struct GNUNET_PQ_PreparedStatement * ps
Prepared statements.
Definition: pq.h:49
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
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
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
const char * name
Name of the statement.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
PGconn * conn
Actual connection.
Definition: pq.h:39
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
const char * sql
Actual SQL statement.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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: