summaryrefslogtreecommitdiff
path: root/src/pq/pq_eval.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-11 20:55:59 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-11 23:36:09 +0200
commite3e21acb23283915c97e6ef1c167325f4592665c (patch)
treed6b677c2f53ffd8253b97be26bcf3a4301f69269 /src/pq/pq_eval.c
parent8ed3ad85fa8c0faa213157610379d69875b10ccb (diff)
downloadgnunet-e3e21acb23283915c97e6ef1c167325f4592665c.tar.gz
gnunet-e3e21acb23283915c97e6ef1c167325f4592665c.zip
libgnunetpq API change to fix #5733
Diffstat (limited to 'src/pq/pq_eval.c')
-rw-r--r--src/pq/pq_eval.c59
1 files changed, 39 insertions, 20 deletions
diff --git a/src/pq/pq_eval.c b/src/pq/pq_eval.c
index 1d041f226..5bcf8ca0e 100644
--- a/src/pq/pq_eval.c
+++ b/src/pq/pq_eval.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017, 2019 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -23,8 +23,7 @@
23 * @author Christian Grothoff 23 * @author Christian Grothoff
24 */ 24 */
25#include "platform.h" 25#include "platform.h"
26#include "gnunet_util_lib.h" 26#include "pq.h"
27#include "gnunet_pq_lib.h"
28 27
29 28
30/** 29/**
@@ -47,7 +46,7 @@
47 * Check the @a result's error code to see what happened. 46 * Check the @a result's error code to see what happened.
48 * Also logs errors. 47 * Also logs errors.
49 * 48 *
50 * @param connection connection to execute the statement in 49 * @param db database to execute the statement with
51 * @param statement_name name of the statement that created @a result 50 * @param statement_name name of the statement that created @a result
52 * @param result result to check 51 * @param result result to check
53 * @return status code from the result, mapping PQ status 52 * @return status code from the result, mapping PQ status
@@ -57,17 +56,31 @@
57 * @deprecated (low level, let's see if we can do with just the high-level functions) 56 * @deprecated (low level, let's see if we can do with just the high-level functions)
58 */ 57 */
59enum GNUNET_DB_QueryStatus 58enum GNUNET_DB_QueryStatus
60GNUNET_PQ_eval_result (PGconn *connection, 59GNUNET_PQ_eval_result (struct GNUNET_PQ_Context *db,
61 const char *statement_name, 60 const char *statement_name,
62 PGresult *result) 61 PGresult *result)
63{ 62{
64 ExecStatusType est; 63 ExecStatusType est;
65 64
65 if (NULL == result)
66 return GNUNET_DB_STATUS_SOFT_ERROR;
66 est = PQresultStatus (result); 67 est = PQresultStatus (result);
67 if ((PGRES_COMMAND_OK != est) && 68 if ((PGRES_COMMAND_OK != est) &&
68 (PGRES_TUPLES_OK != est)) 69 (PGRES_TUPLES_OK != est))
69 { 70 {
70 const char *sqlstate; 71 const char *sqlstate;
72 ConnStatusType status;
73
74 if (CONNECTION_OK != (status = PQstatus (db->conn)))
75 {
76 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
77 "pq",
78 "Database connection failed during query `%s': %d (reconnecting)\n",
79 statement_name,
80 status);
81 GNUNET_PQ_reconnect (db);
82 return GNUNET_DB_STATUS_SOFT_ERROR;
83 }
71 84
72 sqlstate = PQresultErrorField (result, 85 sqlstate = PQresultErrorField (result,
73 PG_DIAG_SQLSTATE); 86 PG_DIAG_SQLSTATE);
@@ -94,7 +107,7 @@ GNUNET_PQ_eval_result (PGconn *connection,
94 PG_DIAG_MESSAGE_DETAIL), 107 PG_DIAG_MESSAGE_DETAIL),
95 PQresultErrorMessage (result), 108 PQresultErrorMessage (result),
96 PQresStatus (PQresultStatus (result)), 109 PQresStatus (PQresultStatus (result)),
97 PQerrorMessage (connection)); 110 PQerrorMessage (db->conn));
98 return GNUNET_DB_STATUS_SOFT_ERROR; 111 return GNUNET_DB_STATUS_SOFT_ERROR;
99 } 112 }
100 if (0 == strcmp (sqlstate, 113 if (0 == strcmp (sqlstate,
@@ -111,7 +124,7 @@ GNUNET_PQ_eval_result (PGconn *connection,
111 PG_DIAG_MESSAGE_DETAIL), 124 PG_DIAG_MESSAGE_DETAIL),
112 PQresultErrorMessage (result), 125 PQresultErrorMessage (result),
113 PQresStatus (PQresultStatus (result)), 126 PQresStatus (PQresultStatus (result)),
114 PQerrorMessage (connection)); 127 PQerrorMessage (db->conn));
115 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 128 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
116 } 129 }
117 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 130 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
@@ -124,7 +137,7 @@ GNUNET_PQ_eval_result (PGconn *connection,
124 PG_DIAG_MESSAGE_DETAIL), 137 PG_DIAG_MESSAGE_DETAIL),
125 PQresultErrorMessage (result), 138 PQresultErrorMessage (result),
126 PQresStatus (PQresultStatus (result)), 139 PQresStatus (PQresultStatus (result)),
127 PQerrorMessage (connection)); 140 PQerrorMessage (db->conn));
128 return GNUNET_DB_STATUS_HARD_ERROR; 141 return GNUNET_DB_STATUS_HARD_ERROR;
129 } 142 }
130 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 143 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
@@ -136,7 +149,7 @@ GNUNET_PQ_eval_result (PGconn *connection,
136 * statement in @a connnection using the given @a params. Returns the 149 * statement in @a connnection using the given @a params. Returns the
137 * resulting session state. 150 * resulting session state.
138 * 151 *
139 * @param connection connection to execute the statement in 152 * @param db database to execute the statement with
140 * @param statement_name name of the statement 153 * @param statement_name name of the statement
141 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) 154 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated)
142 * @return status code from the result, mapping PQ status 155 * @return status code from the result, mapping PQ status
@@ -148,17 +161,19 @@ GNUNET_PQ_eval_result (PGconn *connection,
148 * zero; if INSERT was successful, we return one. 161 * zero; if INSERT was successful, we return one.
149 */ 162 */
150enum GNUNET_DB_QueryStatus 163enum GNUNET_DB_QueryStatus
151GNUNET_PQ_eval_prepared_non_select (PGconn *connection, 164GNUNET_PQ_eval_prepared_non_select (struct GNUNET_PQ_Context *db,
152 const char *statement_name, 165 const char *statement_name,
153 const struct GNUNET_PQ_QueryParam *params) 166 const struct GNUNET_PQ_QueryParam *params)
154{ 167{
155 PGresult *result; 168 PGresult *result;
156 enum GNUNET_DB_QueryStatus qs; 169 enum GNUNET_DB_QueryStatus qs;
157 170
158 result = GNUNET_PQ_exec_prepared (connection, 171 result = GNUNET_PQ_exec_prepared (db,
159 statement_name, 172 statement_name,
160 params); 173 params);
161 qs = GNUNET_PQ_eval_result (connection, 174 if (NULL == result)
175 return GNUNET_DB_STATUS_SOFT_ERROR;
176 qs = GNUNET_PQ_eval_result (db,
162 statement_name, 177 statement_name,
163 result); 178 result);
164 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 179 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
@@ -182,7 +197,7 @@ GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
182 * status including the number of results given to @a rh (possibly zero). 197 * 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. 198 * @a rh will not have been called if the return value is negative.
184 * 199 *
185 * @param connection connection to execute the statement in 200 * @param db database to execute the statement with
186 * @param statement_name name of the statement 201 * @param statement_name name of the statement
187 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) 202 * @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 203 * @param rh function to call with the result set, NULL to ignore
@@ -191,7 +206,7 @@ GNUNET_PQ_eval_prepared_non_select (PGconn *connection,
191 * codes to `enum GNUNET_DB_QueryStatus`. 206 * codes to `enum GNUNET_DB_QueryStatus`.
192 */ 207 */
193enum GNUNET_DB_QueryStatus 208enum GNUNET_DB_QueryStatus
194GNUNET_PQ_eval_prepared_multi_select (PGconn *connection, 209GNUNET_PQ_eval_prepared_multi_select (struct GNUNET_PQ_Context *db,
195 const char *statement_name, 210 const char *statement_name,
196 const struct GNUNET_PQ_QueryParam *params, 211 const struct GNUNET_PQ_QueryParam *params,
197 GNUNET_PQ_PostgresResultHandler rh, 212 GNUNET_PQ_PostgresResultHandler rh,
@@ -201,10 +216,12 @@ GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
201 enum GNUNET_DB_QueryStatus qs; 216 enum GNUNET_DB_QueryStatus qs;
202 unsigned int ret; 217 unsigned int ret;
203 218
204 result = GNUNET_PQ_exec_prepared (connection, 219 result = GNUNET_PQ_exec_prepared (db,
205 statement_name, 220 statement_name,
206 params); 221 params);
207 qs = GNUNET_PQ_eval_result (connection, 222 if (NULL == result)
223 return GNUNET_DB_STATUS_SOFT_ERROR;
224 qs = GNUNET_PQ_eval_result (db,
208 statement_name, 225 statement_name,
209 result); 226 result);
210 if (qs < 0) 227 if (qs < 0)
@@ -230,7 +247,7 @@ GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
230 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the 247 * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the
231 * resulting session status. 248 * resulting session status.
232 * 249 *
233 * @param connection connection to execute the statement in 250 * @param db database to execute the statement with
234 * @param statement_name name of the statement 251 * @param statement_name name of the statement
235 * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) 252 * @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 253 * @param[in,out] rs result specification to use for storing the result of the query
@@ -238,7 +255,7 @@ GNUNET_PQ_eval_prepared_multi_select (PGconn *connection,
238 * codes to `enum GNUNET_DB_QueryStatus`. 255 * codes to `enum GNUNET_DB_QueryStatus`.
239 */ 256 */
240enum GNUNET_DB_QueryStatus 257enum GNUNET_DB_QueryStatus
241GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection, 258GNUNET_PQ_eval_prepared_singleton_select (struct GNUNET_PQ_Context *db,
242 const char *statement_name, 259 const char *statement_name,
243 const struct 260 const struct
244 GNUNET_PQ_QueryParam *params, 261 GNUNET_PQ_QueryParam *params,
@@ -247,10 +264,12 @@ GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection,
247 PGresult *result; 264 PGresult *result;
248 enum GNUNET_DB_QueryStatus qs; 265 enum GNUNET_DB_QueryStatus qs;
249 266
250 result = GNUNET_PQ_exec_prepared (connection, 267 result = GNUNET_PQ_exec_prepared (db,
251 statement_name, 268 statement_name,
252 params); 269 params);
253 qs = GNUNET_PQ_eval_result (connection, 270 if (NULL == result)
271 return GNUNET_DB_STATUS_SOFT_ERROR;
272 qs = GNUNET_PQ_eval_result (db,
254 statement_name, 273 statement_name,
255 result); 274 result);
256 if (qs < 0) 275 if (qs < 0)