glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / sq / sq_prepare.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 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.
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_prepare.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_PrepareStatement`
26  *
27  * @param sql actual SQL statement
28  * @param pstmt where to store the handle
29  * @return initialized struct
30  */
31 struct GNUNET_SQ_PrepareStatement
32 GNUNET_SQ_make_prepare (const char *sql,
33                         sqlite3_stmt **pstmt)
34 {
35   struct GNUNET_SQ_PrepareStatement ps = {
36     .sql = sql,
37     .pstmt = pstmt
38   };
39
40   return ps;
41 }
42
43
44
45 /**
46  * Prepare all statements given in the (NULL,NULL)-terminated
47  * array at @a ps
48  *
49  * @param dbh database to use
50  * @param ps array of statements to prepare
51  * @return #GNUNET_OK on success
52  */
53 int
54 GNUNET_SQ_prepare (sqlite3 *dbh,
55                    const struct GNUNET_SQ_PrepareStatement *ps)
56 {
57   for (unsigned int i=0;NULL != ps[i].sql;i++)
58   {
59     const char *epos = NULL;
60     int ret;
61
62     if (SQLITE_OK !=
63         (ret = sqlite3_prepare_v2 (dbh,
64                                    ps[i].sql,
65                                    strlen (ps[i].sql),
66                                    ps[i].pstmt,
67                                    &epos)))
68     {
69       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
70                   "Failed to prepare SQL `%s': error %d at %s\n",
71                   ps[i].sql,
72                   ret,
73                   epos);
74       return GNUNET_SYSERR;
75     }
76   }
77   return GNUNET_OK;
78 }
79
80 /* end of sq_prepare.c */