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/>
18 * @brief functions to execute SQL statements with arguments and/or results (PostGres)
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
27 * Error code returned by Postgres for deadlock.
29 #define PQ_DIAG_SQLSTATE_DEADLOCK "40P01"
32 * Error code returned by Postgres for uniqueness violation.
34 #define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION "23505"
37 * Error code returned by Postgres on serialization failure.
39 #define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE "40001"
43 * Check the @a result's error code to see what happened.
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)
55 enum GNUNET_PQ_QueryStatus
56 GNUNET_PQ_eval_result (PGconn *connection,
57 const char *statement_name,
60 if (PGRES_COMMAND_OK !=
61 PQresultStatus (result))
65 sqlstate = PQresultErrorField (result,
69 /* very unexpected... */
71 return GNUNET_PQ_STATUS_HARD_ERROR;
73 if ( (0 == strcmp (sqlstate,
74 PQ_DIAG_SQLSTATE_DEADLOCK)) ||
75 (0 == strcmp (sqlstate,
76 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) )
78 /* These two can be retried and have a fair chance of working
80 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
82 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
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;
93 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
95 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
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;
106 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
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.
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.
123 enum GNUNET_PQ_QueryStatus
124 GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
125 const char *statement_name,
126 const struct GNUNET_PQ_QueryParam *params)
129 enum GNUNET_PQ_QueryStatus qs;
131 result = GNUNET_PQ_exec_prepared (connection,
134 qs = GNUNET_PQ_eval_result (connection,
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.
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`.
157 enum GNUNET_PQ_QueryStatus
158 GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
159 const char *statement_name,
160 const struct GNUNET_PQ_QueryParam *params,
161 GNUNET_PQ_PostgresResultHandler rh,
165 enum GNUNET_PQ_QueryStatus qs;
168 result = GNUNET_PQ_exec_prepared (connection,
171 qs = GNUNET_PQ_eval_result (connection,
179 ret = PQntuples (result);
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.
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`.
204 enum GNUNET_PQ_QueryStatus
205 GNUNET_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)
211 enum GNUNET_PQ_QueryStatus qs;
213 result = GNUNET_PQ_exec_prepared (connection,
216 qs = GNUNET_PQ_eval_result (connection,
224 if (0 == PQntuples (result))
227 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
229 if (1 != PQntuples (result))
231 /* more than one result, but there must be at most one */
234 return GNUNET_PQ_STATUS_HARD_ERROR;
237 GNUNET_PQ_extract_result (result,
242 return GNUNET_PQ_STATUS_HARD_ERROR;
245 return GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT;
249 /* end of pq/pq_eval.c */