GNUnet  0.10.x
pq_connect.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2017 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 "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
28 
29 
38 static void
40  const PGresult *res)
41 {
42  /* do nothing, intentionally */
43 }
44 
45 
53 static void
55  const char *message)
56 {
58  "pq",
59  "%s",
60  message);
61 }
62 
63 
72 PGconn *
73 GNUNET_PQ_connect (const char *config_str)
74 {
75  PGconn *conn;
76 
77  conn = PQconnectdb (config_str);
78  if ( (NULL == conn) ||
79  (CONNECTION_OK !=
80  PQstatus (conn)) )
81  {
83  "pq",
84  "Database connection to '%s' failed: %s\n",
85  config_str,
86  (NULL != conn) ?
87  PQerrorMessage (conn)
88  : "PQconnectdb returned NULL");
89  if (NULL != conn)
90  PQfinish (conn);
91  return NULL;
92  }
93  PQsetNoticeReceiver (conn,
95  conn);
96  PQsetNoticeProcessor (conn,
98  conn);
99  return conn;
100 }
101 
102 
111 PGconn *
113  const char *section)
114 {
115  PGconn *dbh;
116  char *conninfo;
117 
118  /* Open database and precompile statements */
119  if (GNUNET_OK !=
121  section,
122  "CONFIG",
123  &conninfo))
124  conninfo = NULL;
125  dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo);
126  GNUNET_free_non_null (conninfo);
127  return dbh;
128 }
129 
130 
131 /* end of pq/pq_connect.c */
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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
static int res
PGconn * GNUNET_PQ_connect(const char *config_str)
Create a connection to the Postgres database using config_str for the configuration.
Definition: pq_connect.c:73
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_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
helper functions for Postgres DB interactions
configuration data
Definition: configuration.c:85
PGconn * GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:112
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:39
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:54
#define GNUNET_log_from(kind, comp,...)