reduce loop counters to more practical levels
[oweals/gnunet.git] / src / pq / pq_exec.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2017 GNUnet e.V.
4
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.
8
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.
12
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/>
15 */
16 /**
17  * @file pq/pq_exec.c
18  * @brief functions to execute plain SQL statements (PostGres)
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
24
25
26 /**
27  * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
28  *
29  * @param sql actual SQL statement
30  * @return initialized struct
31  */
32 struct GNUNET_PQ_ExecuteStatement
33 GNUNET_PQ_make_execute (const char *sql)
34 {
35   struct GNUNET_PQ_ExecuteStatement es = {
36     .sql = sql,
37     .ignore_errors = GNUNET_NO
38   };
39
40   return es;
41 }
42
43
44 /**
45  * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
46  * be tolerated.
47  *
48  * @param sql actual SQL statement
49  * @return initialized struct
50  */
51 struct GNUNET_PQ_ExecuteStatement
52 GNUNET_PQ_make_try_execute (const char *sql)
53 {
54   struct GNUNET_PQ_ExecuteStatement es = {
55     .sql = sql,
56     .ignore_errors = GNUNET_YES
57   };
58
59   return es;
60 }
61
62
63 /**
64  * Request execution of an array of statements @a es from Postgres.
65  *
66  * @param connection connection to execute the statements over
67  * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
68  *            statements.
69  * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
70  *         #GNUNET_SYSERR on error
71  */
72 int
73 GNUNET_PQ_exec_statements (PGconn *connection,
74                            const struct GNUNET_PQ_ExecuteStatement *es)
75 {
76   for (unsigned int i=0; NULL != es[i].sql; i++)
77   {
78     PGresult *result;
79
80     result = PQexec (connection,
81                      es[i].sql);
82     if ( (GNUNET_NO == es[i].ignore_errors) &&
83          (PGRES_COMMAND_OK != PQresultStatus (result)) )
84     {
85       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
86                        "pq",
87                        "Failed to execute `%s': %s/%s/%s/%s/%s",
88                        es[i].sql,
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));
96       PQclear (result);
97       return GNUNET_SYSERR;
98     }
99     PQclear (result);
100   }
101   return GNUNET_OK;
102 }
103
104
105 /* end of pq/pq_exec.c */