aboutsummaryrefslogtreecommitdiff
path: root/src/pq
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-06-01 21:48:19 +0200
committerChristian Grothoff <christian@grothoff.org>2017-06-01 21:48:19 +0200
commit1defd30dfeb1867c2756b3fe6a437f695951d0c9 (patch)
treeb48c0fe6bb32469cfcb4284bfac3142e22417ae8 /src/pq
parentbbbe0b2404d131cc0d9eda26725b65b47a7e073a (diff)
downloadgnunet-1defd30dfeb1867c2756b3fe6a437f695951d0c9.tar.gz
gnunet-1defd30dfeb1867c2756b3fe6a437f695951d0c9.zip
adding more good helpers to libgnunetpq
Diffstat (limited to 'src/pq')
-rw-r--r--src/pq/Makefile.am4
-rw-r--r--src/pq/pq_connect.c127
-rw-r--r--src/pq/pq_eval.c249
-rw-r--r--src/pq/pq_exec.c106
-rw-r--r--src/pq/pq_prepare.c92
5 files changed, 578 insertions, 0 deletions
diff --git a/src/pq/Makefile.am b/src/pq/Makefile.am
index 8bb0a0132..d0c71703b 100644
--- a/src/pq/Makefile.am
+++ b/src/pq/Makefile.am
@@ -15,6 +15,10 @@ endif
15 15
16libgnunetpq_la_SOURCES = \ 16libgnunetpq_la_SOURCES = \
17 pq.c \ 17 pq.c \
18 pq_connect.c \
19 pq_eval.c \
20 pq_exec.c \
21 pq_prepare.c \
18 pq_query_helper.c \ 22 pq_query_helper.c \
19 pq_result_helper.c 23 pq_result_helper.c
20libgnunetpq_la_LIBADD = -lpq \ 24libgnunetpq_la_LIBADD = -lpq \
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 */
34static void
35pq_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 */
49static void
50pq_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 */
68PGconn *
69GNUNET_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 */
107PGconn *
108GNUNET_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 */
diff --git a/src/pq/pq_eval.c b/src/pq/pq_eval.c
new file mode 100644
index 000000000..39a7a2c98
--- /dev/null
+++ b/src/pq/pq_eval.c
@@ -0,0 +1,249 @@
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_eval.c
18 * @brief functions to execute SQL statements with arguments and/or results (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 * Error code returned by Postgres for deadlock.
28 */
29#define PQ_DIAG_SQLSTATE_DEADLOCK "40P01"
30
31/**
32 * Error code returned by Postgres for uniqueness violation.
33 */
34#define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION "23505"
35
36/**
37 * Error code returned by Postgres on serialization failure.
38 */
39#define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE "40001"
40
41
42/**
43 * Check the @a result's error code to see what happened.
44 * Also logs errors.
45 *
46 * @param connection connection to execute the statement in
47 * @param statement_name name of the statement that created @a result
48 * @param result result to check
49 * @return status code from the result, mapping PQ status
50 * codes to `enum GNUNET_PQ_QueryStatus`. Never
51 * returns positive values as this function does
52 * not look at the result set.
53 * @deprecated (low level, let's see if we can do with just the high-level functions)
54 */
55enum GNUNET_PQ_QueryStatus
56GNUNET_PQ_eval_result (PGconn *connection,
57 const char *statement_name,
58 PGresult *result)
59{
60 if (PGRES_COMMAND_OK !=
61 PQresultStatus (result))
62 {
63 const char *sqlstate;
64
65 sqlstate = PQresultErrorField (result,
66 PG_DIAG_SQLSTATE);
67 if (NULL == sqlstate)
68 {
69 /* very unexpected... */
70 GNUNET_break (0);
71 return GNUNET_PQ_STATUS_HARD_ERROR;
72 }
73 if ( (0 == strcmp (sqlstate,
74 PQ_DIAG_SQLSTATE_DEADLOCK)) ||
75 (0 == strcmp (sqlstate,
76 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) )
77 {
78 /* These two can be retried and have a fair chance of working
79 the next time */
80 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
81 "pq",
82 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
83 statement_name,
84 PQresultErrorField (result,
85 PG_DIAG_MESSAGE_PRIMARY),
86 PQresultErrorField (result,
87 PG_DIAG_MESSAGE_DETAIL),
88 PQresultErrorMessage (result),
89 PQresStatus (PQresultStatus (result)),
90 PQerrorMessage (connection));
91 return GNUNET_PQ_STATUS_SOFT_ERROR;
92 }
93 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
94 "pq",
95 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
96 statement_name,
97 PQresultErrorField (result,
98 PG_DIAG_MESSAGE_PRIMARY),
99 PQresultErrorField (result,
100 PG_DIAG_MESSAGE_DETAIL),
101 PQresultErrorMessage (result),
102 PQresStatus (PQresultStatus (result)),
103 PQerrorMessage (connection));
104 return GNUNET_PQ_STATUS_HARD_ERROR;
105 }
106 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
107}
108
109
110/**
111 * Execute a named prepared @a statement that is NOT a SELECT
112 * statement in @a connnection using the given @a params. Returns the
113 * resulting session state.
114 *
115 * @param connection connection to execute the statement in
116 * @param statement_name name of the statement
117 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
118 * @return status code from the result, mapping PQ status
119 * codes to `enum GNUNET_PQ_QueryStatus`. Never
120 * returns positive values as this function does
121 * not look at the result set.
122 */
123enum GNUNET_PQ_QueryStatus
124GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
125 const char *statement_name,
126 const struct GNUNET_PQ_QueryParam *params)
127{
128 PGresult *result;
129 enum GNUNET_PQ_QueryStatus qs;
130
131 result = GNUNET_PQ_exec_prepared (connection,
132 statement_name,
133 params);
134 qs = GNUNET_PQ_eval_result (connection,
135 statement_name,
136 result);
137 PQclear (result);
138 return qs;
139}
140
141
142/**
143 * Execute a named prepared @a statement that is a SELECT statement
144 * which may return multiple results in @a connection using the given
145 * @a params. Call @a rh with the results. Returns the query
146 * status including the number of results given to @a rh (possibly zero).
147 * @a rh will not have been called if the return value is negative.
148 *
149 * @param connection connection to execute the statement in
150 * @param statement_name name of the statement
151 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
152 * @param rh function to call with the result set, NULL to ignore
153 * @param rh_cls closure to pass to @a rh
154 * @return status code from the result, mapping PQ status
155 * codes to `enum GNUNET_PQ_QueryStatus`.
156 */
157enum GNUNET_PQ_QueryStatus
158GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
159 const char *statement_name,
160 const struct GNUNET_PQ_QueryParam *params,
161 GNUNET_PQ_PostgresResultHandler rh,
162 void *rh_cls)
163{
164 PGresult *result;
165 enum GNUNET_PQ_QueryStatus qs;
166 unsigned int ret;
167
168 result = GNUNET_PQ_exec_prepared (connection,
169 statement_name,
170 params);
171 qs = GNUNET_PQ_eval_result (connection,
172 statement_name,
173 result);
174 if (qs < 0)
175 {
176 PQclear (result);
177 return qs;
178 }
179 ret = PQntuples (result);
180 if (NULL != rh)
181 rh (rh_cls,
182 result,
183 ret);
184 PQclear (result);
185 return ret;
186}
187
188
189/**
190 * Execute a named prepared @a statement that is a SELECT statement
191 * which must return a single result in @a connection using the given
192 * @a params. Stores the result (if any) in @a rs, which the caller
193 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
194 * value was #GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT. Returns the
195 * resulting session status.
196 *
197 * @param connection connection to execute the statement in
198 * @param statement_name name of the statement
199 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
200 * @param[in,out] rs result specification to use for storing the result of the query
201 * @return status code from the result, mapping PQ status
202 * codes to `enum GNUNET_PQ_QueryStatus`.
203 */
204enum GNUNET_PQ_QueryStatus
205GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
206 const char *statement_name,
207 const struct GNUNET_PQ_QueryParam *params,
208 struct GNUNET_PQ_ResultSpec *rs)
209{
210 PGresult *result;
211 enum GNUNET_PQ_QueryStatus qs;
212
213 result = GNUNET_PQ_exec_prepared (connection,
214 statement_name,
215 params);
216 qs = GNUNET_PQ_eval_result (connection,
217 statement_name,
218 result);
219 if (qs < 0)
220 {
221 PQclear (result);
222 return qs;
223 }
224 if (0 == PQntuples (result))
225 {
226 PQclear (result);
227 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
228 }
229 if (1 != PQntuples (result))
230 {
231 /* more than one result, but there must be at most one */
232 GNUNET_break (0);
233 PQclear (result);
234 return GNUNET_PQ_STATUS_HARD_ERROR;
235 }
236 if (GNUNET_OK !=
237 GNUNET_PQ_extract_result (result,
238 rs,
239 0))
240 {
241 PQclear (result);
242 return GNUNET_PQ_STATUS_HARD_ERROR;
243 }
244 PQclear (result);
245 return GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT;
246}
247
248
249/* end of pq/pq_eval.c */
diff --git a/src/pq/pq_exec.c b/src/pq/pq_exec.c
new file mode 100644
index 000000000..1e5e4eb76
--- /dev/null
+++ b/src/pq/pq_exec.c
@@ -0,0 +1,106 @@
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_exec.c
18 * @brief functions to execute plain SQL statements (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 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
28 *
29 * @param sql actual SQL statement
30 * @return initialized struct
31 */
32struct GNUNET_PQ_ExecuteStatement
33GNUNET_PQ_make_execute (const char *sql)
34{
35 struct GNUNET_PQ_ExecuteStatement es = {
36 .sql = sql,
37 .ignore_errors = GNUNET_NO
38 };
39
40 return es;
41}
42
43
44/**
45 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
46 * be tolerated.
47 *
48 * @param sql actual SQL statement
49 * @return initialized struct
50 */
51struct GNUNET_PQ_ExecuteStatement
52GNUNET_PQ_make_try_execute (const char *sql)
53{
54 struct GNUNET_PQ_ExecuteStatement es = {
55 .sql = sql,
56 .ignore_errors = GNUNET_YES
57 };
58
59 return es;
60}
61
62
63/**
64 * Request execution of an array of statements @a es from Postgres.
65 *
66 * @param connection connection to execute the statements over
67 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
68 * statements.
69 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
70 * #GNUNET_SYSERR on error
71 */
72int
73GNUNET_PQ_exec_statements (PGconn *connection,
74 const struct GNUNET_PQ_ExecuteStatement *es)
75{
76 for (unsigned int i=0; NULL != es[i].sql; i++)
77 {
78 PGresult *result;
79
80 result = PQexec (connection,
81 es[i].sql);
82
83 if ( (GNUNET_NO == es[i].ignore_errors) &&
84 (PGRES_COMMAND_OK != PQresultStatus (result)) )
85 {
86 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
87 "pq",
88 "Failed to execute `%s': %s/%s/%s/%s/%s",
89 es[i].sql,
90 PQresultErrorField (result,
91 PG_DIAG_MESSAGE_PRIMARY),
92 PQresultErrorField (result,
93 PG_DIAG_MESSAGE_DETAIL),
94 PQresultErrorMessage (result),
95 PQresStatus (PQresultStatus (result)),
96 PQerrorMessage (connection));
97 PQclear (result);
98 return GNUNET_SYSERR;
99 }
100 PQclear (result);
101 }
102 return GNUNET_OK;
103}
104
105
106/* end of pq/pq_exec.c */
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c
new file mode 100644
index 000000000..f533cb564
--- /dev/null
+++ b/src/pq/pq_prepare.c
@@ -0,0 +1,92 @@
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_prepare.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 * Create a `struct GNUNET_PQ_PreparedStatement`.
28 *
29 * @param name name of the statement
30 * @param sql actual SQL statement
31 * @param num_args number of arguments in the statement
32 * @return initialized struct
33 */
34struct GNUNET_PQ_PreparedStatement
35GNUNET_PQ_make_prepare (const char *name,
36 const char *sql,
37 unsigned int num_args)
38{
39 struct GNUNET_PQ_PreparedStatement ps = {
40 .name = name,
41 .sql = sql,
42 .num_arguments = num_args
43 };
44
45 return ps;
46}
47
48
49/**
50 * Request creation of prepared statements @a ps from Postgres.
51 *
52 * @param connection connection to prepare the statements for
53 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
54 * statements.
55 * @return #GNUNET_OK on success,
56 * #GNUNET_SYSERR on error
57 */
58int
59GNUNET_PQ_prepare_statements (PGconn *connection,
60 const struct GNUNET_PQ_PreparedStatement *ps)
61{
62 for (unsigned int i=0;NULL != ps[i].name;i++)
63 {
64 PGresult *ret;
65
66 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
67 "pq",
68 "Preparing SQL statement `%s' as `%s'\n",
69 ps[i].sql,
70 ps[i].name);
71 ret = PQprepare (connection,
72 ps[i].name,
73 ps[i].sql,
74 ps[i].num_arguments,
75 NULL);
76 if (PGRES_COMMAND_OK != PQresultStatus (ret))
77 {
78 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
79 "pq",
80 _("PQprepare (`%s' as `%s') failed with error: %s\n"),
81 ps[i].sql,
82 ps[i].name,
83 PQerrorMessage (connection));
84 PQclear (ret);
85 return GNUNET_SYSERR;
86 }
87 }
88 return GNUNET_OK;
89}
90
91
92/* end of pq/pq_prepare.c */