summaryrefslogtreecommitdiff
path: root/src/pq/pq_eval.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq_eval.c')
-rw-r--r--src/pq/pq_eval.c249
1 files changed, 249 insertions, 0 deletions
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 */