9296dce2e478998b314dca99ab67fc41d751b3fa
[oweals/gnunet.git] / src / pq / pq_eval.c
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  */
55 enum GNUNET_PQ_QueryStatus
56 GNUNET_PQ_eval_result (PGconn *connection,
57                        const char *statement_name,
58                        PGresult *result)
59 {
60   ExecStatusType est;
61
62   est = PQresultStatus (result);
63   if ( (PGRES_COMMAND_OK != est) &&
64        (PGRES_TUPLES_OK != est) )
65   {
66     const char *sqlstate;
67
68     sqlstate = PQresultErrorField (result,
69                                    PG_DIAG_SQLSTATE);
70     if (NULL == sqlstate)
71     {
72       /* very unexpected... */
73       GNUNET_break (0);
74       return GNUNET_PQ_STATUS_HARD_ERROR;
75     }
76     if ( (0 == strcmp (sqlstate,
77                        PQ_DIAG_SQLSTATE_DEADLOCK)) ||
78          (0 == strcmp (sqlstate,
79                        PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) )
80     {
81       /* These two can be retried and have a fair chance of working
82          the next time */
83       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
84                        "pq",
85                        "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
86                        statement_name,
87                        PQresultErrorField (result,
88                                            PG_DIAG_MESSAGE_PRIMARY),
89                        PQresultErrorField (result,
90                                            PG_DIAG_MESSAGE_DETAIL),
91                        PQresultErrorMessage (result),
92                        PQresStatus (PQresultStatus (result)),
93                        PQerrorMessage (connection));
94       return GNUNET_PQ_STATUS_SOFT_ERROR;
95     }
96     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
97                      "pq",
98                      "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
99                      statement_name,
100                      PQresultErrorField (result,
101                                          PG_DIAG_MESSAGE_PRIMARY),
102                      PQresultErrorField (result,
103                                          PG_DIAG_MESSAGE_DETAIL),
104                      PQresultErrorMessage (result),
105                      PQresStatus (PQresultStatus (result)),
106                      PQerrorMessage (connection));
107     return GNUNET_PQ_STATUS_HARD_ERROR;
108   }
109   return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
110 }
111
112
113 /**
114  * Execute a named prepared @a statement that is NOT a SELECT
115  * statement in @a connnection using the given @a params.  Returns the
116  * resulting session state.
117  *
118  * @param connection connection to execute the statement in
119  * @param statement_name name of the statement
120  * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
121  * @return status code from the result, mapping PQ status
122  *         codes to `enum GNUNET_PQ_QueryStatus`.  If the
123  *         statement was a DELETE or UPDATE statement, the
124  *         number of affected rows is returned.
125  */
126 enum GNUNET_PQ_QueryStatus
127 GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
128                                     const char *statement_name,
129                                     const struct GNUNET_PQ_QueryParam *params)
130 {
131   PGresult *result;
132   enum GNUNET_PQ_QueryStatus qs;
133
134   result = GNUNET_PQ_exec_prepared (connection,
135                                     statement_name,
136                                     params);
137   qs = GNUNET_PQ_eval_result (connection,
138                               statement_name,
139                               result);
140   if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS == qs)
141   {
142     const char *tuples;
143
144     /* What an awful API, this function really does return a string */
145     tuples = PQcmdTuples (result);
146     if (NULL != tuples)
147       qs = strtol (tuples, NULL, 10);
148   }
149   PQclear (result);
150   return qs;
151 }
152
153
154 /**
155  * Execute a named prepared @a statement that is a SELECT statement
156  * which may return multiple results in @a connection using the given
157  * @a params.  Call @a rh with the results.  Returns the query
158  * status including the number of results given to @a rh (possibly zero).
159  * @a rh will not have been called if the return value is negative.
160  *
161  * @param connection connection to execute the statement in
162  * @param statement_name name of the statement
163  * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
164  * @param rh function to call with the result set, NULL to ignore
165  * @param rh_cls closure to pass to @a rh
166  * @return status code from the result, mapping PQ status
167  *         codes to `enum GNUNET_PQ_QueryStatus`.
168  */
169 enum GNUNET_PQ_QueryStatus
170 GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
171                                       const char *statement_name,
172                                       const struct GNUNET_PQ_QueryParam *params,
173                                       GNUNET_PQ_PostgresResultHandler rh,
174                                       void *rh_cls)
175 {
176   PGresult *result;
177   enum GNUNET_PQ_QueryStatus qs;
178   unsigned int ret;
179
180   result = GNUNET_PQ_exec_prepared (connection,
181                                     statement_name,
182                                     params);
183   qs = GNUNET_PQ_eval_result (connection,
184                               statement_name,
185                               result);
186   if (qs < 0)
187   {
188     PQclear (result);
189     return qs;
190   }
191   ret = PQntuples (result);
192   if (NULL != rh)
193     rh (rh_cls,
194         result,
195         ret);
196   PQclear (result);
197   return ret;
198 }
199
200
201 /**
202  * Execute a named prepared @a statement that is a SELECT statement
203  * which must return a single result in @a connection using the given
204  * @a params.  Stores the result (if any) in @a rs, which the caller
205  * must then clean up using #GNUNET_PQ_cleanup_result() if the return
206  * value was #GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT.  Returns the
207  * resulting session status.
208  *
209  * @param connection connection to execute the statement in
210  * @param statement_name name of the statement
211  * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
212  * @param[in,out] rs result specification to use for storing the result of the query
213  * @return status code from the result, mapping PQ status
214  *         codes to `enum GNUNET_PQ_QueryStatus`.
215  */
216 enum GNUNET_PQ_QueryStatus
217 GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
218                                           const char *statement_name,
219                                           const struct GNUNET_PQ_QueryParam *params,
220                                           struct GNUNET_PQ_ResultSpec *rs)
221 {
222   PGresult *result;
223   enum GNUNET_PQ_QueryStatus qs;
224
225   result = GNUNET_PQ_exec_prepared (connection,
226                                     statement_name,
227                                     params);
228   qs = GNUNET_PQ_eval_result (connection,
229                               statement_name,
230                               result);
231   if (qs < 0)
232   {
233     PQclear (result);
234     return qs;
235   }
236   if (0 == PQntuples (result))
237   {
238     PQclear (result);
239     return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
240   }
241   if (1 != PQntuples (result))
242   {
243     /* more than one result, but there must be at most one */
244     GNUNET_break (0);
245     PQclear (result);
246     return GNUNET_PQ_STATUS_HARD_ERROR;
247   }
248   if (GNUNET_OK !=
249       GNUNET_PQ_extract_result (result,
250                                 rs,
251                                 0))
252   {
253     PQclear (result);
254     return GNUNET_PQ_STATUS_HARD_ERROR;
255   }
256   PQclear (result);
257   return GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT;
258 }
259
260
261 /* end of pq/pq_eval.c */