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/>.
19 * @file pq/pq_connect.c
20 * @brief functions to connect to libpq (PostGres)
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
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.
33 * @param arg the SQL connection that was used
34 * @param res information about some libpq event
37 pq_notice_receiver_cb (void *arg,
40 /* do nothing, intentionally */
45 * Function called by libpq whenever it wants to log something.
46 * We log those using the Taler logger.
48 * @param arg the SQL connection that was used
49 * @param message information about some libpq event
52 pq_notice_processor_cb (void *arg,
55 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
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.
67 * @param config_str configuration to use
68 * @return NULL on error
71 GNUNET_PQ_connect (const char *config_str)
75 conn = PQconnectdb (config_str);
76 if ( (NULL == conn) ||
80 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
82 "Database connection to '%s' failed: %s\n",
86 : "PQconnectdb returned NULL");
91 PQsetNoticeReceiver (conn,
92 &pq_notice_receiver_cb,
94 PQsetNoticeProcessor (conn,
95 &pq_notice_processor_cb,
102 * Connect to a postgres database using the configuration
103 * option "CONFIG" in @a section.
105 * @param cfg configuration
106 * @param section configuration section to use to get Postgres configuration options
107 * @return the postgres handle, NULL on error
110 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg,
116 /* Open database and precompile statements */
118 GNUNET_CONFIGURATION_get_value_string (cfg,
123 dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo);
124 GNUNET_free_non_null (conninfo);
129 /* end of pq/pq_connect.c */