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,
62 est = PQresultStatus (result);
63 if ( (PGRES_COMMAND_OK != est) &&
64 (PGRES_TUPLES_OK != est) )
68 sqlstate = PQresultErrorField (result,
72 /* very unexpected... */
74 return GNUNET_PQ_STATUS_HARD_ERROR;
76 if ( (0 == strcmp (sqlstate,
77 PQ_DIAG_SQLSTATE_DEADLOCK)) ||
78 (0 == strcmp (sqlstate,
79 PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) )
81 /* These two can be retried and have a fair chance of working
83 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
85 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
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;
96 if (0 == strcmp (sqlstate,
97 PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION))
99 /* Likely no need to retry, INSERT of "same" data. */
100 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
102 "Query `%s' failed with unique violation: %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 (connection));
111 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
113 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
115 "Query `%s' failed with result: %s/%s/%s/%s/%s\n",
117 PQresultErrorField (result,
118 PG_DIAG_MESSAGE_PRIMARY),
119 PQresultErrorField (result,
120 PG_DIAG_MESSAGE_DETAIL),
121 PQresultErrorMessage (result),
122 PQresStatus (PQresultStatus (result)),
123 PQerrorMessage (connection));
124 return GNUNET_PQ_STATUS_HARD_ERROR;
126 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
131 * Execute a named prepared @a statement that is NOT a SELECT
132 * statement in @a connnection using the given @a params. Returns the
133 * resulting session state.
135 * @param connection connection to execute the statement in
136 * @param statement_name name of the statement
137 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
138 * @return status code from the result, mapping PQ status
139 * codes to `enum GNUNET_PQ_QueryStatus`. If the
140 * statement was a DELETE or UPDATE statement, the
141 * number of affected rows is returned.; if the
142 * statment was an INSERT statement, and no row
143 * was added due to a UNIQUE violation, we return
144 * zero; if INSERT was successful, we return one.
146 enum GNUNET_PQ_QueryStatus
147 GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
148 const char *statement_name,
149 const struct GNUNET_PQ_QueryParam *params)
152 enum GNUNET_PQ_QueryStatus qs;
154 result = GNUNET_PQ_exec_prepared (connection,
157 qs = GNUNET_PQ_eval_result (connection,
160 if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS == qs)
164 /* What an awful API, this function really does return a string */
165 tuples = PQcmdTuples (result);
167 qs = strtol (tuples, NULL, 10);
175 * Execute a named prepared @a statement that is a SELECT statement
176 * which may return multiple results in @a connection using the given
177 * @a params. Call @a rh with the results. Returns the query
178 * status including the number of results given to @a rh (possibly zero).
179 * @a rh will not have been called if the return value is negative.
181 * @param connection connection to execute the statement in
182 * @param statement_name name of the statement
183 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
184 * @param rh function to call with the result set, NULL to ignore
185 * @param rh_cls closure to pass to @a rh
186 * @return status code from the result, mapping PQ status
187 * codes to `enum GNUNET_PQ_QueryStatus`.
189 enum GNUNET_PQ_QueryStatus
190 GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
191 const char *statement_name,
192 const struct GNUNET_PQ_QueryParam *params,
193 GNUNET_PQ_PostgresResultHandler rh,
197 enum GNUNET_PQ_QueryStatus qs;
200 result = GNUNET_PQ_exec_prepared (connection,
203 qs = GNUNET_PQ_eval_result (connection,
211 ret = PQntuples (result);
222 * Execute a named prepared @a statement that is a SELECT statement
223 * which must return a single result in @a connection using the given
224 * @a params. Stores the result (if any) in @a rs, which the caller
225 * must then clean up using #GNUNET_PQ_cleanup_result() if the return
226 * value was #GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT. Returns the
227 * resulting session status.
229 * @param connection connection to execute the statement in
230 * @param statement_name name of the statement
231 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
232 * @param[in,out] rs result specification to use for storing the result of the query
233 * @return status code from the result, mapping PQ status
234 * codes to `enum GNUNET_PQ_QueryStatus`.
236 enum GNUNET_PQ_QueryStatus
237 GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
238 const char *statement_name,
239 const struct GNUNET_PQ_QueryParam *params,
240 struct GNUNET_PQ_ResultSpec *rs)
243 enum GNUNET_PQ_QueryStatus qs;
245 result = GNUNET_PQ_exec_prepared (connection,
248 qs = GNUNET_PQ_eval_result (connection,
256 if (0 == PQntuples (result))
259 return GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS;
261 if (1 != PQntuples (result))
263 /* more than one result, but there must be at most one */
266 return GNUNET_PQ_STATUS_HARD_ERROR;
269 GNUNET_PQ_extract_result (result,
274 return GNUNET_PQ_STATUS_HARD_ERROR;
277 return GNUNET_PQ_STATUS_SUCCESS_ONE_RESULT;
281 /* end of pq/pq_eval.c */