remove CYGWIN codeblocks, drop vendored Windows openvpn, drop win32 specific files.
[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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file pq/pq_connect.c
22  * @brief functions to connect to libpq (PostGres)
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
28
29
30 /**
31  * Function called by libpq whenever it wants to log something.
32  * We already log whenever we care, so this function does nothing
33  * and merely exists to silence the libpq logging.
34  *
35  * @param arg the SQL connection that was used
36  * @param res information about some libpq event
37  */
38 static void
39 pq_notice_receiver_cb(void *arg,
40                       const PGresult *res)
41 {
42   /* do nothing, intentionally */
43 }
44
45
46 /**
47  * Function called by libpq whenever it wants to log something.
48  * We log those using the Taler logger.
49  *
50  * @param arg the SQL connection that was used
51  * @param message information about some libpq event
52  */
53 static void
54 pq_notice_processor_cb(void *arg,
55                        const char *message)
56 {
57   GNUNET_log_from(GNUNET_ERROR_TYPE_INFO,
58                   "pq",
59                   "%s",
60                   message);
61 }
62
63
64 /**
65  * Create a connection to the Postgres database using @a config_str
66  * for the configuration.  Initialize logging via GNUnet's log
67  * routines and disable Postgres's logger.
68  *
69  * @param config_str configuration to use
70  * @return NULL on error
71  */
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     {
82       GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR,
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,
94                       &pq_notice_receiver_cb,
95                       conn);
96   PQsetNoticeProcessor(conn,
97                        &pq_notice_processor_cb,
98                        conn);
99   return conn;
100 }
101
102
103 /**
104  * Connect to a postgres database using the configuration
105  * option "CONFIG" in @a section.
106  *
107  * @param cfg configuration
108  * @param section configuration section to use to get Postgres configuration options
109  * @return the postgres handle, NULL on error
110  */
111 PGconn *
112 GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle * cfg,
113                            const char *section)
114 {
115   PGconn *dbh;
116   char *conninfo;
117
118   /* Open database and precompile statements */
119   if (GNUNET_OK !=
120       GNUNET_CONFIGURATION_get_value_string(cfg,
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 */