2 This file is part of GNUnet
3 Copyright (C) 2017, 2019 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
30 * Error code returned by Postgres for deadlock.
32 #define PQ_DIAG_SQLSTATE_DEADLOCK "40P01"
35 * Error code returned by Postgres for uniqueness violation.
37 #define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION "23505"
40 * Error code returned by Postgres on serialization failure.
42 #define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE "40001"
46 * Check the @a result's error code to see what happened.
49 * @param db database to execute the statement with
50 * @param statement_name name of the statement that created @a result
51 * @param result result to check
52 * @return status code from the result, mapping PQ status
53 * codes to `enum GNUNET_DB_QueryStatus`. Never
54 * returns positive values as this function does
55 * not look at the result set.
56 * @deprecated (low level, let's see if we can do with just the high-level functions)
58 enum GNUNET_DB_QueryStatus
59 GNUNET_PQ_eval_result (struct GNUNET_PQ_Context *db,
60 const char *statement_name,
66 return GNUNET_DB_STATUS_SOFT_ERROR;
67 est = PQresultStatus (result);
68 if ((PGRES_COMMAND_OK != est) &&
69 (PGRES_TUPLES_OK != est))
72 ConnStatusType status;
74 if (CONNECTION_OK != (status = PQstatus (db->conn)))
76 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
78 "Database connection failed during query `%s': %d (reconnecting)\n",
81 GNUNET_PQ_reconnect (db);
82 return GNUNET_DB_STATUS_SOFT_ERROR;
85 sqlstate = PQresultErrorField (result,
89 /* very unexpected... */
91 return GNUNET_DB_STATUS_HARD_ERROR;
93 if ((0 == strcmp (sqlstate,
94 PQ_DIAG_SQLSTATE_DEADLOCK)) ||
95 (0 == strcmp (sqlstate,
96 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)))
98 /* These two can be retried and have a fair chance of working
100 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
102 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
104 PQresultErrorField (result,
105 PG_DIAG_MESSAGE_PRIMARY),
106 PQresultErrorField (result,
107 PG_DIAG_MESSAGE_DETAIL),
108 PQresultErrorMessage (result),
109 PQresStatus (PQresultStatus (result)),
110 PQerrorMessage (db->conn));
111 return GNUNET_DB_STATUS_SOFT_ERROR;
113 if (0 == strcmp (sqlstate,
114 PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION))
116 /* Likely no need to retry, INSERT of "same" data. */
117 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
119 "Query `%s' failed with unique violation: %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 (db->conn));
128 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
130 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
132 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
134 PQresultErrorField (result,
135 PG_DIAG_MESSAGE_PRIMARY),
136 PQresultErrorField (result,
137 PG_DIAG_MESSAGE_DETAIL),
138 PQresultErrorMessage (result),
139 PQresStatus (PQresultStatus (result)),
140 PQerrorMessage (db->conn));
141 return GNUNET_DB_STATUS_HARD_ERROR;
143 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
148 * Execute a named prepared @a statement that is NOT a SELECT
149 * statement in @a connnection using the given @a params. Returns the
150 * resulting session state.
152 * @param db database to execute the statement with
153 * @param statement_name name of the statement
154 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
155 * @return status code from the result, mapping PQ status
156 * codes to `enum GNUNET_DB_QueryStatus`. If the
157 * statement was a DELETE or UPDATE statement, the
158 * number of affected rows is returned.; if the
159 * statment was an INSERT statement, and no row
160 * was added due to a UNIQUE violation, we return
161 * zero; if INSERT was successful, we return one.
163 enum GNUNET_DB_QueryStatus
164 GNUNET_PQ_eval_prepared_non_select (struct GNUNET_PQ_Context *db,
165 const char *statement_name,
166 const struct GNUNET_PQ_QueryParam *params)
169 enum GNUNET_DB_QueryStatus qs;
171 result = GNUNET_PQ_exec_prepared (db,
175 return GNUNET_DB_STATUS_SOFT_ERROR;
176 qs = GNUNET_PQ_eval_result (db,
179 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
183 /* What an awful API, this function really does return a string */
184 tuples = PQcmdTuples (result);
186 qs = strtol (tuples, NULL, 10);
194 * Execute a named prepared @a statement that is a SELECT statement
195 * which may return multiple results in @a connection using the given
196 * @a params. Call @a rh with the results. Returns the query
197 * status including the number of results given to @a rh (possibly zero).
198 * @a rh will not have been called if the return value is negative.
200 * @param db database to execute the statement with
201 * @param statement_name name of the statement
202 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
203 * @param rh function to call with the result set, NULL to ignore
204 * @param rh_cls closure to pass to @a rh
205 * @return status code from the result, mapping PQ status
206 * codes to `enum GNUNET_DB_QueryStatus`.
208 enum GNUNET_DB_QueryStatus
209 GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db,
210 const char *statement_name,
211 const struct GNUNET_PQ_QueryParam *params,
212 GNUNET_PQ_PostgresResultHandler rh,
216 enum GNUNET_DB_QueryStatus qs;
219 result = GNUNET_PQ_exec_prepared (db,
223 return GNUNET_DB_STATUS_SOFT_ERROR;
224 qs = GNUNET_PQ_eval_result (db,
232 ret = PQntuples (result);
243 * Execute a named prepared @a statement that is a SELECT statement
244 * which must return a single result in @a connection using the given
245 * @a params. Stores the result (if any) in @a rs, which the caller
246 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
247 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
248 * resulting session status.
250 * @param db database to execute the statement with
251 * @param statement_name name of the statement
252 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
253 * @param[in,out] rs result specification to use for storing the result of the query
254 * @return status code from the result, mapping PQ status
255 * codes to `enum GNUNET_DB_QueryStatus`.
257 enum GNUNET_DB_QueryStatus
258 GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db,
259 const char *statement_name,
261 GNUNET_PQ_QueryParam *params,
262 struct GNUNET_PQ_ResultSpec *rs)
265 enum GNUNET_DB_QueryStatus qs;
267 result = GNUNET_PQ_exec_prepared (db,
271 return GNUNET_DB_STATUS_SOFT_ERROR;
272 qs = GNUNET_PQ_eval_result (db,
280 if (0 == PQntuples (result))
283 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
285 if (1 != PQntuples (result))
287 /* more than one result, but there must be at most one */
290 return GNUNET_DB_STATUS_HARD_ERROR;
293 GNUNET_PQ_extract_result (result,
298 return GNUNET_DB_STATUS_HARD_ERROR;
301 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
305 /* end of pq/pq_eval.c */