aboutsummaryrefslogtreecommitdiff
path: root/src/sq/sq_prepare.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sq/sq_prepare.c')
-rw-r--r--src/sq/sq_prepare.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/sq/sq_prepare.c b/src/sq/sq_prepare.c
new file mode 100644
index 000000000..8c46302f2
--- /dev/null
+++ b/src/sq/sq_prepare.c
@@ -0,0 +1,83 @@
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 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/>.
17*/
18/**
19 * @file sq/sq_prepare.c
20 * @brief helper functions for executing SQL statements
21 * @author Christian Grothoff
22 */
23#include "platform.h"
24#include "gnunet_sq_lib.h"
25
26
27/**
28 * Create a `struct GNUNET_SQ_PrepareStatement`
29 *
30 * @param sql actual SQL statement
31 * @param pstmt where to store the handle
32 * @return initialized struct
33 */
34struct GNUNET_SQ_PrepareStatement
35GNUNET_SQ_make_prepare (const char *sql,
36 sqlite3_stmt **pstmt)
37{
38 struct GNUNET_SQ_PrepareStatement ps = {
39 .sql = sql,
40 .pstmt = pstmt
41 };
42
43 return ps;
44}
45
46
47
48/**
49 * Prepare all statements given in the (NULL,NULL)-terminated
50 * array at @a ps
51 *
52 * @param dbh database to use
53 * @param ps array of statements to prepare
54 * @return #GNUNET_OK on success
55 */
56int
57GNUNET_SQ_prepare (sqlite3 *dbh,
58 const struct GNUNET_SQ_PrepareStatement *ps)
59{
60 for (unsigned int i=0;NULL != ps[i].sql;i++)
61 {
62 const char *epos = NULL;
63 int ret;
64
65 if (SQLITE_OK !=
66 (ret = sqlite3_prepare_v2 (dbh,
67 ps[i].sql,
68 strlen (ps[i].sql),
69 ps[i].pstmt,
70 &epos)))
71 {
72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
73 "Failed to prepare SQL `%s': error %d at %s\n",
74 ps[i].sql,
75 ret,
76 epos);
77 return GNUNET_SYSERR;
78 }
79 }
80 return GNUNET_OK;
81}
82
83/* end of sq_prepare.c */