paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / pq / pq_connect.c
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 /**
19  * @file pq/pq_connect.c
20  * @brief functions to connect to libpq (PostGres)
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
26
27
28 /**
29  * Function called by libpq whenever it wants to log something.
30  * We already log whenever we care, so this function does nothing
31  * and merely exists to silence the libpq logging.
32  *
33  * @param arg the SQL connection that was used
34  * @param res information about some libpq event
35  */
36 static void
37 pq_notice_receiver_cb (void *arg,
38                        const PGresult *res)
39 {
40   /* do nothing, intentionally */
41 }
42
43
44 /**
45  * Function called by libpq whenever it wants to log something.
46  * We log those using the Taler logger.
47  *
48  * @param arg the SQL connection that was used
49  * @param message information about some libpq event
50  */
51 static void
52 pq_notice_processor_cb (void *arg,
53                         const char *message)
54 {
55   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
56                    "pq",
57                    "%s",
58                    message);
59 }
60
61
62 /**
63  * Create a connection to the Postgres database using @a config_str
64  * for the configuration.  Initialize logging via GNUnet's log
65  * routines and disable Postgres's logger.
66  *
67  * @param config_str configuration to use
68  * @return NULL on error
69  */
70 PGconn *
71 GNUNET_PQ_connect (const char *config_str)
72 {
73   PGconn *conn;
74
75   conn = PQconnectdb (config_str);
76   if ( (NULL == conn) ||
77        (CONNECTION_OK !=
78         PQstatus (conn)) )
79   {
80     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
81                      "pq",
82                      "Database connection to '%s' failed: %s\n",
83                      config_str,
84                      (NULL != conn) ?
85                      PQerrorMessage (conn)
86                      : "PQconnectdb returned NULL");
87     if (NULL != conn)
88       PQfinish (conn);
89     return NULL;
90   }
91   PQsetNoticeReceiver (conn,
92                        &pq_notice_receiver_cb,
93                        conn);
94   PQsetNoticeProcessor (conn,
95                         &pq_notice_processor_cb,
96                         conn);
97   return conn;
98 }
99
100
101 /**
102  * Connect to a postgres database using the configuration
103  * option "CONFIG" in @a section.
104  *
105  * @param cfg configuration
106  * @param section configuration section to use to get Postgres configuration options
107  * @return the postgres handle, NULL on error
108  */
109 PGconn *
110 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg,
111                             const char *section)
112 {
113   PGconn *dbh;
114   char *conninfo;
115
116   /* Open database and precompile statements */
117   if (GNUNET_OK !=
118       GNUNET_CONFIGURATION_get_value_string (cfg,
119                                              section,
120                                              "CONFIG",
121                                              &conninfo))
122     conninfo = NULL;
123   dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo);
124   GNUNET_free_non_null (conninfo);
125   return dbh;
126 }
127
128
129 /* end of pq/pq_connect.c */