second batch complete. WE ARE AFFERO AGPL NOW!
[oweals/gnunet.git] / src / sq / sq_exec.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2018 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU 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.
9
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.
14 */
15 /**
16  * @file sq/sq_exec.c
17  * @brief helper functions for executing SQL statements
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_sq_lib.h"
22
23
24 /**
25  * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
26  *
27  * @param sql actual SQL statement
28  * @return initialized struct
29  */
30 struct GNUNET_SQ_ExecuteStatement
31 GNUNET_SQ_make_execute (const char *sql)
32   {
33   struct GNUNET_SQ_ExecuteStatement es = {
34     .sql = sql,
35     .ignore_errors = GNUNET_NO
36   };
37
38   return es;
39 }
40
41
42
43 /**
44  * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
45  * be tolerated.
46  *
47  * @param sql actual SQL statement
48  * @return initialized struct
49  */
50 struct GNUNET_SQ_ExecuteStatement
51 GNUNET_SQ_make_try_execute (const char *sql)
52 {
53   struct GNUNET_SQ_ExecuteStatement es = {
54     .sql = sql,
55     .ignore_errors = GNUNET_YES
56   };
57
58   return es;
59 }
60
61
62 /**
63  * Request execution of an array of statements @a es from Postgres.
64  *
65  * @param dbh database to execute the statements over
66  * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
67  *            statements.
68  * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
69  *         #GNUNET_SYSERR on error
70  */
71 int
72 GNUNET_SQ_exec_statements (sqlite3 *dbh,
73                            const struct GNUNET_SQ_ExecuteStatement *es)
74 {
75   for (unsigned int i=0;NULL != es[i].sql;i++)
76   {
77     char *emsg = NULL;
78
79     if (SQLITE_OK !=
80         sqlite3_exec (dbh,
81                       es[i].sql,
82                       NULL,
83                       NULL,
84                       &emsg))
85     {
86       if (es[i].ignore_errors)
87       {
88         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89                     "Failed to run SQL `%s': %s\n",
90                     es[i].sql,
91                     emsg);
92       }
93       else
94       {
95         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
96                     "Failed to run SQL `%s': %s\n",
97                     es[i].sql,
98                     emsg);
99         sqlite3_free (emsg);
100         return GNUNET_SYSERR;
101       }
102       sqlite3_free (emsg);
103     }
104   }
105   return GNUNET_OK;
106 }
107
108 /* end of sq_exec */