2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file pq/pq_connect.c
22 * @brief functions to connect to libpq (PostGres)
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
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.
35 * @param arg the SQL connection that was used
36 * @param res information about some libpq event
39 pq_notice_receiver_cb (void *arg,
42 /* do nothing, intentionally */
47 * Function called by libpq whenever it wants to log something.
48 * We log those using the Taler logger.
50 * @param arg the SQL connection that was used
51 * @param message information about some libpq event
54 pq_notice_processor_cb (void *arg,
57 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
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.
69 * @param config_str configuration to use
70 * @return NULL on error
73 GNUNET_PQ_connect (const char *config_str)
77 conn = PQconnectdb (config_str);
78 if ( (NULL == conn) ||
82 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
84 "Database connection to '%s' failed: %s\n",
88 : "PQconnectdb returned NULL");
93 PQsetNoticeReceiver (conn,
94 &pq_notice_receiver_cb,
96 PQsetNoticeProcessor (conn,
97 &pq_notice_processor_cb,
104 * Connect to a postgres database using the configuration
105 * option "CONFIG" in @a section.
107 * @param cfg configuration
108 * @param section configuration section to use to get Postgres configuration options
109 * @return the postgres handle, NULL on error
112 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg,
118 /* Open database and precompile statements */
120 GNUNET_CONFIGURATION_get_value_string (cfg,
125 dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo);
126 GNUNET_free_non_null (conninfo);
131 /* end of pq/pq_connect.c */