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 plain SQL statements (PostGres)
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
31 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
33 * @param sql actual SQL statement
34 * @return initialized struct
36 struct GNUNET_PQ_ExecuteStatement
37 GNUNET_PQ_make_execute (const char *sql)
39 struct GNUNET_PQ_ExecuteStatement es = {
41 .ignore_errors = GNUNET_NO
49 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
52 * @param sql actual SQL statement
53 * @return initialized struct
55 struct GNUNET_PQ_ExecuteStatement
56 GNUNET_PQ_make_try_execute (const char *sql)
58 struct GNUNET_PQ_ExecuteStatement es = {
60 .ignore_errors = GNUNET_YES
68 * Request execution of an array of statements @a es from Postgres.
70 * @param connection connection to execute the statements over
71 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
73 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
74 * #GNUNET_SYSERR on error
77 GNUNET_PQ_exec_statements (PGconn *connection,
78 const struct GNUNET_PQ_ExecuteStatement *es)
80 for (unsigned int i=0; NULL != es[i].sql; i++)
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Running statement `%s' on %p\n",
88 result = PQexec (connection,
90 if ( (GNUNET_NO == es[i].ignore_errors) &&
91 (PGRES_COMMAND_OK != PQresultStatus (result)) )
93 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
95 "Failed to execute `%s': %s/%s/%s/%s/%s",
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));
105 return GNUNET_SYSERR;
113 /* end of pq/pq_exec.c */