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