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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
17 * @file pq/pq_connect.c
18 * @brief functions to connect to libpq (PostGres)
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
27 * Function called by libpq whenever it wants to log something.
28 * We already log whenever we care, so this function does nothing
29 * and merely exists to silence the libpq logging.
31 * @param arg the SQL connection that was used
32 * @param res information about some libpq event
35 pq_notice_receiver_cb (void *arg,
38 /* do nothing, intentionally */
43 * Function called by libpq whenever it wants to log something.
44 * We log those using the Taler logger.
46 * @param arg the SQL connection that was used
47 * @param message information about some libpq event
50 pq_notice_processor_cb (void *arg,
53 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
61 * Create a connection to the Postgres database using @a config_str
62 * for the configuration. Initialize logging via GNUnet's log
63 * routines and disable Postgres's logger.
65 * @param config_str configuration to use
66 * @return NULL on error
69 GNUNET_PQ_connect (const char *config_str)
73 conn = PQconnectdb (config_str);
74 if ( (NULL == conn) ||
78 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
80 "Database connection to '%s' failed: %s\n",
84 : "PQconnectdb returned NULL");
89 PQsetNoticeReceiver (conn,
90 &pq_notice_receiver_cb,
92 PQsetNoticeProcessor (conn,
93 &pq_notice_processor_cb,
100 * Connect to a postgres database using the configuration
101 * option "CONFIG" in @a section.
103 * @param cfg configuration
104 * @param section configuration section to use to get Postgres configuration options
105 * @return the postgres handle, NULL on error
108 GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg,
114 /* Open database and precompile statements */
116 GNUNET_CONFIGURATION_get_value_string (cfg,
121 dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo);
122 GNUNET_free_non_null (conninfo);
127 /* end of pq/pq_connect.c */