GNUnet  0.11.x
pq_connect.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2017, 2019 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "pq.h"
27 
28 
37 static void
39  const PGresult *res)
40 {
41  /* do nothing, intentionally */
42  (void) arg;
43  (void) res;
44 }
45 
46 
54 static void
56  const char *message)
57 {
58  (void) arg;
60  "pq",
61  "%s",
62  message);
63 }
64 
65 
85 struct GNUNET_PQ_Context *
87  const struct GNUNET_PQ_ExecuteStatement *es,
88  const struct GNUNET_PQ_PreparedStatement *ps)
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);
102  db->config_str = GNUNET_strdup (config_str);
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 }
128 
129 
135 void
137 {
138  if (CONNECTION_BAD != PQstatus (db->conn))
139  return;
140  GNUNET_PQ_reconnect (db);
141 }
142 
143 
149 void
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 }
197 
198 
216 struct GNUNET_PQ_Context *
218  const char *section,
219  const struct GNUNET_PQ_ExecuteStatement *es,
220  const struct GNUNET_PQ_PreparedStatement *ps)
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 }
237 
238 
245 void
247 {
248  GNUNET_free_non_null (db->es);
249  GNUNET_free_non_null (db->ps);
250  PQfinish (db->conn);
251  GNUNET_free (db);
252 }
253 
254 
255 /* end of pq/pq_connect.c */
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
Definition: pq_connect.c:246
void GNUNET_PQ_reconnect(struct GNUNET_PQ_Context *db)
Reinitialize the database db.
Definition: pq_connect.c:150
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
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
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
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static char * section
Name of the section.
Definition: gnunet-config.c:33
PGconn * conn
Actual connection.
Definition: pq.h:39
shared internal data structures of libgnunetpq
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
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
static int res
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.
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.
Definition: pq_connect.c:217
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
void GNUNET_PQ_reconnect_if_down(struct GNUNET_PQ_Context *db)
Reinitialize the database db if the connection is down.
Definition: pq_connect.c:136
configuration data
Definition: configuration.c:85
const char * name
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
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,...)
Handle to Postgres database.
Definition: pq.h:34
#define GNUNET_free(ptr)
Wrapper around free.