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 plain SQL statements (PostGres)
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
27 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
29 * @param sql actual SQL statement
30 * @return initialized struct
32 struct GNUNET_PQ_ExecuteStatement
33 GNUNET_PQ_make_execute (const char *sql)
35 struct GNUNET_PQ_ExecuteStatement es = {
37 .ignore_errors = GNUNET_NO
45 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
48 * @param sql actual SQL statement
49 * @return initialized struct
51 struct GNUNET_PQ_ExecuteStatement
52 GNUNET_PQ_make_try_execute (const char *sql)
54 struct GNUNET_PQ_ExecuteStatement es = {
56 .ignore_errors = GNUNET_YES
64 * Request execution of an array of statements @a es from Postgres.
66 * @param connection connection to execute the statements over
67 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
69 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
70 * #GNUNET_SYSERR on error
73 GNUNET_PQ_exec_statements (PGconn *connection,
74 const struct GNUNET_PQ_ExecuteStatement *es)
76 for (unsigned int i=0; NULL != es[i].sql; i++)
80 result = PQexec (connection,
82 if ( (GNUNET_NO == es[i].ignore_errors) &&
83 (PGRES_COMMAND_OK != PQresultStatus (result)) )
85 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
87 "Failed to execute `%s': %s/%s/%s/%s/%s",
89 PQresultErrorField (result,
90 PG_DIAG_MESSAGE_PRIMARY),
91 PQresultErrorField (result,
92 PG_DIAG_MESSAGE_DETAIL),
93 PQresultErrorMessage (result),
94 PQresStatus (PQresultStatus (result)),
95 PQerrorMessage (connection));
105 /* end of pq/pq_exec.c */