diff options
Diffstat (limited to 'src/pq/pq_connect.c')
-rw-r--r-- | src/pq/pq_connect.c | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/pq/pq_connect.c b/src/pq/pq_connect.c new file mode 100644 index 000000000..99ad06485 --- /dev/null +++ b/src/pq/pq_connect.c | |||
@@ -0,0 +1,127 @@ | |||
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 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. | ||
8 | |||
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. | ||
12 | |||
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/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file pq/pq_connect.c | ||
18 | * @brief functions to connect to libpq (PostGres) | ||
19 | * @author Christian Grothoff | ||
20 | */ | ||
21 | #include "platform.h" | ||
22 | #include "gnunet_util_lib.h" | ||
23 | #include "gnunet_pq_lib.h" | ||
24 | |||
25 | |||
26 | /** | ||
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. | ||
30 | * | ||
31 | * @param arg the SQL connection that was used | ||
32 | * @param res information about some libpq event | ||
33 | */ | ||
34 | static void | ||
35 | pq_notice_receiver_cb (void *arg, | ||
36 | const PGresult *res) | ||
37 | { | ||
38 | /* do nothing, intentionally */ | ||
39 | } | ||
40 | |||
41 | |||
42 | /** | ||
43 | * Function called by libpq whenever it wants to log something. | ||
44 | * We log those using the Taler logger. | ||
45 | * | ||
46 | * @param arg the SQL connection that was used | ||
47 | * @param message information about some libpq event | ||
48 | */ | ||
49 | static void | ||
50 | pq_notice_processor_cb (void *arg, | ||
51 | const char *message) | ||
52 | { | ||
53 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | ||
54 | "pq", | ||
55 | "%s", | ||
56 | message); | ||
57 | } | ||
58 | |||
59 | |||
60 | /** | ||
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. | ||
64 | * | ||
65 | * @param config_str configuration to use | ||
66 | * @return NULL on error | ||
67 | */ | ||
68 | PGconn * | ||
69 | GNUNET_PQ_connect (const char *config_str) | ||
70 | { | ||
71 | PGconn *conn; | ||
72 | |||
73 | conn = PQconnectdb (config_str); | ||
74 | if ( (NULL == conn) || | ||
75 | (CONNECTION_OK != | ||
76 | PQstatus (conn)) ) | ||
77 | { | ||
78 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
79 | "pq", | ||
80 | "Database connection to '%s' failed: %s\n", | ||
81 | config_str, | ||
82 | (NULL != conn) ? | ||
83 | PQerrorMessage (conn) | ||
84 | : "PQconnectdb returned NULL"); | ||
85 | if (NULL != conn) | ||
86 | PQfinish (conn); | ||
87 | return NULL; | ||
88 | } | ||
89 | PQsetNoticeReceiver (conn, | ||
90 | &pq_notice_receiver_cb, | ||
91 | conn); | ||
92 | PQsetNoticeProcessor (conn, | ||
93 | &pq_notice_processor_cb, | ||
94 | conn); | ||
95 | return conn; | ||
96 | } | ||
97 | |||
98 | |||
99 | /** | ||
100 | * Connect to a postgres database using the configuration | ||
101 | * option "CONFIG" in @a section. | ||
102 | * | ||
103 | * @param cfg configuration | ||
104 | * @param section configuration section to use to get Postgres configuration options | ||
105 | * @return the postgres handle, NULL on error | ||
106 | */ | ||
107 | PGconn * | ||
108 | GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg, | ||
109 | const char *section) | ||
110 | { | ||
111 | PGconn *dbh; | ||
112 | char *conninfo; | ||
113 | |||
114 | /* Open database and precompile statements */ | ||
115 | if (GNUNET_OK != | ||
116 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
117 | section, | ||
118 | "CONFIG", | ||
119 | &conninfo)) | ||
120 | conninfo = NULL; | ||
121 | dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo); | ||
122 | GNUNET_free_non_null (conninfo); | ||
123 | return dbh; | ||
124 | } | ||
125 | |||
126 | |||
127 | /* end of pq/pq_connect.c */ | ||