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
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief functions to execute SQL statements with arguments and/or results (PostGres)
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
31 * Error code returned by Postgres for deadlock.
33 #define PQ_DIAG_SQLSTATE_DEADLOCK "40P01"
36 * Error code returned by Postgres for uniqueness violation.
38 #define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION "23505"
41 * Error code returned by Postgres on serialization failure.
43 #define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE "40001"
47 * Check the @a result's error code to see what happened.
50 * @param connection connection to execute the statement in
51 * @param statement_name name of the statement that created @a result
52 * @param result result to check
53 * @return status code from the result, mapping PQ status
54 * codes to `enum GNUNET_DB_QueryStatus`. Never
55 * returns positive values as this function does
56 * not look at the result set.
57 * @deprecated (low level, let's see if we can do with just the high-level functions)
59 enum GNUNET_DB_QueryStatus
60 GNUNET_PQ_eval_result (PGconn *connection,
61 const char *statement_name,
66 est = PQresultStatus (result);
67 if ( (PGRES_COMMAND_OK != est) &&
68 (PGRES_TUPLES_OK != est) )
72 sqlstate = PQresultErrorField (result,
76 /* very unexpected... */
78 return GNUNET_DB_STATUS_HARD_ERROR;
80 if ( (0 == strcmp (sqlstate,
81 PQ_DIAG_SQLSTATE_DEADLOCK)) ||
82 (0 == strcmp (sqlstate,
83 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) )
85 /* These two can be retried and have a fair chance of working
87 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
89 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
91 PQresultErrorField (result,
92 PG_DIAG_MESSAGE_PRIMARY),
93 PQresultErrorField (result,
94 PG_DIAG_MESSAGE_DETAIL),
95 PQresultErrorMessage (result),
96 PQresStatus (PQresultStatus (result)),
97 PQerrorMessage (connection));
98 return GNUNET_DB_STATUS_SOFT_ERROR;
100 if (0 == strcmp (sqlstate,
101 PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION))
103 /* Likely no need to retry, INSERT of "same" data. */
104 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
106 "Query `%s' failed with unique violation: %s/%s/%s/%s/%s\n",
108 PQresultErrorField (result,
109 PG_DIAG_MESSAGE_PRIMARY),
110 PQresultErrorField (result,
111 PG_DIAG_MESSAGE_DETAIL),
112 PQresultErrorMessage (result),
113 PQresStatus (PQresultStatus (result)),
114 PQerrorMessage (connection));
115 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
117 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
119 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
121 PQresultErrorField (result,
122 PG_DIAG_MESSAGE_PRIMARY),
123 PQresultErrorField (result,
124 PG_DIAG_MESSAGE_DETAIL),
125 PQresultErrorMessage (result),
126 PQresStatus (PQresultStatus (result)),
127 PQerrorMessage (connection));
128 return GNUNET_DB_STATUS_HARD_ERROR;
130 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
135 * Execute a named prepared @a statement that is NOT a SELECT
136 * statement in @a connnection using the given @a params. Returns the
137 * resulting session state.
139 * @param connection connection to execute the statement in
140 * @param statement_name name of the statement
141 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
142 * @return status code from the result, mapping PQ status
143 * codes to `enum GNUNET_DB_QueryStatus`. If the
144 * statement was a DELETE or UPDATE statement, the
145 * number of affected rows is returned.; if the
146 * statment was an INSERT statement, and no row
147 * was added due to a UNIQUE violation, we return
148 * zero; if INSERT was successful, we return one.
150 enum GNUNET_DB_QueryStatus
151 GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
152 const char *statement_name,
153 const struct GNUNET_PQ_QueryParam *params)
156 enum GNUNET_DB_QueryStatus qs;
158 result = GNUNET_PQ_exec_prepared (connection,
161 qs = GNUNET_PQ_eval_result (connection,
164 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
168 /* What an awful API, this function really does return a string */
169 tuples = PQcmdTuples (result);
171 qs = strtol (tuples, NULL, 10);
179 * Execute a named prepared @a statement that is a SELECT statement
180 * which may return multiple results in @a connection using the given
181 * @a params. Call @a rh with the results. Returns the query
182 * status including the number of results given to @a rh (possibly zero).
183 * @a rh will not have been called if the return value is negative.
185 * @param connection connection to execute the statement in
186 * @param statement_name name of the statement
187 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
188 * @param rh function to call with the result set, NULL to ignore
189 * @param rh_cls closure to pass to @a rh
190 * @return status code from the result, mapping PQ status
191 * codes to `enum GNUNET_DB_QueryStatus`.
193 enum GNUNET_DB_QueryStatus
194 GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
195 const char *statement_name,
196 const struct GNUNET_PQ_QueryParam *params,
197 GNUNET_PQ_PostgresResultHandler rh,
201 enum GNUNET_DB_QueryStatus qs;
204 result = GNUNET_PQ_exec_prepared (connection,
207 qs = GNUNET_PQ_eval_result (connection,
215 ret = PQntuples (result);
226 * Execute a named prepared @a statement that is a SELECT statement
227 * which must return a single result in @a connection using the given
228 * @a params. Stores the result (if any) in @a rs, which the caller
229 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
230 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
231 * resulting session status.
233 * @param connection connection to execute the statement in
234 * @param statement_name name of the statement
235 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
236 * @param[in,out] rs result specification to use for storing the result of the query
237 * @return status code from the result, mapping PQ status
238 * codes to `enum GNUNET_DB_QueryStatus`.
240 enum GNUNET_DB_QueryStatus
241 GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
242 const char *statement_name,
243 const struct GNUNET_PQ_QueryParam *params,
244 struct GNUNET_PQ_ResultSpec *rs)
247 enum GNUNET_DB_QueryStatus qs;
249 result = GNUNET_PQ_exec_prepared (connection,
252 qs = GNUNET_PQ_eval_result (connection,
260 if (0 == PQntuples (result))
263 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
265 if (1 != PQntuples (result))
267 /* more than one result, but there must be at most one */
270 return GNUNET_DB_STATUS_HARD_ERROR;
273 GNUNET_PQ_extract_result (result,
278 return GNUNET_DB_STATUS_HARD_ERROR;
281 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
285 /* end of pq/pq_eval.c */